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