Skip to content

Commit

Permalink
[Performance] Reduce possible re-create Scope on ExprScopeFromStmtNod…
Browse files Browse the repository at this point in the history
…eVisitor (#4853)
  • Loading branch information
samsonasik committed Aug 25, 2023
1 parent 8c423b9 commit 55bde86
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ public function processNodes(

$nodeTraverser = new NodeTraverser();
$nodeTraverser->addVisitor(new WrappedNodeRestoringNodeVisitor());
$nodeTraverser->addVisitor(new ExprScopeFromStmtNodeVisitor($this->scopeFactory, $filePath));
$nodeTraverser->addVisitor(new ExprScopeFromStmtNodeVisitor($scope));
$nodeTraverser->traverse($stmts);

return $stmts;
Expand Down
13 changes: 5 additions & 8 deletions src/PHPStan/NodeVisitor/ExprScopeFromStmtNodeVisitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,14 @@
use PHPStan\Analyser\Scope;
use PHPStan\Node\VirtualNode;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\NodeTypeResolver\PHPStan\Scope\ScopeFactory;
use PHPStan\Analyser\MutatingScope;

final class ExprScopeFromStmtNodeVisitor extends NodeVisitorAbstract
{
private ?Stmt $currentStmt = null;

public function __construct(
private readonly ScopeFactory $scopeFactory,
private string $filePath
) {
public function __construct(private readonly MutatingScope $mutatingScope)
{
}

public function enterNode(Node $node): ?Node
Expand All @@ -44,12 +42,11 @@ public function enterNode(Node $node): ?Node
}

// too deep Expr, eg: $$param = $$bar = self::decodeValue($result->getItem()->getTextContent());
$filePath = $this->filePath;
$scope = $this->currentStmt instanceof Stmt
? $this->currentStmt->getAttribute(AttributeKey::SCOPE)
: $this->scopeFactory->createFromFile($filePath);
: $this->mutatingScope;

$scope = $scope instanceof Scope ? $scope : $this->scopeFactory->createFromFile($filePath);
$scope = $scope instanceof Scope ? $scope : $this->mutatingScope;

$node->setAttribute(AttributeKey::SCOPE, $scope);

Expand Down

0 comments on commit 55bde86

Please sign in to comment.