|
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 | /** |
@@ -285,16 +260,20 @@ protected function traverseArray(array $nodes): array |
285 | 260 |
|
286 | 261 | private function ensureReplacementReasonable(Node $old, Node $new): void |
287 | 262 | { |
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 | | - ); |
| 263 | + if ($old instanceof Stmt) { |
| 264 | + if ($new instanceof Expr) { |
| 265 | + throw new LogicException( |
| 266 | + sprintf('Trying to replace statement (%s) ', $old->getType()) . sprintf( |
| 267 | + 'with expression (%s). Are you missing a ', |
| 268 | + $new->getType() |
| 269 | + ) . 'Stmt_Expression wrapper?' |
| 270 | + ); |
| 271 | + } |
| 272 | + |
| 273 | + return; |
295 | 274 | } |
296 | 275 |
|
297 | | - if ($old instanceof Expr && $new instanceof Stmt) { |
| 276 | + if ($new instanceof Stmt) { |
298 | 277 | throw new LogicException( |
299 | 278 | sprintf('Trying to replace expression (%s) ', $old->getType()) . sprintf( |
300 | 279 | 'with statement (%s)', |
|
0 commit comments