|
14 | 14 | use PHPStan\Analyser\InternalThrowPoint; |
15 | 15 | use PHPStan\Analyser\MutatingScope; |
16 | 16 | use PHPStan\Analyser\NodeScopeResolver; |
| 17 | +use PHPStan\Analyser\ProcessClosureResult; |
17 | 18 | use PHPStan\Analyser\Scope; |
18 | 19 | use PHPStan\Analyser\StatementContext; |
19 | 20 | use PHPStan\Analyser\ThrowPoint; |
@@ -85,7 +86,7 @@ public function getClosureType( |
85 | 86 | [$parameters, $isVariadic, $callableParameters, $nativeCallableParameters] = $this->buildParametersAndAcceptors($scope, $expr); |
86 | 87 |
|
87 | 88 | $cachedTypes = $expr->getAttribute('phpstanCachedTypes', []); |
88 | | - $cacheKey = $scope->getClosureScopeCacheKey(); |
| 89 | + $cacheKey = $this->closureContextCacheKey($scope, $expr, $callableParameters, $parameters); |
89 | 90 | if (!$expr instanceof ArrowFunction && array_key_exists($cacheKey, $cachedTypes)) { |
90 | 91 | return $this->createClosureTypeFromCache($expr, $parameters, $isVariadic, $cachedTypes[$cacheKey]); |
91 | 92 | } |
@@ -311,6 +312,46 @@ public function buildClosureTypeForArrowFunction( |
311 | 312 | )); |
312 | 313 | } |
313 | 314 |
|
| 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 | + |
314 | 355 | /** |
315 | 356 | * The cache key of everything this closure's type can depend on: the |
316 | 357 | * enclosing scope plus the parameter types the caller feeds in - |
@@ -730,7 +771,7 @@ private function assembleClosureType( |
730 | 771 | $impurePointsForClosureType = array_map(static fn (ImpurePoint $impurePoint) => new SimpleImpurePoint($impurePoint->getIdentifier(), $impurePoint->getDescription(), $impurePoint->isCertain()), $impurePoints); |
731 | 772 |
|
732 | 773 | $cachedTypes = $expr->getAttribute('phpstanCachedTypes', []); |
733 | | - $cacheKey ??= $scope->getClosureScopeCacheKey(); |
| 774 | + $cacheKey ??= $this->closureContextCacheKey($scope, $expr, null, $parameters); |
734 | 775 | $cachedTypes[$cacheKey] = [ |
735 | 776 | 'returnType' => $returnType, |
736 | 777 | 'throwPoints' => $throwPointsForClosureType, |
|
0 commit comments