99use PhpParser \Node \Expr ;
1010use PhpParser \Node \Stmt ;
1111use PhpParser \NodeVisitor ;
12- use Rector \Configuration \ConfigurationRuleFilter ;
13- use Rector \Contract \Rector \RectorInterface ;
12+ use Rector \Application \RectorRegistry ;
1413use Rector \Rector \RectorRunner ;
15- use Rector \VersionBonding \ComposerPackageConstraintFilter ;
16- use Rector \VersionBonding \PhpVersionedFilter ;
17- use Webmozart \Assert \Assert ;
1814
19- /**
20- * @see \Rector\Tests\PhpParser\NodeTraverser\RectorNodeTraverserTest
21- */
2215final class RectorNodeTraverser
2316{
24- /**
25- * @var RectorInterface[]
26- */
27- private array $ visitors = [];
28-
2917 private bool $ stopTraversal ;
3018
31- private bool $ areNodeVisitorsPrepared = false ;
32-
33- /**
34- * @var array<class-string<Node>, RectorInterface[]>
35- */
36- private array $ visitorsPerNodeClass = [];
37-
38- /**
39- * @param RectorInterface[] $rectors
40- */
4119 public function __construct (
42- private array $ rectors ,
43- private readonly PhpVersionedFilter $ phpVersionedFilter ,
44- private readonly ComposerPackageConstraintFilter $ composerPackageConstraintFilter ,
45- private readonly ConfigurationRuleFilter $ configurationRuleFilter ,
4620 private readonly RectorRunner $ rectorRunner ,
21+ private readonly RectorRegistry $ rectorRegistry ,
4722 ) {
4823 }
4924
@@ -53,57 +28,11 @@ public function __construct(
5328 */
5429 public function traverse (array $ nodes ): array
5530 {
56- $ this ->prepareNodeVisitors ();
57-
5831 $ this ->stopTraversal = false ;
5932
6033 return $ this ->traverseArray ($ nodes );
6134 }
6235
63- /**
64- * @param RectorInterface[] $rectors
65- *
66- * @internal Used only in Rector core, not supported outside. Might change any time.
67- */
68- public function refreshPhpRectors (array $ rectors ): void
69- {
70- Assert ::allIsInstanceOf ($ rectors , RectorInterface::class);
71-
72- $ this ->rectors = $ rectors ;
73- $ this ->visitors = [];
74- $ this ->visitorsPerNodeClass = [];
75-
76- $ this ->areNodeVisitorsPrepared = false ;
77-
78- $ this ->prepareNodeVisitors ();
79- }
80-
81- /**
82- * @return RectorInterface[]
83- *
84- * @api used in tests
85- */
86- public function getVisitorsForNode (Node $ node ): array
87- {
88- $ nodeClass = $ node ::class;
89-
90- if (! isset ($ this ->visitorsPerNodeClass [$ nodeClass ])) {
91- $ this ->visitorsPerNodeClass [$ nodeClass ] = [];
92-
93- /** @var RectorInterface $visitor */
94- foreach ($ this ->visitors as $ visitor ) {
95- foreach ($ visitor ->getNodeTypes () as $ nodeType ) {
96- if (is_a ($ nodeClass , $ nodeType , true )) {
97- $ this ->visitorsPerNodeClass [$ nodeClass ][] = $ visitor ;
98- continue 2 ;
99- }
100- }
101- }
102- }
103-
104- return $ this ->visitorsPerNodeClass [$ nodeClass ];
105- }
106-
10736 private function traverseNode (Node $ node ): void
10837 {
10938 foreach ($ node ->getSubNodeNames () as $ name ) {
@@ -121,7 +50,7 @@ private function traverseNode(Node $node): void
12150 continue ;
12251 }
12352
124- $ currentNodeVisitors = $ this ->getVisitorsForNode ($ subNode );
53+ $ currentNodeVisitors = $ this ->rectorRegistry -> forNode ($ subNode );
12554
12655 foreach ($ currentNodeVisitors as $ currentNodeVisitor ) {
12756 $ return = $ this ->rectorRunner ->run ($ currentNodeVisitor , $ subNode );
@@ -168,8 +97,7 @@ private function traverseArray(array $nodes): array
16897 continue ;
16998 }
17099
171- $ traverseChildren = true ;
172- $ currentNodeVisitors = $ this ->getVisitorsForNode ($ node );
100+ $ currentNodeVisitors = $ this ->rectorRegistry ->forNode ($ node );
173101
174102 foreach ($ currentNodeVisitors as $ currentNodeVisitor ) {
175103 $ return = $ this ->rectorRunner ->run ($ currentNodeVisitor , $ node );
@@ -234,27 +162,4 @@ private function ensureReplacementReasonable(Node $old, Node $new): void
234162 );
235163 }
236164 }
237-
238- /**
239- * This must happen after $this->configuration is set after ProcessCommand::execute() is run, otherwise we get default false positives.
240- *
241- * This should be removed after https://github.com/rectorphp/rector/issues/5584 is resolved
242- */
243- private function prepareNodeVisitors (): void
244- {
245- if ($ this ->areNodeVisitorsPrepared ) {
246- return ;
247- }
248-
249- // filter out by PHP version
250- $ this ->visitors = $ this ->phpVersionedFilter ->filter ($ this ->rectors );
251-
252- // filter out by composer package constraint
253- $ this ->visitors = $ this ->composerPackageConstraintFilter ->filter ($ this ->visitors );
254-
255- // filter by configuration
256- $ this ->visitors = $ this ->configurationRuleFilter ->filter ($ this ->visitors );
257-
258- $ this ->areNodeVisitorsPrepared = true ;
259- }
260165}
0 commit comments