Skip to content

Commit 0307acf

Browse files
committed
avoid double checking node type
1 parent e1fdfaa commit 0307acf

File tree

4 files changed

+20
-18
lines changed

4 files changed

+20
-18
lines changed

composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
"require": {
1515
"php": "^8.2",
1616
"clue/ndjson-react": "^1.3",
17-
"composer/pcre": "^3.3.0",
17+
"composer/pcre": "^3.3.2",
1818
"composer/semver": "^3.4",
1919
"composer/xdebug-handler": "^3.0.5",
2020
"doctrine/inflector": "^2.1",
@@ -50,7 +50,7 @@
5050
"phpstan/phpstan-phpunit": "^2.0",
5151
"phpstan/phpstan-webmozart-assert": "^2.0",
5252
"phpunit/phpunit": "^11.5",
53-
"rector/jack": "^0.4.0",
53+
"rector/jack": "^0.4",
5454
"rector/release-notes-generator": "^0.5",
5555
"rector/swiss-knife": "^2.3",
5656
"rector/type-perfect": "^2.1",

src/PhpParser/NodeTraverser/AbstractImmutableNodeTraverser.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,9 +105,19 @@ protected function traverseNode(Node $node): void
105105
$return = $visitor->enterNode($subNode);
106106
if ($return !== null) {
107107
if ($return instanceof Node) {
108+
$originalSubNodeClass = $subNode::class;
109+
108110
$this->ensureReplacementReasonable($subNode, $return);
109111
$subNode = $return;
110112
$node->{$name} = $return;
113+
114+
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;
119+
}
120+
111121
} elseif ($return === NodeVisitor::DONT_TRAVERSE_CHILDREN) {
112122
$traverseChildren = false;
113123
} elseif ($return === NodeVisitor::DONT_TRAVERSE_CURRENT_AND_CHILDREN) {
@@ -181,8 +191,15 @@ protected function traverseArray(array $nodes): array
181191
$return = $visitor->enterNode($node);
182192
if ($return !== null) {
183193
if ($return instanceof Node) {
194+
$originalNodeNodeClass = $node::class;
184195
$this->ensureReplacementReasonable($node, $return);
185196
$nodes[$i] = $node = $return;
197+
198+
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;
202+
}
186203
} elseif (\is_array($return)) {
187204
$doNodes[] = [$i, $return];
188205
continue 2;

src/PhpParser/NodeTraverser/RectorNodeTraverser.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ public function getVisitorsForNode(Node $node): array
6767

6868
if (! isset($this->visitorsPerNodeClass[$nodeClass])) {
6969
$this->visitorsPerNodeClass[$nodeClass] = [];
70+
7071
/** @var RectorInterface $visitor */
7172
foreach ($this->visitors as $visitor) {
7273
foreach ($visitor->getNodeTypes() as $nodeType) {

src/Rector/AbstractRector.php

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -132,10 +132,6 @@ public function beforeTraverse(array $nodes): ?array
132132
*/
133133
final public function enterNode(Node $node): int|Node|null
134134
{
135-
if (! $this->isMatchingNodeType($node)) {
136-
return null;
137-
}
138-
139135
if (is_a($this, HTMLAverseRectorInterface::class, true) && $this->file->containsHTML()) {
140136
return null;
141137
}
@@ -317,16 +313,4 @@ private function refreshScopeNodes(array | Node $node, string $filePath, ?Mutati
317313
$this->changedNodeScopeRefresher->refresh($node, $filePath, $mutatingScope);
318314
}
319315
}
320-
321-
private function isMatchingNodeType(Node $node): bool
322-
{
323-
$nodeClass = $node::class;
324-
foreach ($this->getNodeTypes() as $nodeType) {
325-
if (is_a($nodeClass, $nodeType, true)) {
326-
return true;
327-
}
328-
}
329-
330-
return false;
331-
}
332316
}

0 commit comments

Comments
 (0)