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