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