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