forked from phpstan/phpstan-src
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTrinaryLogic.php
More file actions
311 lines (266 loc) · 6.61 KB
/
TrinaryLogic.php
File metadata and controls
311 lines (266 loc) · 6.61 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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
<?php declare(strict_types = 1);
namespace PHPStan;
use PHPStan\Type\BooleanType;
use PHPStan\Type\Constant\ConstantBooleanType;
use function array_column;
use function max;
use function min;
/**
* Three-valued logic used throughout PHPStan's type system.
*
* Unlike boolean logic, TrinaryLogic has three states: Yes, No, and Maybe.
* This is essential for static analysis because type relationships aren't always
* certain. For example, a `mixed` type *might* be a string — that's `Maybe`.
*
* Many Type methods return TrinaryLogic instead of bool because the answer may
* depend on runtime values that can't be known statically. Extension developers
* encounter TrinaryLogic extensively when querying type properties:
*
* if ($type->isString()->yes()) {
* // Definitely a string
* }
* if ($type->isString()->maybe()) {
* // Could be a string (e.g. mixed)
* }
* if ($type->isString()->no()) {
* // Definitely not a string
* }
*
* TrinaryLogic supports logical operations (and, or, negate) that propagate
* uncertainty correctly. It is used as a flyweight — instances are cached and
* compared by identity.
*
* @api
* @see https://phpstan.org/developing-extensions/trinary-logic
*/
final class TrinaryLogic
{
private const YES = 3;
private const MAYBE = 1;
private const NO = 0;
/** @var self[] */
private static array $registry = [];
private static self $YES;
private static self $MAYBE;
private static self $NO;
private function __construct(private int $value)
{
}
public static function createYes(): self
{
return self::$YES ??= (self::$registry[self::YES] ??= new self(self::YES));
}
public static function createNo(): self
{
return self::$NO ??= (self::$registry[self::NO] ??= new self(self::NO));
}
public static function createMaybe(): self
{
return self::$MAYBE ??= (self::$registry[self::MAYBE] ??= new self(self::MAYBE));
}
public static function createFromBoolean(bool $value): self
{
$yesNo = $value ? self::YES : self::NO;
return self::$registry[$yesNo] ??= new self($yesNo);
}
private static function create(int $value): self
{
return self::$registry[$value] ??= new self($value);
}
/**
* @phpstan-assert-if-true =false $this->no()
* @phpstan-assert-if-true =false $this->maybe()
*/
public function yes(): bool
{
return $this->value === self::YES;
}
/**
* @phpstan-assert-if-true =false $this->no()
* @phpstan-assert-if-true =false $this->yes()
*/
public function maybe(): bool
{
return $this->value === self::MAYBE;
}
/**
* @phpstan-assert-if-true =false $this->maybe()
* @phpstan-assert-if-true =false $this->yes()
*/
public function no(): bool
{
return $this->value === self::NO;
}
public function toBooleanType(): BooleanType
{
if ($this->value === self::MAYBE) {
return new BooleanType();
}
return new ConstantBooleanType($this->value === self::YES);
}
public function and(?self $operand = null, self ...$rest): self
{
$min = $this->value & ($operand !== null ? $operand->value : self::YES);
foreach ($rest as $restOperand) {
$min &= $restOperand->value;
}
return self::$registry[$min] ??= new self($min);
}
/**
* @template T
* @param T[] $objects
* @param callable(T): self $callback
*/
public function lazyAnd(
array $objects,
callable $callback,
): self
{
if ($this->value === self::NO) {
return $this;
}
$results = [];
foreach ($objects as $object) {
$result = $callback($object);
if ($result->value === self::NO) {
return $result;
}
$results[] = $result;
}
return $this->and(...$results);
}
public function or(?self $operand = null, self ...$rest): self
{
$max = $this->value | ($operand !== null ? $operand->value : self::NO);
foreach ($rest as $restOperand) {
$max |= $restOperand->value;
}
return self::$registry[$max] ??= new self($max);
}
/**
* @template T
* @param T[] $objects
* @param callable(T): self $callback
*/
public function lazyOr(
array $objects,
callable $callback,
): self
{
if ($this->value === self::YES) {
return $this;
}
$results = [];
foreach ($objects as $object) {
$result = $callback($object);
if ($result->value === self::YES) {
return $result;
}
$results[] = $result;
}
return $this->or(...$results);
}
/**
* Returns the operands' value if they all agree, Maybe if any differ.
*/
public static function extremeIdentity(self ...$operands): self
{
if ($operands === []) {
throw new ShouldNotHappenException();
}
$operandValues = array_column($operands, 'value');
$min = min($operandValues);
$max = max($operandValues);
return self::create($min === $max ? $min : self::MAYBE);
}
/**
* @template T
* @param T[] $objects
* @param callable(T): self $callback
*/
public static function lazyExtremeIdentity(
array $objects,
callable $callback,
): self
{
if ($objects === []) {
throw new ShouldNotHappenException();
}
$lastResult = null;
foreach ($objects as $object) {
$result = $callback($object);
if ($lastResult === null) {
$lastResult = $result;
continue;
}
if ($lastResult->equals($result)) {
continue;
}
return self::createMaybe();
}
return $lastResult;
}
/**
* Returns Yes if any operand is Yes, otherwise the minimum.
*/
public static function maxMin(self ...$operands): self
{
if ($operands === []) {
throw new ShouldNotHappenException();
}
$operandValues = array_column($operands, 'value');
return self::create(max($operandValues) > self::MAYBE ? self::YES : min($operandValues));
}
/**
* @template T
* @param T[] $objects
* @param callable(T): self $callback
*/
public static function lazyMaxMin(
array $objects,
callable $callback,
): self
{
$results = [];
foreach ($objects as $object) {
$result = $callback($object);
if ($result->value === self::YES) {
return $result;
}
$results[] = $result;
}
return self::maxMin(...$results);
}
public function negate(): self
{
// 0b11 >> 0 == 0b11 (3)
// 0b11 >> 1 == 0b01 (1)
// 0b11 >> 3 == 0b00 (0)
return self::create(3 >> $this->value);
}
public function equals(self $other): bool
{
return $this === $other;
}
/**
* Returns the stronger of the two values, or null if they are equal (Yes > Maybe > No).
*/
public function compareTo(self $other): ?self
{
if ($this->value > $other->value) {
return $this;
} elseif ($other->value > $this->value) {
return $other;
}
return null;
}
public function describe(): string
{
static $labels = [
self::NO => 'No',
self::MAYBE => 'Maybe',
self::YES => 'Yes',
];
return $labels[$this->value];
}
}