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