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