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