Skip to content

Commit f67bc4e

Browse files
authored
[Performance] Only register WrappedNodeRestoringNodeVisitor() when there is found AlwaysRememberedExpr or Match_ node on processNodes() (#6620)
* [Performance] Only register WrappedNodeRestoringNodeVisitor() when there is found AlwaysRememberedExpr node on processNodes() * fix * fix
1 parent ebfb782 commit f67bc4e

File tree

1 file changed

+20
-4
lines changed

1 file changed

+20
-4
lines changed

src/NodeTypeResolver/PHPStan/Scope/PHPStanNodeScopeResolver.php

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@
8787
use PHPStan\Analyser\MutatingScope;
8888
use PHPStan\Analyser\NodeScopeResolver;
8989
use PHPStan\Analyser\ScopeContext;
90+
use PHPStan\Node\Expr\AlwaysRememberedExpr;
9091
use PHPStan\Node\FunctionCallableNode;
9192
use PHPStan\Node\InstantiationCallableNode;
9293
use PHPStan\Node\MethodCallableNode;
@@ -163,10 +164,13 @@ public function processNodes(
163164
$scope = $formerMutatingScope ?? $this->scopeFactory->createFromFile($filePath);
164165

165166
$hasUnreachableStatementNode = false;
167+
$hasAlwaysRememberedExpr = false;
168+
166169
$nodeCallback = function (Node $node, MutatingScope $mutatingScope) use (
167170
&$nodeCallback,
168171
$filePath,
169-
&$hasUnreachableStatementNode
172+
&$hasUnreachableStatementNode,
173+
&$hasAlwaysRememberedExpr
170174
): void {
171175
// the class reflection is resolved AFTER entering to class node
172176
// so we need to get it from the first after this one
@@ -364,8 +368,13 @@ public function processNodes(
364368
return;
365369
}
366370

367-
if ($node instanceof Match_) {
368-
$this->processMatch($node, $mutatingScope);
371+
if ($node instanceof AlwaysRememberedExpr || $node instanceof Match_) {
372+
$hasAlwaysRememberedExpr = true;
373+
374+
if ($node instanceof Match_) {
375+
$this->processMatch($node, $mutatingScope);
376+
}
377+
369378
return;
370379
}
371380

@@ -411,8 +420,15 @@ public function processNodes(
411420
RectorNodeScopeResolver::processNodes($stmts, $scope);
412421
}
413422

423+
if (! $hasAlwaysRememberedExpr && ! $hasUnreachableStatementNode) {
424+
return $stmts;
425+
}
426+
414427
$nodeTraverser = new NodeTraverser();
415-
$nodeTraverser->addVisitor(new WrappedNodeRestoringNodeVisitor());
428+
429+
if ($hasAlwaysRememberedExpr) {
430+
$nodeTraverser->addVisitor(new WrappedNodeRestoringNodeVisitor());
431+
}
416432

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

0 commit comments

Comments
 (0)