1: <?php declare(strict_types = 1);
2:
3: namespace PHPStan\Type\Accessory;
4:
5: use PhpParser\Node\Identifier;
6: use PHPStan\Php\PhpVersion;
7: use PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode;
8: use PHPStan\PhpDocParser\Ast\Type\TypeNode;
9: use PHPStan\TrinaryLogic;
10: use PHPStan\Turbo\ShadowedByTurboExtension;
11: use PHPStan\Type\AcceptsResult;
12: use PHPStan\Type\ArrayType;
13: use PHPStan\Type\BooleanType;
14: use PHPStan\Type\CompoundType;
15: use PHPStan\Type\Constant\ConstantFloatType;
16: use PHPStan\Type\Constant\ConstantIntegerType;
17: use PHPStan\Type\ErrorType;
18: use PHPStan\Type\InstanceofDeprecated;
19: use PHPStan\Type\IntegerRangeType;
20: use PHPStan\Type\IntersectionType;
21: use PHPStan\Type\IsSuperTypeOfResult;
22: use PHPStan\Type\MixedType;
23: use PHPStan\Type\NativeTypePosition;
24: use PHPStan\Type\Traits\MaybeCallableTypeTrait;
25: use PHPStan\Type\Traits\NonGeneralizableTypeTrait;
26: use PHPStan\Type\Traits\NonGenericTypeTrait;
27: use PHPStan\Type\Traits\NonObjectTypeTrait;
28: use PHPStan\Type\Traits\NonRemoveableTypeTrait;
29: use PHPStan\Type\Traits\OperatorTypeTrait;
30: use PHPStan\Type\Traits\UndecidedBooleanTypeTrait;
31: use PHPStan\Type\Traits\UndecidedComparisonCompoundTypeTrait;
32: use PHPStan\Type\Type;
33: use PHPStan\Type\TypeKind;
34: use PHPStan\Type\UnionType;
35: use PHPStan\Type\ValueSet;
36: use PHPStan\Type\VerbosityLevel;
37:
38: /** @api */
39: #[InstanceofDeprecated(insteadUse: 'Type::isList()')]
40: #[ShadowedByTurboExtension(implementation: __DIR__ . '/../../../turbo-ext/src/AccessoryArrayListType.cpp')]
41: class AccessoryArrayListType implements CompoundType, AccessoryType
42: {
43:
44: use MaybeCallableTypeTrait;
45: use NonObjectTypeTrait;
46: use NonGenericTypeTrait;
47: use UndecidedBooleanTypeTrait;
48: use UndecidedComparisonCompoundTypeTrait;
49: use NonRemoveableTypeTrait;
50: use NonGeneralizableTypeTrait;
51: use OperatorTypeTrait;
52:
53: /** @api */
54: public function __construct()
55: {
56: }
57:
58: public function getReferencedClasses(): array
59: {
60: return [];
61: }
62:
63: public function getObjectClassNames(): array
64: {
65: return [];
66: }
67:
68: public function getObjectClassReflections(): array
69: {
70: return [];
71: }
72:
73: public function getArrays(): array
74: {
75: return [];
76: }
77:
78: public function getConstantArrays(): array
79: {
80: return [];
81: }
82:
83: public function getConstantStrings(): array
84: {
85: return [];
86: }
87:
88: public function accepts(Type $type, bool $strictTypes): AcceptsResult
89: {
90: $isList = $type->isList();
91:
92: if ($isList->yes()) {
93: return AcceptsResult::createYes();
94: }
95:
96: if ($type instanceof CompoundType) {
97: return $type->isAcceptedBy($this, $strictTypes);
98: }
99:
100: return new AcceptsResult($isList, []);
101: }
102:
103: public function isSuperTypeOf(Type $type): IsSuperTypeOfResult
104: {
105: if ($this->equals($type)) {
106: return IsSuperTypeOfResult::createYes();
107: }
108:
109: if ($type instanceof CompoundType) {
110: return $type->isSubTypeOf($this);
111: }
112:
113: return IsSuperTypeOfResult::createFromTrinaryLogic($type->isList());
114: }
115:
116: public function isSubTypeOf(Type $otherType): IsSuperTypeOfResult
117: {
118: if ($otherType instanceof UnionType || $otherType instanceof IntersectionType) {
119: return $otherType->isSuperTypeOf($this);
120: }
121:
122: if ($otherType instanceof self) {
123: return IsSuperTypeOfResult::createFromTrinaryLogic(
124: TrinaryLogic::createYes(),
125: );
126: }
127:
128: return IsSuperTypeOfResult::createFromTrinaryLogic(
129: $otherType->isList()->and(TrinaryLogic::createMaybe()),
130: );
131: }
132:
133: public function isAcceptedBy(Type $acceptingType, bool $strictTypes): AcceptsResult
134: {
135: return $this->isSubTypeOf($acceptingType)->toAcceptsResult();
136: }
137:
138: public function equals(Type $type): bool
139: {
140: return $type instanceof self;
141: }
142:
143: public function describe(VerbosityLevel $level): string
144: {
145: return 'list';
146: }
147:
148: public function isOffsetAccessible(): TrinaryLogic
149: {
150: return TrinaryLogic::createYes();
151: }
152:
153: public function isOffsetAccessLegal(): TrinaryLogic
154: {
155: return TrinaryLogic::createYes();
156: }
157:
158: public function hasOffsetValueType(Type $offsetType): TrinaryLogic
159: {
160: return $this->getIterableKeyType()->isSuperTypeOf($offsetType)->result->and(TrinaryLogic::createMaybe());
161: }
162:
163: public function getOffsetValueType(Type $offsetType): Type
164: {
165: return new MixedType();
166: }
167:
168: public function setOffsetValueType(?Type $offsetType, Type $valueType, bool $unionValues = true): Type
169: {
170: if ($offsetType === null || (new ConstantIntegerType(0))->isSuperTypeOf($offsetType)->yes()) {
171: return $this;
172: }
173:
174: return new ErrorType();
175: }
176:
177: public function setExistingOffsetValueType(Type $offsetType, Type $valueType): Type
178: {
179: return $this;
180: }
181:
182: public function unsetOffset(Type $offsetType): Type
183: {
184: if ($this->hasOffsetValueType($offsetType)->no()) {
185: return $this;
186: }
187:
188: return new ErrorType();
189: }
190:
191: public function getKeysArrayFiltered(Type $filterValueType, TrinaryLogic $strict): Type
192: {
193: return $this->getKeysArray();
194: }
195:
196: public function getKeysArray(): Type
197: {
198: return $this;
199: }
200:
201: public function getValuesArray(): Type
202: {
203: return $this;
204: }
205:
206: public function chunkArray(Type $lengthType, TrinaryLogic $preserveKeys): Type
207: {
208: return $this;
209: }
210:
211: public function fillKeysArray(Type $valueType): Type
212: {
213: return new MixedType();
214: }
215:
216: public function flipArray(): Type
217: {
218: return new MixedType();
219: }
220:
221: public function intersectKeyArray(Type $otherArraysType): Type
222: {
223: if ($otherArraysType->isList()->yes()) {
224: return $this;
225: }
226:
227: return new MixedType();
228: }
229:
230: public function popArray(): Type
231: {
232: return $this;
233: }
234:
235: public function reverseArray(TrinaryLogic $preserveKeys): Type
236: {
237: if ($preserveKeys->no()) {
238: return $this;
239: }
240:
241: return new MixedType();
242: }
243:
244: public function searchArray(Type $needleType, ?TrinaryLogic $strict = null): Type
245: {
246: return new MixedType();
247: }
248:
249: public function shiftArray(): Type
250: {
251: return $this;
252: }
253:
254: public function shuffleArray(): Type
255: {
256: return $this;
257: }
258:
259: public function sliceArray(Type $offsetType, Type $lengthType, TrinaryLogic $preserveKeys): Type
260: {
261: if ($preserveKeys->no()) {
262: return $this;
263: }
264:
265: if ((new ConstantIntegerType(0))->isSuperTypeOf($offsetType)->yes()) {
266: return $this;
267: }
268:
269: return new MixedType();
270: }
271:
272: public function spliceArray(Type $offsetType, Type $lengthType, Type $replacementType): Type
273: {
274: return $this;
275: }
276:
277: public function truncateListToSize(Type $sizeType): Type
278: {
279: // List-ness survives a count narrowing — the resulting array is
280: // still a list, just of a constrained size.
281: return $this;
282: }
283:
284: public function makeListMaybe(): Type
285: {
286: // This accessory is the list assertion itself; weakening the
287: // list-ness to "maybe" means the accessory no longer applies.
288: // Returning `MixedType` lets the enclosing `IntersectionType` drop
289: // it via `TypeCombinator::intersect` while preserving the rest.
290: return new MixedType();
291: }
292:
293: public function mapValueType(callable $cb): Type
294: {
295: // Mapping values doesn't disturb list-ness.
296: return $this;
297: }
298:
299: public function mapKeyType(callable $cb): Type
300: {
301: return $this;
302: }
303:
304: public function makeAllArrayKeysOptional(): Type
305: {
306: // Marking keys optional in an arbitrary list keeps it a list.
307: return $this;
308: }
309:
310: public function changeKeyCaseArray(?int $case): Type
311: {
312: // List keys are integers; case-folding leaves them alone.
313: return $this;
314: }
315:
316: public function filterArrayRemovingFalsey(): Type
317: {
318: // Filtering creates gaps in the integer-key sequence — list-ness lost.
319: return new MixedType();
320: }
321:
322: public function isIterable(): TrinaryLogic
323: {
324: return TrinaryLogic::createYes();
325: }
326:
327: public function isIterableAtLeastOnce(): TrinaryLogic
328: {
329: return TrinaryLogic::createMaybe();
330: }
331:
332: public function getArraySize(): Type
333: {
334: return IntegerRangeType::fromInterval(0, null);
335: }
336:
337: public function getIterableKeyType(): Type
338: {
339: return IntegerRangeType::fromInterval(0, null);
340: }
341:
342: public function getFirstIterableKeyType(): Type
343: {
344: return new ConstantIntegerType(0);
345: }
346:
347: public function getLastIterableKeyType(): Type
348: {
349: return $this->getIterableKeyType();
350: }
351:
352: public function getIterableValueType(): Type
353: {
354: return new MixedType();
355: }
356:
357: public function getFirstIterableValueType(): Type
358: {
359: return new MixedType();
360: }
361:
362: public function getLastIterableValueType(): Type
363: {
364: return new MixedType();
365: }
366:
367: public function isArray(): TrinaryLogic
368: {
369: return TrinaryLogic::createYes();
370: }
371:
372: public function isConstantArray(): TrinaryLogic
373: {
374: return TrinaryLogic::createMaybe();
375: }
376:
377: public function isOversizedArray(): TrinaryLogic
378: {
379: return TrinaryLogic::createMaybe();
380: }
381:
382: public function isList(): TrinaryLogic
383: {
384: return TrinaryLogic::createYes();
385: }
386:
387: public function getKindMask(): int
388: {
389: return TypeKind::ARRAY;
390: }
391:
392: public function isNull(): TrinaryLogic
393: {
394: return TrinaryLogic::createNo();
395: }
396:
397: public function isConstantValue(): TrinaryLogic
398: {
399: return TrinaryLogic::createMaybe();
400: }
401:
402: public function isConstantScalarValue(): TrinaryLogic
403: {
404: return TrinaryLogic::createNo();
405: }
406:
407: public function getConstantScalarTypes(): array
408: {
409: return [];
410: }
411:
412: public function getConstantScalarValues(): array
413: {
414: return [];
415: }
416:
417: public function isTrue(): TrinaryLogic
418: {
419: return TrinaryLogic::createNo();
420: }
421:
422: public function isFalse(): TrinaryLogic
423: {
424: return TrinaryLogic::createNo();
425: }
426:
427: public function isBoolean(): TrinaryLogic
428: {
429: return TrinaryLogic::createNo();
430: }
431:
432: public function isFloat(): TrinaryLogic
433: {
434: return TrinaryLogic::createNo();
435: }
436:
437: public function isInteger(): TrinaryLogic
438: {
439: return TrinaryLogic::createNo();
440: }
441:
442: public function isString(): TrinaryLogic
443: {
444: return TrinaryLogic::createNo();
445: }
446:
447: public function isNumericString(): TrinaryLogic
448: {
449: return TrinaryLogic::createNo();
450: }
451:
452: public function isDecimalIntegerString(): TrinaryLogic
453: {
454: return TrinaryLogic::createNo();
455: }
456:
457: public function isNonEmptyString(): TrinaryLogic
458: {
459: return TrinaryLogic::createNo();
460: }
461:
462: public function isNonFalsyString(): TrinaryLogic
463: {
464: return TrinaryLogic::createNo();
465: }
466:
467: public function isLiteralString(): TrinaryLogic
468: {
469: return TrinaryLogic::createNo();
470: }
471:
472: public function getStringRefinement(): ?int
473: {
474: return null;
475: }
476:
477: public function isLowercaseString(): TrinaryLogic
478: {
479: return TrinaryLogic::createNo();
480: }
481:
482: public function isClassString(): TrinaryLogic
483: {
484: return TrinaryLogic::createNo();
485: }
486:
487: public function isUppercaseString(): TrinaryLogic
488: {
489: return TrinaryLogic::createNo();
490: }
491:
492: public function getClassStringObjectType(): Type
493: {
494: return new ErrorType();
495: }
496:
497: public function getObjectTypeOrClassStringObjectType(): Type
498: {
499: return new ErrorType();
500: }
501:
502: public function isVoid(): TrinaryLogic
503: {
504: return TrinaryLogic::createNo();
505: }
506:
507: public function isScalar(): TrinaryLogic
508: {
509: return TrinaryLogic::createNo();
510: }
511:
512: public function looseCompare(Type $type, PhpVersion $phpVersion): BooleanType
513: {
514: return new BooleanType();
515: }
516:
517: public function toNumber(): Type
518: {
519: return new ErrorType();
520: }
521:
522: public function toBitwiseNotType(): Type
523: {
524: return new ErrorType();
525: }
526:
527: public function toAbsoluteNumber(): Type
528: {
529: return new ErrorType();
530: }
531:
532: public function toInteger(): Type
533: {
534: return new UnionType([
535: new ConstantIntegerType(0),
536: new ConstantIntegerType(1),
537: ]);
538: }
539:
540: public function toFloat(): Type
541: {
542: return new UnionType([
543: new ConstantFloatType(0.0),
544: new ConstantFloatType(1.0),
545: ]);
546: }
547:
548: public function toString(): Type
549: {
550: return new ErrorType();
551: }
552:
553: public function toArray(): Type
554: {
555: return $this;
556: }
557:
558: public function toArrayKey(): Type
559: {
560: return new ErrorType();
561: }
562:
563: public function toCoercedArgumentType(bool $strictTypes): Type
564: {
565: return $this;
566: }
567:
568: public function traverse(callable $cb): Type
569: {
570: return $this;
571: }
572:
573: public function traverseSimultaneously(Type $right, callable $cb): Type
574: {
575: return $this;
576: }
577:
578: public function arithmeticPower(Type $exponent, PhpVersion $phpVersion): Type
579: {
580: return new ErrorType();
581: }
582:
583: public function getFiniteTypes(): array
584: {
585: return [];
586: }
587:
588: public function getDefaultBaseType(): Type
589: {
590: return new ArrayType(new MixedType(), new MixedType());
591: }
592:
593: public function toPhpDocNode(): TypeNode
594: {
595: return new IdentifierTypeNode('list');
596: }
597:
598: public function toNativePhpTypeNode(NativeTypePosition $position)
599: {
600: return $position->isArrayAllowed() ? new Identifier('array') : null;
601: }
602:
603: public function hasTemplateOrLateResolvableType(): bool
604: {
605: return false;
606: }
607:
608: public function hasUnresolvedTemplateArgumentType(): bool
609: {
610: return false;
611: }
612:
613: public function toValueSet(): ValueSet
614: {
615: return ValueSet::createArrays($this);
616: }
617:
618: public function tryUnion(Type $otherType): ?Type
619: {
620: return null;
621: }
622:
623: public function tryIntersect(Type $otherType): ?Type
624: {
625: return null;
626: }
627:
628: }
629: