|
10 | 10 | use PhpParser\Node\Stmt; |
11 | 11 | use PhpParser\NodeVisitor; |
12 | 12 | use Rector\Application\RectorRegistry; |
13 | | -use Rector\Exception\ShouldNotHappenException; |
14 | | -use Rector\PhpParser\Printer\BetterStandardPrinter; |
15 | 13 | use Rector\Rector\RectorRunner; |
16 | 14 | use Rector\ValueObject\Application\File; |
17 | 15 |
|
18 | 16 | final class RectorNodeTraverser |
19 | 17 | { |
20 | | - /** |
21 | | - * Safety cap on local re-visits of a single node, so a rule that never settles cannot loop forever. |
22 | | - */ |
23 | | - private const int MAX_REVISITS = 50; |
24 | | - |
25 | 18 | private bool $stopTraversal; |
26 | 19 |
|
27 | 20 | public function __construct( |
28 | 21 | private readonly RectorRunner $rectorRunner, |
29 | 22 | private readonly RectorRegistry $rectorRegistry, |
30 | | - private readonly BetterStandardPrinter $betterStandardPrinter, |
31 | 23 | ) { |
32 | 24 | } |
33 | 25 |
|
@@ -109,67 +101,33 @@ private function traverseArray(array $nodes, File $file): array |
109 | 101 | } |
110 | 102 |
|
111 | 103 | /** |
112 | | - * Applies the node's rules and traverses into its children, then re-applies its rules while a |
113 | | - * descendant keeps changing - so a rule that depends on a change made deeper in the tree (ordered |
114 | | - * via OrderedRectorInterface, or a plain multi-pass chain) converges within a single file pass. |
115 | | - * |
116 | | - * Termination is structural: the loop stops once a round reports no change, or reports a change |
117 | | - * that leaves the node's printed form identical (a rule that keeps returning the same result). |
| 104 | + * Applies the node's rules, then traverses into its children. |
118 | 105 | * |
119 | 106 | * @param bool $allowListReturn whether an array / REMOVE_NODE return is valid here (only for a |
120 | 107 | * node that lives inside a list, not a single sub-node slot) |
121 | 108 | * @return NodeVisitor::REMOVE_NODE|Node|Node[] |
122 | 109 | */ |
123 | 110 | private function refactorNode(Node $node, File $file, bool $allowListReturn): int|Node|array |
124 | 111 | { |
125 | | - $lastPrint = null; |
126 | | - $revisits = 0; |
| 112 | + $return = $this->applyRectors($node, $file); |
127 | 113 |
|
128 | | - while (true) { |
129 | | - $changeCountBefore = $this->changeCount($file); |
130 | | - $return = $this->applyRectors($node, $file); |
131 | | - |
132 | | - if (\is_array($return) || $return === NodeVisitor::REMOVE_NODE) { |
133 | | - if (! $allowListReturn) { |
134 | | - throw new LogicException('RectorRunner::run() returned invalid value of type ' . gettype($return)); |
135 | | - } |
136 | | - |
137 | | - // a list / removal replaces the node in its parent list; its children are not traversed |
138 | | - return $return; |
139 | | - } |
140 | | - |
141 | | - $typeChanged = $return::class !== $node::class; |
142 | | - $node = $return; |
143 | | - |
144 | | - if ($typeChanged) { |
145 | | - // stop traversing as node type changed and visitors won't work |
146 | | - return $node; |
| 114 | + if (\is_array($return) || $return === NodeVisitor::REMOVE_NODE) { |
| 115 | + if (! $allowListReturn) { |
| 116 | + throw new LogicException('RectorRunner::run() returned invalid value of type ' . gettype($return)); |
147 | 117 | } |
148 | 118 |
|
149 | | - $this->traverseNode($node, $file); |
150 | | - if ($this->stopTraversal) { |
151 | | - return $node; |
152 | | - } |
153 | | - |
154 | | - // no rule (here or deeper) reported a change this round: the subtree is stable |
155 | | - if ($this->changeCount($file) === $changeCountBefore) { |
156 | | - return $node; |
157 | | - } |
| 119 | + // a list / removal replaces the node in its parent list; its children are not traversed |
| 120 | + return $return; |
| 121 | + } |
158 | 122 |
|
159 | | - // a change was reported; keep going only while it actually reshapes the node |
160 | | - $printNow = $this->betterStandardPrinter->print($node); |
161 | | - if ($printNow === $lastPrint) { |
162 | | - return $node; |
163 | | - } |
| 123 | + if ($return::class !== $node::class) { |
| 124 | + // stop traversing as node type changed and visitors won't work |
| 125 | + return $return; |
| 126 | + } |
164 | 127 |
|
165 | | - $lastPrint = $printNow; |
| 128 | + $this->traverseNode($return, $file); |
166 | 129 |
|
167 | | - if (++$revisits >= self::MAX_REVISITS) { |
168 | | - throw new ShouldNotHappenException( |
169 | | - sprintf('Node "%s" did not converge after %d re-visits', $node->getType(), $revisits) |
170 | | - ); |
171 | | - } |
172 | | - } |
| 130 | + return $return; |
173 | 131 | } |
174 | 132 |
|
175 | 133 | /** |
@@ -211,11 +169,6 @@ private function applyRectors(Node $node, File $file): int|Node|array |
211 | 169 | return $node; |
212 | 170 | } |
213 | 171 |
|
214 | | - private function changeCount(File $file): int |
215 | | - { |
216 | | - return count($file->getRectorWithLineChanges()); |
217 | | - } |
218 | | - |
219 | 172 | private function ensureReplacementReasonable(Node $old, Node $new): void |
220 | 173 | { |
221 | 174 | if ($old instanceof Stmt) { |
|
0 commit comments