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\Constant\ConstantStringType;
14: use PHPStan\Type\ErrorType;
15: use PHPStan\Type\FloatType;
16: use PHPStan\Type\GeneralizePrecision;
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\UndecidedBooleanTypeTrait;
27: use PHPStan\Type\Traits\UndecidedComparisonCompoundTypeTrait;
28: use PHPStan\Type\Type;
29: use PHPStan\Type\TypeCombinator;
30: use PHPStan\Type\UnionType;
31: use PHPStan\Type\VerbosityLevel;
32:
33: class AccessoryNonEmptyStringType implements CompoundType, AccessoryType
34: {
35:
36: use MaybeCallableTypeTrait;
37: use NonArrayTypeTrait;
38: use NonObjectTypeTrait;
39: use NonIterableTypeTrait;
40: use UndecidedComparisonCompoundTypeTrait;
41: use NonGenericTypeTrait;
42: use UndecidedBooleanTypeTrait;
43:
44: /** @api */
45: public function __construct()
46: {
47: }
48:
49: public function getReferencedClasses(): array
50: {
51: return [];
52: }
53:
54: public function getObjectClassNames(): array
55: {
56: return [];
57: }
58:
59: public function getObjectClassReflections(): array
60: {
61: return [];
62: }
63:
64: public function getConstantStrings(): array
65: {
66: return [];
67: }
68:
69: public function accepts(Type $type, bool $strictTypes): TrinaryLogic
70: {
71: return $this->acceptsWithReason($type, $strictTypes)->result;
72: }
73:
74: public function acceptsWithReason(Type $type, bool $strictTypes): AcceptsResult
75: {
76: if ($type instanceof CompoundType) {
77: return $type->isAcceptedWithReasonBy($this, $strictTypes);
78: }
79:
80: return new AcceptsResult($type->isNonEmptyString(), []);
81: }
82:
83: public function isSuperTypeOf(Type $type): TrinaryLogic
84: {
85: if ($type instanceof CompoundType) {
86: return $type->isSubTypeOf($this);
87: }
88:
89: if ($this->equals($type)) {
90: return TrinaryLogic::createYes();
91: }
92:
93: if ($type->isNonFalsyString()->yes()) {
94: return TrinaryLogic::createYes();
95: }
96:
97: return $type->isNonEmptyString();
98: }
99:
100: public function isSubTypeOf(Type $otherType): TrinaryLogic
101: {
102: if ($otherType instanceof UnionType || $otherType instanceof IntersectionType) {
103: return $otherType->isSuperTypeOf($this);
104: }
105:
106: return $otherType->isNonEmptyString()
107: ->and($otherType instanceof self ? TrinaryLogic::createYes() : TrinaryLogic::createMaybe());
108: }
109:
110: public function isAcceptedBy(Type $acceptingType, bool $strictTypes): TrinaryLogic
111: {
112: return $this->isAcceptedWithReasonBy($acceptingType, $strictTypes)->result;
113: }
114:
115: public function isAcceptedWithReasonBy(Type $acceptingType, bool $strictTypes): AcceptsResult
116: {
117: return new AcceptsResult($this->isSubTypeOf($acceptingType), []);
118: }
119:
120: public function equals(Type $type): bool
121: {
122: return $type instanceof self;
123: }
124:
125: public function describe(VerbosityLevel $level): string
126: {
127: return 'non-empty-string';
128: }
129:
130: public function isOffsetAccessible(): TrinaryLogic
131: {
132: return TrinaryLogic::createYes();
133: }
134:
135: public function hasOffsetValueType(Type $offsetType): TrinaryLogic
136: {
137: return (new IntegerType())->isSuperTypeOf($offsetType)->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: if ((new ConstantIntegerType(0))->isSuperTypeOf($offsetType)->yes()) {
147: return new IntersectionType([new StringType(), new AccessoryNonEmptyStringType()]);
148: }
149:
150: return new StringType();
151: }
152:
153: public function setOffsetValueType(?Type $offsetType, Type $valueType, bool $unionValues = true): Type
154: {
155: return $this;
156: }
157:
158: public function unsetOffset(Type $offsetType): Type
159: {
160: return new ErrorType();
161: }
162:
163: public function toNumber(): Type
164: {
165: return new ErrorType();
166: }
167:
168: public function toInteger(): Type
169: {
170: return new IntegerType();
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::createMaybe();
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 tryRemove(Type $typeToRemove): ?Type
320: {
321: if ($typeToRemove instanceof ConstantStringType && $typeToRemove->getValue() === '0') {
322: return TypeCombinator::intersect($this, new AccessoryNonFalsyStringType());
323: }
324:
325: return null;
326: }
327:
328: public function exponentiate(Type $exponent): Type
329: {
330: return new BenevolentUnionType([
331: new FloatType(),
332: new IntegerType(),
333: ]);
334: }
335:
336: }
337: