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\Turbo\ShadowedByTurboExtension;
10: use PHPStan\Type\AcceptsResult;
11: use PHPStan\Type\ArrayType;
12: use PHPStan\Type\BooleanType;
13: use PHPStan\Type\CompoundType;
14: use PHPStan\Type\Constant\ConstantBooleanType;
15: use PHPStan\Type\Constant\ConstantFloatType;
16: use PHPStan\Type\Constant\ConstantIntegerType;
17: use PHPStan\Type\ErrorType;
18: use PHPStan\Type\InstanceofDeprecated;
19: use PHPStan\Type\IntegerRangeType;
20: use PHPStan\Type\IntersectionType;
21: use PHPStan\Type\IsSuperTypeOfResult;
22: use PHPStan\Type\MixedType;
23: use PHPStan\Type\Traits\MaybeCallableTypeTrait;
24: use PHPStan\Type\Traits\NonGeneralizableTypeTrait;
25: use PHPStan\Type\Traits\NonGenericTypeTrait;
26: use PHPStan\Type\Traits\NonObjectTypeTrait;
27: use PHPStan\Type\Traits\NonRemoveableTypeTrait;
28: use PHPStan\Type\Traits\TruthyBooleanTypeTrait;
29: use PHPStan\Type\Traits\UndecidedComparisonCompoundTypeTrait;
30: use PHPStan\Type\Type;
31: use PHPStan\Type\UnionType;
32: use PHPStan\Type\VerbosityLevel;
33:
34: #[InstanceofDeprecated(insteadUse: 'Type::isIterableAtLeastOnce()')]
35: #[ShadowedByTurboExtension(implementation: __DIR__ . '/../../../turbo-ext/src/NonEmptyArrayType.cpp')]
36: class NonEmptyArrayType implements CompoundType, AccessoryType
37: {
38:
39: use MaybeCallableTypeTrait;
40: use NonObjectTypeTrait;
41: use TruthyBooleanTypeTrait;
42: use NonGenericTypeTrait;
43: use UndecidedComparisonCompoundTypeTrait;
44: use NonRemoveableTypeTrait;
45: use NonGeneralizableTypeTrait;
46:
47: /** @api */
48: public function __construct()
49: {
50: }
51:
52: public function getReferencedClasses(): array
53: {
54: return [];
55: }
56:
57: public function getObjectClassNames(): array
58: {
59: return [];
60: }
61:
62: public function getObjectClassReflections(): array
63: {
64: return [];
65: }
66:
67: public function getArrays(): array
68: {
69: return [];
70: }
71:
72: public function getConstantArrays(): array
73: {
74: return [];
75: }
76:
77: public function getConstantStrings(): array
78: {
79: return [];
80: }
81:
82: public function accepts(Type $type, bool $strictTypes): AcceptsResult
83: {
84: $isArray = $type->isArray();
85: $isIterableAtLeastOnce = $type->isIterableAtLeastOnce();
86: $isNonEmptyArray = $isArray->and($isIterableAtLeastOnce);
87:
88: if ($isNonEmptyArray->yes()) {
89: return AcceptsResult::createYes();
90: }
91:
92: if ($type instanceof CompoundType) {
93: return $type->isAcceptedBy($this, $strictTypes);
94: }
95:
96: return new AcceptsResult($isNonEmptyArray, []);
97: }
98:
99: public function isSuperTypeOf(Type $type): IsSuperTypeOfResult
100: {
101: if ($this->equals($type)) {
102: return IsSuperTypeOfResult::createYes();
103: }
104:
105: if ($type instanceof CompoundType) {
106: return $type->isSubTypeOf($this);
107: }
108:
109: return new IsSuperTypeOfResult($type->isArray()->and($type->isIterableAtLeastOnce()), []);
110: }
111:
112: public function isSubTypeOf(Type $otherType): IsSuperTypeOfResult
113: {
114: if ($otherType instanceof UnionType || $otherType instanceof IntersectionType) {
115: return $otherType->isSuperTypeOf($this);
116: }
117:
118: return new IsSuperTypeOfResult(
119: $otherType->isArray()->and($otherType->isIterableAtLeastOnce())->and($otherType instanceof self ? TrinaryLogic::createYes() : TrinaryLogic::createMaybe()),
120: [],
121: );
122: }
123:
124: public function isAcceptedBy(Type $acceptingType, bool $strictTypes): AcceptsResult
125: {
126: return $this->isSubTypeOf($acceptingType)->toAcceptsResult();
127: }
128:
129: public function equals(Type $type): bool
130: {
131: return $type instanceof self;
132: }
133:
134: public function describe(VerbosityLevel $level): string
135: {
136: return 'non-empty-array';
137: }
138:
139: public function isOffsetAccessible(): TrinaryLogic
140: {
141: return TrinaryLogic::createYes();
142: }
143:
144: public function isOffsetAccessLegal(): TrinaryLogic
145: {
146: return TrinaryLogic::createYes();
147: }
148:
149: public function hasOffsetValueType(Type $offsetType): TrinaryLogic
150: {
151: return TrinaryLogic::createMaybe();
152: }
153:
154: public function getOffsetValueType(Type $offsetType): Type
155: {
156: return new MixedType();
157: }
158:
159: public function setOffsetValueType(?Type $offsetType, Type $valueType, bool $unionValues = true): Type
160: {
161: return $this;
162: }
163:
164: public function setExistingOffsetValueType(Type $offsetType, Type $valueType): Type
165: {
166: return $this;
167: }
168:
169: public function unsetOffset(Type $offsetType): Type
170: {
171: return new ErrorType();
172: }
173:
174: public function getKeysArrayFiltered(Type $filterValueType, TrinaryLogic $strict): Type
175: {
176: return new ErrorType();
177: }
178:
179: public function getKeysArray(): Type
180: {
181: return $this;
182: }
183:
184: public function getValuesArray(): Type
185: {
186: return $this;
187: }
188:
189: public function chunkArray(Type $lengthType, TrinaryLogic $preserveKeys): Type
190: {
191: return $this;
192: }
193:
194: public function fillKeysArray(Type $valueType): Type
195: {
196: return $this;
197: }
198:
199: public function flipArray(): Type
200: {
201: return $this;
202: }
203:
204: public function intersectKeyArray(Type $otherArraysType): Type
205: {
206: return new MixedType();
207: }
208:
209: public function popArray(): Type
210: {
211: return new MixedType();
212: }
213:
214: public function reverseArray(TrinaryLogic $preserveKeys): Type
215: {
216: return $this;
217: }
218:
219: public function searchArray(Type $needleType, ?TrinaryLogic $strict = null): Type
220: {
221: return new MixedType();
222: }
223:
224: public function shiftArray(): Type
225: {
226: return new MixedType();
227: }
228:
229: public function shuffleArray(): Type
230: {
231: return $this;
232: }
233:
234: public function sliceArray(Type $offsetType, Type $lengthType, TrinaryLogic $preserveKeys): Type
235: {
236: if ((new ConstantIntegerType(0))->isSuperTypeOf($offsetType)->yes() && $lengthType->isNull()->yes()) {
237: return $this;
238: }
239:
240: return new MixedType();
241: }
242:
243: public function spliceArray(Type $offsetType, Type $lengthType, Type $replacementType): Type
244: {
245: if (
246: (new ConstantIntegerType(0))->isSuperTypeOf($lengthType)->yes()
247: || $replacementType->toArray()->isIterableAtLeastOnce()->yes()
248: ) {
249: return $this;
250: }
251:
252: return new MixedType();
253: }
254:
255: public function truncateListToSize(Type $sizeType): Type
256: {
257: // The accessory only asserts "this array is non-empty" — truncating
258: // to a positive size leaves that property in place.
259: return $this;
260: }
261:
262: public function makeListMaybe(): Type
263: {
264: // Non-emptiness is independent of list-ness; weaken-list keeps it.
265: return $this;
266: }
267:
268: public function mapValueType(callable $cb): Type
269: {
270: // Mapping doesn't change the entry count; non-emptiness is preserved.
271: return $this;
272: }
273:
274: public function mapKeyType(callable $cb): Type
275: {
276: return $this;
277: }
278:
279: public function makeAllArrayKeysOptional(): Type
280: {
281: // Without `ConstantArrayType` keys to mark optional, this is a no-op.
282: // Non-emptiness is unrelated to per-key optionality and is preserved.
283: return $this;
284: }
285:
286: public function changeKeyCaseArray(?int $case): Type
287: {
288: // Case-folding keys doesn't change the entry count.
289: return $this;
290: }
291:
292: public function filterArrayRemovingFalsey(): Type
293: {
294: // Filtering may leave the array empty — drop the assertion.
295: return new MixedType();
296: }
297:
298: public function isIterable(): TrinaryLogic
299: {
300: return TrinaryLogic::createYes();
301: }
302:
303: public function isIterableAtLeastOnce(): TrinaryLogic
304: {
305: return TrinaryLogic::createYes();
306: }
307:
308: public function getArraySize(): Type
309: {
310: return IntegerRangeType::fromInterval(1, null);
311: }
312:
313: public function getIterableKeyType(): Type
314: {
315: return new MixedType();
316: }
317:
318: public function getFirstIterableKeyType(): Type
319: {
320: return new MixedType();
321: }
322:
323: public function getLastIterableKeyType(): Type
324: {
325: return new MixedType();
326: }
327:
328: public function getIterableValueType(): Type
329: {
330: return new MixedType();
331: }
332:
333: public function getFirstIterableValueType(): Type
334: {
335: return new MixedType();
336: }
337:
338: public function getLastIterableValueType(): Type
339: {
340: return new MixedType();
341: }
342:
343: public function isArray(): TrinaryLogic
344: {
345: return TrinaryLogic::createYes();
346: }
347:
348: public function isConstantArray(): TrinaryLogic
349: {
350: return TrinaryLogic::createMaybe();
351: }
352:
353: public function isOversizedArray(): TrinaryLogic
354: {
355: return TrinaryLogic::createMaybe();
356: }
357:
358: public function isList(): TrinaryLogic
359: {
360: return TrinaryLogic::createMaybe();
361: }
362:
363: public function isNull(): TrinaryLogic
364: {
365: return TrinaryLogic::createNo();
366: }
367:
368: public function isConstantValue(): TrinaryLogic
369: {
370: return TrinaryLogic::createMaybe();
371: }
372:
373: public function isConstantScalarValue(): TrinaryLogic
374: {
375: return TrinaryLogic::createNo();
376: }
377:
378: public function getConstantScalarTypes(): array
379: {
380: return [];
381: }
382:
383: public function getConstantScalarValues(): array
384: {
385: return [];
386: }
387:
388: public function isTrue(): TrinaryLogic
389: {
390: return TrinaryLogic::createNo();
391: }
392:
393: public function isFalse(): TrinaryLogic
394: {
395: return TrinaryLogic::createNo();
396: }
397:
398: public function isBoolean(): TrinaryLogic
399: {
400: return TrinaryLogic::createNo();
401: }
402:
403: public function isFloat(): TrinaryLogic
404: {
405: return TrinaryLogic::createNo();
406: }
407:
408: public function isInteger(): TrinaryLogic
409: {
410: return TrinaryLogic::createNo();
411: }
412:
413: public function isString(): TrinaryLogic
414: {
415: return TrinaryLogic::createNo();
416: }
417:
418: public function isNumericString(): TrinaryLogic
419: {
420: return TrinaryLogic::createNo();
421: }
422:
423: public function isDecimalIntegerString(): TrinaryLogic
424: {
425: return TrinaryLogic::createNo();
426: }
427:
428: public function isNonEmptyString(): TrinaryLogic
429: {
430: return TrinaryLogic::createNo();
431: }
432:
433: public function isNonFalsyString(): TrinaryLogic
434: {
435: return TrinaryLogic::createNo();
436: }
437:
438: public function isLiteralString(): TrinaryLogic
439: {
440: return TrinaryLogic::createNo();
441: }
442:
443: public function isLowercaseString(): TrinaryLogic
444: {
445: return TrinaryLogic::createNo();
446: }
447:
448: public function isClassString(): TrinaryLogic
449: {
450: return TrinaryLogic::createNo();
451: }
452:
453: public function isUppercaseString(): TrinaryLogic
454: {
455: return TrinaryLogic::createNo();
456: }
457:
458: public function getClassStringObjectType(): Type
459: {
460: return new ErrorType();
461: }
462:
463: public function getObjectTypeOrClassStringObjectType(): Type
464: {
465: return new ErrorType();
466: }
467:
468: public function isVoid(): TrinaryLogic
469: {
470: return TrinaryLogic::createNo();
471: }
472:
473: public function isScalar(): TrinaryLogic
474: {
475: return TrinaryLogic::createNo();
476: }
477:
478: public function looseCompare(Type $type, PhpVersion $phpVersion): BooleanType
479: {
480: if ($type->isArray()->yes() && $type->isIterableAtLeastOnce()->no()) {
481: return new ConstantBooleanType(false);
482: }
483:
484: return new BooleanType();
485: }
486:
487: public function toNumber(): Type
488: {
489: return new ErrorType();
490: }
491:
492: public function toBitwiseNotType(): Type
493: {
494: return new ErrorType();
495: }
496:
497: public function toAbsoluteNumber(): Type
498: {
499: return new ErrorType();
500: }
501:
502: public function toInteger(): Type
503: {
504: return new ConstantIntegerType(1);
505: }
506:
507: public function toFloat(): Type
508: {
509: return new ConstantFloatType(1.0);
510: }
511:
512: public function toString(): Type
513: {
514: return new ErrorType();
515: }
516:
517: public function toArray(): Type
518: {
519: return $this;
520: }
521:
522: public function toArrayKey(): Type
523: {
524: return new ErrorType();
525: }
526:
527: public function toCoercedArgumentType(bool $strictTypes): Type
528: {
529: return $this;
530: }
531:
532: public function traverse(callable $cb): Type
533: {
534: return $this;
535: }
536:
537: public function traverseSimultaneously(Type $right, callable $cb): Type
538: {
539: return $this;
540: }
541:
542: public function exponentiate(Type $exponent): Type
543: {
544: return new ErrorType();
545: }
546:
547: public function getFiniteTypes(): array
548: {
549: return [];
550: }
551:
552: public function getDefaultBaseType(): Type
553: {
554: return new ArrayType(new MixedType(), new MixedType());
555: }
556:
557: public function toPhpDocNode(): TypeNode
558: {
559: return new IdentifierTypeNode('non-empty-array');
560: }
561:
562: public function hasTemplateOrLateResolvableType(): bool
563: {
564: return false;
565: }
566:
567: }
568: