Skip to content

Commit

Permalink
[AutoImport][Php70] Keep FQCN on conflict last name on TernaryToNullC…
Browse files Browse the repository at this point in the history
…oalescingRector (#5251)

* [AutoImport][Renaming] Keep FQCN on conflict last name on TernaryToNullCoalescingRector

* no namespace only

* add namespaced fixture

* more failing fixture

* Fixed 🎉

* comment for future
  • Loading branch information
samsonasik authored Nov 16, 2023
1 parent 5647e1b commit 03de970
Show file tree
Hide file tree
Showing 5 changed files with 112 additions and 1 deletion.
8 changes: 7 additions & 1 deletion packages/PostRector/Rector/NameImportingPostRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,15 @@ private function processNodeName(Name $name, File $file): ?Node

$namespaces = array_filter(
$file->getNewStmts(),
static fn (Stmt $stmt): bool => $stmt instanceof Namespace_
static fn (Stmt $stmt): bool => $stmt instanceof Namespace_ || $stmt instanceof FileWithoutNamespace
);

// handle overlapped resolve last new stmts
// @see https://github.com/rectorphp/rector-src/pull/5251
if ($namespaces === []) {
return null;
}

if (count($namespaces) > 1) {
return null;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace App;

use shop\product\business\ProductFilter;

class NamespacedAfterTernaryToNullCoalesce
{
private function sharedProductFilter(): void
{
$page = isset($_REQUEST['page']) ? $_REQUEST['page'] : 1;

$this->brands = Hersteller::getListForProducts(new \ProductFilter());
}

}

?>
-----
<?php

namespace App;

use shop\product\business\ProductFilter;

class NamespacedAfterTernaryToNullCoalesce
{
private function sharedProductFilter(): void
{
$page = $_REQUEST['page'] ?? 1;

$this->brands = Hersteller::getListForProducts(new \ProductFilter());
}

}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

use shop\product\business\ProductFilter;

class NoNamespaceAfterTernaryToNullCoalesce
{
private function sharedProductFilter(): void
{
$page = isset($_REQUEST['page']) ? $_REQUEST['page'] : 1;

$this->brands = Hersteller::getListForProducts(new \ProductFilter());
}

}

?>
-----
<?php

use shop\product\business\ProductFilter;

class NoNamespaceAfterTernaryToNullCoalesce
{
private function sharedProductFilter(): void
{
$page = $_REQUEST['page'] ?? 1;

$this->brands = Hersteller::getListForProducts(new \ProductFilter());
}

}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

use shop\product\business\ProductFilter;

class NoNamespaceAfterTernaryToNullCoalesceLongName
{
private function sharedProductFilter(): void
{
$page = isset($_REQUEST['page']) ? $_REQUEST['page'] : 1;

$this->brands = Hersteller::getListForProducts(new \some\different\ProductFilter());
}

}

?>
-----
<?php

use shop\product\business\ProductFilter;

class NoNamespaceAfterTernaryToNullCoalesceLongName
{
private function sharedProductFilter(): void
{
$page = $_REQUEST['page'] ?? 1;

$this->brands = Hersteller::getListForProducts(new \some\different\ProductFilter());
}

}

?>
2 changes: 2 additions & 0 deletions tests/Issues/AutoImport/config/configured_rule.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@
declare(strict_types=1);

use Rector\Config\RectorConfig;
use Rector\Php70\Rector\Ternary\TernaryToNullCoalescingRector;
use Rector\Renaming\Rector\Name\RenameClassRector;

return static function (RectorConfig $rectorConfig): void {
$rectorConfig->importNames();
$rectorConfig->ruleWithConfiguration(RenameClassRector::class, [
'Some\Exception' => 'Some\Target\Exception',
]);
$rectorConfig->rule(TernaryToNullCoalescingRector::class);
};

0 comments on commit 03de970

Please sign in to comment.