1: <?php declare(strict_types = 1);
2:
3: namespace PHPStan\Type\Accessory;
4:
5: use PHPStan\TrinaryLogic;
6: use PHPStan\Type\CompoundType;
7: use PHPStan\Type\Constant\ConstantArrayType;
8: use PHPStan\Type\Constant\ConstantIntegerType;
9: use PHPStan\Type\ErrorType;
10: use PHPStan\Type\FloatType;
11: use PHPStan\Type\GeneralizePrecision;
12: use PHPStan\Type\IntegerRangeType;
13: use PHPStan\Type\IntegerType;
14: use PHPStan\Type\IntersectionType;
15: use PHPStan\Type\StringType;
16: use PHPStan\Type\Traits\MaybeCallableTypeTrait;
17: use PHPStan\Type\Traits\NonArrayTypeTrait;
18: use PHPStan\Type\Traits\NonGenericTypeTrait;
19: use PHPStan\Type\Traits\NonIterableTypeTrait;
20: use PHPStan\Type\Traits\NonObjectTypeTrait;
21: use PHPStan\Type\Traits\NonRemoveableTypeTrait;
22: use PHPStan\Type\Traits\TruthyBooleanTypeTrait;
23: use PHPStan\Type\Traits\UndecidedComparisonCompoundTypeTrait;
24: use PHPStan\Type\Type;
25: use PHPStan\Type\UnionType;
26: use PHPStan\Type\VerbosityLevel;
27:
28: class AccessoryNonFalsyStringType implements CompoundType, AccessoryType
29: {
30:
31: use MaybeCallableTypeTrait;
32: use NonArrayTypeTrait;
33: use NonObjectTypeTrait;
34: use NonIterableTypeTrait;
35: use TruthyBooleanTypeTrait;
36: use UndecidedComparisonCompoundTypeTrait;
37: use NonGenericTypeTrait;
38: use NonRemoveableTypeTrait;
39:
40: /** @api */
41: public function __construct()
42: {
43: }
44:
45: public function getReferencedClasses(): array
46: {
47: return [];
48: }
49:
50: public function getConstantStrings(): array
51: {
52: return [];
53: }
54:
55: public function accepts(Type $type, bool $strictTypes): TrinaryLogic
56: {
57: if ($type instanceof CompoundType) {
58: return $type->isAcceptedBy($this, $strictTypes);
59: }
60:
61: return $type->isNonFalsyString();
62: }
63:
64: public function isSuperTypeOf(Type $type): TrinaryLogic
65: {
66: if ($type instanceof CompoundType) {
67: return $type->isSubTypeOf($this);
68: }
69:
70: if ($this->equals($type)) {
71: return TrinaryLogic::createYes();
72: }
73:
74: return $type->isNonFalsyString();
75: }
76:
77: public function isSubTypeOf(Type $otherType): TrinaryLogic
78: {
79: if ($otherType instanceof UnionType || $otherType instanceof IntersectionType) {
80: return $otherType->isSuperTypeOf($this);
81: }
82:
83: if ($otherType instanceof AccessoryNonEmptyStringType) {
84: return TrinaryLogic::createYes();
85: }
86:
87: return $otherType->isNonFalsyString()
88: ->and($otherType instanceof self ? TrinaryLogic::createYes() : TrinaryLogic::createMaybe());
89: }
90:
91: public function isAcceptedBy(Type $acceptingType, bool $strictTypes): TrinaryLogic
92: {
93: return $this->isSubTypeOf($acceptingType);
94: }
95:
96: public function equals(Type $type): bool
97: {
98: return $type instanceof self;
99: }
100:
101: public function describe(VerbosityLevel $level): string
102: {
103: return 'non-falsy-string';
104: }
105:
106: public function isOffsetAccessible(): TrinaryLogic
107: {
108: return TrinaryLogic::createYes();
109: }
110:
111: public function hasOffsetValueType(Type $offsetType): TrinaryLogic
112: {
113: return (new IntegerType())->isSuperTypeOf($offsetType)->and(TrinaryLogic::createMaybe());
114: }
115:
116: public function getOffsetValueType(Type $offsetType): Type
117: {
118: if ($this->hasOffsetValueType($offsetType)->no()) {
119: return new ErrorType();
120: }
121:
122: return new StringType();
123: }
124:
125: public function setOffsetValueType(?Type $offsetType, Type $valueType, bool $unionValues = true): Type
126: {
127: return $this;
128: }
129:
130: public function unsetOffset(Type $offsetType): Type
131: {
132: return new ErrorType();
133: }
134:
135: public function toNumber(): Type
136: {
137: return new ErrorType();
138: }
139:
140: public function toInteger(): Type
141: {
142: return new UnionType([
143: IntegerRangeType::fromInterval(null, -1),
144: IntegerRangeType::fromInterval(1, null),
145: ]);
146: }
147:
148: public function toFloat(): Type
149: {
150: return new FloatType();
151: }
152:
153: public function toString(): Type
154: {
155: return $this;
156: }
157:
158: public function toArray(): Type
159: {
160: return new ConstantArrayType(
161: [new ConstantIntegerType(0)],
162: [$this],
163: [1],
164: [],
165: true,
166: );
167: }
168:
169: public function toArrayKey(): Type
170: {
171: return $this;
172: }
173:
174: public function isNull(): TrinaryLogic
175: {
176: return TrinaryLogic::createNo();
177: }
178:
179: public function isTrue(): TrinaryLogic
180: {
181: return TrinaryLogic::createNo();
182: }
183:
184: public function isFalse(): TrinaryLogic
185: {
186: return TrinaryLogic::createNo();
187: }
188:
189: public function isBoolean(): TrinaryLogic
190: {
191: return TrinaryLogic::createNo();
192: }
193:
194: public function isFloat(): TrinaryLogic
195: {
196: return TrinaryLogic::createNo();
197: }
198:
199: public function isInteger(): TrinaryLogic
200: {
201: return TrinaryLogic::createNo();
202: }
203:
204: public function isString(): TrinaryLogic
205: {
206: return TrinaryLogic::createYes();
207: }
208:
209: public function isNumericString(): TrinaryLogic
210: {
211: return TrinaryLogic::createMaybe();
212: }
213:
214: public function isNonEmptyString(): TrinaryLogic
215: {
216: return TrinaryLogic::createYes();
217: }
218:
219: public function isNonFalsyString(): TrinaryLogic
220: {
221: return TrinaryLogic::createYes();
222: }
223:
224: public function isLiteralString(): TrinaryLogic
225: {
226: return TrinaryLogic::createMaybe();
227: }
228:
229: public function isClassStringType(): TrinaryLogic
230: {
231: return TrinaryLogic::createMaybe();
232: }
233:
234: public function isVoid(): TrinaryLogic
235: {
236: return TrinaryLogic::createNo();
237: }
238:
239: public function isScalar(): TrinaryLogic
240: {
241: return TrinaryLogic::createYes();
242: }
243:
244: public function traverse(callable $cb): Type
245: {
246: return $this;
247: }
248:
249: public function generalize(GeneralizePrecision $precision): Type
250: {
251: return new StringType();
252: }
253:
254: public static function __set_state(array $properties): Type
255: {
256: return new self();
257: }
258:
259: }
260: