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\NullType;
28: use PHPStan\Type\ObjectWithoutClassType;
29: use PHPStan\Type\StringType;
30: use PHPStan\Type\Traits\MaybeCallableTypeTrait;
31: use PHPStan\Type\Traits\NonArrayTypeTrait;
32: use PHPStan\Type\Traits\NonGenericTypeTrait;
33: use PHPStan\Type\Traits\NonIterableTypeTrait;
34: use PHPStan\Type\Traits\NonObjectTypeTrait;
35: use PHPStan\Type\Traits\NonRemoveableTypeTrait;
36: use PHPStan\Type\Traits\TruthyBooleanTypeTrait;
37: use PHPStan\Type\Traits\UndecidedComparisonCompoundTypeTrait;
38: use PHPStan\Type\Type;
39: use PHPStan\Type\TypeCombinator;
40: use PHPStan\Type\UnionType;
41: use PHPStan\Type\VerbosityLevel;
42:
43: #[InstanceofDeprecated(insteadUse: 'Type::isNonFalsyString()')]
44: #[ShadowedByTurboExtension(implementation: __DIR__ . '/../../../turbo-ext/src/AccessoryNonFalsyStringType.cpp')]
45: class AccessoryNonFalsyStringType implements CompoundType, AccessoryType
46: {
47:
48: use MaybeCallableTypeTrait;
49: use NonArrayTypeTrait;
50: use NonObjectTypeTrait;
51: use NonIterableTypeTrait;
52: use TruthyBooleanTypeTrait;
53: use UndecidedComparisonCompoundTypeTrait;
54: use NonGenericTypeTrait;
55: use NonRemoveableTypeTrait;
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: $isNonFalsyString = $type->isNonFalsyString();
85:
86: if ($isNonFalsyString->yes()) {
87: return AcceptsResult::createYes();
88: }
89:
90: if ($type instanceof CompoundType) {
91: return $type->isAcceptedBy($this, $strictTypes);
92: }
93:
94: return new AcceptsResult($isNonFalsyString, []);
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: return IsSuperTypeOfResult::createFromTrinaryLogic($type->isNonFalsyString());
108: }
109:
110: public function isSubTypeOf(Type $otherType): IsSuperTypeOfResult
111: {
112: if ($otherType instanceof UnionType || $otherType instanceof IntersectionType) {
113: return $otherType->isSuperTypeOf($this);
114: }
115:
116: if ($otherType instanceof AccessoryNonEmptyStringType) {
117: return IsSuperTypeOfResult::createYes();
118: }
119:
120: return IsSuperTypeOfResult::createFromTrinaryLogic(
121: $otherType->isNonFalsyString()->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-falsy-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: return new StringType();
162: }
163:
164: public function setOffsetValueType(?Type $offsetType, Type $valueType, bool $unionValues = true): Type
165: {
166: $stringOffset = (new StringType())->setOffsetValueType($offsetType, $valueType, $unionValues);
167:
168: if ($stringOffset instanceof ErrorType) {
169: return $stringOffset;
170: }
171:
172: if ($valueType->isNonFalsyString()->yes()) {
173: return $this;
174: }
175:
176: return new StringType();
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 AccessoryNonEmptyStringType()]);
197: }
198:
199: public function toAbsoluteNumber(): Type
200: {
201: return new ErrorType();
202: }
203:
204: public function toInteger(): Type
205: {
206: // Do not remove `0` since `(int) '00'` is still `0`.
207: return new IntegerType();
208: }
209:
210: public function toFloat(): Type
211: {
212: return new FloatType();
213: }
214:
215: public function toString(): Type
216: {
217: return $this;
218: }
219:
220: public function toArray(): Type
221: {
222: return new ConstantArrayType(
223: [new ConstantIntegerType(0)],
224: [$this],
225: [1],
226: isList: TrinaryLogic::createYes(),
227: );
228: }
229:
230: public function toArrayKey(): Type
231: {
232: return $this;
233: }
234:
235: public function toCoercedArgumentType(bool $strictTypes): Type
236: {
237: if (!$strictTypes) {
238: return TypeCombinator::union($this->toInteger(), $this->toFloat(), $this, $this->toBoolean());
239: }
240:
241: return $this;
242: }
243:
244: public function isNull(): TrinaryLogic
245: {
246: return TrinaryLogic::createNo();
247: }
248:
249: public function isConstantValue(): TrinaryLogic
250: {
251: return TrinaryLogic::createMaybe();
252: }
253:
254: public function isConstantScalarValue(): TrinaryLogic
255: {
256: return TrinaryLogic::createMaybe();
257: }
258:
259: public function getConstantScalarTypes(): array
260: {
261: return [];
262: }
263:
264: public function getConstantScalarValues(): array
265: {
266: return [];
267: }
268:
269: public function isTrue(): TrinaryLogic
270: {
271: return TrinaryLogic::createNo();
272: }
273:
274: public function isFalse(): TrinaryLogic
275: {
276: return TrinaryLogic::createNo();
277: }
278:
279: public function isBoolean(): TrinaryLogic
280: {
281: return TrinaryLogic::createNo();
282: }
283:
284: public function isFloat(): TrinaryLogic
285: {
286: return TrinaryLogic::createNo();
287: }
288:
289: public function isInteger(): TrinaryLogic
290: {
291: return TrinaryLogic::createNo();
292: }
293:
294: public function isString(): TrinaryLogic
295: {
296: return TrinaryLogic::createYes();
297: }
298:
299: public function isNumericString(): TrinaryLogic
300: {
301: return TrinaryLogic::createMaybe();
302: }
303:
304: public function isDecimalIntegerString(): TrinaryLogic
305: {
306: return TrinaryLogic::createMaybe();
307: }
308:
309: public function isNonEmptyString(): TrinaryLogic
310: {
311: return TrinaryLogic::createYes();
312: }
313:
314: public function isNonFalsyString(): TrinaryLogic
315: {
316: return TrinaryLogic::createYes();
317: }
318:
319: public function isLiteralString(): TrinaryLogic
320: {
321: return TrinaryLogic::createMaybe();
322: }
323:
324: public function isLowercaseString(): TrinaryLogic
325: {
326: return TrinaryLogic::createMaybe();
327: }
328:
329: public function isClassString(): TrinaryLogic
330: {
331: return TrinaryLogic::createMaybe();
332: }
333:
334: public function isUppercaseString(): TrinaryLogic
335: {
336: return TrinaryLogic::createMaybe();
337: }
338:
339: public function getClassStringObjectType(): Type
340: {
341: return new ObjectWithoutClassType();
342: }
343:
344: public function getObjectTypeOrClassStringObjectType(): Type
345: {
346: return new ObjectWithoutClassType();
347: }
348:
349: public function isVoid(): TrinaryLogic
350: {
351: return TrinaryLogic::createNo();
352: }
353:
354: public function isScalar(): TrinaryLogic
355: {
356: return TrinaryLogic::createYes();
357: }
358:
359: public function looseCompare(Type $type, PhpVersion $phpVersion): BooleanType
360: {
361: $dominated = TypeCombinator::union(
362: new NullType(),
363: new ConstantBooleanType(false),
364: new ConstantStringType(''),
365: new ConstantArrayType([], []),
366: );
367: if ($dominated->isSuperTypeOf($type)->yes()) {
368: return new ConstantBooleanType(false);
369: }
370:
371: return new BooleanType();
372: }
373:
374: public function traverse(callable $cb): Type
375: {
376: return $this;
377: }
378:
379: public function traverseSimultaneously(Type $right, callable $cb): Type
380: {
381: return $this;
382: }
383:
384: public function generalize(GeneralizePrecision $precision): Type
385: {
386: return new StringType();
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-falsy-string');
410: }
411:
412: public function toNativePhpTypeNode(NativeTypePosition $position)
413: {
414: return $position->isScalarAllowed() ? new Identifier('string') : null;
415: }
416:
417: public function hasTemplateOrLateResolvableType(): bool
418: {
419: return false;
420: }
421:
422: }
423: