Skip to content

Commit

Permalink
[Core] Improve performance: only update and connect parent Node when …
Browse files Browse the repository at this point in the history
…different Node (#3044)
  • Loading branch information
samsonasik committed Nov 11, 2022
1 parent 4d93103 commit fced569
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ use Rector\Tests\Php74\Rector\Property\TypedPropertyRector\Source\AnotherClass a

final class ConflictWithAlias2
{
private \Rector\Tests\Php74\Rector\Property\TypedPropertyRector\Source\AnotherClass $anotherClass;
private AnotherClass $anotherClass;

public function __construct(AnotherClass $anotherClass)
{
Expand Down
26 changes: 23 additions & 3 deletions src/Rector/AbstractRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use PhpParser\Node\Stmt;
use PhpParser\Node\Stmt\Expression;
use PhpParser\Node\Stmt\Nop;
use PhpParser\Node\Stmt\Return_;
use PhpParser\NodeTraverser;
use PhpParser\NodeVisitor\NodeConnectingVisitor;
use PhpParser\NodeVisitor\ParentConnectingVisitor;
Expand Down Expand Up @@ -252,14 +253,33 @@ final public function enterNode(Node $node)
return $originalNode;
}

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

// is equals node type? return node early
if ($originalNode::class === $refactoredNode::class) {
/**
* 1. Expression and Return_ are special
*
* Eg:
* - Expression on Assign with ArrowFunction changed to Closure
* - Return_ on ArrowFunction usage, Return_ is created dynamically on getStmts()
*
* 2. When returned refactored Node doesn't has origNode yet,
* it means returned with New Node instead of re-use existing Node
*/
if (
$refactoredNode instanceof Expression
|| $refactoredNode instanceof Return_
|| ! $refactoredNode->hasAttribute(AttributeKey::ORIGINAL_NODE)
) {
$this->updateAndconnectParentNodes($refactoredNode, $parentNode);
}

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

$this->updateAndconnectParentNodes($refactoredNode, $parentNode);
$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);

Expand Down

0 comments on commit fced569

Please sign in to comment.