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: $allowedArrayKeys = AllowedArrayKeysTypes::getType();
272: $offsetType = TypeCombinator::intersect($allowedArrayKeys, $offsetType)->toArrayKey();
273:
274: if ($this->getKeyType()->isSuperTypeOf($offsetType)->no()
275: && ($offsetType->isString()->no() || !$offsetType->isConstantScalarValue()->no())
276: ) {
277: return TrinaryLogic::createNo();
278: }
279:
280: return TrinaryLogic::createMaybe();
281: }
282:
283: public function getOffsetValueType(Type $offsetType): Type
284: {
285: $offsetType = $offsetType->toArrayKey();
286: if ($this->getKeyType()->isSuperTypeOf($offsetType)->no()
287: && ($offsetType->isString()->no() || !$offsetType->isConstantScalarValue()->no())
288: ) {
289: return new ErrorType();
290: }
291:
292: $type = $this->getItemType();
293: if ($type instanceof ErrorType) {
294: return new MixedType();
295: }
296:
297: return $type;
298: }
299:
300: public function setOffsetValueType(?Type $offsetType, Type $valueType, bool $unionValues = true): Type
301: {
302: if ($offsetType === null) {
303: $isKeyTypeInteger = $this->keyType->isInteger();
304: if ($isKeyTypeInteger->no()) {
305: $offsetType = new IntegerType();
306: } elseif ($isKeyTypeInteger->yes()) {
307: /** @var list<ConstantIntegerType> $constantScalars */
308: $constantScalars = $this->keyType->getConstantScalarTypes();
309: if (count($constantScalars) > 0) {
310: foreach ($constantScalars as $constantScalar) {
311: $constantScalars[] = ConstantTypeHelper::getTypeFromValue($constantScalar->getValue() + 1);
312: }
313:
314: $offsetType = TypeCombinator::union(...$constantScalars);
315: } else {
316: $offsetType = $this->keyType;
317: }
318: } else {
319: $integerTypes = [];
320: TypeTraverser::map($this->keyType, static function (Type $type, callable $traverse) use (&$integerTypes): Type {
321: if ($type instanceof UnionType) {
322: return $traverse($type);
323: }
324:
325: $isInteger = $type->isInteger();
326: if ($isInteger->yes()) {
327: $integerTypes[] = $type;
328: }
329:
330: return $type;
331: });
332: if (count($integerTypes) === 0) {
333: $offsetType = $this->keyType;
334: } else {
335: $offsetType = TypeCombinator::union(...$integerTypes);
336: }
337: }
338: } else {
339: $offsetType = $offsetType->toArrayKey();
340: }
341:
342: if ($offsetType instanceof ConstantStringType || $offsetType instanceof ConstantIntegerType) {
343: if ($offsetType->isSuperTypeOf($this->keyType)->yes()) {
344: $builder = ConstantArrayTypeBuilder::createEmpty();
345: $builder->setOffsetValueType($offsetType, $valueType);
346: return $builder->getArray();
347: }
348:
349: return TypeCombinator::intersect(
350: new self(
351: TypeCombinator::union($this->keyType, $offsetType),
352: TypeCombinator::union($this->itemType, $valueType),
353: ),
354: new HasOffsetValueType($offsetType, $valueType),
355: new NonEmptyArrayType(),
356: );
357: }
358:
359: return TypeCombinator::intersect(
360: new self(
361: TypeCombinator::union($this->keyType, $offsetType),
362: $unionValues ? TypeCombinator::union($this->itemType, $valueType) : $valueType,
363: ),
364: new NonEmptyArrayType(),
365: );
366: }
367:
368: public function setExistingOffsetValueType(Type $offsetType, Type $valueType): Type
369: {
370: return new self(
371: $this->keyType,
372: TypeCombinator::union($this->itemType, $valueType),
373: );
374: }
375:
376: public function unsetOffset(Type $offsetType): Type
377: {
378: $offsetType = $offsetType->toArrayKey();
379:
380: if (
381: ($offsetType instanceof ConstantIntegerType || $offsetType instanceof ConstantStringType)
382: && !$this->keyType->isSuperTypeOf($offsetType)->no()
383: ) {
384: $keyType = TypeCombinator::remove($this->keyType, $offsetType);
385: if ($keyType instanceof NeverType) {
386: return new ConstantArrayType([], []);
387: }
388:
389: return new self($keyType, $this->itemType);
390: }
391:
392: return $this;
393: }
394:
395: public function fillKeysArray(Type $valueType): Type
396: {
397: $itemType = $this->getItemType();
398: if ($itemType->isInteger()->no()) {
399: $stringKeyType = $itemType->toString();
400: if ($stringKeyType instanceof ErrorType) {
401: return $stringKeyType;
402: }
403:
404: return new ArrayType($stringKeyType, $valueType);
405: }
406:
407: return new ArrayType($itemType, $valueType);
408: }
409:
410: public function flipArray(): Type
411: {
412: return new self($this->getIterableValueType()->toArrayKey(), $this->getIterableKeyType());
413: }
414:
415: public function intersectKeyArray(Type $otherArraysType): Type
416: {
417: $isKeySuperType = $otherArraysType->getIterableKeyType()->isSuperTypeOf($this->getIterableKeyType());
418: if ($isKeySuperType->no()) {
419: return ConstantArrayTypeBuilder::createEmpty()->getArray();
420: }
421:
422: if ($isKeySuperType->yes()) {
423: return $this;
424: }
425:
426: return new self($otherArraysType->getIterableKeyType(), $this->getIterableValueType());
427: }
428:
429: public function popArray(): Type
430: {
431: return $this;
432: }
433:
434: public function reverseArray(TrinaryLogic $preserveKeys): Type
435: {
436: return $this;
437: }
438:
439: public function searchArray(Type $needleType, ?TrinaryLogic $strict = null): Type
440: {
441: $strict ??= TrinaryLogic::createMaybe();
442: if ($strict->yes() && $this->getIterableValueType()->isSuperTypeOf($needleType)->no()) {
443: return new ConstantBooleanType(false);
444: }
445:
446: return TypeCombinator::union($this->getIterableKeyType(), new ConstantBooleanType(false));
447: }
448:
449: public function shiftArray(): Type
450: {
451: return $this;
452: }
453:
454: public function shuffleArray(): Type
455: {
456: return TypeCombinator::intersect(new self(new IntegerType(), $this->itemType), new AccessoryArrayListType());
457: }
458:
459: public function sliceArray(Type $offsetType, Type $lengthType, TrinaryLogic $preserveKeys): Type
460: {
461: if ((new ConstantIntegerType(0))->isSuperTypeOf($lengthType)->yes()) {
462: return new ConstantArrayType([], []);
463: }
464:
465: if ($preserveKeys->no() && $this->keyType->isInteger()->yes()) {
466: return TypeCombinator::intersect(new self(new IntegerType(), $this->itemType), new AccessoryArrayListType());
467: }
468:
469: return $this;
470: }
471:
472: public function spliceArray(Type $offsetType, Type $lengthType, Type $replacementType): Type
473: {
474: $replacementArrayType = $replacementType->toArray();
475: $replacementArrayTypeIsIterableAtLeastOnce = $replacementArrayType->isIterableAtLeastOnce();
476:
477: if ((new ConstantIntegerType(0))->isSuperTypeOf($offsetType)->yes() && $lengthType->isNull()->yes() && $replacementArrayTypeIsIterableAtLeastOnce->no()) {
478: return new ConstantArrayType([], []);
479: }
480:
481: $arrayType = new self(
482: TypeCombinator::union($this->getIterableKeyType(), $replacementArrayType->getKeysArray()->getIterableKeyType()),
483: TypeCombinator::union($this->getIterableValueType(), $replacementArrayType->getIterableValueType()),
484: );
485:
486: if ($replacementArrayTypeIsIterableAtLeastOnce->yes()) {
487: $arrayType = TypeCombinator::intersect($arrayType, new NonEmptyArrayType());
488: }
489:
490: return $arrayType;
491: }
492:
493: public function isCallable(): TrinaryLogic
494: {
495: return TrinaryLogic::createMaybe()->and($this->itemType->isString());
496: }
497:
498: public function getCallableParametersAcceptors(ClassMemberAccessAnswerer $scope): array
499: {
500: if ($this->isCallable()->no()) {
501: throw new ShouldNotHappenException();
502: }
503:
504: return [new TrivialParametersAcceptor()];
505: }
506:
507: public function toInteger(): Type
508: {
509: return TypeCombinator::union(
510: new ConstantIntegerType(0),
511: new ConstantIntegerType(1),
512: );
513: }
514:
515: public function toFloat(): Type
516: {
517: return TypeCombinator::union(
518: new ConstantFloatType(0.0),
519: new ConstantFloatType(1.0),
520: );
521: }
522:
523: public function inferTemplateTypes(Type $receivedType): TemplateTypeMap
524: {
525: if ($receivedType instanceof UnionType || $receivedType instanceof IntersectionType) {
526: return $receivedType->inferTemplateTypesOn($this);
527: }
528:
529: if ($receivedType->isArray()->yes()) {
530: $keyTypeMap = $this->getIterableKeyType()->inferTemplateTypes($receivedType->getIterableKeyType());
531: $itemTypeMap = $this->getItemType()->inferTemplateTypes($receivedType->getIterableValueType());
532:
533: return $keyTypeMap->union($itemTypeMap);
534: }
535:
536: return TemplateTypeMap::createEmpty();
537: }
538:
539: public function getReferencedTemplateTypes(TemplateTypeVariance $positionVariance): array
540: {
541: $variance = $positionVariance->compose(TemplateTypeVariance::createCovariant());
542:
543: return array_merge(
544: $this->getIterableKeyType()->getReferencedTemplateTypes($variance),
545: $this->getItemType()->getReferencedTemplateTypes($variance),
546: );
547: }
548:
549: public function traverse(callable $cb): Type
550: {
551: $keyType = $cb($this->keyType);
552: $itemType = $cb($this->itemType);
553:
554: if ($keyType !== $this->keyType || $itemType !== $this->itemType) {
555: if ($keyType instanceof NeverType && $itemType instanceof NeverType) {
556: return new ConstantArrayType([], []);
557: }
558:
559: return new self($keyType, $itemType);
560: }
561:
562: return $this;
563: }
564:
565: public function toPhpDocNode(): TypeNode
566: {
567: $isMixedKeyType = $this->keyType instanceof MixedType && $this->keyType->describe(VerbosityLevel::precise()) === 'mixed' && !$this->keyType->isExplicitMixed();
568: $isMixedItemType = $this->itemType instanceof MixedType && $this->itemType->describe(VerbosityLevel::precise()) === 'mixed' && !$this->itemType->isExplicitMixed();
569:
570: if ($isMixedKeyType) {
571: if ($isMixedItemType) {
572: return new IdentifierTypeNode('array');
573: }
574:
575: return new GenericTypeNode(
576: new IdentifierTypeNode('array'),
577: [
578: $this->itemType->toPhpDocNode(),
579: ],
580: );
581: }
582:
583: return new GenericTypeNode(
584: new IdentifierTypeNode('array'),
585: [
586: $this->keyType->toPhpDocNode(),
587: $this->itemType->toPhpDocNode(),
588: ],
589: );
590: }
591:
592: public function traverseSimultaneously(Type $right, callable $cb): Type
593: {
594: $keyType = $cb($this->keyType, $right->getIterableKeyType());
595: $itemType = $cb($this->itemType, $right->getIterableValueType());
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 tryRemove(Type $typeToRemove): ?Type
609: {
610: if ($typeToRemove->isConstantArray()->yes() && $typeToRemove->isIterableAtLeastOnce()->no()) {
611: return TypeCombinator::intersect($this, new NonEmptyArrayType());
612: }
613:
614: if ($typeToRemove->isSuperTypeOf(new ConstantArrayType([], []))->yes()) {
615: return TypeCombinator::intersect($this, new NonEmptyArrayType());
616: }
617:
618: if ($typeToRemove instanceof NonEmptyArrayType) {
619: return new ConstantArrayType([], []);
620: }
621:
622: return null;
623: }
624:
625: public function getFiniteTypes(): array
626: {
627: return [];
628: }
629:
630: }
631: