| 1: | <?php declare(strict_types = 1); |
| 2: | |
| 3: | namespace PHPStan\Analyser; |
| 4: | |
| 5: | use PHPStan\Turbo\ShadowedByTurboExtension; |
| 6: | use PHPStan\Type\Type; |
| 7: | |
| 8: | |
| 9: | |
| 10: | |
| 11: | |
| 12: | |
| 13: | |
| 14: | |
| 15: | |
| 16: | #[ShadowedByTurboExtension(implementation: __DIR__ . '/../../turbo-ext/src/StatementContext.cpp')] |
| 17: | final class StatementContext |
| 18: | { |
| 19: | |
| 20: | private function __construct( |
| 21: | private bool $isTopLevel, |
| 22: | private int $foreachUnrollFactor = 1, |
| 23: | private bool $resolveTemplateArguments = true, |
| 24: | private ?Type $expectedReturnType = null, |
| 25: | private ?Type $nativeExpectedReturnType = null, |
| 26: | private ?ConvergedLoopHeads $loopHeads = null, |
| 27: | ) |
| 28: | { |
| 29: | } |
| 30: | |
| 31: | |
| 32: | |
| 33: | |
| 34: | public static function createTopLevel(bool $resolveTemplateArguments = true): self |
| 35: | { |
| 36: | return new self(true, resolveTemplateArguments: $resolveTemplateArguments); |
| 37: | } |
| 38: | |
| 39: | |
| 40: | |
| 41: | |
| 42: | public static function createDeep(bool $resolveTemplateArguments = true): self |
| 43: | { |
| 44: | return new self(false, resolveTemplateArguments: $resolveTemplateArguments); |
| 45: | } |
| 46: | |
| 47: | public function isTopLevel(): bool |
| 48: | { |
| 49: | return $this->isTopLevel; |
| 50: | } |
| 51: | |
| 52: | |
| 53: | |
| 54: | |
| 55: | |
| 56: | |
| 57: | public function shouldConvergeLoops(): bool |
| 58: | { |
| 59: | return $this->isTopLevel || $this->loopHeads !== null; |
| 60: | } |
| 61: | |
| 62: | public function getLoopHeads(): ?ConvergedLoopHeads |
| 63: | { |
| 64: | return $this->loopHeads; |
| 65: | } |
| 66: | |
| 67: | |
| 68: | public function enterLoopPass(ConvergedLoopHeads $loopHeads): self |
| 69: | { |
| 70: | return new self(false, $this->foreachUnrollFactor, false, $this->expectedReturnType, $this->nativeExpectedReturnType, $loopHeads); |
| 71: | } |
| 72: | |
| 73: | |
| 74: | |
| 75: | |
| 76: | |
| 77: | |
| 78: | public function withLoopHeads(?ConvergedLoopHeads $loopHeads): self |
| 79: | { |
| 80: | if (!$this->isTopLevel || $loopHeads === null || $loopHeads === $this->loopHeads) { |
| 81: | return $this; |
| 82: | } |
| 83: | |
| 84: | return new self(true, $this->foreachUnrollFactor, $this->resolveTemplateArguments, $this->expectedReturnType, $this->nativeExpectedReturnType, $loopHeads); |
| 85: | } |
| 86: | |
| 87: | public function getForeachUnrollFactor(): int |
| 88: | { |
| 89: | return $this->foreachUnrollFactor; |
| 90: | } |
| 91: | |
| 92: | public function shouldResolveTemplateArguments(): bool |
| 93: | { |
| 94: | return $this->resolveTemplateArguments; |
| 95: | } |
| 96: | |
| 97: | public function withoutTemplateArgumentResolution(): self |
| 98: | { |
| 99: | if (!$this->resolveTemplateArguments) { |
| 100: | return $this; |
| 101: | } |
| 102: | |
| 103: | return new self($this->isTopLevel, $this->foreachUnrollFactor, false, $this->expectedReturnType, $this->nativeExpectedReturnType, $this->loopHeads); |
| 104: | } |
| 105: | |
| 106: | |
| 107: | |
| 108: | |
| 109: | |
| 110: | |
| 111: | public function withExpectedReturnType(?Type $expectedReturnType, ?Type $nativeExpectedReturnType): self |
| 112: | { |
| 113: | if ($expectedReturnType === null && $nativeExpectedReturnType === null) { |
| 114: | return $this; |
| 115: | } |
| 116: | |
| 117: | return new self($this->isTopLevel, $this->foreachUnrollFactor, $this->resolveTemplateArguments, $expectedReturnType, $nativeExpectedReturnType, $this->loopHeads); |
| 118: | } |
| 119: | |
| 120: | public function getExpectedReturnType(): ?Type |
| 121: | { |
| 122: | return $this->expectedReturnType; |
| 123: | } |
| 124: | |
| 125: | public function getNativeExpectedReturnType(): ?Type |
| 126: | { |
| 127: | return $this->nativeExpectedReturnType; |
| 128: | } |
| 129: | |
| 130: | public function enterDeep(): self |
| 131: | { |
| 132: | if ($this->isTopLevel) { |
| 133: | return new self(false, $this->foreachUnrollFactor, $this->resolveTemplateArguments, $this->expectedReturnType, $this->nativeExpectedReturnType); |
| 134: | } |
| 135: | |
| 136: | return $this; |
| 137: | } |
| 138: | |
| 139: | public function enterUnrolledForeach(int $totalKeys): self |
| 140: | { |
| 141: | return new self($this->isTopLevel, $this->foreachUnrollFactor * $totalKeys, $this->resolveTemplateArguments, $this->expectedReturnType, $this->nativeExpectedReturnType, $this->loopHeads); |
| 142: | } |
| 143: | |
| 144: | } |
| 145: | |