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