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\UndecidedBooleanTypeTrait;
30: use PHPStan\Type\Traits\UndecidedComparisonCompoundTypeTrait;
31: use PHPStan\Type\Type;
32: use PHPStan\Type\UnionType;
33: use PHPStan\Type\VerbosityLevel;
34:
35: /** @api */
36: #[InstanceofDeprecated(insteadUse: 'Type::isList()')]
37: #[ShadowedByTurboExtension(implementation: __DIR__ . '/../../../turbo-ext/src/AccessoryArrayListType.cpp')]
38: class AccessoryArrayListType implements CompoundType, AccessoryType
39: {
40:
41: use MaybeCallableTypeTrait;
42: use NonObjectTypeTrait;
43: use NonGenericTypeTrait;
44: use UndecidedBooleanTypeTrait;
45: use UndecidedComparisonCompoundTypeTrait;
46: use NonRemoveableTypeTrait;
47: use NonGeneralizableTypeTrait;
48:
49: /** @api */
50: public function __construct()
51: {
52: }
53:
54: public function getReferencedClasses(): array
55: {
56: return [];
57: }
58:
59: public function getObjectClassNames(): array
60: {
61: return [];
62: }
63:
64: public function getObjectClassReflections(): array
65: {
66: return [];
67: }
68:
69: public function getArrays(): array
70: {
71: return [];
72: }
73:
74: public function getConstantArrays(): array
75: {
76: return [];
77: }
78:
79: public function getConstantStrings(): array
80: {
81: return [];
82: }
83:
84: public function accepts(Type $type, bool $strictTypes): AcceptsResult
85: {
86: $isList = $type->isList();
87:
88: if ($isList->yes()) {
89: return AcceptsResult::createYes();
90: }
91:
92: if ($type instanceof CompoundType) {
93: return $type->isAcceptedBy($this, $strictTypes);
94: }
95:
96: return new AcceptsResult($isList, []);
97: }
98:
99: public function isSuperTypeOf(Type $type): IsSuperTypeOfResult
100: {
101: if ($this->equals($type)) {
102: return IsSuperTypeOfResult::createYes();
103: }
104:
105: if ($type instanceof CompoundType) {
106: return $type->isSubTypeOf($this);
107: }
108:
109: return IsSuperTypeOfResult::createFromTrinaryLogic($type->isList());
110: }
111:
112: public function isSubTypeOf(Type $otherType): IsSuperTypeOfResult
113: {
114: if ($otherType instanceof UnionType || $otherType instanceof IntersectionType) {
115: return $otherType->isSuperTypeOf($this);
116: }
117:
118: if ($otherType instanceof self) {
119: return IsSuperTypeOfResult::createFromTrinaryLogic(
120: TrinaryLogic::createYes(),
121: );
122: }
123:
124: return IsSuperTypeOfResult::createFromTrinaryLogic(
125: $otherType->isList()->and(TrinaryLogic::createMaybe()),
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 toNativePhpTypeNode(NativeTypePosition $position)
585: {
586: return $position->isArrayAllowed() ? new Identifier('array') : null;
587: }
588:
589: public function hasTemplateOrLateResolvableType(): bool
590: {
591: return false;
592: }
593:
594: }
595: