Skip to content

Commit

Permalink
[Core] Fix resolve scope handling when parent Node just re-printed (#…
Browse files Browse the repository at this point in the history
…3056)

* [Core] Fix resolve scope handling when parent Node just re-printed

* comment

* Fix cs

* Fix cs

* Final touch: clean up phpstan ignore message

* Really Final touch: better comment
  • Loading branch information
samsonasik committed Nov 12, 2022
1 parent 295c489 commit 2be958b
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 28 deletions.
4 changes: 0 additions & 4 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -814,9 +814,5 @@ parameters:
message: '#"empty\(\$right\)" is forbidden to use#'
path: src/Util/ArrayParametersMerger.php

-
message: '#Cognitive complexity for "Rector\\Core\\Rector\\AbstractRector\:\:enterNode\(\)" is 11, keep it under 10#'
path: src/Rector/AbstractRector.php

# not relevant anymore
- '#Instead of "Symfony\\Component\\Finder\\SplFileInfo" class/interface use "Symplify\\SmartFileSystem\\SmartFileInfo"#'
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 AnotherClass $anotherClass;
private \Rector\Tests\Php74\Rector\Property\TypedPropertyRector\Source\AnotherClass $anotherClass;

public function __construct(AnotherClass $anotherClass)
{
Expand Down
8 changes: 8 additions & 0 deletions src/NodeAnalyzer/ScopeAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,14 @@ public function resolveScope(
return $this->scopeFactory->createFromFile($filePath);
}

/**
* Node and parent Node doesn't has Scope, and parent Node Start token pos is < 0,
* it means the node and parent node just re-printed, the Scope need to be resolved from file
*/
if ($parentNode->getStartTokenPos() < 0) {
return $this->scopeFactory->createFromFile($filePath);
}

return null;
}
}
26 changes: 3 additions & 23 deletions src/Rector/AbstractRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
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 @@ -253,33 +252,14 @@ 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 parent yet,
* it means returned with New Node instead of re-use existing Node
*/
if (
$refactoredNode instanceof Expression
|| $refactoredNode instanceof Return_
|| ! $refactoredNode->hasAttribute(AttributeKey::PARENT_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 2be958b

Please sign in to comment.