1: <?php declare(strict_types = 1);
2:
3: namespace PHPStan\Type;
4:
5: use PhpParser\Node\Identifier;
6: use PHPStan\Php\PhpVersion;
7: use PHPStan\PhpDocParser\Ast\Type\GenericTypeNode;
8: use PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode;
9: use PHPStan\PhpDocParser\Ast\Type\TypeNode;
10: use PHPStan\TrinaryLogic;
11: use PHPStan\Turbo\ShadowedByTurboExtension;
12: use PHPStan\Type\Enum\EnumCaseObjectType;
13: use PHPStan\Type\Generic\GenericObjectType;
14: use PHPStan\Type\Generic\TemplateMixedType;
15: use PHPStan\Type\Generic\TemplateTypeMap;
16: use PHPStan\Type\Generic\TemplateTypeVariance;
17: use PHPStan\Type\Traits\MaybeArrayTypeTrait;
18: use PHPStan\Type\Traits\MaybeCallableTypeTrait;
19: use PHPStan\Type\Traits\MaybeObjectTypeTrait;
20: use PHPStan\Type\Traits\MaybeOffsetAccessibleTypeTrait;
21: use PHPStan\Type\Traits\NonGeneralizableTypeTrait;
22: use PHPStan\Type\Traits\OperatorTypeTrait;
23: use PHPStan\Type\Traits\UndecidedBooleanTypeTrait;
24: use PHPStan\Type\Traits\UndecidedComparisonCompoundTypeTrait;
25: use Traversable;
26: use function array_merge;
27: use function get_class;
28: use function sprintf;
29:
30: /** @api */
31: #[InstanceofDeprecated(insteadUse: 'Type::isIterable()')]
32: #[ShadowedByTurboExtension(implementation: __DIR__ . '/../../turbo-ext/src/IterableType.cpp')]
33: class IterableType implements CompoundType
34: {
35:
36: use MaybeArrayTypeTrait;
37: use MaybeCallableTypeTrait;
38: use MaybeObjectTypeTrait;
39: use MaybeOffsetAccessibleTypeTrait;
40: use UndecidedBooleanTypeTrait;
41: use UndecidedComparisonCompoundTypeTrait;
42: use NonGeneralizableTypeTrait;
43: use OperatorTypeTrait;
44:
45: /** @api */
46: public function __construct(
47: private Type $keyType,
48: private Type $itemType,
49: )
50: {
51: }
52:
53: public function getKeyType(): Type
54: {
55: return $this->keyType;
56: }
57:
58: public function getItemType(): Type
59: {
60: return $this->itemType;
61: }
62:
63: public function getReferencedClasses(): array
64: {
65: return array_merge(
66: $this->keyType->getReferencedClasses(),
67: $this->getItemType()->getReferencedClasses(),
68: );
69: }
70:
71: public function getObjectClassNames(): array
72: {
73: return [];
74: }
75:
76: public function getObjectClassReflections(): array
77: {
78: return [];
79: }
80:
81: public function getConstantStrings(): array
82: {
83: return [];
84: }
85:
86: public function accepts(Type $type, bool $strictTypes): AcceptsResult
87: {
88: if ($type->isConstantArray()->yes() && $type->isIterableAtLeastOnce()->no()) {
89: return AcceptsResult::createYes();
90: }
91: if ($type->isIterable()->yes()) {
92: return $this->getIterableValueType()->accepts($type->getIterableValueType(), $strictTypes)
93: ->and($this->getIterableKeyType()->accepts($type->getIterableKeyType(), $strictTypes));
94: }
95:
96: if ($type instanceof CompoundType) {
97: return $type->isAcceptedBy($this, $strictTypes);
98: }
99:
100: return AcceptsResult::createNo();
101: }
102:
103: public function isSuperTypeOf(Type $type): IsSuperTypeOfResult
104: {
105: if ($type instanceof CompoundType) {
106: return $type->isSubTypeOf($this);
107: }
108:
109: return IsSuperTypeOfResult::createFromTrinaryLogic($type->isIterable())
110: ->and($this->getIterableValueType()->isSuperTypeOf($type->getIterableValueType()))
111: ->and($this->getIterableKeyType()->isSuperTypeOf($type->getIterableKeyType()));
112: }
113:
114: public function isSuperTypeOfMixed(Type $type): IsSuperTypeOfResult
115: {
116: return IsSuperTypeOfResult::createFromTrinaryLogic($type->isIterable())
117: ->and($this->isNestedTypeSuperTypeOf($this->getIterableValueType(), $type->getIterableValueType()))
118: ->and($this->isNestedTypeSuperTypeOf($this->getIterableKeyType(), $type->getIterableKeyType()));
119: }
120:
121: private function isNestedTypeSuperTypeOf(Type $a, Type $b): IsSuperTypeOfResult
122: {
123: if (!$a instanceof MixedType || !$b instanceof MixedType) {
124: return $a->isSuperTypeOf($b);
125: }
126:
127: if ($a instanceof TemplateMixedType || $b instanceof TemplateMixedType) {
128: return $a->isSuperTypeOf($b);
129: }
130:
131: if ($a->isExplicitMixed()) {
132: if ($b->isExplicitMixed()) {
133: return IsSuperTypeOfResult::createYes();
134: }
135:
136: return IsSuperTypeOfResult::createMaybe();
137: }
138:
139: return IsSuperTypeOfResult::createYes();
140: }
141:
142: public function isSubTypeOf(Type $otherType): IsSuperTypeOfResult
143: {
144: if ($otherType instanceof IntersectionType || $otherType instanceof UnionType) {
145: return $otherType->isSuperTypeOf($this->toArrayOrTraversable());
146: }
147:
148: if ($otherType instanceof self) {
149: $limit = IsSuperTypeOfResult::createYes();
150: } else {
151: $limit = IsSuperTypeOfResult::createMaybe();
152: }
153:
154: if ($otherType->isConstantArray()->yes() && $otherType->isIterableAtLeastOnce()->no()) {
155: return IsSuperTypeOfResult::createMaybe();
156: }
157:
158: return $limit->and(
159: IsSuperTypeOfResult::createFromTrinaryLogic($otherType->isIterable()),
160: $otherType->getIterableValueType()->isSuperTypeOf($this->itemType),
161: $otherType->getIterableKeyType()->isSuperTypeOf($this->keyType),
162: );
163: }
164:
165: public function isAcceptedBy(Type $acceptingType, bool $strictTypes): AcceptsResult
166: {
167: return $this->isSubTypeOf($acceptingType)->toAcceptsResult();
168: }
169:
170: public function equals(Type $type): bool
171: {
172: if (get_class($type) !== static::class) {
173: return false;
174: }
175:
176: return $this->keyType->equals($type->keyType)
177: && $this->itemType->equals($type->itemType);
178: }
179:
180: public function describe(VerbosityLevel $level): string
181: {
182: $isMixedKeyType = $this->keyType instanceof MixedType && $this->keyType->describe(VerbosityLevel::precise()) === 'mixed';
183: $isMixedItemType = $this->itemType instanceof MixedType && $this->itemType->describe(VerbosityLevel::precise()) === 'mixed';
184: if ($isMixedKeyType) {
185: if ($isMixedItemType) {
186: return 'iterable';
187: }
188:
189: return sprintf('iterable<%s>', $this->itemType->describe($level));
190: }
191:
192: return sprintf('iterable<%s, %s>', $this->keyType->describe($level), $this->itemType->describe($level));
193: }
194:
195: public function hasOffsetValueType(Type $offsetType): TrinaryLogic
196: {
197: if ($this->getIterableKeyType()->isSuperTypeOf($offsetType)->no()) {
198: return TrinaryLogic::createNo();
199: }
200:
201: return TrinaryLogic::createMaybe();
202: }
203:
204: public function toNumber(): Type
205: {
206: return new ErrorType();
207: }
208:
209: public function toBitwiseNotType(): Type
210: {
211: return new ErrorType();
212: }
213:
214: public function toAbsoluteNumber(): Type
215: {
216: return new ErrorType();
217: }
218:
219: public function toString(): Type
220: {
221: return new ErrorType();
222: }
223:
224: public function toInteger(): Type
225: {
226: return new ErrorType();
227: }
228:
229: public function toFloat(): Type
230: {
231: return new ErrorType();
232: }
233:
234: public function toArray(): Type
235: {
236: return new ArrayType($this->keyType, $this->getItemType());
237: }
238:
239: public function toArrayOrTraversable(): Type
240: {
241: return new UnionType([
242: new ArrayType($this->keyType, $this->itemType),
243: new GenericObjectType(Traversable::class, [
244: $this->keyType,
245: $this->itemType,
246: ]),
247: ]);
248: }
249:
250: public function toArrayKey(): Type
251: {
252: return new ErrorType();
253: }
254:
255: public function toCoercedArgumentType(bool $strictTypes): Type
256: {
257: return TypeCombinator::union(
258: $this,
259: new ArrayType(
260: TypeCombinator::intersect(
261: $this->keyType->toArrayKey(),
262: new UnionType([
263: new IntegerType(),
264: new StringType(),
265: ]),
266: ),
267: $this->itemType,
268: ),
269: new GenericObjectType(Traversable::class, [
270: $this->keyType,
271: $this->itemType,
272: ]),
273: );
274: }
275:
276: public function isIterable(): TrinaryLogic
277: {
278: return TrinaryLogic::createYes();
279: }
280:
281: public function isIterableAtLeastOnce(): TrinaryLogic
282: {
283: return TrinaryLogic::createMaybe();
284: }
285:
286: public function getArraySize(): Type
287: {
288: return IntegerRangeType::fromInterval(0, null);
289: }
290:
291: public function getIterableKeyType(): Type
292: {
293: return $this->keyType;
294: }
295:
296: public function getFirstIterableKeyType(): Type
297: {
298: return $this->keyType;
299: }
300:
301: public function getLastIterableKeyType(): Type
302: {
303: return $this->keyType;
304: }
305:
306: public function getIterableValueType(): Type
307: {
308: return $this->getItemType();
309: }
310:
311: public function getFirstIterableValueType(): Type
312: {
313: return $this->getItemType();
314: }
315:
316: public function getLastIterableValueType(): Type
317: {
318: return $this->getItemType();
319: }
320:
321: public function isNull(): TrinaryLogic
322: {
323: return TrinaryLogic::createNo();
324: }
325:
326: public function isConstantValue(): TrinaryLogic
327: {
328: return TrinaryLogic::createNo();
329: }
330:
331: public function isConstantScalarValue(): TrinaryLogic
332: {
333: return TrinaryLogic::createNo();
334: }
335:
336: public function getConstantScalarTypes(): array
337: {
338: return [];
339: }
340:
341: public function getConstantScalarValues(): array
342: {
343: return [];
344: }
345:
346: public function isTrue(): TrinaryLogic
347: {
348: return TrinaryLogic::createNo();
349: }
350:
351: public function isFalse(): TrinaryLogic
352: {
353: return TrinaryLogic::createNo();
354: }
355:
356: public function isBoolean(): TrinaryLogic
357: {
358: return TrinaryLogic::createNo();
359: }
360:
361: public function isFloat(): TrinaryLogic
362: {
363: return TrinaryLogic::createNo();
364: }
365:
366: public function isInteger(): TrinaryLogic
367: {
368: return TrinaryLogic::createNo();
369: }
370:
371: public function isString(): TrinaryLogic
372: {
373: return TrinaryLogic::createNo();
374: }
375:
376: public function isNumericString(): TrinaryLogic
377: {
378: return TrinaryLogic::createNo();
379: }
380:
381: public function isDecimalIntegerString(): TrinaryLogic
382: {
383: return TrinaryLogic::createNo();
384: }
385:
386: public function isNonEmptyString(): TrinaryLogic
387: {
388: return TrinaryLogic::createNo();
389: }
390:
391: public function isNonFalsyString(): TrinaryLogic
392: {
393: return TrinaryLogic::createNo();
394: }
395:
396: public function isLiteralString(): TrinaryLogic
397: {
398: return TrinaryLogic::createNo();
399: }
400:
401: public function isLowercaseString(): TrinaryLogic
402: {
403: return TrinaryLogic::createNo();
404: }
405:
406: public function isClassString(): TrinaryLogic
407: {
408: return TrinaryLogic::createNo();
409: }
410:
411: public function isUppercaseString(): TrinaryLogic
412: {
413: return TrinaryLogic::createNo();
414: }
415:
416: public function getClassStringObjectType(): Type
417: {
418: return new ErrorType();
419: }
420:
421: public function getObjectTypeOrClassStringObjectType(): Type
422: {
423: return new ObjectWithoutClassType();
424: }
425:
426: public function isVoid(): TrinaryLogic
427: {
428: return TrinaryLogic::createNo();
429: }
430:
431: public function isScalar(): TrinaryLogic
432: {
433: return TrinaryLogic::createNo();
434: }
435:
436: public function looseCompare(Type $type, PhpVersion $phpVersion): BooleanType
437: {
438: return new BooleanType();
439: }
440:
441: public function getEnumCases(): array
442: {
443: return [];
444: }
445:
446: public function getEnumCaseObject(): ?EnumCaseObjectType
447: {
448: return null;
449: }
450:
451: public function inferTemplateTypes(Type $receivedType): TemplateTypeMap
452: {
453: if ($receivedType instanceof UnionType || $receivedType instanceof IntersectionType) {
454: return $receivedType->inferTemplateTypesOn($this);
455: }
456:
457: // never is iterable, but taking its key and value type apart would bind
458: // the templates to an implicit never that is later read as unresolved.
459: // ArrayType and Traversable<T> infer nothing from never either.
460: if (!$receivedType->isIterable()->yes() || $receivedType instanceof NeverType) {
461: return TemplateTypeMap::createEmpty();
462: }
463:
464: $keyTypeMap = $this->getIterableKeyType()->inferTemplateTypes($receivedType->getIterableKeyType());
465: $valueTypeMap = $this->getIterableValueType()->inferTemplateTypes($receivedType->getIterableValueType());
466:
467: return $keyTypeMap->union($valueTypeMap);
468: }
469:
470: public function getReferencedTemplateTypes(TemplateTypeVariance $positionVariance): array
471: {
472: $variance = $positionVariance->compose(TemplateTypeVariance::createCovariant());
473:
474: return array_merge(
475: $this->getIterableKeyType()->getReferencedTemplateTypes($variance),
476: $this->getIterableValueType()->getReferencedTemplateTypes($variance),
477: );
478: }
479:
480: public function traverse(callable $cb): Type
481: {
482: $keyType = $cb($this->keyType);
483: $itemType = $cb($this->itemType);
484:
485: if ($keyType !== $this->keyType || $itemType !== $this->itemType) {
486: return new self($keyType, $itemType);
487: }
488:
489: return $this;
490: }
491:
492: public function traverseSimultaneously(Type $right, callable $cb): Type
493: {
494: $keyType = $cb($this->keyType, $right->getIterableKeyType());
495: $itemType = $cb($this->itemType, $right->getIterableValueType());
496:
497: if ($keyType !== $this->keyType || $itemType !== $this->itemType) {
498: return new self($keyType, $itemType);
499: }
500:
501: return $this;
502: }
503:
504: public function tryRemove(Type $typeToRemove): ?Type
505: {
506: $arrayType = new ArrayType(new MixedType(), new MixedType());
507: if ($typeToRemove->isSuperTypeOf($arrayType)->yes()) {
508: return new GenericObjectType(Traversable::class, [
509: $this->getIterableKeyType(),
510: $this->getIterableValueType(),
511: ]);
512: }
513:
514: $traversableType = new ObjectType(Traversable::class);
515: if ($typeToRemove->isSuperTypeOf($traversableType)->yes()) {
516: return new ArrayType($this->getIterableKeyType(), $this->getIterableValueType());
517: }
518:
519: return null;
520: }
521:
522: public function arithmeticPower(Type $exponent, PhpVersion $phpVersion): Type
523: {
524: return new ErrorType();
525: }
526:
527: public function getFiniteTypes(): array
528: {
529: return [];
530: }
531:
532: public function toPhpDocNode(): TypeNode
533: {
534: $isMixedKeyType = $this->keyType instanceof MixedType && $this->keyType->describe(VerbosityLevel::precise()) === 'mixed';
535: $isMixedItemType = $this->itemType instanceof MixedType && $this->itemType->describe(VerbosityLevel::precise()) === 'mixed';
536:
537: if ($isMixedKeyType) {
538: if ($isMixedItemType) {
539: return new IdentifierTypeNode('iterable');
540: }
541:
542: return new GenericTypeNode(
543: new IdentifierTypeNode('iterable'),
544: [
545: $this->itemType->toPhpDocNode(),
546: ],
547: );
548: }
549:
550: return new GenericTypeNode(
551: new IdentifierTypeNode('iterable'),
552: [
553: $this->keyType->toPhpDocNode(),
554: $this->itemType->toPhpDocNode(),
555: ],
556: );
557: }
558:
559: public function toNativePhpTypeNode(NativeTypePosition $position)
560: {
561: return $position->isIterableAllowed() ? new Identifier('iterable') : null;
562: }
563:
564: public function hasTemplateOrLateResolvableType(): bool
565: {
566: return $this->keyType->hasTemplateOrLateResolvableType() || $this->itemType->hasTemplateOrLateResolvableType();
567: }
568:
569: public function toValueSet(): ValueSet
570: {
571: return ValueSet::createInvalid();
572: }
573:
574: }
575: