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