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\BenevolentUnionType; |
11: | use PHPStan\Type\BooleanType; |
12: | use PHPStan\Type\CompoundType; |
13: | use PHPStan\Type\Constant\ConstantArrayType; |
14: | use PHPStan\Type\Constant\ConstantBooleanType; |
15: | use PHPStan\Type\Constant\ConstantIntegerType; |
16: | use PHPStan\Type\ErrorType; |
17: | use PHPStan\Type\FloatType; |
18: | use PHPStan\Type\GeneralizePrecision; |
19: | use PHPStan\Type\IntegerType; |
20: | use PHPStan\Type\IntersectionType; |
21: | use PHPStan\Type\IsSuperTypeOfResult; |
22: | use PHPStan\Type\ObjectWithoutClassType; |
23: | use PHPStan\Type\StaticTypeFactory; |
24: | use PHPStan\Type\StringType; |
25: | use PHPStan\Type\Traits\MaybeCallableTypeTrait; |
26: | use PHPStan\Type\Traits\NonArrayTypeTrait; |
27: | use PHPStan\Type\Traits\NonGenericTypeTrait; |
28: | use PHPStan\Type\Traits\NonIterableTypeTrait; |
29: | use PHPStan\Type\Traits\NonObjectTypeTrait; |
30: | use PHPStan\Type\Traits\NonRemoveableTypeTrait; |
31: | use PHPStan\Type\Traits\TruthyBooleanTypeTrait; |
32: | use PHPStan\Type\Traits\UndecidedComparisonCompoundTypeTrait; |
33: | use PHPStan\Type\Type; |
34: | use PHPStan\Type\TypeCombinator; |
35: | use PHPStan\Type\UnionType; |
36: | use PHPStan\Type\VerbosityLevel; |
37: | |
38: | class AccessoryNonFalsyStringType implements CompoundType, AccessoryType |
39: | { |
40: | |
41: | use MaybeCallableTypeTrait; |
42: | use NonArrayTypeTrait; |
43: | use NonObjectTypeTrait; |
44: | use NonIterableTypeTrait; |
45: | use TruthyBooleanTypeTrait; |
46: | use UndecidedComparisonCompoundTypeTrait; |
47: | use NonGenericTypeTrait; |
48: | use NonRemoveableTypeTrait; |
49: | |
50: | |
51: | public function __construct() |
52: | { |
53: | } |
54: | |
55: | public function getReferencedClasses(): array |
56: | { |
57: | return []; |
58: | } |
59: | |
60: | public function getObjectClassNames(): array |
61: | { |
62: | return []; |
63: | } |
64: | |
65: | public function getObjectClassReflections(): array |
66: | { |
67: | return []; |
68: | } |
69: | |
70: | public function getConstantStrings(): array |
71: | { |
72: | return []; |
73: | } |
74: | |
75: | public function accepts(Type $type, bool $strictTypes): AcceptsResult |
76: | { |
77: | if ($type instanceof CompoundType) { |
78: | return $type->isAcceptedBy($this, $strictTypes); |
79: | } |
80: | |
81: | return new AcceptsResult($type->isNonFalsyString(), []); |
82: | } |
83: | |
84: | public function isSuperTypeOf(Type $type): IsSuperTypeOfResult |
85: | { |
86: | if ($type instanceof CompoundType) { |
87: | return $type->isSubTypeOf($this); |
88: | } |
89: | |
90: | if ($this->equals($type)) { |
91: | return IsSuperTypeOfResult::createYes(); |
92: | } |
93: | |
94: | return new IsSuperTypeOfResult($type->isNonFalsyString(), []); |
95: | } |
96: | |
97: | public function isSubTypeOf(Type $otherType): IsSuperTypeOfResult |
98: | { |
99: | if ($otherType instanceof UnionType || $otherType instanceof IntersectionType) { |
100: | return $otherType->isSuperTypeOf($this); |
101: | } |
102: | |
103: | if ($otherType instanceof AccessoryNonEmptyStringType) { |
104: | return IsSuperTypeOfResult::createYes(); |
105: | } |
106: | |
107: | return (new IsSuperTypeOfResult($otherType->isNonFalsyString(), [])) |
108: | ->and($otherType instanceof self ? IsSuperTypeOfResult::createYes() : IsSuperTypeOfResult::createMaybe()); |
109: | } |
110: | |
111: | public function isAcceptedBy(Type $acceptingType, bool $strictTypes): AcceptsResult |
112: | { |
113: | return $this->isSubTypeOf($acceptingType)->toAcceptsResult(); |
114: | } |
115: | |
116: | public function equals(Type $type): bool |
117: | { |
118: | return $type instanceof self; |
119: | } |
120: | |
121: | public function describe(VerbosityLevel $level): string |
122: | { |
123: | return 'non-falsy-string'; |
124: | } |
125: | |
126: | public function isOffsetAccessible(): TrinaryLogic |
127: | { |
128: | return TrinaryLogic::createYes(); |
129: | } |
130: | |
131: | public function isOffsetAccessLegal(): TrinaryLogic |
132: | { |
133: | return TrinaryLogic::createYes(); |
134: | } |
135: | |
136: | public function hasOffsetValueType(Type $offsetType): TrinaryLogic |
137: | { |
138: | return $offsetType->isInteger()->and(TrinaryLogic::createMaybe()); |
139: | } |
140: | |
141: | public function getOffsetValueType(Type $offsetType): Type |
142: | { |
143: | if ($this->hasOffsetValueType($offsetType)->no()) { |
144: | return new ErrorType(); |
145: | } |
146: | |
147: | return new StringType(); |
148: | } |
149: | |
150: | public function setOffsetValueType(?Type $offsetType, Type $valueType, bool $unionValues = true): Type |
151: | { |
152: | $stringOffset = (new StringType())->setOffsetValueType($offsetType, $valueType, $unionValues); |
153: | |
154: | if ($stringOffset instanceof ErrorType) { |
155: | return $stringOffset; |
156: | } |
157: | |
158: | if ($valueType->isNonFalsyString()->yes()) { |
159: | return $this; |
160: | } |
161: | |
162: | return new StringType(); |
163: | } |
164: | |
165: | public function setExistingOffsetValueType(Type $offsetType, Type $valueType): Type |
166: | { |
167: | return $this; |
168: | } |
169: | |
170: | public function unsetOffset(Type $offsetType): Type |
171: | { |
172: | return new ErrorType(); |
173: | } |
174: | |
175: | public function toNumber(): Type |
176: | { |
177: | return new ErrorType(); |
178: | } |
179: | |
180: | public function toAbsoluteNumber(): Type |
181: | { |
182: | return new ErrorType(); |
183: | } |
184: | |
185: | public function toInteger(): Type |
186: | { |
187: | return TypeCombinator::remove(new IntegerType(), new ConstantIntegerType(0)); |
188: | } |
189: | |
190: | public function toFloat(): Type |
191: | { |
192: | return new FloatType(); |
193: | } |
194: | |
195: | public function toString(): Type |
196: | { |
197: | return $this; |
198: | } |
199: | |
200: | public function toArray(): Type |
201: | { |
202: | return new ConstantArrayType( |
203: | [new ConstantIntegerType(0)], |
204: | [$this], |
205: | [1], |
206: | isList: TrinaryLogic::createYes(), |
207: | ); |
208: | } |
209: | |
210: | public function toArrayKey(): Type |
211: | { |
212: | return $this; |
213: | } |
214: | |
215: | public function toCoercedArgumentType(bool $strictTypes): Type |
216: | { |
217: | if (!$strictTypes) { |
218: | return TypeCombinator::union($this->toInteger(), $this->toFloat(), $this, $this->toBoolean()); |
219: | } |
220: | |
221: | return $this; |
222: | } |
223: | |
224: | public function isNull(): TrinaryLogic |
225: | { |
226: | return TrinaryLogic::createNo(); |
227: | } |
228: | |
229: | public function isConstantValue(): TrinaryLogic |
230: | { |
231: | return TrinaryLogic::createMaybe(); |
232: | } |
233: | |
234: | public function isConstantScalarValue(): TrinaryLogic |
235: | { |
236: | return TrinaryLogic::createMaybe(); |
237: | } |
238: | |
239: | public function getConstantScalarTypes(): array |
240: | { |
241: | return []; |
242: | } |
243: | |
244: | public function getConstantScalarValues(): array |
245: | { |
246: | return []; |
247: | } |
248: | |
249: | public function isTrue(): TrinaryLogic |
250: | { |
251: | return TrinaryLogic::createNo(); |
252: | } |
253: | |
254: | public function isFalse(): TrinaryLogic |
255: | { |
256: | return TrinaryLogic::createNo(); |
257: | } |
258: | |
259: | public function isBoolean(): TrinaryLogic |
260: | { |
261: | return TrinaryLogic::createNo(); |
262: | } |
263: | |
264: | public function isFloat(): TrinaryLogic |
265: | { |
266: | return TrinaryLogic::createNo(); |
267: | } |
268: | |
269: | public function isInteger(): TrinaryLogic |
270: | { |
271: | return TrinaryLogic::createNo(); |
272: | } |
273: | |
274: | public function isString(): TrinaryLogic |
275: | { |
276: | return TrinaryLogic::createYes(); |
277: | } |
278: | |
279: | public function isNumericString(): TrinaryLogic |
280: | { |
281: | return TrinaryLogic::createMaybe(); |
282: | } |
283: | |
284: | public function isNonEmptyString(): TrinaryLogic |
285: | { |
286: | return TrinaryLogic::createYes(); |
287: | } |
288: | |
289: | public function isNonFalsyString(): TrinaryLogic |
290: | { |
291: | return TrinaryLogic::createYes(); |
292: | } |
293: | |
294: | public function isLiteralString(): TrinaryLogic |
295: | { |
296: | return TrinaryLogic::createMaybe(); |
297: | } |
298: | |
299: | public function isLowercaseString(): TrinaryLogic |
300: | { |
301: | return TrinaryLogic::createMaybe(); |
302: | } |
303: | |
304: | public function isClassString(): TrinaryLogic |
305: | { |
306: | return TrinaryLogic::createMaybe(); |
307: | } |
308: | |
309: | public function isUppercaseString(): TrinaryLogic |
310: | { |
311: | return TrinaryLogic::createMaybe(); |
312: | } |
313: | |
314: | public function getClassStringObjectType(): Type |
315: | { |
316: | return new ObjectWithoutClassType(); |
317: | } |
318: | |
319: | public function getObjectTypeOrClassStringObjectType(): Type |
320: | { |
321: | return new ObjectWithoutClassType(); |
322: | } |
323: | |
324: | public function isVoid(): TrinaryLogic |
325: | { |
326: | return TrinaryLogic::createNo(); |
327: | } |
328: | |
329: | public function isScalar(): TrinaryLogic |
330: | { |
331: | return TrinaryLogic::createYes(); |
332: | } |
333: | |
334: | public function looseCompare(Type $type, PhpVersion $phpVersion): BooleanType |
335: | { |
336: | $falseyTypes = StaticTypeFactory::falsey(); |
337: | if ($falseyTypes->isSuperTypeOf($type)->yes()) { |
338: | return new ConstantBooleanType(false); |
339: | } |
340: | |
341: | return new BooleanType(); |
342: | } |
343: | |
344: | public function traverse(callable $cb): Type |
345: | { |
346: | return $this; |
347: | } |
348: | |
349: | public function traverseSimultaneously(Type $right, callable $cb): Type |
350: | { |
351: | return $this; |
352: | } |
353: | |
354: | public function generalize(GeneralizePrecision $precision): Type |
355: | { |
356: | return new StringType(); |
357: | } |
358: | |
359: | public function exponentiate(Type $exponent): Type |
360: | { |
361: | return new BenevolentUnionType([ |
362: | new FloatType(), |
363: | new IntegerType(), |
364: | ]); |
365: | } |
366: | |
367: | public function getFiniteTypes(): array |
368: | { |
369: | return []; |
370: | } |
371: | |
372: | public function toPhpDocNode(): TypeNode |
373: | { |
374: | return new IdentifierTypeNode('non-falsy-string'); |
375: | } |
376: | |
377: | } |
378: | |