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