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