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