-
Notifications
You must be signed in to change notification settings - Fork 501
/
Copy pathArrayType.php
586 lines (481 loc) · 15.6 KB
/
ArrayType.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
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
<?php declare(strict_types = 1);
namespace PHPStan\Type;
use PHPStan\Php\PhpVersion;
use PHPStan\PhpDocParser\Ast\Type\GenericTypeNode;
use PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode;
use PHPStan\PhpDocParser\Ast\Type\TypeNode;
use PHPStan\Reflection\ClassMemberAccessAnswerer;
use PHPStan\Reflection\TrivialParametersAcceptor;
use PHPStan\ShouldNotHappenException;
use PHPStan\TrinaryLogic;
use PHPStan\Type\Accessory\AccessoryArrayListType;
use PHPStan\Type\Accessory\HasOffsetValueType;
use PHPStan\Type\Accessory\NonEmptyArrayType;
use PHPStan\Type\Constant\ConstantArrayType;
use PHPStan\Type\Constant\ConstantArrayTypeBuilder;
use PHPStan\Type\Constant\ConstantBooleanType;
use PHPStan\Type\Constant\ConstantFloatType;
use PHPStan\Type\Constant\ConstantIntegerType;
use PHPStan\Type\Constant\ConstantStringType;
use PHPStan\Type\Generic\TemplateMixedType;
use PHPStan\Type\Generic\TemplateStrictMixedType;
use PHPStan\Type\Generic\TemplateTypeMap;
use PHPStan\Type\Generic\TemplateTypeVariance;
use PHPStan\Type\Traits\ArrayTypeTrait;
use PHPStan\Type\Traits\MaybeCallableTypeTrait;
use PHPStan\Type\Traits\NonGeneralizableTypeTrait;
use PHPStan\Type\Traits\NonObjectTypeTrait;
use PHPStan\Type\Traits\UndecidedBooleanTypeTrait;
use PHPStan\Type\Traits\UndecidedComparisonTypeTrait;
use function array_merge;
use function count;
use function sprintf;
/** @api */
class ArrayType implements Type
{
use ArrayTypeTrait;
use MaybeCallableTypeTrait;
use NonObjectTypeTrait;
use UndecidedBooleanTypeTrait;
use UndecidedComparisonTypeTrait;
use NonGeneralizableTypeTrait;
private Type $keyType;
/** @api */
public function __construct(Type $keyType, private Type $itemType)
{
if ($keyType->describe(VerbosityLevel::value()) === '(int|string)') {
$keyType = new MixedType();
}
if ($keyType instanceof StrictMixedType && !$keyType instanceof TemplateStrictMixedType) {
$keyType = new UnionType([new StringType(), new IntegerType()]);
}
$this->keyType = $keyType;
}
public function getKeyType(): Type
{
return $this->keyType;
}
public function getItemType(): Type
{
return $this->itemType;
}
public function getReferencedClasses(): array
{
return array_merge(
$this->keyType->getReferencedClasses(),
$this->getItemType()->getReferencedClasses(),
);
}
public function getConstantArrays(): array
{
return [];
}
public function accepts(Type $type, bool $strictTypes): AcceptsResult
{
if ($type instanceof CompoundType) {
return $type->isAcceptedBy($this, $strictTypes);
}
if ($type instanceof ConstantArrayType) {
$result = AcceptsResult::createYes();
$thisKeyType = $this->keyType;
$itemType = $this->getItemType();
foreach ($type->getKeyTypes() as $i => $keyType) {
$valueType = $type->getValueTypes()[$i];
$acceptsKey = $thisKeyType->accepts($keyType, $strictTypes);
$acceptsValue = $itemType->accepts($valueType, $strictTypes);
$result = $result->and($acceptsKey)->and($acceptsValue);
}
return $result;
}
if ($type instanceof ArrayType) {
return $this->getItemType()->accepts($type->getItemType(), $strictTypes)
->and($this->keyType->accepts($type->keyType, $strictTypes));
}
return AcceptsResult::createNo();
}
public function isSuperTypeOf(Type $type): IsSuperTypeOfResult
{
if ($type instanceof self || $type instanceof ConstantArrayType) {
return $this->getItemType()->isSuperTypeOf($type->getItemType())
->and($this->getIterableKeyType()->isSuperTypeOf($type->getIterableKeyType()));
}
if ($type instanceof CompoundType) {
return $type->isSubTypeOf($this);
}
return IsSuperTypeOfResult::createNo();
}
public function equals(Type $type): bool
{
return $type instanceof self
&& $this->getItemType()->equals($type->getIterableValueType())
&& $this->keyType->equals($type->keyType);
}
public function describe(VerbosityLevel $level): string
{
$isMixedKeyType = $this->keyType instanceof MixedType && $this->keyType->describe(VerbosityLevel::precise()) === 'mixed' && !$this->keyType->isExplicitMixed();
$isMixedItemType = $this->itemType instanceof MixedType && $this->itemType->describe(VerbosityLevel::precise()) === 'mixed' && !$this->itemType->isExplicitMixed();
$valueHandler = function () use ($level, $isMixedKeyType, $isMixedItemType): string {
if ($isMixedKeyType || $this->keyType instanceof NeverType) {
if ($isMixedItemType || $this->itemType instanceof NeverType) {
return 'array';
}
return sprintf('array<%s>', $this->itemType->describe($level));
}
return sprintf('array<%s, %s>', $this->keyType->describe($level), $this->itemType->describe($level));
};
return $level->handle(
$valueHandler,
$valueHandler,
function () use ($level, $isMixedKeyType, $isMixedItemType): string {
if ($isMixedKeyType) {
if ($isMixedItemType) {
return 'array';
}
return sprintf('array<%s>', $this->itemType->describe($level));
}
return sprintf('array<%s, %s>', $this->keyType->describe($level), $this->itemType->describe($level));
},
);
}
public function generalizeValues(): self
{
return new self($this->keyType, $this->itemType->generalize(GeneralizePrecision::lessSpecific()));
}
public function getKeysArrayFiltered(Type $filterValueType, TrinaryLogic $strict): Type
{
return $this->getKeysArray();
}
public function getKeysArray(): Type
{
return TypeCombinator::intersect(new self(new IntegerType(), $this->getIterableKeyType()), new AccessoryArrayListType());
}
public function getValuesArray(): Type
{
return TypeCombinator::intersect(new self(new IntegerType(), $this->itemType), new AccessoryArrayListType());
}
public function isIterableAtLeastOnce(): TrinaryLogic
{
return TrinaryLogic::createMaybe();
}
public function getArraySize(): Type
{
return IntegerRangeType::fromInterval(0, null);
}
public function getIterableKeyType(): Type
{
$keyType = $this->keyType;
if ($keyType instanceof MixedType && !$keyType instanceof TemplateMixedType) {
return new BenevolentUnionType([new IntegerType(), new StringType()]);
}
if ($keyType instanceof StrictMixedType) {
return new BenevolentUnionType([new IntegerType(), new StringType()]);
}
return $keyType;
}
public function getFirstIterableKeyType(): Type
{
return $this->getIterableKeyType();
}
public function getLastIterableKeyType(): Type
{
return $this->getIterableKeyType();
}
public function getIterableValueType(): Type
{
return $this->getItemType();
}
public function getFirstIterableValueType(): Type
{
return $this->getItemType();
}
public function getLastIterableValueType(): Type
{
return $this->getItemType();
}
public function isConstantArray(): TrinaryLogic
{
return TrinaryLogic::createNo();
}
public function isList(): TrinaryLogic
{
if (IntegerRangeType::fromInterval(0, null)->isSuperTypeOf($this->getKeyType())->no()) {
return TrinaryLogic::createNo();
}
return TrinaryLogic::createMaybe();
}
public function isConstantValue(): TrinaryLogic
{
return TrinaryLogic::createNo();
}
public function looseCompare(Type $type, PhpVersion $phpVersion): BooleanType
{
if ($type->isInteger()->yes()) {
return new ConstantBooleanType(false);
}
return new BooleanType();
}
public function hasOffsetValueType(Type $offsetType): TrinaryLogic
{
$offsetType = $offsetType->toArrayKey();
if ($this->getKeyType()->isSuperTypeOf($offsetType)->no()
&& ($offsetType->isString()->no() || !$offsetType->isConstantScalarValue()->no())
) {
return TrinaryLogic::createNo();
}
return TrinaryLogic::createMaybe();
}
public function getOffsetValueType(Type $offsetType): Type
{
$offsetType = $offsetType->toArrayKey();
if ($this->getKeyType()->isSuperTypeOf($offsetType)->no()
&& ($offsetType->isString()->no() || !$offsetType->isConstantScalarValue()->no())
) {
return new ErrorType();
}
$type = $this->getItemType();
if ($type instanceof ErrorType) {
return new MixedType();
}
return $type;
}
public function setOffsetValueType(?Type $offsetType, Type $valueType, bool $unionValues = true): Type
{
if ($offsetType === null) {
$isKeyTypeInteger = $this->keyType->isInteger();
if ($isKeyTypeInteger->no()) {
$offsetType = new IntegerType();
} elseif ($isKeyTypeInteger->yes()) {
/** @var list<ConstantIntegerType> $constantScalars */
$constantScalars = $this->keyType->getConstantScalarTypes();
if (count($constantScalars) > 0) {
foreach ($constantScalars as $constantScalar) {
$constantScalars[] = ConstantTypeHelper::getTypeFromValue($constantScalar->getValue() + 1);
}
$offsetType = TypeCombinator::union(...$constantScalars);
} else {
$offsetType = $this->keyType;
}
} else {
$integerTypes = [];
TypeTraverser::map($this->keyType, static function (Type $type, callable $traverse) use (&$integerTypes): Type {
if ($type instanceof UnionType) {
return $traverse($type);
}
$isInteger = $type->isInteger();
if ($isInteger->yes()) {
$integerTypes[] = $type;
}
return $type;
});
if (count($integerTypes) === 0) {
$offsetType = $this->keyType;
} else {
$offsetType = TypeCombinator::union(...$integerTypes);
}
}
} else {
$offsetType = $offsetType->toArrayKey();
}
if ($offsetType instanceof ConstantStringType || $offsetType instanceof ConstantIntegerType) {
if ($offsetType->isSuperTypeOf($this->keyType)->yes()) {
$builder = ConstantArrayTypeBuilder::createEmpty();
$builder->setOffsetValueType($offsetType, $valueType);
return $builder->getArray();
}
return TypeCombinator::intersect(
new self(
TypeCombinator::union($this->keyType, $offsetType),
TypeCombinator::union($this->itemType, $valueType),
),
new HasOffsetValueType($offsetType, $valueType),
new NonEmptyArrayType(),
);
}
return TypeCombinator::intersect(
new self(
TypeCombinator::union($this->keyType, $offsetType),
$unionValues ? TypeCombinator::union($this->itemType, $valueType) : $valueType,
),
new NonEmptyArrayType(),
);
}
public function setExistingOffsetValueType(Type $offsetType, Type $valueType): Type
{
return new self(
$this->keyType,
TypeCombinator::union($this->itemType, $valueType),
);
}
public function unsetOffset(Type $offsetType): Type
{
$offsetType = $offsetType->toArrayKey();
if (
($offsetType instanceof ConstantIntegerType || $offsetType instanceof ConstantStringType)
&& !$this->keyType->isSuperTypeOf($offsetType)->no()
) {
$keyType = TypeCombinator::remove($this->keyType, $offsetType);
if ($keyType instanceof NeverType) {
return new ConstantArrayType([], []);
}
return new self($keyType, $this->itemType);
}
return $this;
}
public function fillKeysArray(Type $valueType): Type
{
$itemType = $this->getItemType();
if ($itemType->isInteger()->no()) {
$stringKeyType = $itemType->toString();
if ($stringKeyType instanceof ErrorType) {
return $stringKeyType;
}
return new ArrayType($stringKeyType, $valueType);
}
return new ArrayType($itemType, $valueType);
}
public function flipArray(): Type
{
return new self($this->getIterableValueType()->toArrayKey(), $this->getIterableKeyType());
}
public function intersectKeyArray(Type $otherArraysType): Type
{
$isKeySuperType = $otherArraysType->getIterableKeyType()->isSuperTypeOf($this->getIterableKeyType());
if ($isKeySuperType->no()) {
return ConstantArrayTypeBuilder::createEmpty()->getArray();
}
if ($isKeySuperType->yes()) {
return $this;
}
return new self($otherArraysType->getIterableKeyType(), $this->getIterableValueType());
}
public function popArray(): Type
{
return $this;
}
public function reverseArray(TrinaryLogic $preserveKeys): Type
{
return $this;
}
public function searchArray(Type $needleType): Type
{
return TypeCombinator::union($this->getIterableKeyType(), new ConstantBooleanType(false));
}
public function shiftArray(): Type
{
return $this;
}
public function shuffleArray(): Type
{
return TypeCombinator::intersect(new self(new IntegerType(), $this->itemType), new AccessoryArrayListType());
}
public function sliceArray(Type $offsetType, Type $lengthType, TrinaryLogic $preserveKeys): Type
{
return $this;
}
public function isCallable(): TrinaryLogic
{
return TrinaryLogic::createMaybe()->and($this->itemType->isString());
}
public function getCallableParametersAcceptors(ClassMemberAccessAnswerer $scope): array
{
if ($this->isCallable()->no()) {
throw new ShouldNotHappenException();
}
return [new TrivialParametersAcceptor()];
}
public function toInteger(): Type
{
return TypeCombinator::union(
new ConstantIntegerType(0),
new ConstantIntegerType(1),
);
}
public function toFloat(): Type
{
return TypeCombinator::union(
new ConstantFloatType(0.0),
new ConstantFloatType(1.0),
);
}
public function inferTemplateTypes(Type $receivedType): TemplateTypeMap
{
if ($receivedType instanceof UnionType || $receivedType instanceof IntersectionType) {
return $receivedType->inferTemplateTypesOn($this);
}
if ($receivedType->isArray()->yes()) {
$keyTypeMap = $this->getIterableKeyType()->inferTemplateTypes($receivedType->getIterableKeyType());
$itemTypeMap = $this->getItemType()->inferTemplateTypes($receivedType->getIterableValueType());
return $keyTypeMap->union($itemTypeMap);
}
return TemplateTypeMap::createEmpty();
}
public function getReferencedTemplateTypes(TemplateTypeVariance $positionVariance): array
{
$variance = $positionVariance->compose(TemplateTypeVariance::createCovariant());
return array_merge(
$this->getIterableKeyType()->getReferencedTemplateTypes($variance),
$this->getItemType()->getReferencedTemplateTypes($variance),
);
}
public function traverse(callable $cb): Type
{
$keyType = $cb($this->keyType);
$itemType = $cb($this->itemType);
if ($keyType !== $this->keyType || $itemType !== $this->itemType) {
if ($keyType instanceof NeverType && $itemType instanceof NeverType) {
return new ConstantArrayType([], []);
}
return new self($keyType, $itemType);
}
return $this;
}
public function toPhpDocNode(): TypeNode
{
$isMixedKeyType = $this->keyType instanceof MixedType && $this->keyType->describe(VerbosityLevel::precise()) === 'mixed' && !$this->keyType->isExplicitMixed();
$isMixedItemType = $this->itemType instanceof MixedType && $this->itemType->describe(VerbosityLevel::precise()) === 'mixed' && !$this->itemType->isExplicitMixed();
if ($isMixedKeyType) {
if ($isMixedItemType) {
return new IdentifierTypeNode('array');
}
return new GenericTypeNode(
new IdentifierTypeNode('array'),
[
$this->itemType->toPhpDocNode(),
],
);
}
return new GenericTypeNode(
new IdentifierTypeNode('array'),
[
$this->keyType->toPhpDocNode(),
$this->itemType->toPhpDocNode(),
],
);
}
public function traverseSimultaneously(Type $right, callable $cb): Type
{
$keyType = $cb($this->keyType, $right->getIterableKeyType());
$itemType = $cb($this->itemType, $right->getIterableValueType());
if ($keyType !== $this->keyType || $itemType !== $this->itemType) {
if ($keyType instanceof NeverType && $itemType instanceof NeverType) {
return new ConstantArrayType([], []);
}
return new self($keyType, $itemType);
}
return $this;
}
public function tryRemove(Type $typeToRemove): ?Type
{
if ($typeToRemove->isConstantArray()->yes() && $typeToRemove->isIterableAtLeastOnce()->no()) {
return TypeCombinator::intersect($this, new NonEmptyArrayType());
}
if ($typeToRemove instanceof NonEmptyArrayType) {
return new ConstantArrayType([], []);
}
return null;
}
public function getFiniteTypes(): array
{
return [];
}
}