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: * Object of this class is one of the parameters of `NodeScopeResolver::processStmtNodes()`.
10: *
11: * It determines whether loops will be analysed once or multiple times
12: * until the types "stabilize".
13: *
14: * When in doubt, use `StatementContext::createTopLevel()`.
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: * @api
33: */
34: public static function createTopLevel(bool $resolveTemplateArguments = true): self
35: {
36: return new self(true, resolveTemplateArguments: $resolveTemplateArguments);
37: }
38:
39: /**
40: * @api
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: * Whether loops walk their body until the types stabilize: at the top
54: * level, and in a converging loop's passes, where the nested loops start
55: * from the heads they converged to in the previous passes.
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: /** The context of a converging loop's pass. */
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: * The final walk of a top-level loop: the loops nested in it converge from
75: * their own entry, and the loops nested in their passes start from the
76: * heads the enclosing loop's passes reached.
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: * The type the returned expressions of an anonymous function are expected
108: * to have, from the callable type it is passed to - an expected type for
109: * the closures, arrow functions and array literals it returns.
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: