Skip to content

Commit 317ba55

Browse files
authored
[AutoImport] Optimize ClassRenamingPostRector with stop on enterNode() (#7295)
1 parent ed32b19 commit 317ba55

1 file changed

Lines changed: 12 additions & 7 deletions

File tree

src/PostRector/Rector/ClassRenamingPostRector.php

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use PhpParser\Node;
88
use PhpParser\Node\Stmt;
99
use PhpParser\Node\Stmt\Namespace_;
10+
use PhpParser\NodeVisitor;
1011
use Rector\CodingStyle\Application\UseImportsRemover;
1112
use Rector\Configuration\Option;
1213
use Rector\Configuration\Parameter\SimpleParameterProvider;
@@ -47,17 +48,21 @@ public function beforeTraverse(array $nodes): array
4748
}
4849
}
4950

51+
$this->renamedNameCollector->reset();
5052
return $nodes;
5153
}
5254

53-
/**
54-
* @param Node[] $nodes
55-
* @return Stmt[]
56-
*/
57-
public function afterTraverse(array $nodes): array
55+
public function enterNode(Node $node): int
5856
{
59-
$this->renamedNameCollector->reset();
60-
return $nodes;
57+
/**
58+
* We stop the traversal because all the work has already been done in the beforeTraverse() function
59+
*
60+
* Using STOP_TRAVERSAL is usually dangerous as it will stop the processing of all your nodes for all visitors
61+
* but since the PostFileProcessor is using direct new NodeTraverser() and traverse() for only a single
62+
* visitor per execution, using stop traversal here is safe,
63+
* ref https://github.com/rectorphp/rector-src/blob/fc1e742fa4d9861ccdc5933f3b53613b8223438d/src/PostRector/Application/PostFileProcessor.php#L59-L61
64+
*/
65+
return NodeVisitor::STOP_TRAVERSAL;
6166
}
6267

6368
public function shouldTraverse(array $stmts): bool

0 commit comments

Comments
 (0)