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: $newItemTypes = [];
379:
380: foreach ($valueType->getConstantArrays() as $constArray) {
381: $newItemType = $this->itemType;
382: $optionalKeyTypes = [];
383: foreach ($constArray->getKeyTypes() as $i => $keyType) {
384: $newItemType = $newItemType->setExistingOffsetValueType($keyType, $constArray->getOffsetValueType($keyType));
385:
386: if (!$constArray->isOptionalKey($i)) {
387: continue;
388: }
389:
390: $optionalKeyTypes[] = $keyType;
391: }
392: $newItemTypes[] = $newItemType;
393:
394: if ($optionalKeyTypes === []) {
395: continue;
396: }
397:
398: foreach ($optionalKeyTypes as $keyType) {
399: $newItemType = $newItemType->unsetOffset($keyType);
400: }
401: $newItemTypes[] = $newItemType;
402: }
403:
404: $newItemType = TypeCombinator::union(...$newItemTypes);
405: if ($newItemType !== $this->itemType) {
406: return new self(
407: $this->keyType,
408: $newItemType,
409: );
410: }
411: }
412:
413: return new self(
414: $this->keyType,
415: TypeCombinator::union($this->itemType, $valueType),
416: );
417: }
418:
419: public function unsetOffset(Type $offsetType): Type
420: {
421: $offsetType = $offsetType->toArrayKey();
422:
423: if (
424: ($offsetType instanceof ConstantIntegerType || $offsetType instanceof ConstantStringType)
425: && !$this->keyType->isSuperTypeOf($offsetType)->no()
426: ) {
427: $keyType = TypeCombinator::remove($this->keyType, $offsetType);
428: if ($keyType instanceof NeverType) {
429: return new ConstantArrayType([], []);
430: }
431:
432: return new self($keyType, $this->itemType);
433: }
434:
435: return $this;
436: }
437:
438: public function fillKeysArray(Type $valueType): Type
439: {
440: $itemType = $this->getItemType();
441: if ($itemType->isInteger()->no()) {
442: $stringKeyType = $itemType->toString();
443: if ($stringKeyType instanceof ErrorType) {
444: return $stringKeyType;
445: }
446:
447: return new ArrayType($stringKeyType, $valueType);
448: }
449:
450: return new ArrayType($itemType, $valueType);
451: }
452:
453: public function flipArray(): Type
454: {
455: return new self($this->getIterableValueType()->toArrayKey(), $this->getIterableKeyType());
456: }
457:
458: public function intersectKeyArray(Type $otherArraysType): Type
459: {
460: $isKeySuperType = $otherArraysType->getIterableKeyType()->isSuperTypeOf($this->getIterableKeyType());
461: if ($isKeySuperType->no()) {
462: return ConstantArrayTypeBuilder::createEmpty()->getArray();
463: }
464:
465: if ($isKeySuperType->yes()) {
466: return $this;
467: }
468:
469: return new self($otherArraysType->getIterableKeyType(), $this->getIterableValueType());
470: }
471:
472: public function popArray(): Type
473: {
474: return $this;
475: }
476:
477: public function reverseArray(TrinaryLogic $preserveKeys): Type
478: {
479: return $this;
480: }
481:
482: public function searchArray(Type $needleType, ?TrinaryLogic $strict = null): Type
483: {
484: $strict ??= TrinaryLogic::createMaybe();
485: if ($strict->yes() && $this->getIterableValueType()->isSuperTypeOf($needleType)->no()) {
486: return new ConstantBooleanType(false);
487: }
488:
489: return TypeCombinator::union($this->getIterableKeyType(), new ConstantBooleanType(false));
490: }
491:
492: public function shiftArray(): Type
493: {
494: return $this;
495: }
496:
497: public function shuffleArray(): Type
498: {
499: return TypeCombinator::intersect(new self(new IntegerType(), $this->itemType), new AccessoryArrayListType());
500: }
501:
502: public function sliceArray(Type $offsetType, Type $lengthType, TrinaryLogic $preserveKeys): Type
503: {
504: if ((new ConstantIntegerType(0))->isSuperTypeOf($lengthType)->yes()) {
505: return new ConstantArrayType([], []);
506: }
507:
508: if ($preserveKeys->no() && $this->keyType->isInteger()->yes()) {
509: return TypeCombinator::intersect(new self(new IntegerType(), $this->itemType), new AccessoryArrayListType());
510: }
511:
512: return $this;
513: }
514:
515: public function spliceArray(Type $offsetType, Type $lengthType, Type $replacementType): Type
516: {
517: $replacementArrayType = $replacementType->toArray();
518: $replacementArrayTypeIsIterableAtLeastOnce = $replacementArrayType->isIterableAtLeastOnce();
519:
520: if ((new ConstantIntegerType(0))->isSuperTypeOf($offsetType)->yes() && $lengthType->isNull()->yes() && $replacementArrayTypeIsIterableAtLeastOnce->no()) {
521: return new ConstantArrayType([], []);
522: }
523:
524: $arrayType = new self(
525: TypeCombinator::union($this->getIterableKeyType(), $replacementArrayType->getKeysArray()->getIterableKeyType()),
526: TypeCombinator::union($this->getIterableValueType(), $replacementArrayType->getIterableValueType()),
527: );
528:
529: if ($replacementArrayTypeIsIterableAtLeastOnce->yes()) {
530: $arrayType = TypeCombinator::intersect($arrayType, new NonEmptyArrayType());
531: }
532:
533: return $arrayType;
534: }
535:
536: public function isCallable(): TrinaryLogic
537: {
538: return TrinaryLogic::createMaybe()->and($this->itemType->isString());
539: }
540:
541: public function getCallableParametersAcceptors(ClassMemberAccessAnswerer $scope): array
542: {
543: if ($this->isCallable()->no()) {
544: throw new ShouldNotHappenException();
545: }
546:
547: return [new TrivialParametersAcceptor()];
548: }
549:
550: public function toInteger(): Type
551: {
552: return TypeCombinator::union(
553: new ConstantIntegerType(0),
554: new ConstantIntegerType(1),
555: );
556: }
557:
558: public function toFloat(): Type
559: {
560: return TypeCombinator::union(
561: new ConstantFloatType(0.0),
562: new ConstantFloatType(1.0),
563: );
564: }
565:
566: public function inferTemplateTypes(Type $receivedType): TemplateTypeMap
567: {
568: if ($receivedType instanceof UnionType || $receivedType instanceof IntersectionType) {
569: return $receivedType->inferTemplateTypesOn($this);
570: }
571:
572: if ($receivedType->isArray()->yes()) {
573: $keyTypeMap = $this->getIterableKeyType()->inferTemplateTypes($receivedType->getIterableKeyType());
574: $itemTypeMap = $this->getItemType()->inferTemplateTypes($receivedType->getIterableValueType());
575:
576: return $keyTypeMap->union($itemTypeMap);
577: }
578:
579: return TemplateTypeMap::createEmpty();
580: }
581:
582: public function getReferencedTemplateTypes(TemplateTypeVariance $positionVariance): array
583: {
584: $variance = $positionVariance->compose(TemplateTypeVariance::createCovariant());
585:
586: return array_merge(
587: $this->getIterableKeyType()->getReferencedTemplateTypes($variance),
588: $this->getItemType()->getReferencedTemplateTypes($variance),
589: );
590: }
591:
592: public function traverse(callable $cb): Type
593: {
594: $keyType = $cb($this->keyType);
595: $itemType = $cb($this->itemType);
596:
597: if ($keyType !== $this->keyType || $itemType !== $this->itemType) {
598: if ($keyType instanceof NeverType && $itemType instanceof NeverType) {
599: return new ConstantArrayType([], []);
600: }
601:
602: return new self($keyType, $itemType);
603: }
604:
605: return $this;
606: }
607:
608: public function toPhpDocNode(): TypeNode
609: {
610: $isMixedKeyType = $this->keyType instanceof MixedType && $this->keyType->describe(VerbosityLevel::precise()) === 'mixed' && !$this->keyType->isExplicitMixed();
611: $isMixedItemType = $this->itemType instanceof MixedType && $this->itemType->describe(VerbosityLevel::precise()) === 'mixed' && !$this->itemType->isExplicitMixed();
612:
613: if ($isMixedKeyType) {
614: if ($isMixedItemType) {
615: return new IdentifierTypeNode('array');
616: }
617:
618: return new GenericTypeNode(
619: new IdentifierTypeNode('array'),
620: [
621: $this->itemType->toPhpDocNode(),
622: ],
623: );
624: }
625:
626: return new GenericTypeNode(
627: new IdentifierTypeNode('array'),
628: [
629: $this->keyType->toPhpDocNode(),
630: $this->itemType->toPhpDocNode(),
631: ],
632: );
633: }
634:
635: public function traverseSimultaneously(Type $right, callable $cb): Type
636: {
637: $keyType = $cb($this->keyType, $right->getIterableKeyType());
638: $itemType = $cb($this->itemType, $right->getIterableValueType());
639:
640: if ($keyType !== $this->keyType || $itemType !== $this->itemType) {
641: if ($keyType instanceof NeverType && $itemType instanceof NeverType) {
642: return new ConstantArrayType([], []);
643: }
644:
645: return new self($keyType, $itemType);
646: }
647:
648: return $this;
649: }
650:
651: public function tryRemove(Type $typeToRemove): ?Type
652: {
653: if ($typeToRemove->isConstantArray()->yes() && $typeToRemove->isIterableAtLeastOnce()->no()) {
654: return TypeCombinator::intersect($this, new NonEmptyArrayType());
655: }
656:
657: if ($typeToRemove->isSuperTypeOf(new ConstantArrayType([], []))->yes()) {
658: return TypeCombinator::intersect($this, new NonEmptyArrayType());
659: }
660:
661: if ($typeToRemove instanceof NonEmptyArrayType) {
662: return new ConstantArrayType([], []);
663: }
664:
665: return null;
666: }
667:
668: public function getFiniteTypes(): array
669: {
670: return [];
671: }
672:
673: }
674: