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: | |
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 isOffsetAccessLegal(): TrinaryLogic |
146: | { |
147: | return TrinaryLogic::createYes(); |
148: | } |
149: | |
150: | public function hasOffsetValueType(Type $offsetType): TrinaryLogic |
151: | { |
152: | return $this->getIterableKeyType()->isSuperTypeOf($offsetType)->and(TrinaryLogic::createMaybe()); |
153: | } |
154: | |
155: | public function getOffsetValueType(Type $offsetType): Type |
156: | { |
157: | return new MixedType(); |
158: | } |
159: | |
160: | public function setOffsetValueType(?Type $offsetType, Type $valueType, bool $unionValues = true): Type |
161: | { |
162: | if ($offsetType === null || (new ConstantIntegerType(0))->isSuperTypeOf($offsetType)->yes()) { |
163: | return $this; |
164: | } |
165: | |
166: | return new ErrorType(); |
167: | } |
168: | |
169: | public function setExistingOffsetValueType(Type $offsetType, Type $valueType): Type |
170: | { |
171: | if ((new ConstantIntegerType(0))->isSuperTypeOf($offsetType)->yes()) { |
172: | return $this; |
173: | } |
174: | |
175: | return new ErrorType(); |
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 getKeysArray(): Type |
188: | { |
189: | return $this; |
190: | } |
191: | |
192: | public function getValuesArray(): Type |
193: | { |
194: | return $this; |
195: | } |
196: | |
197: | public function fillKeysArray(Type $valueType): Type |
198: | { |
199: | return new MixedType(); |
200: | } |
201: | |
202: | public function flipArray(): Type |
203: | { |
204: | return new MixedType(); |
205: | } |
206: | |
207: | public function intersectKeyArray(Type $otherArraysType): Type |
208: | { |
209: | if ($otherArraysType->isList()->yes()) { |
210: | return $this; |
211: | } |
212: | |
213: | return new MixedType(); |
214: | } |
215: | |
216: | public function popArray(): Type |
217: | { |
218: | return $this; |
219: | } |
220: | |
221: | public function searchArray(Type $needleType): Type |
222: | { |
223: | return new MixedType(); |
224: | } |
225: | |
226: | public function shiftArray(): Type |
227: | { |
228: | return $this; |
229: | } |
230: | |
231: | public function shuffleArray(): Type |
232: | { |
233: | return $this; |
234: | } |
235: | |
236: | public function isIterable(): TrinaryLogic |
237: | { |
238: | return TrinaryLogic::createYes(); |
239: | } |
240: | |
241: | public function isIterableAtLeastOnce(): TrinaryLogic |
242: | { |
243: | return TrinaryLogic::createMaybe(); |
244: | } |
245: | |
246: | public function getArraySize(): Type |
247: | { |
248: | return IntegerRangeType::fromInterval(0, null); |
249: | } |
250: | |
251: | public function getIterableKeyType(): Type |
252: | { |
253: | return IntegerRangeType::fromInterval(0, null); |
254: | } |
255: | |
256: | public function getFirstIterableKeyType(): Type |
257: | { |
258: | return new ConstantIntegerType(0); |
259: | } |
260: | |
261: | public function getLastIterableKeyType(): Type |
262: | { |
263: | return $this->getIterableKeyType(); |
264: | } |
265: | |
266: | public function getIterableValueType(): Type |
267: | { |
268: | return new MixedType(); |
269: | } |
270: | |
271: | public function getFirstIterableValueType(): Type |
272: | { |
273: | return new MixedType(); |
274: | } |
275: | |
276: | public function getLastIterableValueType(): Type |
277: | { |
278: | return new MixedType(); |
279: | } |
280: | |
281: | public function isArray(): TrinaryLogic |
282: | { |
283: | return TrinaryLogic::createYes(); |
284: | } |
285: | |
286: | public function isConstantArray(): TrinaryLogic |
287: | { |
288: | return TrinaryLogic::createMaybe(); |
289: | } |
290: | |
291: | public function isOversizedArray(): TrinaryLogic |
292: | { |
293: | return TrinaryLogic::createMaybe(); |
294: | } |
295: | |
296: | public function isList(): TrinaryLogic |
297: | { |
298: | return TrinaryLogic::createYes(); |
299: | } |
300: | |
301: | public function isNull(): TrinaryLogic |
302: | { |
303: | return TrinaryLogic::createNo(); |
304: | } |
305: | |
306: | public function isConstantValue(): TrinaryLogic |
307: | { |
308: | return TrinaryLogic::createMaybe(); |
309: | } |
310: | |
311: | public function isConstantScalarValue(): TrinaryLogic |
312: | { |
313: | return TrinaryLogic::createNo(); |
314: | } |
315: | |
316: | public function getConstantScalarTypes(): array |
317: | { |
318: | return []; |
319: | } |
320: | |
321: | public function getConstantScalarValues(): array |
322: | { |
323: | return []; |
324: | } |
325: | |
326: | public function isTrue(): TrinaryLogic |
327: | { |
328: | return TrinaryLogic::createNo(); |
329: | } |
330: | |
331: | public function isFalse(): TrinaryLogic |
332: | { |
333: | return TrinaryLogic::createNo(); |
334: | } |
335: | |
336: | public function isBoolean(): TrinaryLogic |
337: | { |
338: | return TrinaryLogic::createNo(); |
339: | } |
340: | |
341: | public function isFloat(): TrinaryLogic |
342: | { |
343: | return TrinaryLogic::createNo(); |
344: | } |
345: | |
346: | public function isInteger(): TrinaryLogic |
347: | { |
348: | return TrinaryLogic::createNo(); |
349: | } |
350: | |
351: | public function isString(): TrinaryLogic |
352: | { |
353: | return TrinaryLogic::createNo(); |
354: | } |
355: | |
356: | public function isNumericString(): TrinaryLogic |
357: | { |
358: | return TrinaryLogic::createNo(); |
359: | } |
360: | |
361: | public function isNonEmptyString(): TrinaryLogic |
362: | { |
363: | return TrinaryLogic::createNo(); |
364: | } |
365: | |
366: | public function isNonFalsyString(): TrinaryLogic |
367: | { |
368: | return TrinaryLogic::createNo(); |
369: | } |
370: | |
371: | public function isLiteralString(): TrinaryLogic |
372: | { |
373: | return TrinaryLogic::createNo(); |
374: | } |
375: | |
376: | public function isClassStringType(): TrinaryLogic |
377: | { |
378: | return TrinaryLogic::createNo(); |
379: | } |
380: | |
381: | public function getClassStringObjectType(): Type |
382: | { |
383: | return new ErrorType(); |
384: | } |
385: | |
386: | public function getObjectTypeOrClassStringObjectType(): Type |
387: | { |
388: | return new ErrorType(); |
389: | } |
390: | |
391: | public function isVoid(): TrinaryLogic |
392: | { |
393: | return TrinaryLogic::createNo(); |
394: | } |
395: | |
396: | public function isScalar(): TrinaryLogic |
397: | { |
398: | return TrinaryLogic::createNo(); |
399: | } |
400: | |
401: | public function looseCompare(Type $type, PhpVersion $phpVersion): BooleanType |
402: | { |
403: | return new BooleanType(); |
404: | } |
405: | |
406: | public function toNumber(): Type |
407: | { |
408: | return new ErrorType(); |
409: | } |
410: | |
411: | public function toInteger(): Type |
412: | { |
413: | return TypeCombinator::union( |
414: | new ConstantIntegerType(0), |
415: | new ConstantIntegerType(1), |
416: | ); |
417: | } |
418: | |
419: | public function toFloat(): Type |
420: | { |
421: | return TypeCombinator::union( |
422: | new ConstantFloatType(0.0), |
423: | new ConstantFloatType(1.0), |
424: | ); |
425: | } |
426: | |
427: | public function toString(): Type |
428: | { |
429: | return new ErrorType(); |
430: | } |
431: | |
432: | public function toArray(): Type |
433: | { |
434: | return $this; |
435: | } |
436: | |
437: | public function toArrayKey(): Type |
438: | { |
439: | return new ErrorType(); |
440: | } |
441: | |
442: | public function traverse(callable $cb): Type |
443: | { |
444: | return $this; |
445: | } |
446: | |
447: | public function traverseSimultaneously(Type $right, callable $cb): Type |
448: | { |
449: | return $this; |
450: | } |
451: | |
452: | public static function __set_state(array $properties): Type |
453: | { |
454: | return new self(); |
455: | } |
456: | |
457: | public static function setListTypeEnabled(bool $enabled): void |
458: | { |
459: | self::$enabled = $enabled; |
460: | } |
461: | |
462: | public static function isListTypeEnabled(): bool |
463: | { |
464: | return self::$enabled; |
465: | } |
466: | |
467: | public static function intersectWith(Type $type): Type |
468: | { |
469: | if (self::$enabled) { |
470: | return TypeCombinator::intersect($type, new self()); |
471: | } |
472: | |
473: | return $type; |
474: | } |
475: | |
476: | public function exponentiate(Type $exponent): Type |
477: | { |
478: | return new ErrorType(); |
479: | } |
480: | |
481: | public function getFiniteTypes(): array |
482: | { |
483: | return []; |
484: | } |
485: | |
486: | public function toPhpDocNode(): TypeNode |
487: | { |
488: | return new IdentifierTypeNode('list'); |
489: | } |
490: | |
491: | } |
492: | |