/
MutableUnion.php
500 lines (429 loc) · 14 KB
/
MutableUnion.php
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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
<?php
namespace Psalm\Type;
use Psalm\Internal\DataFlow\DataFlowNode;
use Psalm\Internal\Type\TypeCombiner;
use Psalm\Internal\TypeVisitor\FromDocblockSetter;
use Psalm\Type;
use Psalm\Type\Atomic\Scalar;
use Psalm\Type\Atomic\TArray;
use Psalm\Type\Atomic\TClassString;
use Psalm\Type\Atomic\TFalse;
use Psalm\Type\Atomic\TFloat;
use Psalm\Type\Atomic\TInt;
use Psalm\Type\Atomic\TIntRange;
use Psalm\Type\Atomic\TLiteralFloat;
use Psalm\Type\Atomic\TLiteralInt;
use Psalm\Type\Atomic\TLiteralString;
use Psalm\Type\Atomic\TMixed;
use Psalm\Type\Atomic\TNamedObject;
use Psalm\Type\Atomic\TString;
use Psalm\Type\Atomic\TTemplateParamClass;
use Psalm\Type\Atomic\TTrue;
use Stringable;
use function count;
use function get_class;
use function get_object_vars;
use function strpos;
final class MutableUnion implements TypeNode, Stringable
{
use UnionTrait;
/**
* @var non-empty-array<string, Atomic>
*/
private $types;
/**
* Whether the type originated in a docblock
*
* @var bool
*/
public $from_docblock = false;
/**
* Whether the type originated from integer calculation
*
* @var bool
*/
public $from_calculation = false;
/**
* Whether the type originated from a property
*
* This helps turn isset($foo->bar) into a different sort of issue
*
* @var bool
*/
public $from_property = false;
/**
* Whether the type originated from *static* property
*
* Unlike non-static properties, static properties have no prescribed place
* like __construct() to be initialized in
*
* @var bool
*/
public $from_static_property = false;
/**
* Whether the property that this type has been derived from has been initialized in a constructor
*
* @var bool
*/
public $initialized = true;
/**
* Which class the type was initialised in
*
* @var ?string
*/
public $initialized_class;
/**
* Whether or not the type has been checked yet
*
* @var bool
*/
public $checked = false;
/**
* @var bool
*/
public $failed_reconciliation = false;
/**
* Whether or not to ignore issues with possibly-null values
*
* @var bool
*/
public $ignore_nullable_issues = false;
/**
* Whether or not to ignore issues with possibly-false values
*
* @var bool
*/
public $ignore_falsable_issues = false;
/**
* Whether or not to ignore issues with isset on this type
*
* @var bool
*/
public $ignore_isset = false;
/**
* Whether or not this variable is possibly undefined
*
* @var bool
*/
public $possibly_undefined = false;
/**
* Whether or not this variable is possibly undefined
*
* @var bool
*/
public $possibly_undefined_from_try = false;
/**
* Whether or not this union had a template, since replaced
*
* @var bool
*/
public $had_template = false;
/**
* Whether or not this union comes from a template "as" default
*
* @var bool
*/
public $from_template_default = false;
/**
* @var array<string, TLiteralString>
*/
private $literal_string_types = [];
/**
* @var array<string, TClassString>
*/
private $typed_class_strings = [];
/**
* @var array<string, TLiteralInt>
*/
private $literal_int_types = [];
/**
* @var array<string, TLiteralFloat>
*/
private $literal_float_types = [];
/**
* True if the type was passed or returned by reference, or if the type refers to an object's
* property or an item in an array. Note that this is not true for locally created references
* that don't refer to properties or array items (see Context::$references_in_scope).
*
* @var bool
*/
public $by_ref = false;
/**
* @var bool
*/
public $reference_free = false;
/**
* @var bool
*/
public $allow_mutations = true;
/**
* @var bool
*/
public $has_mutations = true;
/**
* This is a cache of getId on non-exact mode
* @var null|string
*/
private $id;
/**
* This is a cache of getId on exact mode
* @var null|string
*/
private $exact_id;
/**
* @var array<string, DataFlowNode>
*/
public $parent_nodes = [];
/**
* @var bool
*/
public $different = false;
/** @psalm-suppress PossiblyUnusedProperty */
public bool $propagate_parent_nodes = false;
/**
* @psalm-external-mutation-free
* @param non-empty-array<Atomic> $types
*/
public function setTypes(array $types): self
{
$this->literal_float_types = [];
$this->literal_int_types = [];
$this->literal_string_types = [];
$this->typed_class_strings = [];
$this->checked = false;
$from_docblock = false;
$keyed_types = [];
foreach ($types as $type) {
$key = $type->getKey();
$keyed_types[$key] = $type;
if ($type instanceof TLiteralInt) {
$this->literal_int_types[$key] = $type;
} elseif ($type instanceof TLiteralString) {
$this->literal_string_types[$key] = $type;
} elseif ($type instanceof TLiteralFloat) {
$this->literal_float_types[$key] = $type;
} elseif ($type instanceof TClassString
&& ($type->as_type || $type instanceof TTemplateParamClass)
) {
$this->typed_class_strings[$key] = $type;
}
$from_docblock = $from_docblock || $type->from_docblock;
}
$this->types = $keyed_types;
$this->from_docblock = $from_docblock;
return $this;
}
/**
* @psalm-external-mutation-free
*/
public function addType(Atomic $type): self
{
$this->types[$type->getKey()] = $type;
if ($type instanceof TLiteralString) {
$this->literal_string_types[$type->getKey()] = $type;
} elseif ($type instanceof TLiteralInt) {
$this->literal_int_types[$type->getKey()] = $type;
} elseif ($type instanceof TLiteralFloat) {
$this->literal_float_types[$type->getKey()] = $type;
} elseif ($type instanceof TString && $this->literal_string_types) {
foreach ($this->literal_string_types as $key => $_) {
unset($this->literal_string_types[$key], $this->types[$key]);
}
if (!$type instanceof TClassString
|| (!$type->as_type && !$type instanceof TTemplateParamClass)
) {
foreach ($this->typed_class_strings as $key => $_) {
unset($this->typed_class_strings[$key], $this->types[$key]);
}
}
} elseif ($type instanceof TInt && $this->literal_int_types) {
//we remove any literal that is already included in a wider type
$int_type_in_range = TIntRange::convertToIntRange($type);
foreach ($this->literal_int_types as $key => $literal_int_type) {
if ($int_type_in_range->contains($literal_int_type->value)) {
unset($this->literal_int_types[$key], $this->types[$key]);
}
}
} elseif ($type instanceof TFloat && $this->literal_float_types) {
foreach ($this->literal_float_types as $key => $_) {
unset($this->literal_float_types[$key], $this->types[$key]);
}
}
$this->bustCache();
return $this;
}
/**
* @psalm-external-mutation-free
*/
public function removeType(string $type_string): bool
{
if (isset($this->types[$type_string])) {
unset($this->types[$type_string]);
if (strpos($type_string, '(')) {
unset(
$this->literal_string_types[$type_string],
$this->literal_int_types[$type_string],
$this->literal_float_types[$type_string]
);
}
$this->bustCache();
return true;
}
if ($type_string === 'string') {
if ($this->literal_string_types) {
foreach ($this->literal_string_types as $literal_key => $_) {
unset($this->types[$literal_key]);
}
$this->literal_string_types = [];
}
if ($this->typed_class_strings) {
foreach ($this->typed_class_strings as $typed_class_key => $_) {
unset($this->types[$typed_class_key]);
}
$this->typed_class_strings = [];
}
unset($this->types['class-string'], $this->types['trait-string']);
} elseif ($type_string === 'int' && $this->literal_int_types) {
foreach ($this->literal_int_types as $literal_key => $_) {
unset($this->types[$literal_key]);
}
$this->literal_int_types = [];
} elseif ($type_string === 'float' && $this->literal_float_types) {
foreach ($this->literal_float_types as $literal_key => $_) {
unset($this->types[$literal_key]);
}
$this->literal_float_types = [];
}
return false;
}
public function setFromDocblock(bool $fromDocblock = true): self
{
$this->from_docblock = $fromDocblock;
(new FromDocblockSetter($fromDocblock))->traverseArray($this->types);
return $this;
}
/**
* @psalm-external-mutation-free
*/
public function bustCache(): void
{
$this->id = null;
$this->exact_id = null;
}
/**
* @psalm-external-mutation-free
* @param Union|MutableUnion $old_type
* @param Union|MutableUnion|null $new_type
*/
public function substitute($old_type, $new_type = null): self
{
if ($this->hasMixed() && !$this->isEmptyMixed()) {
return $this;
}
$old_type = $old_type->getBuilder();
if ($new_type) {
$new_type = $new_type->getBuilder();
}
if ($new_type && $new_type->ignore_nullable_issues) {
$this->ignore_nullable_issues = true;
}
if ($new_type && $new_type->ignore_falsable_issues) {
$this->ignore_falsable_issues = true;
}
foreach ($old_type->types as $old_type_part) {
$had = isset($this->types[$old_type_part->getKey()]);
$this->removeType($old_type_part->getKey());
if (!$had) {
if ($old_type_part instanceof TFalse
&& isset($this->types['bool'])
&& !isset($this->types['true'])
) {
$this->removeType('bool');
$this->types['true'] = new TTrue;
} elseif ($old_type_part instanceof TTrue
&& isset($this->types['bool'])
&& !isset($this->types['false'])
) {
$this->removeType('bool');
$this->types['false'] = new TFalse;
} elseif (isset($this->types['iterable'])) {
if ($old_type_part instanceof TNamedObject
&& $old_type_part->value === 'Traversable'
&& !isset($this->types['array'])
) {
$this->removeType('iterable');
$this->types['array'] = new TArray([Type::getArrayKey(), Type::getMixed()]);
}
if ($old_type_part instanceof TArray
&& !isset($this->types['traversable'])
) {
$this->removeType('iterable');
$this->types['traversable'] = new TNamedObject('Traversable');
}
} elseif (isset($this->types['array-key'])) {
if ($old_type_part instanceof TString
&& !isset($this->types['int'])
) {
$this->removeType('array-key');
$this->types['int'] = new TInt();
}
if ($old_type_part instanceof TInt
&& !isset($this->types['string'])
) {
$this->removeType('array-key');
$this->types['string'] = new TString();
}
}
}
}
if ($new_type) {
foreach ($new_type->types as $key => $new_type_part) {
if (!isset($this->types[$key])
|| ($new_type_part instanceof Scalar
&& get_class($new_type_part) === get_class($this->types[$key]))
) {
$this->types[$key] = $new_type_part;
} else {
$this->types[$key] = TypeCombiner::combine([$new_type_part, $this->types[$key]])->getSingleAtomic();
}
}
} elseif (count($this->types) === 0) {
$this->types['mixed'] = new TMixed();
}
$this->bustCache();
return $this;
}
/**
* @psalm-mutation-free
*/
public function getBuilder(): self
{
return $this;
}
/**
* @psalm-mutation-free
*/
public function freeze(): Union
{
/** @psalm-suppress InvalidArgument It's actually filtered internally */
return new Union($this->getAtomicTypes(), get_object_vars($this));
}
public static function visitMutable(MutableTypeVisitor $visitor, &$node, bool $cloned): bool
{
$result = true;
$changed = false;
foreach ($node->types as &$type) {
$type_orig = $type;
$result = $visitor->traverse($type);
$changed = $changed || $type_orig !== $type;
if (!$result) {
break;
}
}
unset($type);
if ($changed) {
$node->setTypes($node->types);
}
return $result;
}
}