From 0a14ee12bcfbfc69f38ef8bf4d2832ad7e8e3fd6 Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Tue, 16 Sep 2025 19:57:40 +0700 Subject: [PATCH] [AutoImport] Optimize ClassRenamingPostRector with stop on enterNode() --- .../Rector/ClassRenamingPostRector.php | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/src/PostRector/Rector/ClassRenamingPostRector.php b/src/PostRector/Rector/ClassRenamingPostRector.php index 6452af23751..a8dfa5858c8 100644 --- a/src/PostRector/Rector/ClassRenamingPostRector.php +++ b/src/PostRector/Rector/ClassRenamingPostRector.php @@ -7,6 +7,7 @@ use PhpParser\Node; use PhpParser\Node\Stmt; use PhpParser\Node\Stmt\Namespace_; +use PhpParser\NodeVisitor; use Rector\CodingStyle\Application\UseImportsRemover; use Rector\Configuration\Option; use Rector\Configuration\Parameter\SimpleParameterProvider; @@ -47,17 +48,21 @@ public function beforeTraverse(array $nodes): array } } + $this->renamedNameCollector->reset(); return $nodes; } - /** - * @param Node[] $nodes - * @return Stmt[] - */ - public function afterTraverse(array $nodes): array + public function enterNode(Node $node): int { - $this->renamedNameCollector->reset(); - return $nodes; + /** + * We stop the traversal because all the work has already been done in the beforeTraverse() function + * + * Using STOP_TRAVERSAL is usually dangerous as it will stop the processing of all your nodes for all visitors + * but since the PostFileProcessor is using direct new NodeTraverser() and traverse() for only a single + * visitor per execution, using stop traversal here is safe, + * ref https://github.com/rectorphp/rector-src/blob/fc1e742fa4d9861ccdc5933f3b53613b8223438d/src/PostRector/Application/PostFileProcessor.php#L59-L61 + */ + return NodeVisitor::STOP_TRAVERSAL; } public function shouldTraverse(array $stmts): bool