Skip to content

Commit

Permalink
[PostRector] Remove parent lookup on ClassRenamingPostRector (#3959)
Browse files Browse the repository at this point in the history
* [PostRector] Remove parent lookup on ClassRenamingPostRector

* [ci-review] Rector Rectify

* Fix cs

* update kernel cache key

* add fixture no namespace

* Final touch: ensure reset

---------

Co-authored-by: GitHub Action <actions@github.com>
  • Loading branch information
samsonasik and actions-user committed May 25, 2023
1 parent 794bc8b commit fc1fabe
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 6 deletions.
29 changes: 24 additions & 5 deletions packages/PostRector/Rector/ClassRenamingPostRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Rector\PostRector\Rector;

use PhpParser\Node\Stmt;
use PhpParser\Node;
use PhpParser\Node\Stmt\Namespace_;
use PHPStan\Analyser\Scope;
Expand All @@ -12,7 +13,6 @@
use Rector\Core\Configuration\RenamedClassesDataCollector;
use Rector\Core\Contract\Rector\RectorInterface;
use Rector\Core\NonPhpFile\Rector\RenameClassNonPhpRector;
use Rector\Core\PhpParser\Node\BetterNodeFinder;
use Rector\Core\PhpParser\Node\CustomNode\FileWithoutNamespace;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\PostRector\Contract\Rector\PostRectorDependencyInterface;
Expand All @@ -23,11 +23,12 @@

final class ClassRenamingPostRector extends AbstractPostRector implements PostRectorDependencyInterface
{
private FileWithoutNamespace|Namespace_|null $rootNode = null;

public function __construct(
private readonly ClassRenamer $classRenamer,
private readonly RenamedClassesDataCollector $renamedClassesDataCollector,
private readonly RectorConfigProvider $rectorConfigProvider,
private readonly BetterNodeFinder $betterNodeFinder,
private readonly UseImportsRemover $useImportsRemover
) {
}
Expand All @@ -38,6 +39,25 @@ public function getPriority(): int
return 650;
}

/**
* @param Stmt[] $nodes
* @return Stmt[]
*/
public function beforeTraverse(array $nodes): array
{
// ensure reset early on every run to avoid reuse existing value
$this->rootNode = null;

foreach ($nodes as $node) {
if ($node instanceof FileWithoutNamespace || $node instanceof Namespace_) {
$this->rootNode = $node;
break;
}
}

return $nodes;
}

/**
* @return class-string<RectorInterface>[]
*/
Expand All @@ -64,13 +84,12 @@ public function enterNode(Node $node): ?Node
return $result;
}

$rootNode = $this->betterNodeFinder->findParentByTypes($node, [Namespace_::class, FileWithoutNamespace::class]);
if (! $rootNode instanceof Node) {
if (! $this->rootNode instanceof FileWithoutNamespace && ! $this->rootNode instanceof Namespace_) {
return $result;
}

$removedUses = $this->renamedClassesDataCollector->getOldClasses();
$this->useImportsRemover->removeImportsFromStmts($rootNode->stmts, $removedUses);
$this->useImportsRemover->removeImportsFromStmts($this->rootNode->stmts, $removedUses);

return $result;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

use Rector\Tests\Renaming\Rector\Name\RenameClassRector\Source\FirstNamespace\SomeServiceClass;

class RenameClassNotInsideNamespace
{
public function aFunction()
{
$someService = new SomeServiceClass();
}
}

?>
-----
<?php

use Rector\Tests\Renaming\Rector\Name\RenameClassRector\Source\SecondNamespace\SomeServiceClass;

class RenameClassNotInsideNamespace
{
public function aFunction()
{
$someService = new SomeServiceClass();
}
}

?>
2 changes: 1 addition & 1 deletion src/Kernel/RectorKernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ final class RectorKernel
/**
* @var string
*/
private const CACHE_KEY = 'v31';
private const CACHE_KEY = 'v32';

private ContainerInterface|null $container = null;

Expand Down

0 comments on commit fc1fabe

Please sign in to comment.