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