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