| 1: | <?php declare(strict_types = 1); |
| 2: | |
| 3: | namespace PHPStan\Analyser; |
| 4: | |
| 5: | use Closure; |
| 6: | use PhpParser\Node; |
| 7: | use PhpParser\Node\Expr; |
| 8: | use PhpParser\Node\Expr\ArrayDimFetch; |
| 9: | use PhpParser\Node\Expr\FuncCall; |
| 10: | use PhpParser\Node\Expr\List_; |
| 11: | use PhpParser\Node\Expr\MethodCall; |
| 12: | use PhpParser\Node\Expr\New_; |
| 13: | use PhpParser\Node\Expr\PropertyFetch; |
| 14: | use PhpParser\Node\Expr\StaticCall; |
| 15: | use PhpParser\Node\Expr\StaticPropertyFetch; |
| 16: | use PhpParser\Node\Expr\Variable; |
| 17: | use PhpParser\Node\Identifier; |
| 18: | use PhpParser\Node\Stmt\Class_; |
| 19: | use PhpParser\Node\Stmt\Echo_; |
| 20: | use PhpParser\Node\Stmt\Foreach_; |
| 21: | use PhpParser\Node\Stmt\If_; |
| 22: | use PhpParser\Node\Stmt\Return_; |
| 23: | use PhpParser\Node\Stmt\Static_; |
| 24: | use PhpParser\Node\Stmt\Switch_; |
| 25: | use PhpParser\NodeFinder; |
| 26: | use PHPStan\Analyser\ExprHandler\Helper\NonNullabilityHelper; |
| 27: | use PHPStan\Analyser\Generics\TemplateArgumentConstraints; |
| 28: | use PHPStan\Analyser\Generics\TemplateArgumentFrame; |
| 29: | use PHPStan\Analyser\Generics\TemplateArgumentObserver; |
| 30: | use PHPStan\Analyser\Generics\TemplateArgumentStats; |
| 31: | use PHPStan\Dependency\Dependencies; |
| 32: | use PHPStan\DependencyInjection\AutowiredExtensions; |
| 33: | use PHPStan\DependencyInjection\AutowiredService; |
| 34: | use PHPStan\DependencyInjection\Container; |
| 35: | use PHPStan\DependencyInjection\ExtensionsCollection; |
| 36: | use PHPStan\File\FileHelper; |
| 37: | use PHPStan\Node\FunctionCallableNode; |
| 38: | use PHPStan\Node\FunctionCallExpressionNode; |
| 39: | use PHPStan\Node\InstantiationCallableNode; |
| 40: | use PHPStan\Node\MethodCallableNode; |
| 41: | use PHPStan\Node\MethodCallExpressionNode; |
| 42: | use PHPStan\Node\StaticMethodCallableNode; |
| 43: | use PHPStan\Node\StaticMethodCallExpressionNode; |
| 44: | use PHPStan\Reflection\Php\PhpMethodFromParserNodeReflection; |
| 45: | use PHPStan\Reflection\Php\PhpPropertyReflection; |
| 46: | use PHPStan\ShouldNotHappenException; |
| 47: | use PHPStan\Turbo\ShadowedByTurboExtension; |
| 48: | use PHPStan\Type\ErrorType; |
| 49: | use PHPStan\Type\MixedType; |
| 50: | use PHPStan\Type\Type; |
| 51: | use PHPStan\Type\TypeUtils; |
| 52: | use function array_merge; |
| 53: | use function array_pop; |
| 54: | use function get_class; |
| 55: | use function getenv; |
| 56: | use function is_array; |
| 57: | use function is_string; |
| 58: | use function spl_object_id; |
| 59: | use function sprintf; |
| 60: | |
| 61: | #[AutowiredService] |
| 62: | #[ShadowedByTurboExtension(implementation: __DIR__ . '/../../turbo-ext/src/NodeScopeResolver.cpp')] |
| 63: | class NodeScopeResolver |
| 64: | { |
| 65: | |
| 66: | public const LOOP_SCOPE_ITERATIONS = 10; |
| 67: | public const GENERALIZE_AFTER_ITERATION = 1; |
| 68: | |
| 69: | |
| 70: | public const LOOP_SCOPE_FORGETTING_ITERATIONS_LIMIT = 10; |
| 71: | |
| 72: | |
| 73: | private array $analysedFiles = []; |
| 74: | |
| 75: | |
| 76: | |
| 77: | |
| 78: | |
| 79: | protected bool $returnStoredExpressionResults = false; |
| 80: | |
| 81: | |
| 82: | |
| 83: | |
| 84: | |
| 85: | |
| 86: | |
| 87: | |
| 88: | |
| 89: | |
| 90: | private ?Expr $consumeStoredExpression = null; |
| 91: | |
| 92: | private ?NonNullabilityHelper $nonNullabilityHelper = null; |
| 93: | |
| 94: | |
| 95: | |
| 96: | |
| 97: | |
| 98: | |
| 99: | |
| 100: | |
| 101: | |
| 102: | |
| 103: | private array $nodeGatherers = []; |
| 104: | |
| 105: | |
| 106: | public static bool $guardNewWorld = false; |
| 107: | |
| 108: | |
| 109: | |
| 110: | |
| 111: | |
| 112: | |
| 113: | |
| 114: | |
| 115: | |
| 116: | public static array $guardRealExprIds = []; |
| 117: | |
| 118: | |
| 119: | |
| 120: | |
| 121: | |
| 122: | |
| 123: | |
| 124: | |
| 125: | public static array $guardProcessedExprIds = []; |
| 126: | |
| 127: | |
| 128: | |
| 129: | |
| 130: | public function __construct( |
| 131: | private readonly Container $container, |
| 132: | private readonly TemplateArgumentObserver $templateArgumentObserver, |
| 133: | private readonly FileHelper $fileHelper, |
| 134: | #[AutowiredExtensions(of: PerFileAnalysisResettable::class)] |
| 135: | private readonly ExtensionsCollection $perFileAnalysisResettables, |
| 136: | private readonly ExpressionResultFactory $expressionResultFactory, |
| 137: | private readonly StatementsHandler $statementsHandler, |
| 138: | ) |
| 139: | { |
| 140: | self::$guardNewWorld = getenv('PHPSTAN_GUARD_NW') === '1'; |
| 141: | TemplateArgumentStats::enableFromEnvironment(); |
| 142: | } |
| 143: | |
| 144: | |
| 145: | |
| 146: | |
| 147: | |
| 148: | |
| 149: | |
| 150: | |
| 151: | |
| 152: | |
| 153: | public function setAnalysedFiles(array $files): void |
| 154: | { |
| 155: | $analysedFiles = []; |
| 156: | foreach ($files as $file) { |
| 157: | $analysedFiles[$this->fileHelper->normalizePath($file)] = true; |
| 158: | } |
| 159: | $this->analysedFiles = $analysedFiles; |
| 160: | } |
| 161: | |
| 162: | |
| 163: | |
| 164: | |
| 165: | |
| 166: | |
| 167: | |
| 168: | |
| 169: | |
| 170: | |
| 171: | |
| 172: | |
| 173: | private function getNonNullabilityHelper(): NonNullabilityHelper |
| 174: | { |
| 175: | return $this->nonNullabilityHelper ??= $this->container->getByType(NonNullabilityHelper::class); |
| 176: | } |
| 177: | |
| 178: | public function resetPerFileAnalysisState(): void |
| 179: | { |
| 180: | foreach ($this->perFileAnalysisResettables->getAll() as $resettableService) { |
| 181: | $resettableService->resetFileAnalysisState(); |
| 182: | } |
| 183: | } |
| 184: | |
| 185: | |
| 186: | |
| 187: | |
| 188: | |
| 189: | |
| 190: | public function processNodes( |
| 191: | array $nodes, |
| 192: | MutatingScope $scope, |
| 193: | callable $nodeCallback, |
| 194: | ): void |
| 195: | { |
| 196: | $this->processFileNodes($nodes, $scope, $nodeCallback); |
| 197: | } |
| 198: | |
| 199: | |
| 200: | |
| 201: | |
| 202: | |
| 203: | |
| 204: | |
| 205: | public function processFileNodes( |
| 206: | array $nodes, |
| 207: | MutatingScope $scope, |
| 208: | callable $nodeCallback, |
| 209: | ): ?Dependencies |
| 210: | { |
| 211: | $scope = $scope->toWalkScope(); |
| 212: | if (self::$guardNewWorld) { |
| 213: | self::$guardRealExprIds = []; |
| 214: | self::$guardProcessedExprIds = []; |
| 215: | foreach ((new NodeFinder())->findInstanceOf($nodes, Expr::class) as $realExpr) { |
| 216: | self::$guardRealExprIds[spl_object_id($realExpr)] = true; |
| 217: | } |
| 218: | } |
| 219: | |
| 220: | $expressionResultStorage = new ExpressionResultStorage(); |
| 221: | $scope->pushExpressionResultStorage($expressionResultStorage); |
| 222: | |
| 223: | |
| 224: | $gatherers = $this->suspendNodeGatherers(); |
| 225: | try { |
| 226: | return $this->statementsHandler->processNodesWithStorage($this, $nodes, $scope, $expressionResultStorage, $nodeCallback); |
| 227: | } finally { |
| 228: | $this->restoreNodeGatherers($gatherers); |
| 229: | $scope->popExpressionResultStorage(); |
| 230: | } |
| 231: | } |
| 232: | |
| 233: | public function storeExpressionResult(ExpressionResultStorage $storage, Expr $expr, ExpressionResult $expressionResult): void |
| 234: | { |
| 235: | if (self::$guardNewWorld) { |
| 236: | self::$guardProcessedExprIds[spl_object_id($expr)] = true; |
| 237: | } |
| 238: | |
| 239: | $storage->storeExpressionResult($expr, $expressionResult); |
| 240: | } |
| 241: | |
| 242: | |
| 243: | |
| 244: | |
| 245: | |
| 246: | |
| 247: | public function narrowScopeWithCondition(MutatingScope $scope, Expr $expr, TypeSpecifierContext $context): MutatingScope |
| 248: | { |
| 249: | $specifiedTypes = $scope->specifyTypesOfNewWorldHandlerNode($expr, $context); |
| 250: | |
| 251: | return $scope->applySpecifiedTypes($specifiedTypes); |
| 252: | } |
| 253: | |
| 254: | |
| 255: | |
| 256: | |
| 257: | |
| 258: | |
| 259: | public function processStmtNodes( |
| 260: | Node $parentNode, |
| 261: | array $stmts, |
| 262: | MutatingScope $scope, |
| 263: | callable $nodeCallback, |
| 264: | StatementContext $context, |
| 265: | ): StatementResult |
| 266: | { |
| 267: | |
| 268: | |
| 269: | |
| 270: | |
| 271: | $scope = $scope->toWalkScope(); |
| 272: | $storage = new ExpressionResultStorage(); |
| 273: | $scope->pushExpressionResultStorage($storage); |
| 274: | |
| 275: | |
| 276: | |
| 277: | $gatherers = $this->suspendNodeGatherers(); |
| 278: | try { |
| 279: | return $this->processStmtNodesInternal( |
| 280: | $parentNode, |
| 281: | $stmts, |
| 282: | $scope, |
| 283: | $storage, |
| 284: | $nodeCallback, |
| 285: | $context, |
| 286: | )->toPublic(); |
| 287: | } finally { |
| 288: | $this->restoreNodeGatherers($gatherers); |
| 289: | $scope->popExpressionResultStorage(); |
| 290: | } |
| 291: | } |
| 292: | |
| 293: | |
| 294: | |
| 295: | |
| 296: | |
| 297: | |
| 298: | |
| 299: | |
| 300: | |
| 301: | public function processLoopFinalStmtNodes( |
| 302: | Node $parentNode, |
| 303: | array $stmts, |
| 304: | MutatingScope $scope, |
| 305: | ExpressionResultStorage $storage, |
| 306: | callable $nodeCallback, |
| 307: | StatementContext $context, |
| 308: | array $lastPass, |
| 309: | ): InternalStatementResult |
| 310: | { |
| 311: | $pushStorage = $scope->getCurrentExpressionResultStorage() !== $storage; |
| 312: | if ($pushStorage) { |
| 313: | $scope->pushExpressionResultStorage($storage); |
| 314: | } |
| 315: | try { |
| 316: | return $this->statementsHandler->processLoopFinalStmtNodes($this, $parentNode, $stmts, $scope, $storage, $nodeCallback, $context, $lastPass); |
| 317: | } finally { |
| 318: | if ($pushStorage) { |
| 319: | $scope->popExpressionResultStorage(); |
| 320: | } |
| 321: | } |
| 322: | } |
| 323: | |
| 324: | |
| 325: | |
| 326: | |
| 327: | |
| 328: | |
| 329: | |
| 330: | |
| 331: | |
| 332: | |
| 333: | public function processLoopPassStmtNodes( |
| 334: | Node $parentNode, |
| 335: | array $stmts, |
| 336: | MutatingScope $scope, |
| 337: | ExpressionResultStorage $storage, |
| 338: | callable $nodeCallback, |
| 339: | StatementContext $context, |
| 340: | ?array $previous, |
| 341: | ?array &$record, |
| 342: | ): InternalStatementResult |
| 343: | { |
| 344: | $pushStorage = $scope->getCurrentExpressionResultStorage() !== $storage; |
| 345: | if ($pushStorage) { |
| 346: | $scope->pushExpressionResultStorage($storage); |
| 347: | } |
| 348: | try { |
| 349: | return $this->statementsHandler->processLoopPassStmtNodes($this, $parentNode, $stmts, $scope, $storage, $nodeCallback, $context, $previous, $record); |
| 350: | } finally { |
| 351: | if ($pushStorage) { |
| 352: | $scope->popExpressionResultStorage(); |
| 353: | } |
| 354: | } |
| 355: | } |
| 356: | |
| 357: | |
| 358: | |
| 359: | |
| 360: | |
| 361: | public function processStmtNodesInternal( |
| 362: | Node $parentNode, |
| 363: | array $stmts, |
| 364: | MutatingScope $scope, |
| 365: | ExpressionResultStorage $storage, |
| 366: | callable $nodeCallback, |
| 367: | StatementContext $context, |
| 368: | ): InternalStatementResult |
| 369: | { |
| 370: | |
| 371: | |
| 372: | |
| 373: | |
| 374: | |
| 375: | |
| 376: | $pushStorage = $scope->getCurrentExpressionResultStorage() !== $storage; |
| 377: | if ($pushStorage) { |
| 378: | $scope->pushExpressionResultStorage($storage); |
| 379: | } |
| 380: | try { |
| 381: | return $this->statementsHandler->doProcessStmtNodes($this, $parentNode, $stmts, $scope, $storage, $nodeCallback, $context); |
| 382: | } finally { |
| 383: | if ($pushStorage) { |
| 384: | $scope->popExpressionResultStorage(); |
| 385: | } |
| 386: | } |
| 387: | } |
| 388: | |
| 389: | |
| 390: | |
| 391: | |
| 392: | public function processStmtNode( |
| 393: | Node\Stmt $stmt, |
| 394: | MutatingScope $scope, |
| 395: | ExpressionResultStorage $storage, |
| 396: | callable $nodeCallback, |
| 397: | StatementContext $context, |
| 398: | ): InternalStatementResult |
| 399: | { |
| 400: | $overridingThrowPoints = null; |
| 401: | if ( |
| 402: | !$stmt instanceof Static_ |
| 403: | && !$stmt instanceof Node\Stmt\Global_ |
| 404: | && !$stmt instanceof Node\Stmt\Property |
| 405: | && !$stmt instanceof Node\Stmt\ClassConst |
| 406: | && !$stmt instanceof Node\Stmt\Const_ |
| 407: | && !$stmt instanceof Node\Stmt\ClassLike |
| 408: | && !$stmt instanceof Node\Stmt\Function_ |
| 409: | && !$stmt instanceof Node\Stmt\ClassMethod |
| 410: | ) { |
| 411: | if (!$stmt instanceof Foreach_) { |
| 412: | $scope = $this->statementsHandler->processStmtVarAnnotation($this, $scope, $storage, $stmt, null, $nodeCallback); |
| 413: | } |
| 414: | $overridingThrowPoints = $this->statementsHandler->getOverridingThrowPoints($stmt, $scope); |
| 415: | } |
| 416: | $varTagDependencies = $this->statementsHandler->getVarTagDependencies($stmt, $scope); |
| 417: | |
| 418: | if ($stmt instanceof Node\Stmt\ClassMethod) { |
| 419: | |
| 420: | |
| 421: | |
| 422: | |
| 423: | |
| 424: | if (!$scope->isInClass()) { |
| 425: | throw new ShouldNotHappenException(); |
| 426: | } |
| 427: | if ($scope->isInTrait()) { |
| 428: | $nativeClassReflection = $scope->getClassReflection()->getNativeReflection(); |
| 429: | $methodName = $stmt->name->toString(); |
| 430: | if ( |
| 431: | $nativeClassReflection->hasMethod($methodName) |
| 432: | && $nativeClassReflection->getMethod($methodName)->getBetterReflection()->getDeclaringClass()->getName() !== $scope->getTraitReflection()->getName() |
| 433: | ) { |
| 434: | return new InternalStatementResult($scope, hasYield: false, isAlwaysTerminating: false, exitPoints: [], throwPoints: [], impurePoints: []); |
| 435: | } |
| 436: | } |
| 437: | } |
| 438: | |
| 439: | |
| 440: | |
| 441: | |
| 442: | |
| 443: | |
| 444: | $deferredStmtCallback = $stmt instanceof Return_ || $stmt instanceof Node\Stmt\Expression || $stmt instanceof Echo_ |
| 445: | || $stmt instanceof If_ || $stmt instanceof Switch_ || $stmt instanceof Foreach_ |
| 446: | || $stmt instanceof Node\Stmt\Unset_ || $stmt instanceof Node\Stmt\ClassConst |
| 447: | || $stmt instanceof Node\Stmt\Const_ || $stmt instanceof Node\Stmt\While_ |
| 448: | || $stmt instanceof Node\Stmt\Do_; |
| 449: | if (!$deferredStmtCallback) { |
| 450: | $this->callNodeCallback($nodeCallback, $stmt, $scope, $storage); |
| 451: | } |
| 452: | |
| 453: | $stmtHandler = StmtHandlerRegistry::resolve($stmt, $this->container); |
| 454: | if ($stmtHandler !== null) { |
| 455: | $stmtResult = $stmtHandler->processStmt($this, $stmt, $scope, $storage, $nodeCallback, $context); |
| 456: | if ($varTagDependencies !== null) { |
| 457: | $stmtResult = $stmtResult->withDependencies(Dependencies::merge($stmtResult->getDependencies(), $varTagDependencies)); |
| 458: | } |
| 459: | if ($overridingThrowPoints !== null) { |
| 460: | |
| 461: | |
| 462: | $overridingThrowFlows = []; |
| 463: | foreach ($overridingThrowPoints as $overridingThrowPoint) { |
| 464: | $overridingThrowFlows[] = VariableFlow::throwing($overridingThrowPoint->getType(), true, $overridingThrowPoint->canContainAnyThrowable()); |
| 465: | } |
| 466: | |
| 467: | return new InternalStatementResult( |
| 468: | $stmtResult->getScope(), |
| 469: | hasYield: $stmtResult->hasYield(), |
| 470: | isAlwaysTerminating: $stmtResult->isAlwaysTerminating(), |
| 471: | exitPoints: $stmtResult->getExitPoints(), |
| 472: | throwPoints: $overridingThrowPoints, |
| 473: | impurePoints: $stmtResult->getImpurePoints(), |
| 474: | endStatements: $stmtResult->getEndStatements(), |
| 475: | variableFlow: VariableFlow::sequence(...$overridingThrowFlows, ...[$stmtResult->getVariableFlow()]), |
| 476: | dependencies: $stmtResult->getDependencies(), |
| 477: | ); |
| 478: | } |
| 479: | |
| 480: | return $stmtResult; |
| 481: | } |
| 482: | |
| 483: | |
| 484: | return new InternalStatementResult($scope, hasYield: false, isAlwaysTerminating: false, exitPoints: [], throwPoints: $overridingThrowPoints ?? [], impurePoints: [], dependencies: $varTagDependencies); |
| 485: | } |
| 486: | |
| 487: | public function isAnalysedFile(string $fileName): bool |
| 488: | { |
| 489: | return isset($this->analysedFiles[$fileName]); |
| 490: | } |
| 491: | |
| 492: | |
| 493: | public function isReturningStoredExpressionResults(): bool |
| 494: | { |
| 495: | return $this->returnStoredExpressionResults; |
| 496: | } |
| 497: | |
| 498: | public function lookForSetAllowedUndefinedExpressions(MutatingScope $scope, Expr $expr): MutatingScope |
| 499: | { |
| 500: | return $this->lookForExpressionCallback($scope, $expr, static fn (MutatingScope $scope, Expr $expr): MutatingScope => $scope->setAllowedUndefinedExpression($expr)); |
| 501: | } |
| 502: | |
| 503: | public function lookForUnsetAllowedUndefinedExpressions(MutatingScope $scope, Expr $expr): MutatingScope |
| 504: | { |
| 505: | return $this->lookForExpressionCallback($scope, $expr, static fn (MutatingScope $scope, Expr $expr): MutatingScope => $scope->unsetAllowedUndefinedExpression($expr)); |
| 506: | } |
| 507: | |
| 508: | |
| 509: | |
| 510: | |
| 511: | private function lookForExpressionCallback(MutatingScope $scope, Expr $expr, Closure $callback): MutatingScope |
| 512: | { |
| 513: | if (!$expr instanceof ArrayDimFetch || $expr->dim !== null) { |
| 514: | $scope = $callback($scope, $expr); |
| 515: | } |
| 516: | |
| 517: | if ($expr instanceof ArrayDimFetch) { |
| 518: | $scope = $this->lookForExpressionCallback($scope, $expr->var, $callback); |
| 519: | } elseif ($expr instanceof PropertyFetch || $expr instanceof Expr\NullsafePropertyFetch || $expr instanceof Expr\NullsafeMethodCall) { |
| 520: | $scope = $this->lookForExpressionCallback($scope, $expr->var, $callback); |
| 521: | } elseif ($expr instanceof StaticPropertyFetch && $expr->class instanceof Expr) { |
| 522: | $scope = $this->lookForExpressionCallback($scope, $expr->class, $callback); |
| 523: | } elseif ($expr instanceof List_) { |
| 524: | foreach ($expr->items as $item) { |
| 525: | if ($item === null) { |
| 526: | continue; |
| 527: | } |
| 528: | |
| 529: | $scope = $this->lookForExpressionCallback($scope, $item->value, $callback); |
| 530: | } |
| 531: | } |
| 532: | |
| 533: | return $scope; |
| 534: | } |
| 535: | |
| 536: | |
| 537: | |
| 538: | |
| 539: | |
| 540: | |
| 541: | |
| 542: | |
| 543: | |
| 544: | |
| 545: | |
| 546: | |
| 547: | |
| 548: | |
| 549: | |
| 550: | |
| 551: | public function processExprNodeConsumingStored(Node\Stmt $stmt, Expr $expr, Expr $consumedExpr, MutatingScope $scope, ExpressionResultStorage $storage, callable $nodeCallback, ExpressionContext $context): ExpressionResult |
| 552: | { |
| 553: | $previous = $this->consumeStoredExpression; |
| 554: | $this->consumeStoredExpression = $consumedExpr; |
| 555: | try { |
| 556: | return $this->processExprNode($stmt, $expr, $scope, $storage, $nodeCallback, $context); |
| 557: | } finally { |
| 558: | $this->consumeStoredExpression = $previous; |
| 559: | } |
| 560: | } |
| 561: | |
| 562: | public function processExprOnDemand(Expr $expr, MutatingScope $scope, ExpressionResultStorage $storage): ExpressionResult |
| 563: | { |
| 564: | |
| 565: | |
| 566: | |
| 567: | |
| 568: | if ( |
| 569: | ExprHandlerRegistry::resolve($expr, $this->container) === null |
| 570: | && !($expr instanceof Expr\CallLike && $expr->isPartialFunctionApplication()) |
| 571: | ) { |
| 572: | $mixed = new MixedType(); |
| 573: | return $this->expressionResultFactory->create( |
| 574: | $scope, |
| 575: | beforeScope: $scope, |
| 576: | expr: $expr, |
| 577: | hasYield: false, |
| 578: | isAlwaysTerminating: false, |
| 579: | throwPoints: [], |
| 580: | impurePoints: [], |
| 581: | typeCallback: null, |
| 582: | specifyTypesCallback: SpecifiedTypes::emptySpecifyCallback(), |
| 583: | type: $mixed, |
| 584: | nativeType: $mixed, |
| 585: | ); |
| 586: | } |
| 587: | |
| 588: | |
| 589: | |
| 590: | |
| 591: | |
| 592: | |
| 593: | $previous = $this->returnStoredExpressionResults; |
| 594: | $this->returnStoredExpressionResults = true; |
| 595: | $scope->pushExpressionResultStorage($storage); |
| 596: | try { |
| 597: | return $this->processExprNode( |
| 598: | new Node\Stmt\Expression($expr), |
| 599: | $expr, |
| 600: | $scope, |
| 601: | $storage, |
| 602: | new NoopNodeCallback(), |
| 603: | ExpressionContext::createTopLevel(resolveTemplateArguments: false), |
| 604: | ); |
| 605: | } finally { |
| 606: | $scope->popExpressionResultStorage(); |
| 607: | $this->returnStoredExpressionResults = $previous; |
| 608: | } |
| 609: | } |
| 610: | |
| 611: | |
| 612: | |
| 613: | |
| 614: | |
| 615: | |
| 616: | |
| 617: | |
| 618: | public function readStoredResult(Expr $expr, ExpressionResultStorage $storage): ExpressionResult |
| 619: | { |
| 620: | $result = $storage->findExpressionResult($expr); |
| 621: | if ($result === null) { |
| 622: | throw new ShouldNotHappenException(sprintf( |
| 623: | '%s on line %d has no stored ExpressionResult - it was not processed by processExprNode().', |
| 624: | get_class($expr), |
| 625: | $expr->getStartLine(), |
| 626: | )); |
| 627: | } |
| 628: | |
| 629: | return $result; |
| 630: | } |
| 631: | |
| 632: | |
| 633: | |
| 634: | |
| 635: | |
| 636: | |
| 637: | |
| 638: | |
| 639: | public function readTypeOfMaybeStored(Expr $expr, MutatingScope $scope): Type |
| 640: | { |
| 641: | $storage = $scope->getCurrentExpressionResultStorage(); |
| 642: | $result = $storage?->findExpressionResult($expr); |
| 643: | if ($result !== null) { |
| 644: | return $result->getTypeOnScope($scope, $scope->nativeTypesPromoted); |
| 645: | } |
| 646: | |
| 647: | return $this->readScopeStateOrSyntheticType($expr, $scope); |
| 648: | } |
| 649: | |
| 650: | |
| 651: | |
| 652: | |
| 653: | |
| 654: | |
| 655: | |
| 656: | |
| 657: | |
| 658: | |
| 659: | |
| 660: | public function findScopeStateType(Expr $expr, MutatingScope $scope): ?Type |
| 661: | { |
| 662: | if ($expr instanceof Expr\Variable && is_string($expr->name)) { |
| 663: | if ($scope->hasVariableType($expr->name)->no()) { |
| 664: | return new ErrorType(); |
| 665: | } |
| 666: | |
| 667: | return $scope->getVariableType($expr->name); |
| 668: | } |
| 669: | |
| 670: | |
| 671: | |
| 672: | |
| 673: | if ($expr instanceof Node\Scalar\String_ || $expr instanceof Node\Scalar\Int_ || $expr instanceof Node\Scalar\Float_) { |
| 674: | return $scope->getStateType($expr); |
| 675: | } |
| 676: | |
| 677: | |
| 678: | |
| 679: | |
| 680: | if ( |
| 681: | !$expr instanceof Expr\Closure |
| 682: | && !$expr instanceof Expr\ArrowFunction |
| 683: | && $scope->hasExpressionType($expr)->yes() |
| 684: | ) { |
| 685: | return TypeUtils::resolveLateResolvableTypes($scope->getTrackedExpressionType($expr)); |
| 686: | } |
| 687: | |
| 688: | return null; |
| 689: | } |
| 690: | |
| 691: | |
| 692: | |
| 693: | |
| 694: | |
| 695: | |
| 696: | |
| 697: | public function readScopeStateOrSyntheticType(Expr $expr, MutatingScope $scope): Type |
| 698: | { |
| 699: | return $this->findScopeStateType($expr, $scope) ?? $this->processSyntheticOnDemand($expr, $scope)->getTypeOnScope($scope, $scope->nativeTypesPromoted); |
| 700: | } |
| 701: | |
| 702: | |
| 703: | |
| 704: | |
| 705: | |
| 706: | |
| 707: | |
| 708: | public function requireScopeStateType(Expr $expr, MutatingScope $scope): Type |
| 709: | { |
| 710: | $type = $this->findScopeStateType($expr, $scope); |
| 711: | if ($type === null) { |
| 712: | throw new ShouldNotHappenException(sprintf( |
| 713: | '%s on line %d is not tracked on the scope it was pinned as tracked on.', |
| 714: | get_class($expr), |
| 715: | $expr->getStartLine(), |
| 716: | )); |
| 717: | } |
| 718: | |
| 719: | return $type; |
| 720: | } |
| 721: | |
| 722: | |
| 723: | |
| 724: | |
| 725: | |
| 726: | |
| 727: | |
| 728: | |
| 729: | private function guardAgainstUnprocessedRealNode(Expr $expr, string $caller): void |
| 730: | { |
| 731: | if ( |
| 732: | !self::$guardNewWorld |
| 733: | || !isset(self::$guardRealExprIds[spl_object_id($expr)]) |
| 734: | || isset(self::$guardProcessedExprIds[spl_object_id($expr)]) |
| 735: | ) { |
| 736: | return; |
| 737: | } |
| 738: | |
| 739: | throw new ShouldNotHappenException(sprintf( |
| 740: | '%s() asked about non-synthetic %s on line %d before it was processed by processExprNode() - it should consume the node\'s ExpressionResult instead.', |
| 741: | $caller, |
| 742: | get_class($expr), |
| 743: | $expr->getStartLine(), |
| 744: | )); |
| 745: | } |
| 746: | |
| 747: | |
| 748: | |
| 749: | |
| 750: | |
| 751: | |
| 752: | |
| 753: | |
| 754: | public function processSyntheticOnDemand(Expr $expr, MutatingScope $scope): ExpressionResult |
| 755: | { |
| 756: | $this->guardAgainstUnprocessedRealNode($expr, __FUNCTION__); |
| 757: | $current = $scope->getCurrentExpressionResultStorage() ?? new ExpressionResultStorage(); |
| 758: | |
| 759: | return $this->processExprOnDemand($expr, $scope, $current->duplicate()); |
| 760: | } |
| 761: | |
| 762: | |
| 763: | |
| 764: | |
| 765: | public function processExprNode( |
| 766: | Node\Stmt $stmt, |
| 767: | Expr $expr, |
| 768: | MutatingScope $scope, |
| 769: | ExpressionResultStorage $storage, |
| 770: | callable $nodeCallback, |
| 771: | ExpressionContext $context, |
| 772: | ): ExpressionResult |
| 773: | { |
| 774: | $consume = $this->consumeStoredExpression === $expr; |
| 775: | if ($this->returnStoredExpressionResults || $consume) { |
| 776: | $storedResult = $storage->findExpressionResult($expr); |
| 777: | |
| 778: | |
| 779: | |
| 780: | |
| 781: | |
| 782: | |
| 783: | |
| 784: | if ($storedResult !== null && ($consume || $storedResult->askScopeVariableStateMatches($scope, $scope->nativeTypesPromoted))) { |
| 785: | |
| 786: | |
| 787: | |
| 788: | if ($storedResult->getBeforeScope() === $scope) { |
| 789: | return $storedResult; |
| 790: | } |
| 791: | |
| 792: | $reanchored = $storedResult->atAskPosition($scope); |
| 793: | if ($consume) { |
| 794: | |
| 795: | |
| 796: | |
| 797: | |
| 798: | |
| 799: | $this->storeExpressionResult($storage, $expr, $reanchored); |
| 800: | } |
| 801: | |
| 802: | return $reanchored; |
| 803: | } |
| 804: | } |
| 805: | |
| 806: | return $this->processExprNodeInternal($stmt, $expr, $scope, $storage, $nodeCallback, $context); |
| 807: | } |
| 808: | |
| 809: | |
| 810: | |
| 811: | |
| 812: | private function processExprNodeInternal( |
| 813: | Node\Stmt $stmt, |
| 814: | Expr $expr, |
| 815: | MutatingScope $scope, |
| 816: | ExpressionResultStorage $storage, |
| 817: | callable $nodeCallback, |
| 818: | ExpressionContext $context, |
| 819: | ): ExpressionResult |
| 820: | { |
| 821: | |
| 822: | |
| 823: | if ($expr instanceof Expr\CallLike && !$expr instanceof Expr\NullsafeMethodCall && $expr->isPartialFunctionApplication()) { |
| 824: | if ($expr instanceof FuncCall) { |
| 825: | $newExpr = new FunctionCallableNode($expr->name, $expr); |
| 826: | } elseif ($expr instanceof MethodCall) { |
| 827: | $newExpr = new MethodCallableNode($expr->var, $expr->name, $expr); |
| 828: | } elseif ($expr instanceof StaticCall) { |
| 829: | $newExpr = new StaticMethodCallableNode($expr->class, $expr->name, $expr); |
| 830: | } elseif ($expr instanceof New_ && !$expr->class instanceof Class_) { |
| 831: | $newExpr = new InstantiationCallableNode($expr->class, $expr); |
| 832: | } else { |
| 833: | throw new ShouldNotHappenException(); |
| 834: | } |
| 835: | |
| 836: | $newExprResult = $this->processExprNode($stmt, $newExpr, $scope, $storage, $nodeCallback, $context); |
| 837: | $expressionResult = $this->expressionResultFactory->create( |
| 838: | $newExprResult->getScope(), |
| 839: | beforeScope: $scope, |
| 840: | expr: $expr, |
| 841: | hasYield: $newExprResult->hasYield(), |
| 842: | isAlwaysTerminating: $newExprResult->isAlwaysTerminating(), |
| 843: | throwPoints: $newExprResult->getThrowPoints(), |
| 844: | impurePoints: $newExprResult->getImpurePoints(), |
| 845: | variableFlow: $newExprResult->getVariableFlow(), |
| 846: | |
| 847: | |
| 848: | typeCallback: static fn (bool $nativeTypesPromoted): Type => ($nativeTypesPromoted ? $newExprResult->getNativeType() : $newExprResult->getType()), |
| 849: | specifyTypesCallback: SpecifiedTypes::emptySpecifyCallback(), |
| 850: | dependencies: $newExprResult->getDependencies(), |
| 851: | ); |
| 852: | $this->storeExpressionResult($storage, $expr, $expressionResult); |
| 853: | return $expressionResult; |
| 854: | } |
| 855: | |
| 856: | $exprHandler = ExprHandlerRegistry::resolve($expr, $this->container); |
| 857: | if ($exprHandler !== null) { |
| 858: | $expressionResult = $exprHandler->processExpr($this, $stmt, $expr, $scope, $storage, $nodeCallback, $context); |
| 859: | |
| 860: | |
| 861: | |
| 862: | $expressionResult = $this->getNonNullabilityHelper()->applyPendingEnsure($expr, $expressionResult); |
| 863: | $this->storeExpressionResult($storage, $expr, $expressionResult); |
| 864: | |
| 865: | |
| 866: | $frame = $scope->getCurrentTemplateArgumentFrame(); |
| 867: | if ( |
| 868: | $frame !== null && $frame->isObserving() |
| 869: | && $expr instanceof Expr\CallLike |
| 870: | ) { |
| 871: | $constraints = $this->templateArgumentObserver->collectSites($expressionResult->getType()); |
| 872: | $expressionResult = $expressionResult->withScope($expressionResult->getScope()->addTemplateArgumentConstraints($constraints)); |
| 873: | $this->storeExpressionResult($storage, $expr, $expressionResult); |
| 874: | } |
| 875: | |
| 876: | |
| 877: | |
| 878: | |
| 879: | |
| 880: | |
| 881: | |
| 882: | $this->callNodeCallbackWithExpression($nodeCallback, $expr, $scope, $storage, $context); |
| 883: | |
| 884: | |
| 885: | |
| 886: | if ($expr instanceof FuncCall) { |
| 887: | $this->callNodeCallbackWithExpression($nodeCallback, new FunctionCallExpressionNode($expr, $expressionResult, $expressionResult->getArgsResult()), $scope, $storage, $context); |
| 888: | } elseif ($expr instanceof MethodCall) { |
| 889: | $this->callNodeCallbackWithExpression($nodeCallback, new MethodCallExpressionNode($expr, $expressionResult, $expressionResult->getArgsResult()), $scope, $storage, $context); |
| 890: | } elseif ($expr instanceof StaticCall) { |
| 891: | $this->callNodeCallbackWithExpression($nodeCallback, new StaticMethodCallExpressionNode($expr, $expressionResult, $expressionResult->getArgsResult()), $scope, $storage, $context); |
| 892: | } |
| 893: | return $expressionResult; |
| 894: | } |
| 895: | |
| 896: | throw new ShouldNotHappenException(sprintf('Unhandled expr: %s', get_class($expr))); |
| 897: | } |
| 898: | |
| 899: | |
| 900: | |
| 901: | |
| 902: | |
| 903: | |
| 904: | |
| 905: | |
| 906: | |
| 907: | |
| 908: | |
| 909: | public function getImpurePointsFromPropertyHook( |
| 910: | MutatingScope $scope, |
| 911: | PropertyFetch $propertyFetch, |
| 912: | PhpPropertyReflection $propertyReflection, |
| 913: | string $hookName, |
| 914: | ): array |
| 915: | { |
| 916: | if ($this->isPropertyHookBackingValueAccess($scope, $propertyFetch)) { |
| 917: | return []; |
| 918: | } |
| 919: | |
| 920: | if (!$propertyReflection->hasHook($hookName)) { |
| 921: | return []; |
| 922: | } |
| 923: | |
| 924: | if (!$propertyReflection->getHook($hookName)->hasSideEffects()->yes()) { |
| 925: | return []; |
| 926: | } |
| 927: | |
| 928: | return [ |
| 929: | new ImpurePoint( |
| 930: | $scope, |
| 931: | $propertyFetch, |
| 932: | 'propertyHookCall', |
| 933: | sprintf( |
| 934: | 'call to %s hook of property %s::$%s', |
| 935: | $hookName, |
| 936: | $propertyReflection->getDeclaringClass()->getDisplayName(), |
| 937: | $propertyReflection->getName(), |
| 938: | ), |
| 939: | true, |
| 940: | ), |
| 941: | ]; |
| 942: | } |
| 943: | |
| 944: | |
| 945: | |
| 946: | |
| 947: | |
| 948: | private function isPropertyHookBackingValueAccess(MutatingScope $scope, PropertyFetch $propertyFetch): bool |
| 949: | { |
| 950: | $scopeFunction = $scope->getFunction(); |
| 951: | |
| 952: | return $scopeFunction instanceof PhpMethodFromParserNodeReflection |
| 953: | && $scopeFunction->isPropertyHook() |
| 954: | && $propertyFetch->var instanceof Variable |
| 955: | && $propertyFetch->var->name === 'this' |
| 956: | && $propertyFetch->name instanceof Identifier |
| 957: | && $propertyFetch->name->toString() === $scopeFunction->getHookedPropertyName(); |
| 958: | } |
| 959: | |
| 960: | |
| 961: | |
| 962: | |
| 963: | public function getAssignedVariables(Expr $expr): array |
| 964: | { |
| 965: | if ($expr instanceof Expr\Variable) { |
| 966: | if (is_string($expr->name)) { |
| 967: | return [$expr->name]; |
| 968: | } |
| 969: | |
| 970: | return []; |
| 971: | } |
| 972: | |
| 973: | if ($expr instanceof Expr\List_) { |
| 974: | $names = []; |
| 975: | foreach ($expr->items as $item) { |
| 976: | if ($item === null) { |
| 977: | continue; |
| 978: | } |
| 979: | |
| 980: | $names = array_merge($names, $this->getAssignedVariables($item->value)); |
| 981: | } |
| 982: | |
| 983: | return $names; |
| 984: | } |
| 985: | |
| 986: | if ($expr instanceof ArrayDimFetch) { |
| 987: | return $this->getAssignedVariables($expr->var); |
| 988: | } |
| 989: | |
| 990: | return []; |
| 991: | } |
| 992: | |
| 993: | private const REPLAYABLE_BODY_ATTRIBUTE = 'convergenceReplayableBody'; |
| 994: | |
| 995: | private const CONTEXT_SENSITIVE_STATEMENT_ATTRIBUTE = 'convergenceContextSensitiveStatement'; |
| 996: | |
| 997: | |
| 998: | |
| 999: | |
| 1000: | |
| 1001: | |
| 1002: | |
| 1003: | |
| 1004: | |
| 1005: | |
| 1006: | |
| 1007: | public function isReplayableConvergenceBody(Node $loopNode, array $bodyStmts): bool |
| 1008: | { |
| 1009: | $cached = $loopNode->getAttribute(self::REPLAYABLE_BODY_ATTRIBUTE); |
| 1010: | if ($cached !== null) { |
| 1011: | return $cached; |
| 1012: | } |
| 1013: | |
| 1014: | $replayable = true; |
| 1015: | foreach ($bodyStmts as $bodyStmt) { |
| 1016: | if ($this->hasContextSensitiveConstruct($bodyStmt)) { |
| 1017: | $replayable = false; |
| 1018: | break; |
| 1019: | } |
| 1020: | } |
| 1021: | $loopNode->setAttribute(self::REPLAYABLE_BODY_ATTRIBUTE, $replayable); |
| 1022: | |
| 1023: | return $replayable; |
| 1024: | } |
| 1025: | |
| 1026: | |
| 1027: | public function isContextSensitiveStatement(Node\Stmt $stmt): bool |
| 1028: | { |
| 1029: | $cached = $stmt->getAttribute(self::CONTEXT_SENSITIVE_STATEMENT_ATTRIBUTE); |
| 1030: | if ($cached !== null) { |
| 1031: | return $cached; |
| 1032: | } |
| 1033: | |
| 1034: | $sensitive = $this->hasContextSensitiveConstruct($stmt); |
| 1035: | $stmt->setAttribute(self::CONTEXT_SENSITIVE_STATEMENT_ATTRIBUTE, $sensitive); |
| 1036: | |
| 1037: | return $sensitive; |
| 1038: | } |
| 1039: | |
| 1040: | private function hasContextSensitiveConstruct(Node $node): bool |
| 1041: | { |
| 1042: | if ($node instanceof Expr\Closure) { |
| 1043: | return false; |
| 1044: | } |
| 1045: | if ( |
| 1046: | $node instanceof Node\Stmt\While_ |
| 1047: | || $node instanceof Node\Stmt\Do_ |
| 1048: | || $node instanceof Node\Stmt\For_ |
| 1049: | || $node instanceof Foreach_ |
| 1050: | || $node instanceof Node\Stmt\Label |
| 1051: | || $node instanceof Node\Stmt\ClassLike |
| 1052: | ) { |
| 1053: | return true; |
| 1054: | } |
| 1055: | |
| 1056: | foreach ($node->getSubNodeNames() as $subNodeName) { |
| 1057: | $subNode = $node->$subNodeName; |
| 1058: | if ($subNode instanceof Node) { |
| 1059: | if ($this->hasContextSensitiveConstruct($subNode)) { |
| 1060: | return true; |
| 1061: | } |
| 1062: | } elseif (is_array($subNode)) { |
| 1063: | foreach ($subNode as $item) { |
| 1064: | if ($item instanceof Node && $this->hasContextSensitiveConstruct($item)) { |
| 1065: | return true; |
| 1066: | } |
| 1067: | } |
| 1068: | } |
| 1069: | } |
| 1070: | |
| 1071: | return false; |
| 1072: | } |
| 1073: | |
| 1074: | |
| 1075: | |
| 1076: | |
| 1077: | |
| 1078: | |
| 1079: | |
| 1080: | public function pushNodeGatherer(callable $gatherer): void |
| 1081: | { |
| 1082: | $this->nodeGatherers[] = $gatherer; |
| 1083: | } |
| 1084: | |
| 1085: | public function popNodeGatherer(): void |
| 1086: | { |
| 1087: | array_pop($this->nodeGatherers); |
| 1088: | } |
| 1089: | |
| 1090: | |
| 1091: | |
| 1092: | |
| 1093: | |
| 1094: | |
| 1095: | |
| 1096: | public function suspendNodeGatherers(): array |
| 1097: | { |
| 1098: | $gatherers = $this->nodeGatherers; |
| 1099: | $this->nodeGatherers = []; |
| 1100: | |
| 1101: | return $gatherers; |
| 1102: | } |
| 1103: | |
| 1104: | |
| 1105: | |
| 1106: | |
| 1107: | public function restoreNodeGatherers(array $gatherers): void |
| 1108: | { |
| 1109: | $this->nodeGatherers = $gatherers; |
| 1110: | } |
| 1111: | |
| 1112: | |
| 1113: | |
| 1114: | |
| 1115: | public function replayRecording(RecordingNodeCallback $recording, callable $nodeCallback, ExpressionResultStorage $storage, MutatingScope $scope): void |
| 1116: | { |
| 1117: | $this->replayRecordingRange($recording, 0, $recording->count(), $nodeCallback, $storage, $scope); |
| 1118: | } |
| 1119: | |
| 1120: | |
| 1121: | |
| 1122: | |
| 1123: | |
| 1124: | |
| 1125: | |
| 1126: | |
| 1127: | |
| 1128: | |
| 1129: | public function replayRecordingRange(RecordingNodeCallback $recording, int $from, int $to, callable $nodeCallback, ExpressionResultStorage $storage, MutatingScope $scope): void |
| 1130: | { |
| 1131: | $pairs = $recording->getPairs(); |
| 1132: | $scope->pushExpressionResultStorage($storage); |
| 1133: | try { |
| 1134: | for ($i = $from; $i < $to; $i++) { |
| 1135: | [$node, $pairScope] = $pairs[$i]; |
| 1136: | if (!$pairScope instanceof MutatingScope) { |
| 1137: | throw new ShouldNotHappenException(); |
| 1138: | } |
| 1139: | $this->callNodeCallback($nodeCallback, $node, $pairScope, $storage); |
| 1140: | } |
| 1141: | } finally { |
| 1142: | $scope->popExpressionResultStorage(); |
| 1143: | } |
| 1144: | } |
| 1145: | |
| 1146: | |
| 1147: | |
| 1148: | |
| 1149: | public function callNodeCallbackWithExpression( |
| 1150: | callable $nodeCallback, |
| 1151: | Node $expr, |
| 1152: | MutatingScope $scope, |
| 1153: | ExpressionResultStorage $storage, |
| 1154: | ExpressionContext $context, |
| 1155: | ): void |
| 1156: | { |
| 1157: | if ($context->isDeep()) { |
| 1158: | $scope = $scope->exitFirstLevelStatements(); |
| 1159: | } |
| 1160: | $this->callNodeCallback($nodeCallback, $expr, $scope, $storage); |
| 1161: | } |
| 1162: | |
| 1163: | |
| 1164: | |
| 1165: | |
| 1166: | public function callNodeCallback( |
| 1167: | callable $nodeCallback, |
| 1168: | Node $node, |
| 1169: | MutatingScope $scope, |
| 1170: | ExpressionResultStorage $storage, |
| 1171: | ): void |
| 1172: | { |
| 1173: | |
| 1174: | |
| 1175: | |
| 1176: | |
| 1177: | |
| 1178: | |
| 1179: | foreach ($this->nodeGatherers as $gatherer) { |
| 1180: | $gatherer($node, $scope); |
| 1181: | } |
| 1182: | |
| 1183: | if ($nodeCallback instanceof NoopNodeCallback) { |
| 1184: | return; |
| 1185: | } |
| 1186: | |
| 1187: | if ($nodeCallback instanceof RecordingNodeCallback) { |
| 1188: | |
| 1189: | |
| 1190: | $nodeCallback($node, $scope); |
| 1191: | return; |
| 1192: | } |
| 1193: | |
| 1194: | |
| 1195: | |
| 1196: | |
| 1197: | $nodeCallback($node, $scope->toNodeCallbackScope()); |
| 1198: | } |
| 1199: | |
| 1200: | |
| 1201: | |
| 1202: | |
| 1203: | |
| 1204: | |
| 1205: | |
| 1206: | public function observingTemplateArgumentFrame(MutatingScope $scope): ?TemplateArgumentFrame |
| 1207: | { |
| 1208: | $frame = $scope->getCurrentTemplateArgumentFrame(); |
| 1209: | if ($frame === null || !$frame->isObservingClosures() || $scope->getTemplateArgumentConstraints() === null) { |
| 1210: | return null; |
| 1211: | } |
| 1212: | |
| 1213: | return $frame; |
| 1214: | } |
| 1215: | |
| 1216: | |
| 1217: | |
| 1218: | |
| 1219: | |
| 1220: | public function collectReturnSend(MutatingScope $scope, ExpressionResult $returnedResult): TemplateArgumentConstraints |
| 1221: | { |
| 1222: | $frame = $this->observingTemplateArgumentFrame($returnedResult->getScope()); |
| 1223: | if ($frame === null) { |
| 1224: | return TemplateArgumentConstraints::createEmpty(); |
| 1225: | } |
| 1226: | $declaredReturnType = self::getDeclaredReturnType($scope); |
| 1227: | if ($declaredReturnType === null) { |
| 1228: | return TemplateArgumentConstraints::createEmpty(); |
| 1229: | } |
| 1230: | |
| 1231: | return $this->templateArgumentObserver->collectSend($declaredReturnType, $returnedResult->getType()); |
| 1232: | } |
| 1233: | |
| 1234: | |
| 1235: | |
| 1236: | |
| 1237: | |
| 1238: | |
| 1239: | |
| 1240: | public function collectYieldSend(MutatingScope $scope, ?Type $keyType, ?Type $valueType): TemplateArgumentConstraints |
| 1241: | { |
| 1242: | $constraints = TemplateArgumentConstraints::createEmpty(); |
| 1243: | if ($this->observingTemplateArgumentFrame($scope) === null) { |
| 1244: | return $constraints; |
| 1245: | } |
| 1246: | $declaredReturnType = self::getDeclaredReturnType($scope); |
| 1247: | if ($declaredReturnType === null || !$declaredReturnType->isIterable()->yes()) { |
| 1248: | foreach ([$keyType, $valueType] as $yieldedType) { |
| 1249: | if ($yieldedType === null) { |
| 1250: | continue; |
| 1251: | } |
| 1252: | $constraints = $constraints->merge($this->templateArgumentObserver->collectEscape($yieldedType)); |
| 1253: | } |
| 1254: | |
| 1255: | return $constraints; |
| 1256: | } |
| 1257: | if ($keyType !== null) { |
| 1258: | $constraints = $constraints->merge($this->templateArgumentObserver->collectSend($declaredReturnType->getIterableKeyType(), $keyType)); |
| 1259: | } |
| 1260: | if ($valueType !== null) { |
| 1261: | $constraints = $constraints->merge($this->templateArgumentObserver->collectSend($declaredReturnType->getIterableValueType(), $valueType)); |
| 1262: | } |
| 1263: | |
| 1264: | return $constraints; |
| 1265: | } |
| 1266: | |
| 1267: | |
| 1268: | |
| 1269: | |
| 1270: | public function collectYieldFromSend(MutatingScope $scope, Type $delegatedType): TemplateArgumentConstraints |
| 1271: | { |
| 1272: | if ($this->observingTemplateArgumentFrame($scope) === null) { |
| 1273: | return TemplateArgumentConstraints::createEmpty(); |
| 1274: | } |
| 1275: | if (!$delegatedType->isIterable()->yes()) { |
| 1276: | return $this->templateArgumentObserver->collectEscape($delegatedType); |
| 1277: | } |
| 1278: | |
| 1279: | return $this->collectYieldSend($scope, $delegatedType->getIterableKeyType(), $delegatedType->getIterableValueType()); |
| 1280: | } |
| 1281: | |
| 1282: | private static function getDeclaredReturnType(MutatingScope $scope): ?Type |
| 1283: | { |
| 1284: | if ($scope->isInAnonymousFunction()) { |
| 1285: | return $scope->getAnonymousFunctionReturnType(); |
| 1286: | } |
| 1287: | |
| 1288: | $function = $scope->getFunction(); |
| 1289: | |
| 1290: | return $function?->getReturnType(); |
| 1291: | } |
| 1292: | |
| 1293: | } |
| 1294: | |