1: <?php declare(strict_types = 1);
2:
3: namespace PHPStan\Type\Constant;
4:
5: use PHPStan\ShouldNotHappenException;
6: use PHPStan\Type\Accessory\NonEmptyArrayType;
7: use PHPStan\Type\ArrayType;
8: use PHPStan\Type\Type;
9: use PHPStan\Type\TypeCombinator;
10: use PHPStan\Type\TypeUtils;
11: use function array_filter;
12: use function array_map;
13: use function array_unique;
14: use function array_values;
15: use function count;
16: use function in_array;
17: use function is_float;
18: use function max;
19: use function range;
20:
21: /** @api */
22: class ConstantArrayTypeBuilder
23: {
24:
25: public const ARRAY_COUNT_LIMIT = 256;
26:
27: private bool $degradeToGeneralArray = false;
28:
29: /**
30: * @param array<int, Type> $keyTypes
31: * @param array<int, Type> $valueTypes
32: * @param non-empty-list<int> $nextAutoIndexes
33: * @param array<int> $optionalKeys
34: */
35: private function __construct(
36: private array $keyTypes,
37: private array $valueTypes,
38: private array $nextAutoIndexes,
39: private array $optionalKeys,
40: )
41: {
42: }
43:
44: public static function createEmpty(): self
45: {
46: return new self([], [], [0], []);
47: }
48:
49: public static function createFromConstantArray(ConstantArrayType $startArrayType): self
50: {
51: $builder = new self(
52: $startArrayType->getKeyTypes(),
53: $startArrayType->getValueTypes(),
54: $startArrayType->getNextAutoIndexes(),
55: $startArrayType->getOptionalKeys(),
56: );
57:
58: if (count($startArrayType->getKeyTypes()) > self::ARRAY_COUNT_LIMIT) {
59: $builder->degradeToGeneralArray();
60: }
61:
62: return $builder;
63: }
64:
65: public function setOffsetValueType(?Type $offsetType, Type $valueType, bool $optional = false): void
66: {
67: if ($offsetType !== null) {
68: $offsetType = ArrayType::castToArrayKeyType($offsetType);
69: }
70:
71: if (!$this->degradeToGeneralArray) {
72: if ($offsetType === null) {
73: $newAutoIndexes = $optional ? $this->nextAutoIndexes : [];
74: $hasOptional = false;
75: foreach ($this->keyTypes as $i => $keyType) {
76: if (!$keyType instanceof ConstantIntegerType) {
77: continue;
78: }
79:
80: if (!in_array($keyType->getValue(), $this->nextAutoIndexes, true)) {
81: continue;
82: }
83:
84: $this->valueTypes[$i] = TypeCombinator::union($this->valueTypes[$i], $valueType);
85:
86: if (!$hasOptional && !$optional) {
87: $this->optionalKeys = array_values(array_filter($this->optionalKeys, static fn (int $index): bool => $index !== $i));
88: }
89:
90: /** @var int|float $newAutoIndex */
91: $newAutoIndex = $keyType->getValue() + 1;
92: if (is_float($newAutoIndex)) {
93: $newAutoIndex = $keyType->getValue();
94: }
95:
96: $newAutoIndexes[] = $newAutoIndex;
97: $hasOptional = true;
98: }
99:
100: $max = max($this->nextAutoIndexes);
101:
102: $this->keyTypes[] = new ConstantIntegerType($max);
103: $this->valueTypes[] = $valueType;
104:
105: /** @var int|float $newAutoIndex */
106: $newAutoIndex = $max + 1;
107: if (is_float($newAutoIndex)) {
108: $newAutoIndex = $max;
109: }
110:
111: $newAutoIndexes[] = $newAutoIndex;
112: $this->nextAutoIndexes = array_unique($newAutoIndexes);
113:
114: if ($optional || $hasOptional) {
115: $this->optionalKeys[] = count($this->keyTypes) - 1;
116: }
117:
118: if (count($this->keyTypes) > self::ARRAY_COUNT_LIMIT) {
119: $this->degradeToGeneralArray = true;
120: }
121:
122: return;
123: }
124:
125: if ($offsetType instanceof ConstantIntegerType || $offsetType instanceof ConstantStringType) {
126: /** @var ConstantIntegerType|ConstantStringType $keyType */
127: foreach ($this->keyTypes as $i => $keyType) {
128: if ($keyType->getValue() !== $offsetType->getValue()) {
129: continue;
130: }
131:
132: if ($optional) {
133: $valueType = TypeCombinator::union($valueType, $this->valueTypes[$i]);
134: }
135:
136: $this->valueTypes[$i] = $valueType;
137:
138: if (!$optional) {
139: $this->optionalKeys = array_values(array_filter($this->optionalKeys, static fn (int $index): bool => $index !== $i));
140: if ($keyType instanceof ConstantIntegerType) {
141: $nextAutoIndexes = array_values(array_filter($this->nextAutoIndexes, static fn (int $index) => $index > $keyType->getValue()));
142: if (count($nextAutoIndexes) === 0) {
143: throw new ShouldNotHappenException();
144: }
145: $this->nextAutoIndexes = $nextAutoIndexes;
146: }
147: }
148: return;
149: }
150:
151: $this->keyTypes[] = $offsetType;
152: $this->valueTypes[] = $valueType;
153:
154: if ($offsetType instanceof ConstantIntegerType) {
155: $max = max($this->nextAutoIndexes);
156: if ($offsetType->getValue() >= $max) {
157: /** @var int|float $newAutoIndex */
158: $newAutoIndex = $offsetType->getValue() + 1;
159: if (is_float($newAutoIndex)) {
160: $newAutoIndex = $max;
161: }
162: if (!$optional) {
163: $this->nextAutoIndexes = [$newAutoIndex];
164: } else {
165: $this->nextAutoIndexes[] = $newAutoIndex;
166: }
167: }
168: }
169:
170: if ($optional) {
171: $this->optionalKeys[] = count($this->keyTypes) - 1;
172: }
173:
174: if (count($this->keyTypes) > self::ARRAY_COUNT_LIMIT) {
175: $this->degradeToGeneralArray = true;
176: }
177:
178: return;
179: }
180:
181: $scalarTypes = TypeUtils::getConstantScalars($offsetType);
182: if (count($scalarTypes) === 0) {
183: $integerRanges = TypeUtils::getIntegerRanges($offsetType);
184: if (count($integerRanges) > 0) {
185: foreach ($integerRanges as $integerRange) {
186: if ($integerRange->getMin() === null) {
187: break;
188: }
189: if ($integerRange->getMax() === null) {
190: break;
191: }
192:
193: $rangeLength = $integerRange->getMax() - $integerRange->getMin();
194: if ($rangeLength >= self::ARRAY_COUNT_LIMIT) {
195: $scalarTypes = [];
196: break;
197: }
198:
199: foreach (range($integerRange->getMin(), $integerRange->getMax()) as $rangeValue) {
200: $scalarTypes[] = new ConstantIntegerType($rangeValue);
201: }
202: }
203: }
204: }
205: if (count($scalarTypes) > 0 && count($scalarTypes) < self::ARRAY_COUNT_LIMIT) {
206: $match = true;
207: $valueTypes = $this->valueTypes;
208: foreach ($scalarTypes as $scalarType) {
209: $scalarOffsetType = ArrayType::castToArrayKeyType($scalarType);
210: if (!$scalarOffsetType instanceof ConstantIntegerType && !$scalarOffsetType instanceof ConstantStringType) {
211: throw new ShouldNotHappenException();
212: }
213: $offsetMatch = false;
214:
215: /** @var ConstantIntegerType|ConstantStringType $keyType */
216: foreach ($this->keyTypes as $i => $keyType) {
217: if ($keyType->getValue() !== $scalarOffsetType->getValue()) {
218: continue;
219: }
220:
221: $valueTypes[$i] = TypeCombinator::union($valueTypes[$i], $valueType);
222: $offsetMatch = true;
223: }
224:
225: if ($offsetMatch) {
226: continue;
227: }
228:
229: $match = false;
230: }
231:
232: if ($match) {
233: $this->valueTypes = $valueTypes;
234: return;
235: }
236: }
237: }
238:
239: if ($offsetType === null) {
240: $offsetType = TypeCombinator::union(...array_map(static fn (int $index) => new ConstantIntegerType($index), $this->nextAutoIndexes));
241: }
242:
243: $this->keyTypes[] = $offsetType;
244: $this->valueTypes[] = $valueType;
245: if ($optional) {
246: $this->optionalKeys[] = count($this->keyTypes) - 1;
247: }
248: $this->degradeToGeneralArray = true;
249: }
250:
251: public function degradeToGeneralArray(): void
252: {
253: $this->degradeToGeneralArray = true;
254: }
255:
256: public function getArray(): Type
257: {
258: $keyTypesCount = count($this->keyTypes);
259: if ($keyTypesCount === 0) {
260: return new ConstantArrayType([], []);
261: }
262:
263: if (!$this->degradeToGeneralArray) {
264: /** @var array<int, ConstantIntegerType|ConstantStringType> $keyTypes */
265: $keyTypes = $this->keyTypes;
266: return new ConstantArrayType($keyTypes, $this->valueTypes, $this->nextAutoIndexes, $this->optionalKeys);
267: }
268:
269: $array = new ArrayType(
270: TypeCombinator::union(...$this->keyTypes),
271: TypeCombinator::union(...$this->valueTypes),
272: );
273:
274: if (count($this->optionalKeys) < $keyTypesCount) {
275: return TypeCombinator::intersect($array, new NonEmptyArrayType());
276: }
277:
278: return $array;
279: }
280:
281: }
282: