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