Skip to content

Commit e0cf030

Browse files
committed
optimize AbstractImmutableNodeTraverser, remove deprecated constants
1 parent 147c266 commit e0cf030

File tree

2 files changed

+14
-51
lines changed

2 files changed

+14
-51
lines changed

src/PhpParser/NodeTraverser/AbstractImmutableNodeTraverser.php

Lines changed: 14 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -10,29 +10,10 @@
1010
use PhpParser\Node\Stmt;
1111
use PhpParser\NodeTraverserInterface;
1212
use PhpParser\NodeVisitor;
13+
use Rector\Exception\ShouldNotHappenException;
1314

1415
abstract class AbstractImmutableNodeTraverser implements NodeTraverserInterface
1516
{
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-
3617
/**
3718
* @var list<NodeVisitor> Visitors
3819
*/
@@ -63,15 +44,9 @@ public function addVisitor(NodeVisitor $visitor): void
6344
$this->visitors[] = $visitor;
6445
}
6546

66-
/**
67-
* Removes an added visitor.
68-
*/
6947
public function removeVisitor(NodeVisitor $visitor): void
7048
{
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.');
7550
}
7651

7752
/**
@@ -285,16 +260,20 @@ protected function traverseArray(array $nodes): array
285260

286261
private function ensureReplacementReasonable(Node $old, Node $new): void
287262
{
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;
295274
}
296275

297-
if ($old instanceof Expr && $new instanceof Stmt) {
276+
if ($new instanceof Stmt) {
298277
throw new LogicException(
299278
sprintf('Trying to replace expression (%s) ', $old->getType()) . sprintf(
300279
'with statement (%s)',

tests/PhpParser/NodeTraverser/RectorNodeTraverserTest.php

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -76,20 +76,4 @@ public function testGetVisitorsForNodeWhenAllVisitorsMatch(): void
7676

7777
$this->assertEquals([$this->ruleUsingClassRector, $this->ruleUsingClassLikeRector], $visitors);
7878
}
79-
80-
public function testGetVisitorsForNodeUsesCachedValue(): void
81-
{
82-
$class = new Class_('test');
83-
$this->rectorNodeTraverser->addVisitor($this->ruleUsingClassRector);
84-
$this->rectorNodeTraverser->addVisitor($this->ruleUsingClassLikeRector);
85-
86-
$visitors = $this->rectorNodeTraverser->getVisitorsForNode($class);
87-
88-
$this->assertEquals([$this->ruleUsingClassRector, $this->ruleUsingClassLikeRector], $visitors);
89-
90-
$this->rectorNodeTraverser->removeVisitor($this->ruleUsingClassRector);
91-
$visitors = $this->rectorNodeTraverser->getVisitorsForNode($class);
92-
93-
$this->assertEquals([$this->ruleUsingClassRector, $this->ruleUsingClassLikeRector], $visitors);
94-
}
9579
}

0 commit comments

Comments
 (0)