1: <?php declare(strict_types = 1);
2:
3: namespace PHPStan\Type;
4:
5: use PHPStan\PhpDocParser\Ast\Type\ConditionalTypeNode;
6: use PHPStan\PhpDocParser\Ast\Type\TypeNode;
7: use PHPStan\Turbo\ShadowedByTurboExtension;
8: use PHPStan\Type\Generic\TemplateType;
9: use PHPStan\Type\Generic\TemplateTypeVariance;
10: use PHPStan\Type\Traits\LateResolvableTypeTrait;
11: use PHPStan\Type\Traits\NonGeneralizableTypeTrait;
12: use function array_merge;
13: use function sprintf;
14:
15: /** @api */
16: #[ShadowedByTurboExtension(implementation: __DIR__ . '/../../turbo-ext/src/ConditionalType.cpp')]
17: final class ConditionalType implements CompoundType, LateResolvableType
18: {
19:
20: use LateResolvableTypeTrait;
21: use NonGeneralizableTypeTrait;
22:
23: private ?Type $normalizedIf = null;
24:
25: private ?Type $normalizedElse = null;
26:
27: public function __construct(
28: private Type $subject,
29: private Type $target,
30: private Type $if,
31: private Type $else,
32: private bool $negated,
33: )
34: {
35: }
36:
37: public function getSubject(): Type
38: {
39: return $this->subject;
40: }
41:
42: public function getTarget(): Type
43: {
44: return $this->target;
45: }
46:
47: public function getIf(): Type
48: {
49: return $this->if;
50: }
51:
52: public function getElse(): Type
53: {
54: return $this->else;
55: }
56:
57: public function isNegated(): bool
58: {
59: return $this->negated;
60: }
61:
62: public function isSuperTypeOf(Type $type): IsSuperTypeOfResult
63: {
64: if (
65: $type instanceof self
66: && $this->negated === $type->negated
67: && $this->subject->equals($type->subject)
68: && $this->target->equals($type->target)
69: ) {
70: return $this->if->isSuperTypeOf($type->if)
71: ->and($this->else->isSuperTypeOf($type->else));
72: }
73:
74: return $this->isSuperTypeOfDefault($type);
75: }
76:
77: public function getReferencedClasses(): array
78: {
79: return array_merge(
80: $this->subject->getReferencedClasses(),
81: $this->target->getReferencedClasses(),
82: $this->if->getReferencedClasses(),
83: $this->else->getReferencedClasses(),
84: );
85: }
86:
87: public function getReferencedTemplateTypes(TemplateTypeVariance $positionVariance): array
88: {
89: return array_merge(
90: $this->subject->getReferencedTemplateTypes($positionVariance),
91: $this->target->getReferencedTemplateTypes($positionVariance),
92: $this->if->getReferencedTemplateTypes($positionVariance),
93: $this->else->getReferencedTemplateTypes($positionVariance),
94: );
95: }
96:
97: public function equals(Type $type): bool
98: {
99: return $type instanceof self
100: && $this->negated === $type->negated
101: && $this->subject->equals($type->subject)
102: && $this->target->equals($type->target)
103: && $this->if->equals($type->if)
104: && $this->else->equals($type->else);
105: }
106:
107: public function describe(VerbosityLevel $level): string
108: {
109: return sprintf(
110: '(%s %s %s ? %s : %s)',
111: $this->subject->describe($level),
112: $this->negated ? 'is not' : 'is',
113: $this->target->describe($level),
114: $this->if->describe($level),
115: $this->else->describe($level),
116: );
117: }
118:
119: public function isResolvable(): bool
120: {
121: if (!TypeUtils::containsTemplateType($this->subject) && !TypeUtils::containsTemplateType($this->target)) {
122: return true;
123: }
124:
125: $isSuperType = $this->target->isSuperTypeOf($this->subject);
126:
127: return $isSuperType->yes() || $isSuperType->no();
128: }
129:
130: protected function getResult(): Type
131: {
132: $isSuperType = $this->target->isSuperTypeOf($this->subject);
133:
134: if ($isSuperType->yes()) {
135: return !$this->negated ? $this->getNormalizedIf() : $this->getNormalizedElse();
136: }
137:
138: if ($isSuperType->no()) {
139: return !$this->negated ? $this->getNormalizedElse() : $this->getNormalizedIf();
140: }
141:
142: return TypeCombinator::union(
143: $this->getNormalizedIf(),
144: $this->getNormalizedElse(),
145: );
146: }
147:
148: public function traverse(callable $cb): Type
149: {
150: $subject = $cb($this->subject);
151: $target = $cb($this->target);
152: $if = $cb($this->getNormalizedIf());
153: $else = $cb($this->getNormalizedElse());
154:
155: if (
156: $this->subject === $subject
157: && $this->target === $target
158: && $this->getNormalizedIf() === $if
159: && $this->getNormalizedElse() === $else
160: ) {
161: return $this;
162: }
163:
164: return $this->withNormalizedBranches($subject, $target, $if, $else);
165: }
166:
167: public function traverseSimultaneously(Type $right, callable $cb): Type
168: {
169: if (!$right instanceof self) {
170: return $this;
171: }
172:
173: $subject = $cb($this->subject, $right->subject);
174: $target = $cb($this->target, $right->target);
175: $if = $cb($this->getNormalizedIf(), $right->getNormalizedIf());
176: $else = $cb($this->getNormalizedElse(), $right->getNormalizedElse());
177:
178: if (
179: $this->subject === $subject
180: && $this->target === $target
181: && $this->getNormalizedIf() === $if
182: && $this->getNormalizedElse() === $else
183: ) {
184: return $this;
185: }
186:
187: return $this->withNormalizedBranches($subject, $target, $if, $else);
188: }
189:
190: public function toPhpDocNode(): TypeNode
191: {
192: return new ConditionalTypeNode(
193: $this->subject->toPhpDocNode(),
194: $this->target->toPhpDocNode(),
195: $this->if->toPhpDocNode(),
196: $this->else->toPhpDocNode(),
197: $this->negated,
198: );
199: }
200:
201: /**
202: * The branches passed in are images of normalized branches, so the references to the
203: * subject in them have already been narrowed, and follow whatever the subject maps to.
204: * Normalizing them again against the mapped subject would narrow unrelated types that
205: * happen to equal it, and re-traverse every conditional type nested in the branches.
206: */
207: private function withNormalizedBranches(Type $subject, Type $target, Type $if, Type $else): self
208: {
209: $type = new self($subject, $target, $if, $else, $this->negated);
210: $type->normalizedIf = $if;
211: $type->normalizedElse = $else;
212:
213: return $type;
214: }
215:
216: private function getNormalizedIf(): Type
217: {
218: return $this->normalizedIf ??= $this->narrowSubjectIn($this->if, !$this->negated);
219: }
220:
221: private function getNormalizedElse(): Type
222: {
223: return $this->normalizedElse ??= $this->narrowSubjectIn($this->else, $this->negated);
224: }
225:
226: /**
227: * Replaces the references to the subject in a branch with what the branch knows about
228: * it: `subject & target` where the condition holds, `subject ~ target` where it does not
229: * (see NarrowedSubjectType).
230: *
231: * A branch can only reference the subject while it is a template type. Once the subject
232: * resolves to a concrete type, an equal type inside the branch is not a mention of it -
233: * the `mixed` value type of an `array` branch has nothing to do with a `mixed` subject -
234: * so nothing is narrowed here; the references narrowed earlier follow the resolution on
235: * their own. Matching by identity instead would still hit such unrelated types whenever
236: * they happen to share an instance - the turbo extension memoizes TypeCombinator
237: * results, so each `int|false` resolved from a PHPDoc is the same object.
238: */
239: private function narrowSubjectIn(Type $branch, bool $conditionHolds): Type
240: {
241: if (!$this->subject instanceof TemplateType) {
242: return $branch;
243: }
244:
245: return NarrowedSubjectType::narrowReferences($branch, $this->subject, $this->target, $conditionHolds);
246: }
247:
248: }
249: