Skip to content

Commit e811984

Browse files
committed
[perf] leaveNode early if no node to remove or replace with array
1 parent 241920f commit e811984

File tree

1 file changed

+6
-1
lines changed

1 file changed

+6
-1
lines changed

src/Rector/AbstractRector.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,14 +191,19 @@ final public function enterNode(Node $node): int|Node|null
191191

192192
/**
193193
* Replacing nodes in leaveNode() method avoids infinite recursion
194-
* see"infinite recursion" in https://github.com/nikic/PHP-Parser/blob/master/doc/component/Walking_the_AST.markdown
194+
* see "infinite recursion" in https://github.com/nikic/PHP-Parser/blob/master/doc/component/Walking_the_AST.markdown
195195
*/
196196
final public function leaveNode(Node $node): array|int|Node|null
197197
{
198198
if ($node->hasAttribute(AttributeKey::ORIGINAL_NODE)) {
199199
return null;
200200
}
201201

202+
// perf: hash node only if there is some object to be removed or replaced by array
203+
if ($this->toBeRemovedNodeId === null && $this->nodesToReturn === []) {
204+
return null;
205+
}
206+
202207
$objectId = spl_object_id($node);
203208
if ($this->toBeRemovedNodeId === $objectId) {
204209
$this->toBeRemovedNodeId = null;

0 commit comments

Comments
 (0)