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