Skip to content

Commit

Permalink
[Performance] Only register WrappedNodeRestoringNodeVisitor() when th…
Browse files Browse the repository at this point in the history
…ere is found AlwaysRememberedExpr node on processNodes()
  • Loading branch information
samsonasik committed Dec 20, 2024
1 parent ebfb782 commit 12c94b5
Showing 1 changed file with 15 additions and 2 deletions.
17 changes: 15 additions & 2 deletions src/NodeTypeResolver/PHPStan/Scope/PHPStanNodeScopeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@
use PHPStan\Analyser\MutatingScope;
use PHPStan\Analyser\NodeScopeResolver;
use PHPStan\Analyser\ScopeContext;
use PHPStan\Node\Expr\AlwaysRememberedExpr;
use PHPStan\Node\FunctionCallableNode;
use PHPStan\Node\InstantiationCallableNode;
use PHPStan\Node\MethodCallableNode;
Expand Down Expand Up @@ -163,10 +164,13 @@ public function processNodes(
$scope = $formerMutatingScope ?? $this->scopeFactory->createFromFile($filePath);

$hasUnreachableStatementNode = false;
$hasAlwaysRememberedExpr = false;

$nodeCallback = function (Node $node, MutatingScope $mutatingScope) use (
&$nodeCallback,
$filePath,
&$hasUnreachableStatementNode
&$hasUnreachableStatementNode,
&$hasAlwaysRememberedExpr
): void {
// the class reflection is resolved AFTER entering to class node
// so we need to get it from the first after this one
Expand Down Expand Up @@ -198,6 +202,8 @@ public function processNodes(
// do not return early, as its properties will be checked next
if (! $node instanceof VirtualNode) {
$node->setAttribute(AttributeKey::SCOPE, $mutatingScope);
} elseif ($node instanceof AlwaysRememberedExpr) {
$hasAlwaysRememberedExpr = true;
}

if ($node instanceof FileWithoutNamespace) {
Expand Down Expand Up @@ -411,8 +417,15 @@ public function processNodes(
RectorNodeScopeResolver::processNodes($stmts, $scope);
}

if (! $hasAlwaysRememberedExpr && ! $hasUnreachableStatementNode) {
return $stmts;
}

$nodeTraverser = new NodeTraverser();
$nodeTraverser->addVisitor(new WrappedNodeRestoringNodeVisitor());

if ($hasAlwaysRememberedExpr) {
$nodeTraverser->addVisitor(new WrappedNodeRestoringNodeVisitor());
}

if ($hasUnreachableStatementNode) {
$nodeTraverser->addVisitor(new UnreachableStatementNodeVisitor($this, $filePath, $scope));
Expand Down

0 comments on commit 12c94b5

Please sign in to comment.