1: <?php declare(strict_types = 1);
2:
3: namespace PHPStan\Type;
4:
5: use PhpParser\Node\Identifier;
6: use PHPStan\DependencyInjection\ReportUnsafeArrayStringKeyCastingToggle;
7: use PHPStan\Php\PhpVersion;
8: use PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode;
9: use PHPStan\PhpDocParser\Ast\Type\TypeNode;
10: use PHPStan\Reflection\ReflectionProviderStaticAccessor;
11: use PHPStan\ShouldNotHappenException;
12: use PHPStan\TrinaryLogic;
13: use PHPStan\Turbo\ShadowedByTurboExtension;
14: use PHPStan\Type\Accessory\AccessoryDecimalIntegerStringType;
15: use PHPStan\Type\Accessory\AccessoryNonEmptyStringType;
16: use PHPStan\Type\Constant\ConstantArrayType;
17: use PHPStan\Type\Constant\ConstantBooleanType;
18: use PHPStan\Type\Constant\ConstantIntegerType;
19: use PHPStan\Type\Constant\ConstantStringType;
20: use PHPStan\Type\Traits\MaybeCallableTypeTrait;
21: use PHPStan\Type\Traits\NonArrayTypeTrait;
22: use PHPStan\Type\Traits\NonGeneralizableTypeTrait;
23: use PHPStan\Type\Traits\NonGenericTypeTrait;
24: use PHPStan\Type\Traits\NonIterableTypeTrait;
25: use PHPStan\Type\Traits\NonObjectTypeTrait;
26: use PHPStan\Type\Traits\OperatorTypeTrait;
27: use PHPStan\Type\Traits\UndecidedBooleanTypeTrait;
28: use PHPStan\Type\Traits\UndecidedComparisonTypeTrait;
29: use function count;
30:
31: /** @api */
32: #[InstanceofDeprecated(insteadUse: 'Type::isString()')]
33: #[ShadowedByTurboExtension(implementation: __DIR__ . '/../../turbo-ext/src/StringType.cpp')]
34: class StringType implements Type
35: {
36:
37: use JustNullableTypeTrait;
38: use MaybeCallableTypeTrait;
39: use NonArrayTypeTrait;
40: use NonIterableTypeTrait;
41: use NonObjectTypeTrait;
42: use UndecidedBooleanTypeTrait;
43: use UndecidedComparisonTypeTrait;
44: use NonGenericTypeTrait;
45: use NonGeneralizableTypeTrait;
46: use OperatorTypeTrait;
47:
48: /** @api */
49: public function __construct()
50: {
51: }
52:
53: public function describe(VerbosityLevel $level): string
54: {
55: return 'string';
56: }
57:
58: public function getConstantStrings(): array
59: {
60: return [];
61: }
62:
63: public function isOffsetAccessible(): TrinaryLogic
64: {
65: return TrinaryLogic::createYes();
66: }
67:
68: public function isOffsetAccessLegal(): TrinaryLogic
69: {
70: return TrinaryLogic::createYes();
71: }
72:
73: public function hasOffsetValueType(Type $offsetType): TrinaryLogic
74: {
75: return $offsetType->isInteger()->and(TrinaryLogic::createMaybe());
76: }
77:
78: public function getOffsetValueType(Type $offsetType): Type
79: {
80: if ($this->hasOffsetValueType($offsetType)->no()) {
81: return new ErrorType();
82: }
83:
84: return new IntersectionType([
85: new StringType(),
86: new AccessoryNonEmptyStringType(),
87: ]);
88: }
89:
90: public function setOffsetValueType(?Type $offsetType, Type $valueType, bool $unionValues = true): Type
91: {
92: if ($offsetType === null) {
93: return new ErrorType();
94: }
95:
96: $valueStringType = $valueType->toString();
97: if ($valueStringType instanceof ErrorType) {
98: return new ErrorType();
99: }
100:
101: if ($offsetType->isInteger()->yes() || $offsetType instanceof MixedType) {
102: return new IntersectionType([
103: new StringType(),
104: new AccessoryNonEmptyStringType(),
105: ]);
106: }
107:
108: return new ErrorType();
109: }
110:
111: public function setExistingOffsetValueType(Type $offsetType, Type $valueType): Type
112: {
113: return $this;
114: }
115:
116: public function unsetOffset(Type $offsetType): Type
117: {
118: return new ErrorType();
119: }
120:
121: public function accepts(Type $type, bool $strictTypes): AcceptsResult
122: {
123: if ($type instanceof self) {
124: return AcceptsResult::createYes();
125: }
126:
127: if ($type instanceof CompoundType) {
128: return $type->isAcceptedBy($this, $strictTypes);
129: }
130:
131: $thatClassNames = $type->getObjectClassNames();
132: if (count($thatClassNames) > 1) {
133: throw new ShouldNotHappenException();
134: }
135:
136: if ($thatClassNames === [] || $strictTypes) {
137: return AcceptsResult::createNo();
138: }
139:
140: $reflectionProvider = ReflectionProviderStaticAccessor::getInstance();
141: if (!$reflectionProvider->hasClass($thatClassNames[0])) {
142: return AcceptsResult::createNo();
143: }
144:
145: $typeClass = $reflectionProvider->getClass($thatClassNames[0]);
146: return AcceptsResult::createFromBoolean(
147: $typeClass->hasNativeMethod('__toString'),
148: );
149: }
150:
151: public function toNumber(): Type
152: {
153: return new ErrorType();
154: }
155:
156: public function toBitwiseNotType(): Type
157: {
158: return new StringType();
159: }
160:
161: public function toAbsoluteNumber(): Type
162: {
163: return new ErrorType();
164: }
165:
166: public function toInteger(): Type
167: {
168: return new IntegerType();
169: }
170:
171: public function toFloat(): Type
172: {
173: return new FloatType();
174: }
175:
176: public function toString(): Type
177: {
178: return $this;
179: }
180:
181: public function toArray(): Type
182: {
183: return new ConstantArrayType(
184: [new ConstantIntegerType(0)],
185: [$this],
186: [1],
187: isList: TrinaryLogic::createYes(),
188: );
189: }
190:
191: public function toArrayKey(): Type
192: {
193: $level = ReportUnsafeArrayStringKeyCastingToggle::getLevel();
194: if ($level !== ReportUnsafeArrayStringKeyCastingToggle::PREVENT) {
195: return $this;
196: }
197:
198: $isDecimalIntString = $this->isDecimalIntegerString();
199: if ($isDecimalIntString->no()) {
200: return $this;
201: } elseif ($isDecimalIntString->yes()) {
202: return new IntegerType();
203: }
204:
205: return new UnionType([
206: new IntegerType(),
207: TypeCombinator::intersect($this, new AccessoryDecimalIntegerStringType(inverse: true)),
208: ]);
209: }
210:
211: public function toCoercedArgumentType(bool $strictTypes): Type
212: {
213: if (!$strictTypes) {
214: if ($this->isNumericString()->no()) {
215: return TypeCombinator::union($this, $this->toBoolean());
216: }
217: return TypeCombinator::union($this->toInteger(), $this->toFloat(), $this, $this->toBoolean());
218: }
219:
220: return $this;
221: }
222:
223: public function getKindMask(): int
224: {
225: return TypeKind::STRING;
226: }
227:
228: public function isNull(): TrinaryLogic
229: {
230: return TrinaryLogic::createNo();
231: }
232:
233: public function isTrue(): TrinaryLogic
234: {
235: return TrinaryLogic::createNo();
236: }
237:
238: public function isFalse(): TrinaryLogic
239: {
240: return TrinaryLogic::createNo();
241: }
242:
243: public function isBoolean(): TrinaryLogic
244: {
245: return TrinaryLogic::createNo();
246: }
247:
248: public function isFloat(): TrinaryLogic
249: {
250: return TrinaryLogic::createNo();
251: }
252:
253: public function isInteger(): TrinaryLogic
254: {
255: return TrinaryLogic::createNo();
256: }
257:
258: public function isString(): TrinaryLogic
259: {
260: return TrinaryLogic::createYes();
261: }
262:
263: public function isNumericString(): TrinaryLogic
264: {
265: return TrinaryLogic::createMaybe();
266: }
267:
268: public function isDecimalIntegerString(): TrinaryLogic
269: {
270: return TrinaryLogic::createMaybe();
271: }
272:
273: public function isNonEmptyString(): TrinaryLogic
274: {
275: return TrinaryLogic::createMaybe();
276: }
277:
278: public function isNonFalsyString(): TrinaryLogic
279: {
280: return TrinaryLogic::createMaybe();
281: }
282:
283: public function isLiteralString(): TrinaryLogic
284: {
285: return TrinaryLogic::createMaybe();
286: }
287:
288: /** A subclass may hold strings the cells do not describe, so only string itself holds them all. */
289: public function getStringRefinement(): ?int
290: {
291: return static::class === self::class ? StringRefinement::ALL : null;
292: }
293:
294: public function isLowercaseString(): TrinaryLogic
295: {
296: return TrinaryLogic::createMaybe();
297: }
298:
299: public function isUppercaseString(): TrinaryLogic
300: {
301: return TrinaryLogic::createMaybe();
302: }
303:
304: public function isClassString(): TrinaryLogic
305: {
306: return TrinaryLogic::createMaybe();
307: }
308:
309: public function getClassStringObjectType(): Type
310: {
311: return new ObjectWithoutClassType();
312: }
313:
314: public function getObjectTypeOrClassStringObjectType(): Type
315: {
316: return new ObjectWithoutClassType();
317: }
318:
319: public function isScalar(): TrinaryLogic
320: {
321: return TrinaryLogic::createYes();
322: }
323:
324: public function looseCompare(Type $type, PhpVersion $phpVersion): BooleanType
325: {
326: if ($type->isArray()->yes()) {
327: return new ConstantBooleanType(false);
328: }
329:
330: return new BooleanType();
331: }
332:
333: public function hasMethod(string $methodName): TrinaryLogic
334: {
335: if ($this->isClassString()->yes()) {
336: return TrinaryLogic::createMaybe();
337: }
338: return TrinaryLogic::createNo();
339: }
340:
341: public function tryRemove(Type $typeToRemove): ?Type
342: {
343: if ($typeToRemove instanceof ConstantStringType && $typeToRemove->getValue() === '') {
344: return TypeCombinator::intersect($this, new AccessoryNonEmptyStringType());
345: }
346:
347: if ($typeToRemove instanceof AccessoryNonEmptyStringType) {
348: return new ConstantStringType('');
349: }
350:
351: return null;
352: }
353:
354: public function getFiniteTypes(): array
355: {
356: return [];
357: }
358:
359: public function arithmeticPower(Type $exponent, PhpVersion $phpVersion): Type
360: {
361: return $this->toValueSet()->arithmeticPower($exponent->toValueSet(), $phpVersion);
362: }
363:
364: public function toPhpDocNode(): TypeNode
365: {
366: return new IdentifierTypeNode('string');
367: }
368:
369: public function toNativePhpTypeNode(NativeTypePosition $position)
370: {
371: return $position->isScalarAllowed() ? new Identifier('string') : null;
372: }
373:
374: public function hasTemplateOrLateResolvableType(): bool
375: {
376: return false;
377: }
378:
379: public function hasUnresolvedTemplateArgumentType(): bool
380: {
381: return false;
382: }
383:
384: public function toValueSet(): ValueSet
385: {
386: return ValueSet::createStrings($this);
387: }
388:
389: public function tryUnion(Type $otherType): ?Type
390: {
391: return null;
392: }
393:
394: public function tryIntersect(Type $otherType): ?Type
395: {
396: return null;
397: }
398:
399: }
400: