-
Notifications
You must be signed in to change notification settings - Fork 504
/
Copy pathConstantArrayTypeBuilder.php
332 lines (275 loc) · 8.83 KB
/
ConstantArrayTypeBuilder.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
<?php declare(strict_types = 1);
namespace PHPStan\Type\Constant;
use PHPStan\ShouldNotHappenException;
use PHPStan\TrinaryLogic;
use PHPStan\Type\Accessory\AccessoryArrayListType;
use PHPStan\Type\Accessory\NonEmptyArrayType;
use PHPStan\Type\Accessory\OversizedArrayType;
use PHPStan\Type\ArrayType;
use PHPStan\Type\Type;
use PHPStan\Type\TypeCombinator;
use PHPStan\Type\TypeUtils;
use function array_filter;
use function array_map;
use function array_unique;
use function array_values;
use function count;
use function in_array;
use function is_float;
use function max;
use function min;
use function range;
/**
* @api
*/
final class ConstantArrayTypeBuilder
{
public const ARRAY_COUNT_LIMIT = 256;
private bool $degradeToGeneralArray = false;
private bool $oversized = false;
/**
* @param array<int, Type> $keyTypes
* @param array<int, Type> $valueTypes
* @param non-empty-list<int> $nextAutoIndexes
* @param array<int> $optionalKeys
*/
private function __construct(
private array $keyTypes,
private array $valueTypes,
private array $nextAutoIndexes,
private array $optionalKeys,
private TrinaryLogic $isList,
)
{
}
public static function createEmpty(): self
{
return new self([], [], [0], [], TrinaryLogic::createYes());
}
public static function createEmptyIndeterminate(): self
{
return new self([], [], [0], [], TrinaryLogic::createMaybe());
}
public static function createFromConstantArray(ConstantArrayType $startArrayType): self
{
$builder = new self(
$startArrayType->getKeyTypes(),
$startArrayType->getValueTypes(),
$startArrayType->getNextAutoIndexes(),
$startArrayType->getOptionalKeys(),
$startArrayType->isList(),
);
if (count($startArrayType->getKeyTypes()) > self::ARRAY_COUNT_LIMIT) {
$builder->degradeToGeneralArray(true);
}
return $builder;
}
public function setOffsetValueType(?Type $offsetType, Type $valueType, bool $optional = false): void
{
if ($offsetType !== null) {
$offsetType = $offsetType->toArrayKey();
}
if (!$this->degradeToGeneralArray) {
if ($offsetType === null) {
$newAutoIndexes = $optional ? $this->nextAutoIndexes : [];
$hasOptional = false;
foreach ($this->keyTypes as $i => $keyType) {
if (!$keyType instanceof ConstantIntegerType) {
continue;
}
if (!in_array($keyType->getValue(), $this->nextAutoIndexes, true)) {
continue;
}
$this->valueTypes[$i] = TypeCombinator::union($this->valueTypes[$i], $valueType);
if (!$hasOptional && !$optional) {
$this->optionalKeys = array_values(array_filter($this->optionalKeys, static fn (int $index): bool => $index !== $i));
}
/** @var int|float $newAutoIndex */
$newAutoIndex = $keyType->getValue() + 1;
if (is_float($newAutoIndex)) {
$newAutoIndex = $keyType->getValue();
}
$newAutoIndexes[] = $newAutoIndex;
$hasOptional = true;
}
$max = max($this->nextAutoIndexes);
$this->keyTypes[] = new ConstantIntegerType($max);
$this->valueTypes[] = $valueType;
/** @var int|float $newAutoIndex */
$newAutoIndex = $max + 1;
if (is_float($newAutoIndex)) {
$newAutoIndex = $max;
}
$newAutoIndexes[] = $newAutoIndex;
$this->nextAutoIndexes = array_values(array_unique($newAutoIndexes));
if ($optional || $hasOptional) {
$this->optionalKeys[] = count($this->keyTypes) - 1;
}
if (count($this->keyTypes) > self::ARRAY_COUNT_LIMIT) {
$this->degradeToGeneralArray = true;
$this->oversized = true;
}
return;
}
if ($offsetType instanceof ConstantIntegerType || $offsetType instanceof ConstantStringType) {
/** @var ConstantIntegerType|ConstantStringType $keyType */
foreach ($this->keyTypes as $i => $keyType) {
if ($keyType->getValue() !== $offsetType->getValue()) {
continue;
}
if ($optional) {
$valueType = TypeCombinator::union($valueType, $this->valueTypes[$i]);
}
$this->valueTypes[$i] = $valueType;
if (!$optional) {
$this->optionalKeys = array_values(array_filter($this->optionalKeys, static fn (int $index): bool => $index !== $i));
if ($keyType instanceof ConstantIntegerType) {
$nextAutoIndexes = array_values(array_filter($this->nextAutoIndexes, static fn (int $index) => $index > $keyType->getValue()));
if (count($nextAutoIndexes) === 0) {
throw new ShouldNotHappenException();
}
$this->nextAutoIndexes = $nextAutoIndexes;
}
}
return;
}
$this->keyTypes[] = $offsetType;
$this->valueTypes[] = $valueType;
if ($offsetType instanceof ConstantIntegerType) {
$min = min($this->nextAutoIndexes);
$max = max($this->nextAutoIndexes);
$offsetValue = $offsetType->getValue();
if ($offsetValue >= 0) {
if ($offsetValue > $min) {
if ($offsetValue <= $max) {
$this->isList = $this->isList->and(TrinaryLogic::createMaybe());
} else {
$this->isList = TrinaryLogic::createNo();
}
}
} else {
$this->isList = TrinaryLogic::createNo();
}
if ($offsetValue >= $max) {
/** @var int|float $newAutoIndex */
$newAutoIndex = $offsetValue + 1;
if (is_float($newAutoIndex)) {
$newAutoIndex = $max;
}
if (!$optional) {
$this->nextAutoIndexes = [$newAutoIndex];
} else {
$this->nextAutoIndexes[] = $newAutoIndex;
}
}
} else {
$this->isList = TrinaryLogic::createNo();
}
if ($optional) {
$this->optionalKeys[] = count($this->keyTypes) - 1;
}
if (count($this->keyTypes) > self::ARRAY_COUNT_LIMIT) {
$this->degradeToGeneralArray = true;
$this->oversized = true;
}
return;
}
$scalarTypes = $offsetType->getConstantScalarTypes();
if (count($scalarTypes) === 0) {
$integerRanges = TypeUtils::getIntegerRanges($offsetType);
if (count($integerRanges) > 0) {
foreach ($integerRanges as $integerRange) {
if ($integerRange->getMin() === null) {
break;
}
if ($integerRange->getMax() === null) {
break;
}
$rangeLength = $integerRange->getMax() - $integerRange->getMin();
if ($rangeLength >= self::ARRAY_COUNT_LIMIT) {
$scalarTypes = [];
break;
}
foreach (range($integerRange->getMin(), $integerRange->getMax()) as $rangeValue) {
$scalarTypes[] = new ConstantIntegerType($rangeValue);
}
}
}
}
if (count($scalarTypes) > 0 && count($scalarTypes) < self::ARRAY_COUNT_LIMIT) {
$match = true;
$valueTypes = $this->valueTypes;
foreach ($scalarTypes as $scalarType) {
$scalarOffsetType = $scalarType->toArrayKey();
if (!$scalarOffsetType instanceof ConstantIntegerType && !$scalarOffsetType instanceof ConstantStringType) {
throw new ShouldNotHappenException();
}
$offsetMatch = false;
/** @var ConstantIntegerType|ConstantStringType $keyType */
foreach ($this->keyTypes as $i => $keyType) {
if ($keyType->getValue() !== $scalarOffsetType->getValue()) {
continue;
}
$valueTypes[$i] = TypeCombinator::union($valueTypes[$i], $valueType);
$offsetMatch = true;
}
if ($offsetMatch) {
continue;
}
$match = false;
}
if ($match) {
$this->valueTypes = $valueTypes;
return;
}
}
$this->isList = TrinaryLogic::createNo();
}
if ($offsetType === null) {
$offsetType = TypeCombinator::union(...array_map(static fn (int $index) => new ConstantIntegerType($index), $this->nextAutoIndexes));
} else {
$this->isList = TrinaryLogic::createNo();
}
$this->keyTypes[] = $offsetType;
$this->valueTypes[] = $valueType;
if ($optional) {
$this->optionalKeys[] = count($this->keyTypes) - 1;
}
$this->degradeToGeneralArray = true;
}
public function degradeToGeneralArray(bool $oversized = false): void
{
$this->degradeToGeneralArray = true;
$this->oversized = $this->oversized || $oversized;
}
public function getArray(): Type
{
$keyTypesCount = count($this->keyTypes);
if ($keyTypesCount === 0) {
return new ConstantArrayType([], []);
}
if (!$this->degradeToGeneralArray) {
/** @var array<int, ConstantIntegerType|ConstantStringType> $keyTypes */
$keyTypes = $this->keyTypes;
return new ConstantArrayType($keyTypes, $this->valueTypes, $this->nextAutoIndexes, $this->optionalKeys, $this->isList);
}
$array = new ArrayType(
TypeCombinator::union(...$this->keyTypes),
TypeCombinator::union(...$this->valueTypes),
);
if (count($this->optionalKeys) < $keyTypesCount) {
$array = TypeCombinator::intersect($array, new NonEmptyArrayType());
}
if ($this->oversized) {
$array = TypeCombinator::intersect($array, new OversizedArrayType());
}
if ($this->isList->yes()) {
$array = TypeCombinator::intersect($array, new AccessoryArrayListType());
}
return $array;
}
public function isList(): bool
{
return $this->isList->yes();
}
}