Skip to content

Commit 38c5d3b

Browse files
ondrejmirtesclaude
andcommitted
Seed the closure type cache from the handler's single body walk
ClosureHandler::processExpr() walks the closure body for the rules; the lazy getClosureType() pricing then walked it again. The handler now seeds the per-node type cache from the gathered walk data, so the pricing asks answer without a second walk. The cache key becomes flavour- and parameter-aware (closureContextCacheKey), and the native-flavour slot is seeded with the same build: a closure's native type equals its phpdoc type, which until now only held when the promoted scope's holder hash happened to collide with the plain one - the pin is now deliberate and collision-independent. array_map() callbacks and immediately invoked closures seed through a getClosureType() walk with their call-site parameter types. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01DaBZjgksga4c5s6Q9FniY7
1 parent 46f8fc0 commit 38c5d3b

2 files changed

Lines changed: 44 additions & 2 deletions

File tree

‎src/Analyser/ExprHandler/ClosureHandler.php‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ public function supports(Expr $expr): bool
4242
public function processExpr(NodeScopeResolver $nodeScopeResolver, Stmt $stmt, Expr $expr, MutatingScope $scope, ExpressionResultStorage $storage, callable $nodeCallback, ExpressionContext $context): ExpressionResult
4343
{
4444
$processClosureResult = $nodeScopeResolver->processClosureNode($stmt, $expr, $scope, $storage, $nodeCallback, $context, null);
45+
$this->closureTypeResolver->seedCacheFromClosureWalk($scope, $expr, $processClosureResult);
4546

4647
return $this->expressionResultFactory->create(
4748
$processClosureResult->applyByRefUseScope($processClosureResult->getScope()),

‎src/Analyser/ExprHandler/Helper/ClosureTypeResolver.php‎

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use PHPStan\Analyser\InternalThrowPoint;
1515
use PHPStan\Analyser\MutatingScope;
1616
use PHPStan\Analyser\NodeScopeResolver;
17+
use PHPStan\Analyser\ProcessClosureResult;
1718
use PHPStan\Analyser\Scope;
1819
use PHPStan\Analyser\StatementContext;
1920
use PHPStan\Analyser\ThrowPoint;
@@ -85,7 +86,7 @@ public function getClosureType(
8586
[$parameters, $isVariadic, $callableParameters, $nativeCallableParameters] = $this->buildParametersAndAcceptors($scope, $expr);
8687

8788
$cachedTypes = $expr->getAttribute('phpstanCachedTypes', []);
88-
$cacheKey = $scope->getClosureScopeCacheKey();
89+
$cacheKey = $this->closureContextCacheKey($scope, $expr, $callableParameters, $parameters);
8990
if (!$expr instanceof ArrowFunction && array_key_exists($cacheKey, $cachedTypes)) {
9091
return $this->createClosureTypeFromCache($expr, $parameters, $isVariadic, $cachedTypes[$cacheKey]);
9192
}
@@ -311,6 +312,46 @@ public function buildClosureTypeForArrowFunction(
311312
));
312313
}
313314

315+
/**
316+
* Seeds the per-node closure type cache from the single body walk the
317+
* engine just performed (see ClosureHandler::processExpr()), so later
318+
* getClosureType() asks answer from the walk instead of walking the
319+
* body again.
320+
*
321+
* A closure's native type equals its phpdoc type - the native-flavour
322+
* slot is seeded with the same build, keyed exactly as the
323+
* promoted-scope ask computes its key.
324+
*/
325+
public function seedCacheFromClosureWalk(MutatingScope $scope, Node\Expr\Closure $expr, ProcessClosureResult $processClosureResult): void
326+
{
327+
// for array_map() callbacks and immediately invoked closures this
328+
// delegates to a getClosureType() walk with the call-site parameter
329+
// types; either way the phpdoc build lands in the cache under the
330+
// key the plain ask computes
331+
$this->buildClosureTypeForClosure(
332+
$scope,
333+
$expr,
334+
$processClosureResult->getGatheredReturnStatements(),
335+
$processClosureResult->getGatheredYieldStatements(),
336+
$processClosureResult->getExecutionEnds(),
337+
$processClosureResult->getThrowPoints(),
338+
$processClosureResult->getClosureTypeImpurePoints(),
339+
$processClosureResult->getInvalidateExpressions(),
340+
);
341+
342+
[$parameters, , $callableParameters] = $this->buildParametersAndAcceptors($scope, $expr);
343+
$phpdocKey = $this->closureContextCacheKey($scope, $expr, $callableParameters, $parameters);
344+
$cachedTypes = $expr->getAttribute('phpstanCachedTypes', []);
345+
if (!array_key_exists($phpdocKey, $cachedTypes)) {
346+
return;
347+
}
348+
349+
$promotedScope = $scope->doNotTreatPhpDocTypesAsCertain();
350+
[$promotedParameters, , $promotedCallableParameters] = $this->buildParametersAndAcceptors($promotedScope, $expr);
351+
$cachedTypes[$this->closureContextCacheKey($promotedScope, $expr, $promotedCallableParameters, $promotedParameters)] = $cachedTypes[$phpdocKey];
352+
$expr->setAttribute('phpstanCachedTypes', $cachedTypes);
353+
}
354+
314355
/**
315356
* The cache key of everything this closure's type can depend on: the
316357
* enclosing scope plus the parameter types the caller feeds in -
@@ -730,7 +771,7 @@ private function assembleClosureType(
730771
$impurePointsForClosureType = array_map(static fn (ImpurePoint $impurePoint) => new SimpleImpurePoint($impurePoint->getIdentifier(), $impurePoint->getDescription(), $impurePoint->isCertain()), $impurePoints);
731772

732773
$cachedTypes = $expr->getAttribute('phpstanCachedTypes', []);
733-
$cacheKey ??= $scope->getClosureScopeCacheKey();
774+
$cacheKey ??= $this->closureContextCacheKey($scope, $expr, null, $parameters);
734775
$cachedTypes[$cacheKey] = [
735776
'returnType' => $returnType,
736777
'throwPoints' => $throwPointsForClosureType,

0 commit comments

Comments
 (0)