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