forked from vimeo/psalm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Union.php
385 lines (337 loc) · 9.1 KB
/
Union.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
<?php
namespace Psalm\Type;
use Psalm\Internal\DataFlow\DataFlowNode;
use Psalm\Internal\TypeVisitor\FromDocblockSetter;
use Psalm\Storage\ImmutableNonCloneableTrait;
use Psalm\Type\Atomic\TClassString;
use Psalm\Type\Atomic\TLiteralFloat;
use Psalm\Type\Atomic\TLiteralInt;
use Psalm\Type\Atomic\TLiteralString;
use Stringable;
use function get_object_vars;
/**
* @psalm-immutable
* @psalm-type TProperties=array{
* from_docblock?: bool,
* from_calculation?: bool,
* from_property?: bool,
* from_static_property?: bool,
* initialized?: bool,
* initialized_class?: ?string,
* checked?: bool,
* failed_reconciliation?: bool,
* ignore_nullable_issues?: bool,
* ignore_falsable_issues?: bool,
* ignore_isset?: bool,
* possibly_undefined?: bool,
* possibly_undefined_from_try?: bool,
* explicit_never?: bool,
* had_template?: bool,
* from_template_default?: bool,
* by_ref?: bool,
* reference_free?: bool,
* allow_mutations?: bool,
* has_mutations?: bool,
* different?: bool,
* parent_nodes?: array<string, DataFlowNode>
* }
*/
final class Union implements TypeNode, Stringable
{
use ImmutableNonCloneableTrait;
use UnionTrait;
/**
* @psalm-readonly
* @var non-empty-array<string, Atomic>
*/
private array $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 this type had never set explicitly
* since it's the bottom type, it's combined into everything else and lost
*
* @var bool
*/
public $explicit_never = 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 array $literal_string_types = [];
/**
* @var array<string, TClassString>
*/
private array $typed_class_strings = [];
/**
* @var array<string, TLiteralInt>
*/
private array $literal_int_types = [];
/**
* @var array<string, TLiteralFloat>
*/
private array $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
*/
private ?string $id = null;
/**
* This is a cache of getId on exact mode
*/
private ?string $exact_id;
/**
* @var array<string, DataFlowNode>
*/
public $parent_nodes = [];
public bool $propagate_parent_nodes = false;
/**
* @var bool
*/
public $different = false;
/**
* @param TProperties $properties
* @return static
*/
public function setProperties(array $properties): self
{
$obj = null;
foreach ($properties as $key => $value) {
if ($this->{$key} !== $value) {
if ($obj === null) {
$obj = clone $this;
}
/** @psalm-suppress ImpurePropertyAssignment We just cloned this object */
$obj->{$key} = $value;
}
}
return $obj ?? $this;
}
/**
* @return static
*/
public function setDifferent(bool $different): self
{
if ($different === $this->different) {
return $this;
}
$cloned = clone $this;
$cloned->different = $different;
return $cloned;
}
/**
* @param array<string, DataFlowNode> $parent_nodes
* @return static
*/
public function setParentNodes(array $parent_nodes, bool $propagate_changes = false): self
{
if ($parent_nodes === $this->parent_nodes) {
return $this;
}
$cloned = clone $this;
$cloned->parent_nodes = $parent_nodes;
$cloned->propagate_parent_nodes = $propagate_changes;
return $cloned;
}
/**
* @param array<string, DataFlowNode> $parent_nodes
* @return static
*/
public function addParentNodes(array $parent_nodes): self
{
if (!$parent_nodes) {
return $this;
}
$parent_nodes = $this->parent_nodes + $parent_nodes;
if ($parent_nodes === $this->parent_nodes) {
return $this;
}
$cloned = clone $this;
$cloned->parent_nodes = $parent_nodes;
return $cloned;
}
/** @return static */
public function setPossiblyUndefined(bool $possibly_undefined, ?bool $from_try = null): self
{
$from_try ??= $this->possibly_undefined_from_try;
if ($this->possibly_undefined === $possibly_undefined
&& $this->possibly_undefined_from_try == $from_try
) {
return $this;
}
$cloned = clone $this;
$cloned->possibly_undefined = $possibly_undefined;
$cloned->possibly_undefined_from_try = $from_try;
return $cloned;
}
/** @return static */
public function setByRef(bool $by_ref)
{
if ($by_ref === $this->by_ref) {
return $this;
}
$cloned = clone $this;
$cloned->by_ref = $by_ref;
return $cloned;
}
/**
* @psalm-mutation-free
* @param non-empty-array<Atomic> $types
*/
public function setTypes(array $types): self
{
if ($types === $this->types) {
return $this;
}
return $this->getBuilder()->setTypes($types)->freeze();
}
/**
* @psalm-mutation-free
*/
public function getBuilder(): MutableUnion
{
/** @psalm-suppress InvalidArgument It's actually filtered internally */
return new MutableUnion($this->getAtomicTypes(), get_object_vars($this));
}
/**
* @psalm-mutation-free
*/
public function setFromDocblock(bool $fromDocblock = true): self
{
$cloned = clone $this;
/** @psalm-suppress ImpureMethodCall Acting on clone */
(new FromDocblockSetter($fromDocblock))->traverse($cloned);
return $cloned;
}
/**
* @phpcsSuppress SlevomatCodingStandard.TypeHints.ParameterTypeHint.MissingAnyTypeHint
*/
public static function visitMutable(MutableTypeVisitor $visitor, &$node, bool $cloned): bool
{
$result = true;
$changed = false;
$types = $node->types;
foreach ($types as &$type) {
$type_orig = $type;
$result = $visitor->traverse($type);
$changed = $changed || $type_orig !== $type;
if (!$result) {
break;
}
}
unset($type);
if ($changed) {
$node = $node->setTypes($types);
}
return $result;
}
}