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: /** @api */
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 (
220: (new ConstantIntegerType(0))->isSuperTypeOf($offsetType)->yes()
221: && ($lengthType->isNull()->yes() || IntegerRangeType::fromInterval(1, null)->isSuperTypeOf($lengthType)->yes())
222: ) {
223: return $this;
224: }
225:
226: return new MixedType();
227: }
228:
229: public function isIterable(): TrinaryLogic
230: {
231: return TrinaryLogic::createYes();
232: }
233:
234: public function isIterableAtLeastOnce(): TrinaryLogic
235: {
236: return TrinaryLogic::createYes();
237: }
238:
239: public function getArraySize(): Type
240: {
241: return IntegerRangeType::fromInterval(1, null);
242: }
243:
244: public function getIterableKeyType(): Type
245: {
246: return new MixedType();
247: }
248:
249: public function getFirstIterableKeyType(): Type
250: {
251: return new MixedType();
252: }
253:
254: public function getLastIterableKeyType(): Type
255: {
256: return new MixedType();
257: }
258:
259: public function getIterableValueType(): Type
260: {
261: return new MixedType();
262: }
263:
264: public function getFirstIterableValueType(): Type
265: {
266: return new MixedType();
267: }
268:
269: public function getLastIterableValueType(): Type
270: {
271: return new MixedType();
272: }
273:
274: public function isArray(): TrinaryLogic
275: {
276: return TrinaryLogic::createYes();
277: }
278:
279: public function isConstantArray(): TrinaryLogic
280: {
281: return TrinaryLogic::createMaybe();
282: }
283:
284: public function isOversizedArray(): TrinaryLogic
285: {
286: return TrinaryLogic::createMaybe();
287: }
288:
289: public function isList(): TrinaryLogic
290: {
291: return TrinaryLogic::createMaybe();
292: }
293:
294: public function isNull(): TrinaryLogic
295: {
296: return TrinaryLogic::createNo();
297: }
298:
299: public function isConstantValue(): TrinaryLogic
300: {
301: return TrinaryLogic::createMaybe();
302: }
303:
304: public function isConstantScalarValue(): TrinaryLogic
305: {
306: return TrinaryLogic::createNo();
307: }
308:
309: public function getConstantScalarTypes(): array
310: {
311: return [];
312: }
313:
314: public function getConstantScalarValues(): array
315: {
316: return [];
317: }
318:
319: public function isTrue(): TrinaryLogic
320: {
321: return TrinaryLogic::createNo();
322: }
323:
324: public function isFalse(): TrinaryLogic
325: {
326: return TrinaryLogic::createNo();
327: }
328:
329: public function isBoolean(): TrinaryLogic
330: {
331: return TrinaryLogic::createNo();
332: }
333:
334: public function isFloat(): TrinaryLogic
335: {
336: return TrinaryLogic::createNo();
337: }
338:
339: public function isInteger(): TrinaryLogic
340: {
341: return TrinaryLogic::createNo();
342: }
343:
344: public function isString(): TrinaryLogic
345: {
346: return TrinaryLogic::createNo();
347: }
348:
349: public function isNumericString(): TrinaryLogic
350: {
351: return TrinaryLogic::createNo();
352: }
353:
354: public function isNonEmptyString(): TrinaryLogic
355: {
356: return TrinaryLogic::createNo();
357: }
358:
359: public function isNonFalsyString(): TrinaryLogic
360: {
361: return TrinaryLogic::createNo();
362: }
363:
364: public function isLiteralString(): TrinaryLogic
365: {
366: return TrinaryLogic::createNo();
367: }
368:
369: public function isLowercaseString(): TrinaryLogic
370: {
371: return TrinaryLogic::createNo();
372: }
373:
374: public function isClassString(): TrinaryLogic
375: {
376: return TrinaryLogic::createNo();
377: }
378:
379: public function isUppercaseString(): TrinaryLogic
380: {
381: return TrinaryLogic::createNo();
382: }
383:
384: public function getClassStringObjectType(): Type
385: {
386: return new ErrorType();
387: }
388:
389: public function getObjectTypeOrClassStringObjectType(): Type
390: {
391: return new ErrorType();
392: }
393:
394: public function isVoid(): TrinaryLogic
395: {
396: return TrinaryLogic::createNo();
397: }
398:
399: public function isScalar(): TrinaryLogic
400: {
401: return TrinaryLogic::createNo();
402: }
403:
404: public function looseCompare(Type $type, PhpVersion $phpVersion): BooleanType
405: {
406: if ($type->isArray()->yes() && $type->isIterableAtLeastOnce()->no()) {
407: return new ConstantBooleanType(false);
408: }
409:
410: return new BooleanType();
411: }
412:
413: public function toNumber(): Type
414: {
415: return new ErrorType();
416: }
417:
418: public function toAbsoluteNumber(): Type
419: {
420: return new ErrorType();
421: }
422:
423: public function toInteger(): Type
424: {
425: return new ConstantIntegerType(1);
426: }
427:
428: public function toFloat(): Type
429: {
430: return new ConstantFloatType(1.0);
431: }
432:
433: public function toString(): Type
434: {
435: return new ErrorType();
436: }
437:
438: public function toArray(): Type
439: {
440: return $this;
441: }
442:
443: public function toArrayKey(): Type
444: {
445: return new ErrorType();
446: }
447:
448: public function traverse(callable $cb): Type
449: {
450: return $this;
451: }
452:
453: public function traverseSimultaneously(Type $right, callable $cb): Type
454: {
455: return $this;
456: }
457:
458: public function exponentiate(Type $exponent): Type
459: {
460: return new ErrorType();
461: }
462:
463: public function getFiniteTypes(): array
464: {
465: return [];
466: }
467:
468: public function toPhpDocNode(): TypeNode
469: {
470: return new IdentifierTypeNode('non-empty-array');
471: }
472:
473: }
474: