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