Skip to content

Commit

Permalink
[Performance] Only save nodesToReturn[$objectHash] on return array of…
Browse files Browse the repository at this point in the history
… nodes on AbstractRector (#4865)

* [Performance] Only safe nodesToReturn[$objectHash] on return array of nodes on AbstractRector

* [ci-review] Rector Rectify

---------

Co-authored-by: GitHub Action <actions@github.com>
  • Loading branch information
samsonasik and actions-user committed Aug 27, 2023
1 parent 3f5ff9e commit 7e4a920
Showing 1 changed file with 6 additions and 18 deletions.
24 changes: 6 additions & 18 deletions src/Rector/AbstractRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -221,24 +221,14 @@ final public function enterNode(Node $node): int|Node|null
*/
public function leaveNode(Node $node): array|int|Node|null
{
if ($this->toBeRemovedNodeHash !== null && $this->toBeRemovedNodeHash === spl_object_hash($node)) {
$objectHash = spl_object_hash($node);
if ($this->toBeRemovedNodeHash !== null && $this->toBeRemovedNodeHash === $objectHash) {
$this->toBeRemovedNodeHash = null;

return NodeTraverser::REMOVE_NODE;
}

$objectHash = spl_object_hash($node);
$result = $this->nodesToReturn[$objectHash] ?? $node;

if (is_array($result)) {
return $result;
}

if ($result::class === $node::class) {
return $result;
}

return $node;
return $this->nodesToReturn[$objectHash] ?? $node;
}

protected function isName(Node $node, string $name): bool
Expand Down Expand Up @@ -315,24 +305,22 @@ private function postRefactorProcess(
/** @var MutatingScope|null $currentScope */
$currentScope = $node->getAttribute(AttributeKey::SCOPE);

// search "infinite recursion" in https://github.com/nikic/PHP-Parser/blob/master/doc/component/Walking_the_AST.markdown
$originalNodeHash = spl_object_hash($originalNode);

if (is_array($refactoredNode)) {
$firstNode = current($refactoredNode);
$this->mirrorComments($firstNode, $originalNode);

$this->refreshScopeNodes($refactoredNode, $filePath, $currentScope);

// search "infinite recursion" in https://github.com/nikic/PHP-Parser/blob/master/doc/component/Walking_the_AST.markdown
$originalNodeHash = spl_object_hash($originalNode);

// will be replaced in leaveNode() the original node must be passed
$this->nodesToReturn[$originalNodeHash] = $refactoredNode;

return $originalNode;
}

$this->refreshScopeNodes($refactoredNode, $filePath, $currentScope);

$this->nodesToReturn[$originalNodeHash] = $refactoredNode;
return $refactoredNode;
}

Expand Down

0 comments on commit 7e4a920

Please sign in to comment.