1: | <?php declare(strict_types = 1); |
2: | |
3: | namespace PHPStan\Type; |
4: | |
5: | use PHPStan\Php\PhpVersion; |
6: | use PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode; |
7: | use PHPStan\PhpDocParser\Ast\Type\TypeNode; |
8: | use PHPStan\Reflection\ReflectionProviderStaticAccessor; |
9: | use PHPStan\ShouldNotHappenException; |
10: | use PHPStan\TrinaryLogic; |
11: | use PHPStan\Type\Accessory\AccessoryNonEmptyStringType; |
12: | use PHPStan\Type\Constant\ConstantArrayType; |
13: | use PHPStan\Type\Constant\ConstantIntegerType; |
14: | use PHPStan\Type\Constant\ConstantStringType; |
15: | use PHPStan\Type\Traits\MaybeCallableTypeTrait; |
16: | use PHPStan\Type\Traits\NonArrayTypeTrait; |
17: | use PHPStan\Type\Traits\NonGeneralizableTypeTrait; |
18: | use PHPStan\Type\Traits\NonGenericTypeTrait; |
19: | use PHPStan\Type\Traits\NonIterableTypeTrait; |
20: | use PHPStan\Type\Traits\NonObjectTypeTrait; |
21: | use PHPStan\Type\Traits\UndecidedBooleanTypeTrait; |
22: | use PHPStan\Type\Traits\UndecidedComparisonTypeTrait; |
23: | use function count; |
24: | |
25: | |
26: | class StringType implements Type |
27: | { |
28: | |
29: | use JustNullableTypeTrait; |
30: | use MaybeCallableTypeTrait; |
31: | use NonArrayTypeTrait; |
32: | use NonIterableTypeTrait; |
33: | use NonObjectTypeTrait; |
34: | use UndecidedBooleanTypeTrait; |
35: | use UndecidedComparisonTypeTrait; |
36: | use NonGenericTypeTrait; |
37: | use NonGeneralizableTypeTrait; |
38: | |
39: | |
40: | public function __construct() |
41: | { |
42: | } |
43: | |
44: | public function describe(VerbosityLevel $level): string |
45: | { |
46: | return 'string'; |
47: | } |
48: | |
49: | public function getConstantStrings(): array |
50: | { |
51: | return []; |
52: | } |
53: | |
54: | public function isOffsetAccessible(): TrinaryLogic |
55: | { |
56: | return TrinaryLogic::createYes(); |
57: | } |
58: | |
59: | public function isOffsetAccessLegal(): TrinaryLogic |
60: | { |
61: | return TrinaryLogic::createYes(); |
62: | } |
63: | |
64: | public function hasOffsetValueType(Type $offsetType): TrinaryLogic |
65: | { |
66: | return $offsetType->isInteger()->and(TrinaryLogic::createMaybe()); |
67: | } |
68: | |
69: | public function getOffsetValueType(Type $offsetType): Type |
70: | { |
71: | if ($this->hasOffsetValueType($offsetType)->no()) { |
72: | return new ErrorType(); |
73: | } |
74: | |
75: | return new IntersectionType([ |
76: | new StringType(), |
77: | new AccessoryNonEmptyStringType(), |
78: | ]); |
79: | } |
80: | |
81: | public function setOffsetValueType(?Type $offsetType, Type $valueType, bool $unionValues = true): Type |
82: | { |
83: | if ($offsetType === null) { |
84: | return new ErrorType(); |
85: | } |
86: | |
87: | $valueStringType = $valueType->toString(); |
88: | if ($valueStringType instanceof ErrorType) { |
89: | return new ErrorType(); |
90: | } |
91: | |
92: | if ($offsetType->isInteger()->yes() || $offsetType instanceof MixedType) { |
93: | return new IntersectionType([ |
94: | new StringType(), |
95: | new AccessoryNonEmptyStringType(), |
96: | ]); |
97: | } |
98: | |
99: | return new ErrorType(); |
100: | } |
101: | |
102: | public function setExistingOffsetValueType(Type $offsetType, Type $valueType): Type |
103: | { |
104: | return $this; |
105: | } |
106: | |
107: | public function unsetOffset(Type $offsetType): Type |
108: | { |
109: | return new ErrorType(); |
110: | } |
111: | |
112: | public function accepts(Type $type, bool $strictTypes): AcceptsResult |
113: | { |
114: | if ($type instanceof self) { |
115: | return AcceptsResult::createYes(); |
116: | } |
117: | |
118: | if ($type instanceof CompoundType) { |
119: | return $type->isAcceptedBy($this, $strictTypes); |
120: | } |
121: | |
122: | $thatClassNames = $type->getObjectClassNames(); |
123: | if (count($thatClassNames) > 1) { |
124: | throw new ShouldNotHappenException(); |
125: | } |
126: | |
127: | if ($thatClassNames === [] || $strictTypes) { |
128: | return AcceptsResult::createNo(); |
129: | } |
130: | |
131: | $reflectionProvider = ReflectionProviderStaticAccessor::getInstance(); |
132: | if (!$reflectionProvider->hasClass($thatClassNames[0])) { |
133: | return AcceptsResult::createNo(); |
134: | } |
135: | |
136: | $typeClass = $reflectionProvider->getClass($thatClassNames[0]); |
137: | return AcceptsResult::createFromBoolean( |
138: | $typeClass->hasNativeMethod('__toString'), |
139: | ); |
140: | } |
141: | |
142: | public function toNumber(): Type |
143: | { |
144: | return new ErrorType(); |
145: | } |
146: | |
147: | public function toAbsoluteNumber(): Type |
148: | { |
149: | return new ErrorType(); |
150: | } |
151: | |
152: | public function toInteger(): Type |
153: | { |
154: | return new IntegerType(); |
155: | } |
156: | |
157: | public function toFloat(): Type |
158: | { |
159: | return new FloatType(); |
160: | } |
161: | |
162: | public function toString(): Type |
163: | { |
164: | return $this; |
165: | } |
166: | |
167: | public function toArray(): Type |
168: | { |
169: | return new ConstantArrayType( |
170: | [new ConstantIntegerType(0)], |
171: | [$this], |
172: | [1], |
173: | [], |
174: | TrinaryLogic::createYes(), |
175: | ); |
176: | } |
177: | |
178: | public function toArrayKey(): Type |
179: | { |
180: | return $this; |
181: | } |
182: | |
183: | public function isNull(): TrinaryLogic |
184: | { |
185: | return TrinaryLogic::createNo(); |
186: | } |
187: | |
188: | public function isTrue(): TrinaryLogic |
189: | { |
190: | return TrinaryLogic::createNo(); |
191: | } |
192: | |
193: | public function isFalse(): TrinaryLogic |
194: | { |
195: | return TrinaryLogic::createNo(); |
196: | } |
197: | |
198: | public function isBoolean(): TrinaryLogic |
199: | { |
200: | return TrinaryLogic::createNo(); |
201: | } |
202: | |
203: | public function isFloat(): TrinaryLogic |
204: | { |
205: | return TrinaryLogic::createNo(); |
206: | } |
207: | |
208: | public function isInteger(): TrinaryLogic |
209: | { |
210: | return TrinaryLogic::createNo(); |
211: | } |
212: | |
213: | public function isString(): TrinaryLogic |
214: | { |
215: | return TrinaryLogic::createYes(); |
216: | } |
217: | |
218: | public function isNumericString(): TrinaryLogic |
219: | { |
220: | return TrinaryLogic::createMaybe(); |
221: | } |
222: | |
223: | public function isNonEmptyString(): TrinaryLogic |
224: | { |
225: | return TrinaryLogic::createMaybe(); |
226: | } |
227: | |
228: | public function isNonFalsyString(): TrinaryLogic |
229: | { |
230: | return TrinaryLogic::createMaybe(); |
231: | } |
232: | |
233: | public function isLiteralString(): TrinaryLogic |
234: | { |
235: | return TrinaryLogic::createMaybe(); |
236: | } |
237: | |
238: | public function isLowercaseString(): TrinaryLogic |
239: | { |
240: | return TrinaryLogic::createMaybe(); |
241: | } |
242: | |
243: | public function isUppercaseString(): TrinaryLogic |
244: | { |
245: | return TrinaryLogic::createMaybe(); |
246: | } |
247: | |
248: | public function isClassString(): TrinaryLogic |
249: | { |
250: | return TrinaryLogic::createMaybe(); |
251: | } |
252: | |
253: | public function getClassStringObjectType(): Type |
254: | { |
255: | return new ObjectWithoutClassType(); |
256: | } |
257: | |
258: | public function getObjectTypeOrClassStringObjectType(): Type |
259: | { |
260: | return new ObjectWithoutClassType(); |
261: | } |
262: | |
263: | public function isScalar(): TrinaryLogic |
264: | { |
265: | return TrinaryLogic::createYes(); |
266: | } |
267: | |
268: | public function looseCompare(Type $type, PhpVersion $phpVersion): BooleanType |
269: | { |
270: | return new BooleanType(); |
271: | } |
272: | |
273: | public function hasMethod(string $methodName): TrinaryLogic |
274: | { |
275: | if ($this->isClassString()->yes()) { |
276: | return TrinaryLogic::createMaybe(); |
277: | } |
278: | return TrinaryLogic::createNo(); |
279: | } |
280: | |
281: | public function tryRemove(Type $typeToRemove): ?Type |
282: | { |
283: | if ($typeToRemove instanceof ConstantStringType && $typeToRemove->getValue() === '') { |
284: | return TypeCombinator::intersect($this, new AccessoryNonEmptyStringType()); |
285: | } |
286: | |
287: | if ($typeToRemove instanceof AccessoryNonEmptyStringType) { |
288: | return new ConstantStringType(''); |
289: | } |
290: | |
291: | return null; |
292: | } |
293: | |
294: | public function getFiniteTypes(): array |
295: | { |
296: | return []; |
297: | } |
298: | |
299: | public function exponentiate(Type $exponent): Type |
300: | { |
301: | return ExponentiateHelper::exponentiate($this, $exponent); |
302: | } |
303: | |
304: | public function toPhpDocNode(): TypeNode |
305: | { |
306: | return new IdentifierTypeNode('string'); |
307: | } |
308: | |
309: | } |
310: | |