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