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