File tree Expand file tree Collapse file tree 4 files changed +107
-7
lines changed
src/PhpParser/NodeTraverser
tests/PhpParser/NodeTraverser/StopTraverseOnTypeChange Expand file tree Collapse file tree 4 files changed +107
-7
lines changed Original file line number Diff line number Diff line change @@ -112,10 +112,8 @@ protected function traverseNode(Node $node): void
112112 $ node ->{$ name } = $ return ;
113113
114114 if ($ originalSubNodeClass !== $ subNode ::class) {
115- // stop traversing as node has changed its class and visitors do not work
116- $ traverseChildren = false ;
117- $ this ->stopTraversal = true ;
118- break 2 ;
115+ // stop traversing as node type changed and visitors won't work
116+ return ;
119117 }
120118
121119 } elseif ($ return === NodeVisitor::DONT_TRAVERSE_CHILDREN ) {
@@ -196,9 +194,8 @@ protected function traverseArray(array $nodes): array
196194 $ nodes [$ i ] = $ node = $ return ;
197195
198196 if ($ originalNodeNodeClass !== $ return ::class) {
199- // stop traversing as node has changed its class and visitors do not work
200- $ this ->stopTraversal = true ;
201- break 2 ;
197+ // stop traversing as node type changed and visitors won't work
198+ return $ nodes ;
202199 }
203200 } elseif (\is_array ($ return )) {
204201 $ doNodes [] = [$ i , $ return ];
Original file line number Diff line number Diff line change 1+ <?php
2+
3+ namespace Rector \Tests \PhpParser \NodeTraverser \StopTraverseOnTypeChange \Class_ ;
4+
5+ use PhpParser \Node ;
6+ use Rector \Rector \AbstractRector ;
7+ use Symplify \RuleDocGenerator \ValueObject \RuleDefinition ;
8+
9+ final class RuleChangingClassToTraitRector extends AbstractRector
10+ {
11+
12+ public function getRuleDefinition (): RuleDefinition
13+ {
14+ }
15+
16+ public function getNodeTypes (): array
17+ {
18+ return [Node \Stmt \Class_::class];
19+ }
20+
21+ /**
22+ * @param Node\Stmt\Class_ $node
23+ * @return Node\Stmt\Trait_
24+ */
25+ public function refactor (Node $ node )
26+ {
27+ return new Node \Stmt \Trait_ ('SomeTrait ' );
28+ }
29+ }
Original file line number Diff line number Diff line change 1+ <?php
2+
3+ namespace Rector \Tests \PhpParser \NodeTraverser \StopTraverseOnTypeChange \Class_ ;
4+
5+ use PhpParser \Node ;
6+ use PhpParser \Node \Stmt \Class_ ;
7+ use Rector \Rector \AbstractRector ;
8+ use Symplify \RuleDocGenerator \ValueObject \RuleDefinition ;
9+ use Webmozart \Assert \Assert ;
10+
11+ final class RuleCheckingClassRector extends AbstractRector
12+ {
13+
14+ public function getRuleDefinition (): RuleDefinition
15+ {
16+ }
17+
18+ public function getNodeTypes (): array
19+ {
20+ return [Class_::class];
21+ }
22+
23+ /**
24+ * @param Class_ $node
25+ * @return Class_
26+ */
27+ public function refactor (Node $ node )
28+ {
29+ Assert::isInstanceOf ($ node , Class_::class);
30+
31+ return $ node ;
32+ }
33+ }
Original file line number Diff line number Diff line change 1+ <?php
2+
3+ declare (strict_types=1 );
4+
5+ namespace Rector \Tests \PhpParser \NodeTraverser \StopTraverseOnTypeChange ;
6+
7+ use PhpParser \Node \Stmt \Class_ ;
8+ use PhpParser \Node \Stmt \Trait_ ;
9+ use Rector \Application \Provider \CurrentFileProvider ;
10+ use Rector \PhpParser \NodeTraverser \RectorNodeTraverser ;
11+ use Rector \Testing \PHPUnit \AbstractLazyTestCase ;
12+ use Rector \Tests \PhpParser \NodeTraverser \StopTraverseOnTypeChange \Class_ \RuleChangingClassToTraitRector ;
13+ use Rector \Tests \PhpParser \NodeTraverser \StopTraverseOnTypeChange \Class_ \RuleCheckingClassRector ;
14+
15+ final class StopTraverseOnTypeChangeTest extends AbstractLazyTestCase
16+ {
17+ private RectorNodeTraverser $ rectorNodeTraverser ;
18+
19+ protected function setUp (): void
20+ {
21+ $ this ->rectorNodeTraverser = $ this ->make (RectorNodeTraverser::class);
22+
23+ $ this ->rectorNodeTraverser ->refreshPhpRectors ([
24+ $ this ->make (RuleChangingClassToTraitRector::class),
25+ $ this ->make (RuleCheckingClassRector::class)
26+ ]);
27+
28+ $ currentFileProvider = $ this ->make (CurrentFileProvider::class);
29+
30+ // @todo
31+ // $currentFileProvider->setFile();
32+ }
33+
34+ public function testGetVisitorsForNodeWhenNoVisitorsAvailable (): void
35+ {
36+ $ class = new Class_ ('test ' );
37+
38+ $ changedNodes = $ this ->rectorNodeTraverser ->traverse ([$ class ]);
39+ $ this ->assertContainsOnlyInstancesOf (Trait_::class, $ changedNodes );
40+ }
41+ }
You can’t perform that action at this time.
0 commit comments