forked from phpstan/phpstan-src
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIsSuperTypeOfResult.php
More file actions
219 lines (188 loc) · 4.96 KB
/
IsSuperTypeOfResult.php
File metadata and controls
219 lines (188 loc) · 4.96 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
<?php declare(strict_types = 1);
namespace PHPStan\Type;
use PHPStan\ShouldNotHappenException;
use PHPStan\TrinaryLogic;
use function array_merge;
use function array_unique;
use function array_values;
/**
* Result of a Type::isSuperTypeOf() check — whether one type is a supertype of another.
*
* Wraps a TrinaryLogic result together with human-readable reasons explaining the
* relationship. This is the primary mechanism for comparing types in PHPStan's type system.
*
* `isSuperTypeOf()` answers: "Can all values of type B also be values of type A?"
* For example:
* - `(new StringType())->isSuperTypeOf(new ConstantStringType('hello'))` → Yes
* - `(new IntegerType())->isSuperTypeOf(new StringType())` → No
* - `(new StringType())->isSuperTypeOf(new MixedType())` → Maybe
*
* This is distinct from `accepts()` which also considers rule levels and PHPDoc context.
* Use `isSuperTypeOf()` for type-theoretic comparisons and `accepts()` for assignability checks.
*
* Can be converted to AcceptsResult via toAcceptsResult().
*
* @api
*/
final class IsSuperTypeOfResult
{
private static self $YES;
private static self $MAYBE;
private static self $NO;
/**
* @api
* @param list<string> $reasons Human-readable explanations of the type relationship
*/
public function __construct(
public readonly TrinaryLogic $result,
public readonly array $reasons,
)
{
}
/**
* @phpstan-assert-if-true =false $this->no()
* @phpstan-assert-if-true =false $this->maybe()
*/
public function yes(): bool
{
return $this->result->yes();
}
/**
* @phpstan-assert-if-true =false $this->no()
* @phpstan-assert-if-true =false $this->yes()
*/
public function maybe(): bool
{
return $this->result->maybe();
}
/**
* @phpstan-assert-if-true =false $this->maybe()
* @phpstan-assert-if-true =false $this->yes()
*/
public function no(): bool
{
return $this->result->no();
}
public static function createYes(): self
{
return self::$YES ??= new self(TrinaryLogic::createYes(), []);
}
/** @param list<string> $reasons */
public static function createNo(array $reasons = []): self
{
if ($reasons === []) {
return self::$NO ??= new self(TrinaryLogic::createNo(), $reasons);
}
return new self(TrinaryLogic::createNo(), $reasons);
}
public static function createMaybe(): self
{
return self::$MAYBE ??= new self(TrinaryLogic::createMaybe(), []);
}
public static function createFromBoolean(bool $value): self
{
if ($value === true) {
return self::createYes();
}
return self::createNo();
}
public function toAcceptsResult(): AcceptsResult
{
return new AcceptsResult($this->result, $this->reasons);
}
public function and(self ...$others): self
{
$results = [];
$reasons = [];
foreach ($others as $other) {
$results[] = $other->result;
$reasons[] = $other->reasons;
}
return new self(
$this->result->and(...$results),
array_values(array_unique(array_merge($this->reasons, ...$reasons))),
);
}
public function or(self ...$others): self
{
$results = [];
$reasons = [];
foreach ($others as $other) {
$results[] = $other->result;
$reasons[] = $other->reasons;
}
return new self(
$this->result->or(...$results),
array_values(array_unique(array_merge($this->reasons, ...$reasons))),
);
}
/** @param callable(string): string $cb */
public function decorateReasons(callable $cb): self
{
$reasons = [];
foreach ($this->reasons as $reason) {
$reasons[] = $cb($reason);
}
return new self($this->result, $reasons);
}
/** @see TrinaryLogic::extremeIdentity() */
public static function extremeIdentity(self ...$operands): self
{
if ($operands === []) {
throw new ShouldNotHappenException();
}
$results = [];
$reasons = [];
foreach ($operands as $operand) {
$results[] = $operand->result;
foreach ($operand->reasons as $reason) {
$reasons[] = $reason;
}
}
return new self(TrinaryLogic::extremeIdentity(...$results), array_values(array_unique($reasons)));
}
/** @see TrinaryLogic::maxMin() */
public static function maxMin(self ...$operands): self
{
if ($operands === []) {
throw new ShouldNotHappenException();
}
$results = [];
$reasons = [];
foreach ($operands as $operand) {
$results[] = $operand->result;
foreach ($operand->reasons as $reason) {
$reasons[] = $reason;
}
}
return new self(TrinaryLogic::maxMin(...$results), array_values(array_unique($reasons)));
}
/**
* @template T
* @param T[] $objects
* @param callable(T): self $callback
*/
public static function lazyMaxMin(
array $objects,
callable $callback,
): self
{
$results = [];
foreach ($objects as $object) {
$isSuperTypeOf = $callback($object);
if ($isSuperTypeOf->result->yes()) {
return $isSuperTypeOf;
}
$results[] = $isSuperTypeOf;
}
return self::maxMin(...$results);
}
public function negate(): self
{
return new self($this->result->negate(), $this->reasons);
}
public function describe(): string
{
return $this->result->describe();
}
}