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\Type\AcceptsResult;
10: use PHPStan\Type\BooleanType;
11: use PHPStan\Type\CompoundType;
12: use PHPStan\Type\Constant\ConstantFloatType;
13: use PHPStan\Type\Constant\ConstantIntegerType;
14: use PHPStan\Type\ErrorType;
15: use PHPStan\Type\IntegerRangeType;
16: use PHPStan\Type\IntersectionType;
17: use PHPStan\Type\MixedType;
18: use PHPStan\Type\Traits\MaybeCallableTypeTrait;
19: use PHPStan\Type\Traits\NonGeneralizableTypeTrait;
20: use PHPStan\Type\Traits\NonGenericTypeTrait;
21: use PHPStan\Type\Traits\NonObjectTypeTrait;
22: use PHPStan\Type\Traits\NonRemoveableTypeTrait;
23: use PHPStan\Type\Traits\UndecidedBooleanTypeTrait;
24: use PHPStan\Type\Traits\UndecidedComparisonCompoundTypeTrait;
25: use PHPStan\Type\Type;
26: use PHPStan\Type\TypeCombinator;
27: use PHPStan\Type\UnionType;
28: use PHPStan\Type\VerbosityLevel;
29:
30: /** @api */
31: class AccessoryArrayListType implements CompoundType, AccessoryType
32: {
33:
34: use MaybeCallableTypeTrait;
35: use NonObjectTypeTrait;
36: use NonGenericTypeTrait;
37: use UndecidedBooleanTypeTrait;
38: use UndecidedComparisonCompoundTypeTrait;
39: use NonRemoveableTypeTrait;
40: use NonGeneralizableTypeTrait;
41:
42: private static bool $enabled = false;
43:
44: public function __construct()
45: {
46: }
47:
48: public function getReferencedClasses(): array
49: {
50: return [];
51: }
52:
53: public function getObjectClassNames(): array
54: {
55: return [];
56: }
57:
58: public function getObjectClassReflections(): array
59: {
60: return [];
61: }
62:
63: public function getArrays(): array
64: {
65: return [];
66: }
67:
68: public function getConstantArrays(): array
69: {
70: return [];
71: }
72:
73: public function getConstantStrings(): array
74: {
75: return [];
76: }
77:
78: public function accepts(Type $type, bool $strictTypes): TrinaryLogic
79: {
80: return $this->acceptsWithReason($type, $strictTypes)->result;
81: }
82:
83: public function acceptsWithReason(Type $type, bool $strictTypes): AcceptsResult
84: {
85: if ($type instanceof CompoundType) {
86: return $type->isAcceptedWithReasonBy($this, $strictTypes);
87: }
88:
89: $isArray = $type->isArray();
90: $isList = $type->isList();
91:
92: return new AcceptsResult($isArray->and($isList), []);
93: }
94:
95: public function isSuperTypeOf(Type $type): TrinaryLogic
96: {
97: if ($this->equals($type)) {
98: return TrinaryLogic::createYes();
99: }
100:
101: if ($type instanceof CompoundType) {
102: return $type->isSubTypeOf($this);
103: }
104:
105: return $type->isArray()
106: ->and($type->isList());
107: }
108:
109: public function isSubTypeOf(Type $otherType): TrinaryLogic
110: {
111: if ($otherType instanceof UnionType || $otherType instanceof IntersectionType) {
112: return $otherType->isSuperTypeOf($this);
113: }
114:
115: return $otherType->isArray()
116: ->and($otherType->isList())
117: ->and($otherType instanceof self ? TrinaryLogic::createYes() : TrinaryLogic::createMaybe());
118: }
119:
120: public function isAcceptedBy(Type $acceptingType, bool $strictTypes): TrinaryLogic
121: {
122: return $this->isAcceptedWithReasonBy($acceptingType, $strictTypes)->result;
123: }
124:
125: public function isAcceptedWithReasonBy(Type $acceptingType, bool $strictTypes): AcceptsResult
126: {
127: return new AcceptsResult($this->isSubTypeOf($acceptingType), []);
128: }
129:
130: public function equals(Type $type): bool
131: {
132: return $type instanceof self;
133: }
134:
135: public function describe(VerbosityLevel $level): string
136: {
137: return 'list';
138: }
139:
140: public function isOffsetAccessible(): TrinaryLogic
141: {
142: return TrinaryLogic::createYes();
143: }
144:
145: public function hasOffsetValueType(Type $offsetType): TrinaryLogic
146: {
147: return $this->getIterableKeyType()->isSuperTypeOf($offsetType)->and(TrinaryLogic::createMaybe());
148: }
149:
150: public function getOffsetValueType(Type $offsetType): Type
151: {
152: return new MixedType();
153: }
154:
155: public function setOffsetValueType(?Type $offsetType, Type $valueType, bool $unionValues = true): Type
156: {
157: if ($offsetType === null || (new ConstantIntegerType(0))->isSuperTypeOf($offsetType)->yes()) {
158: return $this;
159: }
160:
161: return new ErrorType();
162: }
163:
164: public function setExistingOffsetValueType(Type $offsetType, Type $valueType): Type
165: {
166: if ((new ConstantIntegerType(0))->isSuperTypeOf($offsetType)->yes()) {
167: return $this;
168: }
169:
170: return new ErrorType();
171: }
172:
173: public function unsetOffset(Type $offsetType): Type
174: {
175: if ($this->hasOffsetValueType($offsetType)->no()) {
176: return $this;
177: }
178:
179: return new ErrorType();
180: }
181:
182: public function getKeysArray(): Type
183: {
184: return $this;
185: }
186:
187: public function getValuesArray(): Type
188: {
189: return $this;
190: }
191:
192: public function fillKeysArray(Type $valueType): Type
193: {
194: return new MixedType();
195: }
196:
197: public function flipArray(): Type
198: {
199: return new MixedType();
200: }
201:
202: public function intersectKeyArray(Type $otherArraysType): Type
203: {
204: if ($otherArraysType->isList()->yes()) {
205: return $this;
206: }
207:
208: return new MixedType();
209: }
210:
211: public function popArray(): Type
212: {
213: return $this;
214: }
215:
216: public function searchArray(Type $needleType): Type
217: {
218: return new MixedType();
219: }
220:
221: public function shiftArray(): Type
222: {
223: return $this;
224: }
225:
226: public function shuffleArray(): Type
227: {
228: return $this;
229: }
230:
231: public function isIterable(): TrinaryLogic
232: {
233: return TrinaryLogic::createYes();
234: }
235:
236: public function isIterableAtLeastOnce(): TrinaryLogic
237: {
238: return TrinaryLogic::createMaybe();
239: }
240:
241: public function getArraySize(): Type
242: {
243: return IntegerRangeType::fromInterval(0, null);
244: }
245:
246: public function getIterableKeyType(): Type
247: {
248: return IntegerRangeType::fromInterval(0, null);
249: }
250:
251: public function getFirstIterableKeyType(): Type
252: {
253: return new ConstantIntegerType(0);
254: }
255:
256: public function getLastIterableKeyType(): Type
257: {
258: return $this->getIterableKeyType();
259: }
260:
261: public function getIterableValueType(): Type
262: {
263: return new MixedType();
264: }
265:
266: public function getFirstIterableValueType(): Type
267: {
268: return new MixedType();
269: }
270:
271: public function getLastIterableValueType(): Type
272: {
273: return new MixedType();
274: }
275:
276: public function isArray(): TrinaryLogic
277: {
278: return TrinaryLogic::createYes();
279: }
280:
281: public function isConstantArray(): TrinaryLogic
282: {
283: return TrinaryLogic::createMaybe();
284: }
285:
286: public function isOversizedArray(): TrinaryLogic
287: {
288: return TrinaryLogic::createMaybe();
289: }
290:
291: public function isList(): TrinaryLogic
292: {
293: return TrinaryLogic::createYes();
294: }
295:
296: public function isNull(): TrinaryLogic
297: {
298: return TrinaryLogic::createNo();
299: }
300:
301: public function isConstantValue(): TrinaryLogic
302: {
303: return TrinaryLogic::createMaybe();
304: }
305:
306: public function isConstantScalarValue(): TrinaryLogic
307: {
308: return TrinaryLogic::createNo();
309: }
310:
311: public function getConstantScalarTypes(): array
312: {
313: return [];
314: }
315:
316: public function getConstantScalarValues(): array
317: {
318: return [];
319: }
320:
321: public function isTrue(): TrinaryLogic
322: {
323: return TrinaryLogic::createNo();
324: }
325:
326: public function isFalse(): TrinaryLogic
327: {
328: return TrinaryLogic::createNo();
329: }
330:
331: public function isBoolean(): TrinaryLogic
332: {
333: return TrinaryLogic::createNo();
334: }
335:
336: public function isFloat(): TrinaryLogic
337: {
338: return TrinaryLogic::createNo();
339: }
340:
341: public function isInteger(): TrinaryLogic
342: {
343: return TrinaryLogic::createNo();
344: }
345:
346: public function isString(): TrinaryLogic
347: {
348: return TrinaryLogic::createNo();
349: }
350:
351: public function isNumericString(): TrinaryLogic
352: {
353: return TrinaryLogic::createNo();
354: }
355:
356: public function isNonEmptyString(): TrinaryLogic
357: {
358: return TrinaryLogic::createNo();
359: }
360:
361: public function isNonFalsyString(): TrinaryLogic
362: {
363: return TrinaryLogic::createNo();
364: }
365:
366: public function isLiteralString(): TrinaryLogic
367: {
368: return TrinaryLogic::createNo();
369: }
370:
371: public function isClassStringType(): TrinaryLogic
372: {
373: return TrinaryLogic::createNo();
374: }
375:
376: public function getClassStringObjectType(): Type
377: {
378: return new ErrorType();
379: }
380:
381: public function getObjectTypeOrClassStringObjectType(): Type
382: {
383: return new ErrorType();
384: }
385:
386: public function isVoid(): TrinaryLogic
387: {
388: return TrinaryLogic::createNo();
389: }
390:
391: public function isScalar(): TrinaryLogic
392: {
393: return TrinaryLogic::createNo();
394: }
395:
396: public function looseCompare(Type $type, PhpVersion $phpVersion): BooleanType
397: {
398: return new BooleanType();
399: }
400:
401: public function toNumber(): Type
402: {
403: return new ErrorType();
404: }
405:
406: public function toInteger(): Type
407: {
408: return TypeCombinator::union(
409: new ConstantIntegerType(0),
410: new ConstantIntegerType(1),
411: );
412: }
413:
414: public function toFloat(): Type
415: {
416: return TypeCombinator::union(
417: new ConstantFloatType(0.0),
418: new ConstantFloatType(1.0),
419: );
420: }
421:
422: public function toString(): Type
423: {
424: return new ErrorType();
425: }
426:
427: public function toArray(): Type
428: {
429: return $this;
430: }
431:
432: public function toArrayKey(): Type
433: {
434: return new ErrorType();
435: }
436:
437: public function traverse(callable $cb): Type
438: {
439: return $this;
440: }
441:
442: public function traverseSimultaneously(Type $right, callable $cb): Type
443: {
444: return $this;
445: }
446:
447: public static function __set_state(array $properties): Type
448: {
449: return new self();
450: }
451:
452: public static function setListTypeEnabled(bool $enabled): void
453: {
454: self::$enabled = $enabled;
455: }
456:
457: public static function isListTypeEnabled(): bool
458: {
459: return self::$enabled;
460: }
461:
462: public static function intersectWith(Type $type): Type
463: {
464: if (self::$enabled) {
465: return TypeCombinator::intersect($type, new self());
466: }
467:
468: return $type;
469: }
470:
471: public function exponentiate(Type $exponent): Type
472: {
473: return new ErrorType();
474: }
475:
476: public function getFiniteTypes(): array
477: {
478: return [];
479: }
480:
481: public function toPhpDocNode(): TypeNode
482: {
483: return new IdentifierTypeNode('list');
484: }
485:
486: }
487: