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