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