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