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