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\Constant\ConstantStringType;
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\StringType;
24: use PHPStan\Type\Traits\MaybeCallableTypeTrait;
25: use PHPStan\Type\Traits\NonArrayTypeTrait;
26: use PHPStan\Type\Traits\NonGenericTypeTrait;
27: use PHPStan\Type\Traits\NonIterableTypeTrait;
28: use PHPStan\Type\Traits\NonObjectTypeTrait;
29: use PHPStan\Type\Traits\UndecidedBooleanTypeTrait;
30: use PHPStan\Type\Traits\UndecidedComparisonCompoundTypeTrait;
31: use PHPStan\Type\Type;
32: use PHPStan\Type\TypeCombinator;
33: use PHPStan\Type\UnionType;
34: use PHPStan\Type\VerbosityLevel;
35:
36: class AccessoryNonEmptyStringType implements CompoundType, AccessoryType
37: {
38:
39: use MaybeCallableTypeTrait;
40: use NonArrayTypeTrait;
41: use NonObjectTypeTrait;
42: use NonIterableTypeTrait;
43: use UndecidedComparisonCompoundTypeTrait;
44: use NonGenericTypeTrait;
45: use UndecidedBooleanTypeTrait;
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->isNonEmptyString(), []);
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: if ($type->isNonFalsyString()->yes()) {
92: return IsSuperTypeOfResult::createYes();
93: }
94:
95: return new IsSuperTypeOfResult($type->isNonEmptyString(), []);
96: }
97:
98: public function isSubTypeOf(Type $otherType): IsSuperTypeOfResult
99: {
100: if ($otherType instanceof UnionType || $otherType instanceof IntersectionType) {
101: return $otherType->isSuperTypeOf($this);
102: }
103:
104: return (new IsSuperTypeOfResult($otherType->isNonEmptyString(), []))
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-empty-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: if ((new ConstantIntegerType(0))->isSuperTypeOf($offsetType)->yes()) {
145: return new IntersectionType([new StringType(), new AccessoryNonEmptyStringType()]);
146: }
147:
148: return new StringType();
149: }
150:
151: public function setOffsetValueType(?Type $offsetType, Type $valueType, bool $unionValues = true): Type
152: {
153: $stringOffset = (new StringType())->setOffsetValueType($offsetType, $valueType, $unionValues);
154:
155: if ($stringOffset instanceof ErrorType) {
156: return $stringOffset;
157: }
158:
159: return $this;
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::createMaybe();
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 isUppercaseString(): TrinaryLogic
299: {
300: return TrinaryLogic::createMaybe();
301: }
302:
303: public function getClassStringObjectType(): Type
304: {
305: return new ObjectWithoutClassType();
306: }
307:
308: public function getObjectTypeOrClassStringObjectType(): Type
309: {
310: return new ObjectWithoutClassType();
311: }
312:
313: public function isVoid(): TrinaryLogic
314: {
315: return TrinaryLogic::createNo();
316: }
317:
318: public function isScalar(): TrinaryLogic
319: {
320: return TrinaryLogic::createYes();
321: }
322:
323: public function looseCompare(Type $type, PhpVersion $phpVersion): BooleanType
324: {
325: return new BooleanType();
326: }
327:
328: public function traverse(callable $cb): Type
329: {
330: return $this;
331: }
332:
333: public function traverseSimultaneously(Type $right, callable $cb): Type
334: {
335: return $this;
336: }
337:
338: public function generalize(GeneralizePrecision $precision): Type
339: {
340: return new StringType();
341: }
342:
343: public function tryRemove(Type $typeToRemove): ?Type
344: {
345: if ($typeToRemove instanceof ConstantStringType && $typeToRemove->getValue() === '0') {
346: return TypeCombinator::intersect($this, new AccessoryNonFalsyStringType());
347: }
348:
349: return null;
350: }
351:
352: public function exponentiate(Type $exponent): Type
353: {
354: return new BenevolentUnionType([
355: new FloatType(),
356: new IntegerType(),
357: ]);
358: }
359:
360: public function getFiniteTypes(): array
361: {
362: return [];
363: }
364:
365: public function toPhpDocNode(): TypeNode
366: {
367: return new IdentifierTypeNode('non-empty-string');
368: }
369:
370: }
371: