1: <?php declare(strict_types = 1);
2:
3: namespace PHPStan\Type;
4:
5: use PHPStan\Php\PhpVersion;
6: use PHPStan\PhpDocParser\Ast\Type\GenericTypeNode;
7: use PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode;
8: use PHPStan\PhpDocParser\Ast\Type\TypeNode;
9: use PHPStan\Reflection\ClassMemberAccessAnswerer;
10: use PHPStan\Reflection\TrivialParametersAcceptor;
11: use PHPStan\Rules\Arrays\AllowedArrayKeysTypes;
12: use PHPStan\ShouldNotHappenException;
13: use PHPStan\TrinaryLogic;
14: use PHPStan\Type\Accessory\AccessoryArrayListType;
15: use PHPStan\Type\Accessory\HasOffsetValueType;
16: use PHPStan\Type\Accessory\NonEmptyArrayType;
17: use PHPStan\Type\Constant\ConstantArrayType;
18: use PHPStan\Type\Constant\ConstantArrayTypeBuilder;
19: use PHPStan\Type\Constant\ConstantBooleanType;
20: use PHPStan\Type\Constant\ConstantFloatType;
21: use PHPStan\Type\Constant\ConstantIntegerType;
22: use PHPStan\Type\Constant\ConstantStringType;
23: use PHPStan\Type\Generic\TemplateMixedType;
24: use PHPStan\Type\Generic\TemplateStrictMixedType;
25: use PHPStan\Type\Generic\TemplateTypeMap;
26: use PHPStan\Type\Generic\TemplateTypeVariance;
27: use PHPStan\Type\Traits\ArrayTypeTrait;
28: use PHPStan\Type\Traits\MaybeCallableTypeTrait;
29: use PHPStan\Type\Traits\NonGeneralizableTypeTrait;
30: use PHPStan\Type\Traits\NonObjectTypeTrait;
31: use PHPStan\Type\Traits\UndecidedBooleanTypeTrait;
32: use PHPStan\Type\Traits\UndecidedComparisonTypeTrait;
33: use function array_merge;
34: use function count;
35: use function sprintf;
36:
37: /** @api */
38: class ArrayType implements Type
39: {
40:
41: use ArrayTypeTrait;
42: use MaybeCallableTypeTrait;
43: use NonObjectTypeTrait;
44: use UndecidedBooleanTypeTrait;
45: use UndecidedComparisonTypeTrait;
46: use NonGeneralizableTypeTrait;
47:
48: private Type $keyType;
49:
50: /** @api */
51: public function __construct(Type $keyType, private Type $itemType)
52: {
53: if ($keyType->describe(VerbosityLevel::value()) === '(int|string)') {
54: $keyType = new MixedType();
55: }
56: if ($keyType instanceof StrictMixedType && !$keyType instanceof TemplateStrictMixedType) {
57: $keyType = new UnionType([new StringType(), new IntegerType()]);
58: }
59:
60: $this->keyType = $keyType;
61: }
62:
63: public function getKeyType(): Type
64: {
65: return $this->keyType;
66: }
67:
68: public function getItemType(): Type
69: {
70: return $this->itemType;
71: }
72:
73: public function getReferencedClasses(): array
74: {
75: return array_merge(
76: $this->keyType->getReferencedClasses(),
77: $this->getItemType()->getReferencedClasses(),
78: );
79: }
80:
81: public function getConstantArrays(): array
82: {
83: return [];
84: }
85:
86: public function accepts(Type $type, bool $strictTypes): AcceptsResult
87: {
88: if ($type instanceof CompoundType) {
89: return $type->isAcceptedBy($this, $strictTypes);
90: }
91:
92: if ($type instanceof ConstantArrayType) {
93: $result = AcceptsResult::createYes();
94: $thisKeyType = $this->keyType;
95: $itemType = $this->getItemType();
96: foreach ($type->getKeyTypes() as $i => $keyType) {
97: $valueType = $type->getValueTypes()[$i];
98: $acceptsKey = $thisKeyType->accepts($keyType, $strictTypes);
99: $acceptsValue = $itemType->accepts($valueType, $strictTypes);
100: $result = $result->and($acceptsKey)->and($acceptsValue);
101: }
102:
103: return $result;
104: }
105:
106: if ($type instanceof ArrayType) {
107: return $this->getItemType()->accepts($type->getItemType(), $strictTypes)
108: ->and($this->keyType->accepts($type->keyType, $strictTypes));
109: }
110:
111: return AcceptsResult::createNo();
112: }
113:
114: public function isSuperTypeOf(Type $type): IsSuperTypeOfResult
115: {
116: if ($type instanceof self || $type instanceof ConstantArrayType) {
117: return $this->getItemType()->isSuperTypeOf($type->getItemType())
118: ->and($this->getIterableKeyType()->isSuperTypeOf($type->getIterableKeyType()));
119: }
120:
121: if ($type instanceof CompoundType) {
122: return $type->isSubTypeOf($this);
123: }
124:
125: return IsSuperTypeOfResult::createNo();
126: }
127:
128: public function equals(Type $type): bool
129: {
130: return $type instanceof self
131: && $this->getItemType()->equals($type->getIterableValueType())
132: && $this->keyType->equals($type->keyType);
133: }
134:
135: public function describe(VerbosityLevel $level): string
136: {
137: $isMixedKeyType = $this->keyType instanceof MixedType && $this->keyType->describe(VerbosityLevel::precise()) === 'mixed' && !$this->keyType->isExplicitMixed();
138: $isMixedItemType = $this->itemType instanceof MixedType && $this->itemType->describe(VerbosityLevel::precise()) === 'mixed' && !$this->itemType->isExplicitMixed();
139:
140: $valueHandler = function () use ($level, $isMixedKeyType, $isMixedItemType): string {
141: if ($isMixedKeyType || $this->keyType instanceof NeverType) {
142: if ($isMixedItemType || $this->itemType instanceof NeverType) {
143: return 'array';
144: }
145:
146: return sprintf('array<%s>', $this->itemType->describe($level));
147: }
148:
149: return sprintf('array<%s, %s>', $this->keyType->describe($level), $this->itemType->describe($level));
150: };
151:
152: return $level->handle(
153: $valueHandler,
154: $valueHandler,
155: function () use ($level, $isMixedKeyType, $isMixedItemType): string {
156: if ($isMixedKeyType) {
157: if ($isMixedItemType) {
158: return 'array';
159: }
160:
161: return sprintf('array<%s>', $this->itemType->describe($level));
162: }
163:
164: return sprintf('array<%s, %s>', $this->keyType->describe($level), $this->itemType->describe($level));
165: },
166: );
167: }
168:
169: public function generalizeValues(): self
170: {
171: return new self($this->keyType, $this->itemType->generalize(GeneralizePrecision::lessSpecific()));
172: }
173:
174: public function getKeysArrayFiltered(Type $filterValueType, TrinaryLogic $strict): Type
175: {
176: return $this->getKeysArray();
177: }
178:
179: public function getKeysArray(): Type
180: {
181: return TypeCombinator::intersect(new self(new IntegerType(), $this->getIterableKeyType()), new AccessoryArrayListType());
182: }
183:
184: public function getValuesArray(): Type
185: {
186: return TypeCombinator::intersect(new self(new IntegerType(), $this->itemType), new AccessoryArrayListType());
187: }
188:
189: public function isIterableAtLeastOnce(): TrinaryLogic
190: {
191: return TrinaryLogic::createMaybe();
192: }
193:
194: public function getArraySize(): Type
195: {
196: return IntegerRangeType::fromInterval(0, null);
197: }
198:
199: public function getIterableKeyType(): Type
200: {
201: $keyType = $this->keyType;
202: if ($keyType instanceof MixedType && !$keyType instanceof TemplateMixedType) {
203: return new BenevolentUnionType([new IntegerType(), new StringType()]);
204: }
205: if ($keyType instanceof StrictMixedType) {
206: return new BenevolentUnionType([new IntegerType(), new StringType()]);
207: }
208:
209: return $keyType;
210: }
211:
212: public function getFirstIterableKeyType(): Type
213: {
214: return $this->getIterableKeyType();
215: }
216:
217: public function getLastIterableKeyType(): Type
218: {
219: return $this->getIterableKeyType();
220: }
221:
222: public function getIterableValueType(): Type
223: {
224: return $this->getItemType();
225: }
226:
227: public function getFirstIterableValueType(): Type
228: {
229: return $this->getItemType();
230: }
231:
232: public function getLastIterableValueType(): Type
233: {
234: return $this->getItemType();
235: }
236:
237: public function isConstantArray(): TrinaryLogic
238: {
239: return TrinaryLogic::createNo();
240: }
241:
242: public function isList(): TrinaryLogic
243: {
244: if (IntegerRangeType::fromInterval(0, null)->isSuperTypeOf($this->getKeyType())->no()) {
245: return TrinaryLogic::createNo();
246: }
247:
248: if ($this->getKeyType()->isSuperTypeOf(new ConstantIntegerType(0))->no()) {
249: return TrinaryLogic::createNo();
250: }
251:
252: return TrinaryLogic::createMaybe();
253: }
254:
255: public function isConstantValue(): TrinaryLogic
256: {
257: return TrinaryLogic::createNo();
258: }
259:
260: public function looseCompare(Type $type, PhpVersion $phpVersion): BooleanType
261: {
262: if ($type->isInteger()->yes()) {
263: return new ConstantBooleanType(false);
264: }
265:
266: return new BooleanType();
267: }
268:
269: public function hasOffsetValueType(Type $offsetType): TrinaryLogic
270: {
271: $offsetArrayKeyType = $offsetType->toArrayKey();
272: if ($offsetArrayKeyType instanceof ErrorType) {
273: $allowedArrayKeys = AllowedArrayKeysTypes::getType();
274: $offsetArrayKeyType = TypeCombinator::intersect($allowedArrayKeys, $offsetType)->toArrayKey();
275: if ($offsetArrayKeyType instanceof NeverType) {
276: return TrinaryLogic::createNo();
277: }
278: }
279: $offsetType = $offsetArrayKeyType;
280:
281: if ($this->getKeyType()->isSuperTypeOf($offsetType)->no()
282: && ($offsetType->isString()->no() || !$offsetType->isConstantScalarValue()->no())
283: ) {
284: return TrinaryLogic::createNo();
285: }
286:
287: return TrinaryLogic::createMaybe();
288: }
289:
290: public function getOffsetValueType(Type $offsetType): Type
291: {
292: $offsetType = $offsetType->toArrayKey();
293: if ($this->getKeyType()->isSuperTypeOf($offsetType)->no()
294: && ($offsetType->isString()->no() || !$offsetType->isConstantScalarValue()->no())
295: ) {
296: return new ErrorType();
297: }
298:
299: $type = $this->getItemType();
300: if ($type instanceof ErrorType) {
301: return new MixedType();
302: }
303:
304: return $type;
305: }
306:
307: public function setOffsetValueType(?Type $offsetType, Type $valueType, bool $unionValues = true): Type
308: {
309: if ($offsetType === null) {
310: $isKeyTypeInteger = $this->keyType->isInteger();
311: if ($isKeyTypeInteger->no()) {
312: $offsetType = new IntegerType();
313: } elseif ($isKeyTypeInteger->yes()) {
314: /** @var list<ConstantIntegerType> $constantScalars */
315: $constantScalars = $this->keyType->getConstantScalarTypes();
316: if (count($constantScalars) > 0) {
317: foreach ($constantScalars as $constantScalar) {
318: $constantScalars[] = ConstantTypeHelper::getTypeFromValue($constantScalar->getValue() + 1);
319: }
320:
321: $offsetType = TypeCombinator::union(...$constantScalars);
322: } else {
323: $offsetType = $this->keyType;
324: }
325: } else {
326: $integerTypes = [];
327: TypeTraverser::map($this->keyType, static function (Type $type, callable $traverse) use (&$integerTypes): Type {
328: if ($type instanceof UnionType) {
329: return $traverse($type);
330: }
331:
332: $isInteger = $type->isInteger();
333: if ($isInteger->yes()) {
334: $integerTypes[] = $type;
335: }
336:
337: return $type;
338: });
339: if (count($integerTypes) === 0) {
340: $offsetType = $this->keyType;
341: } else {
342: $offsetType = TypeCombinator::union(...$integerTypes);
343: }
344: }
345: } else {
346: $offsetType = $offsetType->toArrayKey();
347: }
348:
349: if ($offsetType instanceof ConstantStringType || $offsetType instanceof ConstantIntegerType) {
350: if ($offsetType->isSuperTypeOf($this->keyType)->yes()) {
351: $builder = ConstantArrayTypeBuilder::createEmpty();
352: $builder->setOffsetValueType($offsetType, $valueType);
353: return $builder->getArray();
354: }
355:
356: return TypeCombinator::intersect(
357: new self(
358: TypeCombinator::union($this->keyType, $offsetType),
359: TypeCombinator::union($this->itemType, $valueType),
360: ),
361: new HasOffsetValueType($offsetType, $valueType),
362: new NonEmptyArrayType(),
363: );
364: }
365:
366: return TypeCombinator::intersect(
367: new self(
368: TypeCombinator::union($this->keyType, $offsetType),
369: $unionValues ? TypeCombinator::union($this->itemType, $valueType) : $valueType,
370: ),
371: new NonEmptyArrayType(),
372: );
373: }
374:
375: public function setExistingOffsetValueType(Type $offsetType, Type $valueType): Type
376: {
377: if ($this->itemType->isConstantArray()->yes() && $valueType->isConstantArray()->yes()) {
378: $newItemType = $this->itemType;
379: foreach ($valueType->getConstantArrays() as $constArray) {
380: foreach ($constArray->getKeyTypes() as $keyType) {
381: $newItemType = $newItemType->setExistingOffsetValueType($keyType, $constArray->getOffsetValueType($keyType));
382: }
383: }
384:
385: if ($newItemType !== $this->itemType) {
386: return new self(
387: $this->keyType,
388: $newItemType,
389: );
390: }
391: }
392:
393: return new self(
394: $this->keyType,
395: TypeCombinator::union($this->itemType, $valueType),
396: );
397: }
398:
399: public function unsetOffset(Type $offsetType): Type
400: {
401: $offsetType = $offsetType->toArrayKey();
402:
403: if (
404: ($offsetType instanceof ConstantIntegerType || $offsetType instanceof ConstantStringType)
405: && !$this->keyType->isSuperTypeOf($offsetType)->no()
406: ) {
407: $keyType = TypeCombinator::remove($this->keyType, $offsetType);
408: if ($keyType instanceof NeverType) {
409: return new ConstantArrayType([], []);
410: }
411:
412: return new self($keyType, $this->itemType);
413: }
414:
415: return $this;
416: }
417:
418: public function fillKeysArray(Type $valueType): Type
419: {
420: $itemType = $this->getItemType();
421: if ($itemType->isInteger()->no()) {
422: $stringKeyType = $itemType->toString();
423: if ($stringKeyType instanceof ErrorType) {
424: return $stringKeyType;
425: }
426:
427: return new ArrayType($stringKeyType, $valueType);
428: }
429:
430: return new ArrayType($itemType, $valueType);
431: }
432:
433: public function flipArray(): Type
434: {
435: return new self($this->getIterableValueType()->toArrayKey(), $this->getIterableKeyType());
436: }
437:
438: public function intersectKeyArray(Type $otherArraysType): Type
439: {
440: $isKeySuperType = $otherArraysType->getIterableKeyType()->isSuperTypeOf($this->getIterableKeyType());
441: if ($isKeySuperType->no()) {
442: return ConstantArrayTypeBuilder::createEmpty()->getArray();
443: }
444:
445: if ($isKeySuperType->yes()) {
446: return $this;
447: }
448:
449: return new self($otherArraysType->getIterableKeyType(), $this->getIterableValueType());
450: }
451:
452: public function popArray(): Type
453: {
454: return $this;
455: }
456:
457: public function reverseArray(TrinaryLogic $preserveKeys): Type
458: {
459: return $this;
460: }
461:
462: public function searchArray(Type $needleType, ?TrinaryLogic $strict = null): Type
463: {
464: $strict ??= TrinaryLogic::createMaybe();
465: if ($strict->yes() && $this->getIterableValueType()->isSuperTypeOf($needleType)->no()) {
466: return new ConstantBooleanType(false);
467: }
468:
469: return TypeCombinator::union($this->getIterableKeyType(), new ConstantBooleanType(false));
470: }
471:
472: public function shiftArray(): Type
473: {
474: return $this;
475: }
476:
477: public function shuffleArray(): Type
478: {
479: return TypeCombinator::intersect(new self(new IntegerType(), $this->itemType), new AccessoryArrayListType());
480: }
481:
482: public function sliceArray(Type $offsetType, Type $lengthType, TrinaryLogic $preserveKeys): Type
483: {
484: if ((new ConstantIntegerType(0))->isSuperTypeOf($lengthType)->yes()) {
485: return new ConstantArrayType([], []);
486: }
487:
488: if ($preserveKeys->no() && $this->keyType->isInteger()->yes()) {
489: return TypeCombinator::intersect(new self(new IntegerType(), $this->itemType), new AccessoryArrayListType());
490: }
491:
492: return $this;
493: }
494:
495: public function spliceArray(Type $offsetType, Type $lengthType, Type $replacementType): Type
496: {
497: $replacementArrayType = $replacementType->toArray();
498: $replacementArrayTypeIsIterableAtLeastOnce = $replacementArrayType->isIterableAtLeastOnce();
499:
500: if ((new ConstantIntegerType(0))->isSuperTypeOf($offsetType)->yes() && $lengthType->isNull()->yes() && $replacementArrayTypeIsIterableAtLeastOnce->no()) {
501: return new ConstantArrayType([], []);
502: }
503:
504: $arrayType = new self(
505: TypeCombinator::union($this->getIterableKeyType(), $replacementArrayType->getKeysArray()->getIterableKeyType()),
506: TypeCombinator::union($this->getIterableValueType(), $replacementArrayType->getIterableValueType()),
507: );
508:
509: if ($replacementArrayTypeIsIterableAtLeastOnce->yes()) {
510: $arrayType = TypeCombinator::intersect($arrayType, new NonEmptyArrayType());
511: }
512:
513: return $arrayType;
514: }
515:
516: public function isCallable(): TrinaryLogic
517: {
518: return TrinaryLogic::createMaybe()->and($this->itemType->isString());
519: }
520:
521: public function getCallableParametersAcceptors(ClassMemberAccessAnswerer $scope): array
522: {
523: if ($this->isCallable()->no()) {
524: throw new ShouldNotHappenException();
525: }
526:
527: return [new TrivialParametersAcceptor()];
528: }
529:
530: public function toInteger(): Type
531: {
532: return TypeCombinator::union(
533: new ConstantIntegerType(0),
534: new ConstantIntegerType(1),
535: );
536: }
537:
538: public function toFloat(): Type
539: {
540: return TypeCombinator::union(
541: new ConstantFloatType(0.0),
542: new ConstantFloatType(1.0),
543: );
544: }
545:
546: public function inferTemplateTypes(Type $receivedType): TemplateTypeMap
547: {
548: if ($receivedType instanceof UnionType || $receivedType instanceof IntersectionType) {
549: return $receivedType->inferTemplateTypesOn($this);
550: }
551:
552: if ($receivedType->isArray()->yes()) {
553: $keyTypeMap = $this->getIterableKeyType()->inferTemplateTypes($receivedType->getIterableKeyType());
554: $itemTypeMap = $this->getItemType()->inferTemplateTypes($receivedType->getIterableValueType());
555:
556: return $keyTypeMap->union($itemTypeMap);
557: }
558:
559: return TemplateTypeMap::createEmpty();
560: }
561:
562: public function getReferencedTemplateTypes(TemplateTypeVariance $positionVariance): array
563: {
564: $variance = $positionVariance->compose(TemplateTypeVariance::createCovariant());
565:
566: return array_merge(
567: $this->getIterableKeyType()->getReferencedTemplateTypes($variance),
568: $this->getItemType()->getReferencedTemplateTypes($variance),
569: );
570: }
571:
572: public function traverse(callable $cb): Type
573: {
574: $keyType = $cb($this->keyType);
575: $itemType = $cb($this->itemType);
576:
577: if ($keyType !== $this->keyType || $itemType !== $this->itemType) {
578: if ($keyType instanceof NeverType && $itemType instanceof NeverType) {
579: return new ConstantArrayType([], []);
580: }
581:
582: return new self($keyType, $itemType);
583: }
584:
585: return $this;
586: }
587:
588: public function toPhpDocNode(): TypeNode
589: {
590: $isMixedKeyType = $this->keyType instanceof MixedType && $this->keyType->describe(VerbosityLevel::precise()) === 'mixed' && !$this->keyType->isExplicitMixed();
591: $isMixedItemType = $this->itemType instanceof MixedType && $this->itemType->describe(VerbosityLevel::precise()) === 'mixed' && !$this->itemType->isExplicitMixed();
592:
593: if ($isMixedKeyType) {
594: if ($isMixedItemType) {
595: return new IdentifierTypeNode('array');
596: }
597:
598: return new GenericTypeNode(
599: new IdentifierTypeNode('array'),
600: [
601: $this->itemType->toPhpDocNode(),
602: ],
603: );
604: }
605:
606: return new GenericTypeNode(
607: new IdentifierTypeNode('array'),
608: [
609: $this->keyType->toPhpDocNode(),
610: $this->itemType->toPhpDocNode(),
611: ],
612: );
613: }
614:
615: public function traverseSimultaneously(Type $right, callable $cb): Type
616: {
617: $keyType = $cb($this->keyType, $right->getIterableKeyType());
618: $itemType = $cb($this->itemType, $right->getIterableValueType());
619:
620: if ($keyType !== $this->keyType || $itemType !== $this->itemType) {
621: if ($keyType instanceof NeverType && $itemType instanceof NeverType) {
622: return new ConstantArrayType([], []);
623: }
624:
625: return new self($keyType, $itemType);
626: }
627:
628: return $this;
629: }
630:
631: public function tryRemove(Type $typeToRemove): ?Type
632: {
633: if ($typeToRemove->isConstantArray()->yes() && $typeToRemove->isIterableAtLeastOnce()->no()) {
634: return TypeCombinator::intersect($this, new NonEmptyArrayType());
635: }
636:
637: if ($typeToRemove->isSuperTypeOf(new ConstantArrayType([], []))->yes()) {
638: return TypeCombinator::intersect($this, new NonEmptyArrayType());
639: }
640:
641: if ($typeToRemove instanceof NonEmptyArrayType) {
642: return new ConstantArrayType([], []);
643: }
644:
645: return null;
646: }
647:
648: public function getFiniteTypes(): array
649: {
650: return [];
651: }
652:
653: }
654: