1: <?php declare(strict_types = 1);
2:
3: namespace PHPStan\Type;
4:
5: use PHPStan\Type\Accessory\AccessoryDecimalIntegerStringType;
6: use PHPStan\Type\Accessory\AccessoryLiteralStringType;
7: use PHPStan\Type\Accessory\AccessoryLowercaseStringType;
8: use PHPStan\Type\Accessory\AccessoryNonEmptyStringType;
9: use PHPStan\Type\Accessory\AccessoryNonFalsyStringType;
10: use PHPStan\Type\Accessory\AccessoryNumericStringType;
11: use PHPStan\Type\Accessory\AccessoryUppercaseStringType;
12: use PHPStan\Type\Constant\ConstantStringType;
13:
14: /**
15: * Bits of Type::getStringRefinement(): the strings split into cells that the
16: * string accessory types never split further. Every refinement of string - a
17: * conjunction of accessories - holds whole cells, so the union of two
18: * refinements is a refinement exactly when some conjunction holds the cells of
19: * both, and a constant string fills a cell only when it is the cell's sole value
20: * (the empty string and '0').
21: *
22: * Whether a string is a literal-string is not decided by its value, so every
23: * cell of values comes twice: the low bits are the literal strings, the same
24: * bits shifted by NON_LITERAL_SHIFT the others. The constants below name the
25: * cells of values; of() makes the cells of both halves from them.
26: *
27: * @api
28: */
29: final class StringRefinement
30: {
31:
32: /** '' */
33: public const EMPTY = 1;
34: /** '0' */
35: public const ZERO = 2;
36: /** the other decimal-int-strings, such as '1' or '-7' */
37: public const DECIMAL = 4;
38: /** numeric-strings that are not decimal-int-strings, of either case, such as '1.5' */
39: public const NUMERIC = 8;
40: /** ... lowercase only, such as '1e5' */
41: public const NUMERIC_LOWER = 16;
42: /** ... uppercase only, such as '1E5' */
43: public const NUMERIC_UPPER = 32;
44: /** non-empty strings that are not numeric, of either case, such as '-' */
45: public const TEXT = 64;
46: /** ... lowercase only, such as 'a' */
47: public const TEXT_LOWER = 128;
48: /** ... uppercase only, such as 'A' */
49: public const TEXT_UPPER = 256;
50: /** ... neither, such as 'aA' */
51: public const TEXT_MIXED = 512;
52:
53: public const NON_LITERAL_SHIFT = 10;
54:
55: public const VALUES = 1023;
56: public const LITERAL = self::VALUES;
57: public const ALL = self::VALUES | (self::VALUES << self::NON_LITERAL_SHIFT);
58:
59: /**
60: * The cells of the strings of these values, literal or not.
61: *
62: * @param int<0, self::VALUES> $values
63: * @return int-mask-of<self::*>
64: */
65: public static function of(int $values): int
66: {
67: return $values | ($values << self::NON_LITERAL_SHIFT);
68: }
69:
70: /**
71: * The cell of a constant string, a literal one. Its predicates decide it, so
72: * the rules of what is numeric or lowercase stay where they are defined.
73: *
74: * @return int<1, self::VALUES>
75: */
76: public static function ofConstant(ConstantStringType $string): int
77: {
78: $value = $string->getValue();
79: if ($value === '') {
80: return self::EMPTY;
81: }
82: if ($value === '0') {
83: return self::ZERO;
84: }
85: if ($string->isDecimalIntegerString()->yes()) {
86: return self::DECIMAL;
87: }
88:
89: $lower = $string->isLowercaseString()->yes();
90: $upper = $string->isUppercaseString()->yes();
91: if ($string->isNumericString()->yes()) {
92: return $lower ? ($upper ? self::NUMERIC : self::NUMERIC_LOWER) : self::NUMERIC_UPPER;
93: }
94: if ($lower) {
95: return $upper ? self::TEXT : self::TEXT_LOWER;
96: }
97:
98: return $upper ? self::TEXT_UPPER : self::TEXT_MIXED;
99: }
100:
101: /** Whether the cells are the sole value of a constant string. */
102: public static function isSingleValue(int $cells): bool
103: {
104: return $cells === self::EMPTY || $cells === self::ZERO;
105: }
106:
107: /**
108: * The refinement of string holding exactly these cells, or null when no
109: * conjunction of the string accessories does.
110: */
111: public static function toType(int $cells): ?Type
112: {
113: $accessories = [];
114: $held = self::ALL;
115: foreach (self::getAccessories() as $accessory) {
116: $accessoryCells = $accessory->getStringRefinement();
117: if ($accessoryCells === null || ($cells & ~$accessoryCells) !== 0) {
118: continue;
119: }
120:
121: $accessories[] = $accessory;
122: $held &= $accessoryCells;
123: }
124:
125: if ($held !== $cells) {
126: return null;
127: }
128:
129: // an accessory another one implies says nothing more: decimal-int-string is lowercase
130: $minimal = [];
131: foreach ($accessories as $i => $accessory) {
132: $accessoryCells = $accessory->getStringRefinement() ?? self::ALL;
133: foreach ($accessories as $j => $other) {
134: $otherCells = $other->getStringRefinement() ?? self::ALL;
135: if ($i !== $j && $otherCells !== $accessoryCells && ($otherCells & ~$accessoryCells) === 0) {
136: continue 2;
137: }
138: }
139: $minimal[] = $accessory;
140: }
141:
142: return TypeCombinator::intersect(new StringType(), ...$minimal);
143: }
144:
145: /** @return list<Type> */
146: private static function getAccessories(): array
147: {
148: return [
149: new AccessoryNonEmptyStringType(),
150: new AccessoryNonFalsyStringType(),
151: new AccessoryNumericStringType(),
152: new AccessoryDecimalIntegerStringType(),
153: new AccessoryDecimalIntegerStringType(inverse: true),
154: new AccessoryLowercaseStringType(),
155: new AccessoryUppercaseStringType(),
156: new AccessoryLiteralStringType(),
157: ];
158: }
159:
160: }
161: