forked from phpstan/phpstan-src
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConditionalType.php
More file actions
226 lines (186 loc) · 5.49 KB
/
ConditionalType.php
File metadata and controls
226 lines (186 loc) · 5.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
<?php declare(strict_types = 1);
namespace PHPStan\Type;
use PHPStan\PhpDocParser\Ast\Type\ConditionalTypeNode;
use PHPStan\PhpDocParser\Ast\Type\TypeNode;
use PHPStan\Type\Generic\TemplateTypeVariance;
use PHPStan\Type\Traits\LateResolvableTypeTrait;
use PHPStan\Type\Traits\NonGeneralizableTypeTrait;
use function array_merge;
use function sprintf;
/** @api */
final class ConditionalType implements CompoundType, LateResolvableType
{
use LateResolvableTypeTrait;
use NonGeneralizableTypeTrait;
private ?Type $normalizedIf = null;
private ?Type $normalizedElse = null;
private ?Type $subjectWithTargetIntersectedType = null;
private ?Type $subjectWithTargetRemovedType = null;
public function __construct(
private Type $subject,
private Type $target,
private Type $if,
private Type $else,
private bool $negated,
)
{
}
public function getSubject(): Type
{
return $this->subject;
}
public function getTarget(): Type
{
return $this->target;
}
public function getIf(): Type
{
return $this->if;
}
public function getElse(): Type
{
return $this->else;
}
public function isNegated(): bool
{
return $this->negated;
}
public function isSuperTypeOf(Type $type): IsSuperTypeOfResult
{
if ($type instanceof self) {
return $this->if->isSuperTypeOf($type->if)
->and($this->else->isSuperTypeOf($type->else));
}
return $this->isSuperTypeOfDefault($type);
}
public function getReferencedClasses(): array
{
return array_merge(
$this->subject->getReferencedClasses(),
$this->target->getReferencedClasses(),
$this->if->getReferencedClasses(),
$this->else->getReferencedClasses(),
);
}
public function getReferencedTemplateTypes(TemplateTypeVariance $positionVariance): array
{
return array_merge(
$this->subject->getReferencedTemplateTypes($positionVariance),
$this->target->getReferencedTemplateTypes($positionVariance),
$this->if->getReferencedTemplateTypes($positionVariance),
$this->else->getReferencedTemplateTypes($positionVariance),
);
}
public function equals(Type $type): bool
{
return $type instanceof self
&& $this->subject->equals($type->subject)
&& $this->target->equals($type->target)
&& $this->if->equals($type->if)
&& $this->else->equals($type->else);
}
public function describe(VerbosityLevel $level): string
{
return sprintf(
'(%s %s %s ? %s : %s)',
$this->subject->describe($level),
$this->negated ? 'is not' : 'is',
$this->target->describe($level),
$this->if->describe($level),
$this->else->describe($level),
);
}
public function isResolvable(): bool
{
if (!TypeUtils::containsTemplateType($this->subject) && !TypeUtils::containsTemplateType($this->target)) {
return true;
}
$isSuperType = $this->target->isSuperTypeOf($this->subject);
return $isSuperType->yes() || $isSuperType->no();
}
protected function getResult(): Type
{
$isSuperType = $this->target->isSuperTypeOf($this->subject);
if ($isSuperType->yes()) {
return !$this->negated ? $this->getNormalizedIf() : $this->getNormalizedElse();
}
if ($isSuperType->no()) {
return !$this->negated ? $this->getNormalizedElse() : $this->getNormalizedIf();
}
return TypeCombinator::union(
$this->getNormalizedIf(),
$this->getNormalizedElse(),
);
}
public function traverse(callable $cb): Type
{
$subject = $cb($this->subject);
$target = $cb($this->target);
$if = $cb($this->getNormalizedIf());
$else = $cb($this->getNormalizedElse());
if (
$this->subject === $subject
&& $this->target === $target
&& $this->getNormalizedIf() === $if
&& $this->getNormalizedElse() === $else
) {
return $this;
}
return new self($subject, $target, $if, $else, $this->negated);
}
public function traverseSimultaneously(Type $right, callable $cb): Type
{
if (!$right instanceof self) {
return $this;
}
$subject = $cb($this->subject, $right->subject);
$target = $cb($this->target, $right->target);
$if = $cb($this->getNormalizedIf(), $right->getNormalizedIf());
$else = $cb($this->getNormalizedElse(), $right->getNormalizedElse());
if (
$this->subject === $subject
&& $this->target === $target
&& $this->getNormalizedIf() === $if
&& $this->getNormalizedElse() === $else
) {
return $this;
}
return new self($subject, $target, $if, $else, $this->negated);
}
public function toPhpDocNode(): TypeNode
{
return new ConditionalTypeNode(
$this->subject->toPhpDocNode(),
$this->target->toPhpDocNode(),
$this->if->toPhpDocNode(),
$this->else->toPhpDocNode(),
$this->negated,
);
}
private function getNormalizedIf(): Type
{
return $this->normalizedIf ??= TypeTraverser::map(
$this->if,
fn (Type $type, callable $traverse) => $type === $this->subject
? (!$this->negated ? $this->getSubjectWithTargetIntersectedType() : $this->getSubjectWithTargetRemovedType())
: $traverse($type),
);
}
private function getNormalizedElse(): Type
{
return $this->normalizedElse ??= TypeTraverser::map(
$this->else,
fn (Type $type, callable $traverse) => $type === $this->subject
? (!$this->negated ? $this->getSubjectWithTargetRemovedType() : $this->getSubjectWithTargetIntersectedType())
: $traverse($type),
);
}
private function getSubjectWithTargetIntersectedType(): Type
{
return $this->subjectWithTargetIntersectedType ??= TypeCombinator::intersect($this->subject, $this->target);
}
private function getSubjectWithTargetRemovedType(): Type
{
return $this->subjectWithTargetRemovedType ??= TypeCombinator::remove($this->subject, $this->target);
}
}