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\ConstantFloatType;
13: use PHPStan\Type\Constant\ConstantIntegerType;
14: use PHPStan\Type\ErrorType;
15: use PHPStan\Type\IntegerRangeType;
16: use PHPStan\Type\IntersectionType;
17: use PHPStan\Type\MixedType;
18: use PHPStan\Type\Traits\MaybeCallableTypeTrait;
19: use PHPStan\Type\Traits\NonGeneralizableTypeTrait;
20: use PHPStan\Type\Traits\NonGenericTypeTrait;
21: use PHPStan\Type\Traits\NonObjectTypeTrait;
22: use PHPStan\Type\Traits\NonRemoveableTypeTrait;
23: use PHPStan\Type\Traits\TruthyBooleanTypeTrait;
24: use PHPStan\Type\Traits\UndecidedComparisonCompoundTypeTrait;
25: use PHPStan\Type\Type;
26: use PHPStan\Type\UnionType;
27: use PHPStan\Type\VerbosityLevel;
28:
29: class NonEmptyArrayType implements CompoundType, AccessoryType
30: {
31:
32: use MaybeCallableTypeTrait;
33: use NonObjectTypeTrait;
34: use TruthyBooleanTypeTrait;
35: use NonGenericTypeTrait;
36: use UndecidedComparisonCompoundTypeTrait;
37: use NonRemoveableTypeTrait;
38: use NonGeneralizableTypeTrait;
39:
40: /** @api */
41: public function __construct()
42: {
43: }
44:
45: public function getReferencedClasses(): array
46: {
47: return [];
48: }
49:
50: public function getObjectClassNames(): array
51: {
52: return [];
53: }
54:
55: public function getObjectClassReflections(): array
56: {
57: return [];
58: }
59:
60: public function getArrays(): array
61: {
62: return [];
63: }
64:
65: public function getConstantArrays(): array
66: {
67: return [];
68: }
69:
70: public function getConstantStrings(): array
71: {
72: return [];
73: }
74:
75: public function accepts(Type $type, bool $strictTypes): TrinaryLogic
76: {
77: return $this->acceptsWithReason($type, $strictTypes)->result;
78: }
79:
80: public function acceptsWithReason(Type $type, bool $strictTypes): AcceptsResult
81: {
82: if ($type instanceof CompoundType) {
83: return $type->isAcceptedWithReasonBy($this, $strictTypes);
84: }
85:
86: $isArray = $type->isArray();
87: $isIterableAtLeastOnce = $type->isIterableAtLeastOnce();
88:
89: return new AcceptsResult($isArray->and($isIterableAtLeastOnce), []);
90: }
91:
92: public function isSuperTypeOf(Type $type): TrinaryLogic
93: {
94: if ($this->equals($type)) {
95: return TrinaryLogic::createYes();
96: }
97:
98: if ($type instanceof CompoundType) {
99: return $type->isSubTypeOf($this);
100: }
101:
102: return $type->isArray()
103: ->and($type->isIterableAtLeastOnce());
104: }
105:
106: public function isSubTypeOf(Type $otherType): TrinaryLogic
107: {
108: if ($otherType instanceof UnionType || $otherType instanceof IntersectionType) {
109: return $otherType->isSuperTypeOf($this);
110: }
111:
112: return $otherType->isArray()
113: ->and($otherType->isIterableAtLeastOnce())
114: ->and($otherType instanceof self ? TrinaryLogic::createYes() : TrinaryLogic::createMaybe());
115: }
116:
117: public function isAcceptedBy(Type $acceptingType, bool $strictTypes): TrinaryLogic
118: {
119: return $this->isAcceptedWithReasonBy($acceptingType, $strictTypes)->result;
120: }
121:
122: public function isAcceptedWithReasonBy(Type $acceptingType, bool $strictTypes): AcceptsResult
123: {
124: return new AcceptsResult($this->isSubTypeOf($acceptingType), []);
125: }
126:
127: public function equals(Type $type): bool
128: {
129: return $type instanceof self;
130: }
131:
132: public function describe(VerbosityLevel $level): string
133: {
134: return 'non-empty-array';
135: }
136:
137: public function isOffsetAccessible(): 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 getKeysArray(): Type
168: {
169: return $this;
170: }
171:
172: public function getValuesArray(): 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 searchArray(Type $needleType): Type
198: {
199: return new MixedType();
200: }
201:
202: public function shiftArray(): Type
203: {
204: return new MixedType();
205: }
206:
207: public function shuffleArray(): Type
208: {
209: return $this;
210: }
211:
212: public function isIterable(): TrinaryLogic
213: {
214: return TrinaryLogic::createYes();
215: }
216:
217: public function isIterableAtLeastOnce(): TrinaryLogic
218: {
219: return TrinaryLogic::createYes();
220: }
221:
222: public function getArraySize(): Type
223: {
224: return IntegerRangeType::fromInterval(1, null);
225: }
226:
227: public function getIterableKeyType(): Type
228: {
229: return new MixedType();
230: }
231:
232: public function getFirstIterableKeyType(): Type
233: {
234: return new MixedType();
235: }
236:
237: public function getLastIterableKeyType(): Type
238: {
239: return new MixedType();
240: }
241:
242: public function getIterableValueType(): Type
243: {
244: return new MixedType();
245: }
246:
247: public function getFirstIterableValueType(): Type
248: {
249: return new MixedType();
250: }
251:
252: public function getLastIterableValueType(): Type
253: {
254: return new MixedType();
255: }
256:
257: public function isArray(): TrinaryLogic
258: {
259: return TrinaryLogic::createYes();
260: }
261:
262: public function isConstantArray(): TrinaryLogic
263: {
264: return TrinaryLogic::createMaybe();
265: }
266:
267: public function isOversizedArray(): TrinaryLogic
268: {
269: return TrinaryLogic::createMaybe();
270: }
271:
272: public function isList(): TrinaryLogic
273: {
274: return TrinaryLogic::createMaybe();
275: }
276:
277: public function isNull(): TrinaryLogic
278: {
279: return TrinaryLogic::createNo();
280: }
281:
282: public function isConstantValue(): TrinaryLogic
283: {
284: return TrinaryLogic::createMaybe();
285: }
286:
287: public function isConstantScalarValue(): TrinaryLogic
288: {
289: return TrinaryLogic::createNo();
290: }
291:
292: public function getConstantScalarTypes(): array
293: {
294: return [];
295: }
296:
297: public function getConstantScalarValues(): array
298: {
299: return [];
300: }
301:
302: public function isTrue(): TrinaryLogic
303: {
304: return TrinaryLogic::createNo();
305: }
306:
307: public function isFalse(): TrinaryLogic
308: {
309: return TrinaryLogic::createNo();
310: }
311:
312: public function isBoolean(): TrinaryLogic
313: {
314: return TrinaryLogic::createNo();
315: }
316:
317: public function isFloat(): TrinaryLogic
318: {
319: return TrinaryLogic::createNo();
320: }
321:
322: public function isInteger(): TrinaryLogic
323: {
324: return TrinaryLogic::createNo();
325: }
326:
327: public function isString(): TrinaryLogic
328: {
329: return TrinaryLogic::createNo();
330: }
331:
332: public function isNumericString(): TrinaryLogic
333: {
334: return TrinaryLogic::createNo();
335: }
336:
337: public function isNonEmptyString(): TrinaryLogic
338: {
339: return TrinaryLogic::createNo();
340: }
341:
342: public function isNonFalsyString(): TrinaryLogic
343: {
344: return TrinaryLogic::createNo();
345: }
346:
347: public function isLiteralString(): TrinaryLogic
348: {
349: return TrinaryLogic::createNo();
350: }
351:
352: public function isClassStringType(): TrinaryLogic
353: {
354: return TrinaryLogic::createNo();
355: }
356:
357: public function getClassStringObjectType(): Type
358: {
359: return new ErrorType();
360: }
361:
362: public function getObjectTypeOrClassStringObjectType(): Type
363: {
364: return new ErrorType();
365: }
366:
367: public function isVoid(): TrinaryLogic
368: {
369: return TrinaryLogic::createNo();
370: }
371:
372: public function isScalar(): TrinaryLogic
373: {
374: return TrinaryLogic::createNo();
375: }
376:
377: public function looseCompare(Type $type, PhpVersion $phpVersion): BooleanType
378: {
379: return new BooleanType();
380: }
381:
382: public function toNumber(): Type
383: {
384: return new ErrorType();
385: }
386:
387: public function toInteger(): Type
388: {
389: return new ConstantIntegerType(1);
390: }
391:
392: public function toFloat(): Type
393: {
394: return new ConstantFloatType(1.0);
395: }
396:
397: public function toString(): Type
398: {
399: return new ErrorType();
400: }
401:
402: public function toArray(): Type
403: {
404: return $this;
405: }
406:
407: public function toArrayKey(): Type
408: {
409: return new ErrorType();
410: }
411:
412: public function traverse(callable $cb): Type
413: {
414: return $this;
415: }
416:
417: public function traverseSimultaneously(Type $right, callable $cb): Type
418: {
419: return $this;
420: }
421:
422: public function exponentiate(Type $exponent): Type
423: {
424: return new ErrorType();
425: }
426:
427: public function getFiniteTypes(): array
428: {
429: return [];
430: }
431:
432: public static function __set_state(array $properties): Type
433: {
434: return new self();
435: }
436:
437: public function toPhpDocNode(): TypeNode
438: {
439: return new IdentifierTypeNode('non-empty-array');
440: }
441:
442: }
443: