|
10 | 10 | use PhpParser\Node\Stmt; |
11 | 11 | use PhpParser\NodeTraverserInterface; |
12 | 12 | use PhpParser\NodeVisitor; |
| 13 | +use Rector\Exception\ShouldNotHappenException; |
13 | 14 |
|
14 | 15 | abstract class AbstractImmutableNodeTraverser implements NodeTraverserInterface |
15 | 16 | { |
16 | | - /** |
17 | | - * @deprecated Use NodeVisitor::DONT_TRAVERSE_CHILDREN instead. |
18 | | - */ |
19 | | - public const DONT_TRAVERSE_CHILDREN = NodeVisitor::DONT_TRAVERSE_CHILDREN; |
20 | | - |
21 | | - /** |
22 | | - * @deprecated Use NodeVisitor::STOP_TRAVERSAL instead. |
23 | | - */ |
24 | | - public const STOP_TRAVERSAL = NodeVisitor::STOP_TRAVERSAL; |
25 | | - |
26 | | - /** |
27 | | - * @deprecated Use NodeVisitor::REMOVE_NODE instead. |
28 | | - */ |
29 | | - public const REMOVE_NODE = NodeVisitor::REMOVE_NODE; |
30 | | - |
31 | | - /** |
32 | | - * @deprecated Use NodeVisitor::DONT_TRAVERSE_CURRENT_AND_CHILDREN instead. |
33 | | - */ |
34 | | - public const DONT_TRAVERSE_CURRENT_AND_CHILDREN = NodeVisitor::DONT_TRAVERSE_CURRENT_AND_CHILDREN; |
35 | | - |
36 | 17 | /** |
37 | 18 | * @var list<NodeVisitor> Visitors |
38 | 19 | */ |
@@ -63,15 +44,9 @@ public function addVisitor(NodeVisitor $visitor): void |
63 | 44 | $this->visitors[] = $visitor; |
64 | 45 | } |
65 | 46 |
|
66 | | - /** |
67 | | - * Removes an added visitor. |
68 | | - */ |
69 | 47 | public function removeVisitor(NodeVisitor $visitor): void |
70 | 48 | { |
71 | | - $index = array_search($visitor, $this->visitors, true); |
72 | | - if ($index !== false) { |
73 | | - array_splice($this->visitors, $index, 1, []); |
74 | | - } |
| 49 | + throw new ShouldNotHappenException('The immutable node traverser does not support removing visitors.'); |
75 | 50 | } |
76 | 51 |
|
77 | 52 | /** |
@@ -106,11 +81,6 @@ public function traverse(array $nodes): array |
106 | 81 | */ |
107 | 82 | abstract public function getVisitorsForNode(Node $node): array; |
108 | 83 |
|
109 | | - /** |
110 | | - * Recursively traverse a node. |
111 | | - * |
112 | | - * @param Node $node Node to traverse. |
113 | | - */ |
114 | 84 | protected function traverseNode(Node $node): void |
115 | 85 | { |
116 | 86 | foreach ($node->getSubNodeNames() as $name) { |
@@ -189,10 +159,7 @@ protected function traverseNode(Node $node): void |
189 | 159 | } |
190 | 160 |
|
191 | 161 | /** |
192 | | - * Recursively traverse array (usually of nodes). |
193 | | - * |
194 | | - * @param Node[] $nodes Array to traverse |
195 | | - * |
| 162 | + * @param Node[] $nodes |
196 | 163 | * @return array Result of traversal (may be original array or changed one) |
197 | 164 | */ |
198 | 165 | protected function traverseArray(array $nodes): array |
@@ -285,16 +252,20 @@ protected function traverseArray(array $nodes): array |
285 | 252 |
|
286 | 253 | private function ensureReplacementReasonable(Node $old, Node $new): void |
287 | 254 | { |
288 | | - if ($old instanceof Stmt && $new instanceof Expr) { |
289 | | - throw new LogicException( |
290 | | - sprintf('Trying to replace statement (%s) ', $old->getType()) . sprintf( |
291 | | - 'with expression (%s). Are you missing a ', |
292 | | - $new->getType() |
293 | | - ) . 'Stmt_Expression wrapper?' |
294 | | - ); |
| 255 | + if ($old instanceof Stmt) { |
| 256 | + if ($new instanceof Expr) { |
| 257 | + throw new LogicException( |
| 258 | + sprintf('Trying to replace statement (%s) ', $old->getType()) . sprintf( |
| 259 | + 'with expression (%s). Are you missing a ', |
| 260 | + $new->getType() |
| 261 | + ) . 'Stmt_Expression wrapper?' |
| 262 | + ); |
| 263 | + } |
| 264 | + |
| 265 | + return; |
295 | 266 | } |
296 | 267 |
|
297 | | - if ($old instanceof Expr && $new instanceof Stmt) { |
| 268 | + if ($new instanceof Stmt) { |
298 | 269 | throw new LogicException( |
299 | 270 | sprintf('Trying to replace expression (%s) ', $old->getType()) . sprintf( |
300 | 271 | 'with statement (%s)', |
|
0 commit comments