Skip to content

Commit 28520db

Browse files
ondrejmirtesclaude
andcommitted
Carry the closure body walk's gathered statements on ProcessClosureResult
processClosureNode() already sees every return, yield and execution end of the closure body on the scope they occur on. Record the statement-scope pairs and carry them - together with the impure points pre-merged in getClosureType() order - on ProcessClosureResult, so a closure type can later be built from the single walk instead of walking the body again. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01DaBZjgksga4c5s6Q9FniY7
1 parent 369bfdf commit 28520db

2 files changed

Lines changed: 75 additions & 3 deletions

File tree

‎src/Analyser/NodeScopeResolver.php‎

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3038,10 +3038,12 @@ public function processClosureNode(
30383038

30393039
$executionEnds = [];
30403040
$gatheredReturnStatements = [];
3041+
$gatheredReturnStatementsWithScope = [];
30413042
$gatheredYieldStatements = [];
3043+
$gatheredYieldStatementsWithScope = [];
30423044
$closureImpurePoints = [];
30433045
$invalidateExpressions = [];
3044-
$closureStmtsCallback = new GatheringNodeCallback(static function (Node $node, Scope $scope) use (&$executionEnds, &$gatheredReturnStatements, &$gatheredYieldStatements, &$closureScope, &$closureImpurePoints, &$invalidateExpressions): void {
3046+
$closureStmtsCallback = new GatheringNodeCallback(static function (Node $node, Scope $scope) use (&$executionEnds, &$gatheredReturnStatements, &$gatheredReturnStatementsWithScope, &$gatheredYieldStatements, &$gatheredYieldStatementsWithScope, &$closureScope, &$closureImpurePoints, &$invalidateExpressions): void {
30453047
if ($scope->getAnonymousFunctionReflection() !== $closureScope->getAnonymousFunctionReflection()) {
30463048
return;
30473049
}
@@ -3066,12 +3068,14 @@ public function processClosureNode(
30663068
}
30673069
if ($node instanceof Expr\Yield_ || $node instanceof Expr\YieldFrom) {
30683070
$gatheredYieldStatements[] = $node;
3071+
$gatheredYieldStatementsWithScope[] = [$node, $scope];
30693072
}
30703073
if (!$node instanceof Return_) {
30713074
return;
30723075
}
30733076

30743077
$gatheredReturnStatements[] = new ReturnStatement($scope, $node);
3078+
$gatheredReturnStatementsWithScope[] = [$node, $scope];
30753079
}, $nodeCallback);
30763080

30773081
if (count($byRefUses) === 0) {
@@ -3086,7 +3090,16 @@ public function processClosureNode(
30863090
array_merge($publicStatementResult->getImpurePoints(), $closureImpurePoints),
30873091
), $closureScope, $storage);
30883092

3089-
return new ProcessClosureResult($scope, $statementResult->getThrowPoints(), $statementResult->getImpurePoints(), $invalidateExpressions);
3093+
return new ProcessClosureResult(
3094+
$scope,
3095+
$statementResult->getThrowPoints(),
3096+
$statementResult->getImpurePoints(),
3097+
$invalidateExpressions,
3098+
$gatheredReturnStatementsWithScope,
3099+
$gatheredYieldStatementsWithScope,
3100+
$executionEnds,
3101+
array_merge($closureImpurePoints, $statementResult->getImpurePoints()),
3102+
);
30903103
}
30913104

30923105
$originalStorage = $storage;
@@ -3136,7 +3149,18 @@ public function processClosureNode(
31363149
array_merge($publicStatementResult->getImpurePoints(), $closureImpurePoints),
31373150
), $closureScope, $storage);
31383151

3139-
return new ProcessClosureResult($scope, $statementResult->getThrowPoints(), $statementResult->getImpurePoints(), $invalidateExpressions, $closureResultScope, $byRefUses);
3152+
return new ProcessClosureResult(
3153+
$scope,
3154+
$statementResult->getThrowPoints(),
3155+
$statementResult->getImpurePoints(),
3156+
$invalidateExpressions,
3157+
$gatheredReturnStatementsWithScope,
3158+
$gatheredYieldStatementsWithScope,
3159+
$executionEnds,
3160+
array_merge($closureImpurePoints, $statementResult->getImpurePoints()),
3161+
$closureResultScope,
3162+
$byRefUses,
3163+
);
31403164
}
31413165

31423166
/**

‎src/Analyser/ProcessClosureResult.php‎

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@
33
namespace PHPStan\Analyser;
44

55
use PhpParser\Node\ClosureUse;
6+
use PhpParser\Node\Expr\Yield_;
7+
use PhpParser\Node\Expr\YieldFrom;
8+
use PhpParser\Node\Stmt\Return_;
9+
use PHPStan\Node\ExecutionEndNode;
610
use PHPStan\Node\InvalidateExprNode;
711

812
final class ProcessClosureResult
@@ -12,13 +16,21 @@ final class ProcessClosureResult
1216
* @param InternalThrowPoint[] $throwPoints
1317
* @param ImpurePoint[] $impurePoints
1418
* @param InvalidateExprNode[] $invalidateExpressions
19+
* @param list<array{Return_, Scope}> $gatheredReturnStatements
20+
* @param list<array{Yield_|YieldFrom, Scope}> $gatheredYieldStatements
21+
* @param list<ExecutionEndNode> $executionEnds
22+
* @param ImpurePoint[] $closureTypeImpurePoints already merged in getClosureType() order (property-assign impure points first, then statement result impure points)
1523
* @param ClosureUse[] $byRefUses
1624
*/
1725
public function __construct(
1826
private MutatingScope $scope,
1927
private array $throwPoints,
2028
private array $impurePoints,
2129
private array $invalidateExpressions,
30+
private array $gatheredReturnStatements,
31+
private array $gatheredYieldStatements,
32+
private array $executionEnds,
33+
private array $closureTypeImpurePoints,
2234
private ?MutatingScope $byRefClosureResultScope = null,
2335
private array $byRefUses = [],
2436
)
@@ -63,4 +75,40 @@ public function getInvalidateExpressions(): array
6375
return $this->invalidateExpressions;
6476
}
6577

78+
/**
79+
* @return list<array{Return_, Scope}>
80+
*/
81+
public function getGatheredReturnStatements(): array
82+
{
83+
return $this->gatheredReturnStatements;
84+
}
85+
86+
/**
87+
* @return list<array{Yield_|YieldFrom, Scope}>
88+
*/
89+
public function getGatheredYieldStatements(): array
90+
{
91+
return $this->gatheredYieldStatements;
92+
}
93+
94+
/**
95+
* @return list<ExecutionEndNode>
96+
*/
97+
public function getExecutionEnds(): array
98+
{
99+
return $this->executionEnds;
100+
}
101+
102+
/**
103+
* The closure body's impure points already merged in getClosureType() order
104+
* (property-assign impure points first, then statement result impure points),
105+
* ready to feed ClosureTypeResolver::buildClosureTypeForClosure().
106+
*
107+
* @return ImpurePoint[]
108+
*/
109+
public function getClosureTypeImpurePoints(): array
110+
{
111+
return $this->closureTypeImpurePoints;
112+
}
113+
66114
}

0 commit comments

Comments
 (0)