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: }
131:
132: return;
133: }
134:
135: if ($offsetType instanceof ConstantIntegerType || $offsetType instanceof ConstantStringType) {
136: /** @var ConstantIntegerType|ConstantStringType $keyType */
137: foreach ($this->keyTypes as $i => $keyType) {
138: if ($keyType->getValue() !== $offsetType->getValue()) {
139: continue;
140: }
141:
142: if ($optional) {
143: $valueType = TypeCombinator::union($valueType, $this->valueTypes[$i]);
144: }
145:
146: $this->valueTypes[$i] = $valueType;
147:
148: if (!$optional) {
149: $this->optionalKeys = array_values(array_filter($this->optionalKeys, static fn (int $index): bool => $index !== $i));
150: if ($keyType instanceof ConstantIntegerType) {
151: $nextAutoIndexes = array_values(array_filter($this->nextAutoIndexes, static fn (int $index) => $index > $keyType->getValue()));
152: if (count($nextAutoIndexes) === 0) {
153: throw new ShouldNotHappenException();
154: }
155: $this->nextAutoIndexes = $nextAutoIndexes;
156: }
157: }
158: return;
159: }
160:
161: $this->keyTypes[] = $offsetType;
162: $this->valueTypes[] = $valueType;
163:
164: if ($offsetType instanceof ConstantIntegerType) {
165: $min = min($this->nextAutoIndexes);
166: $max = max($this->nextAutoIndexes);
167: if ($offsetType->getValue() > $min) {
168: if ($offsetType->getValue() <= $max) {
169: $this->isList = $this->isList->and(TrinaryLogic::createMaybe());
170: } else {
171: $this->isList = TrinaryLogic::createNo();
172: }
173: }
174: if ($offsetType->getValue() >= $max) {
175: /** @var int|float $newAutoIndex */
176: $newAutoIndex = $offsetType->getValue() + 1;
177: if (is_float($newAutoIndex)) {
178: $newAutoIndex = $max;
179: }
180: if (!$optional) {
181: $this->nextAutoIndexes = [$newAutoIndex];
182: } else {
183: $this->nextAutoIndexes[] = $newAutoIndex;
184: }
185: }
186: } else {
187: $this->isList = TrinaryLogic::createNo();
188: }
189:
190: if ($optional) {
191: $this->optionalKeys[] = count($this->keyTypes) - 1;
192: }
193:
194: if (count($this->keyTypes) > self::ARRAY_COUNT_LIMIT) {
195: $this->degradeToGeneralArray = true;
196: }
197:
198: return;
199: }
200:
201: $scalarTypes = $offsetType->getConstantScalarTypes();
202: if (count($scalarTypes) === 0) {
203: $integerRanges = TypeUtils::getIntegerRanges($offsetType);
204: if (count($integerRanges) > 0) {
205: foreach ($integerRanges as $integerRange) {
206: if ($integerRange->getMin() === null) {
207: break;
208: }
209: if ($integerRange->getMax() === null) {
210: break;
211: }
212:
213: $rangeLength = $integerRange->getMax() - $integerRange->getMin();
214: if ($rangeLength >= self::ARRAY_COUNT_LIMIT) {
215: $scalarTypes = [];
216: break;
217: }
218:
219: foreach (range($integerRange->getMin(), $integerRange->getMax()) as $rangeValue) {
220: $scalarTypes[] = new ConstantIntegerType($rangeValue);
221: }
222: }
223: }
224: }
225: if (count($scalarTypes) > 0 && count($scalarTypes) < self::ARRAY_COUNT_LIMIT) {
226: $match = true;
227: $valueTypes = $this->valueTypes;
228: foreach ($scalarTypes as $scalarType) {
229: $scalarOffsetType = $scalarType->toArrayKey();
230: if (!$scalarOffsetType instanceof ConstantIntegerType && !$scalarOffsetType instanceof ConstantStringType) {
231: throw new ShouldNotHappenException();
232: }
233: $offsetMatch = false;
234:
235: /** @var ConstantIntegerType|ConstantStringType $keyType */
236: foreach ($this->keyTypes as $i => $keyType) {
237: if ($keyType->getValue() !== $scalarOffsetType->getValue()) {
238: continue;
239: }
240:
241: $valueTypes[$i] = TypeCombinator::union($valueTypes[$i], $valueType);
242: $offsetMatch = true;
243: }
244:
245: if ($offsetMatch) {
246: continue;
247: }
248:
249: $match = false;
250: }
251:
252: if ($match) {
253: $this->valueTypes = $valueTypes;
254: return;
255: }
256: }
257:
258: $this->isList = TrinaryLogic::createNo();
259: }
260:
261: if ($offsetType === null) {
262: $offsetType = TypeCombinator::union(...array_map(static fn (int $index) => new ConstantIntegerType($index), $this->nextAutoIndexes));
263: } else {
264: $this->isList = TrinaryLogic::createNo();
265: }
266:
267: $this->keyTypes[] = $offsetType;
268: $this->valueTypes[] = $valueType;
269: if ($optional) {
270: $this->optionalKeys[] = count($this->keyTypes) - 1;
271: }
272: $this->degradeToGeneralArray = true;
273: }
274:
275: public function degradeToGeneralArray(bool $oversized = false): void
276: {
277: $this->degradeToGeneralArray = true;
278: $this->oversized = $this->oversized || $oversized;
279: }
280:
281: public function getArray(): Type
282: {
283: $keyTypesCount = count($this->keyTypes);
284: if ($keyTypesCount === 0) {
285: return new ConstantArrayType([], []);
286: }
287:
288: if (!$this->degradeToGeneralArray) {
289: /** @var array<int, ConstantIntegerType|ConstantStringType> $keyTypes */
290: $keyTypes = $this->keyTypes;
291: return new ConstantArrayType($keyTypes, $this->valueTypes, $this->nextAutoIndexes, $this->optionalKeys, $this->isList);
292: }
293:
294: $array = new ArrayType(
295: TypeCombinator::union(...$this->keyTypes),
296: TypeCombinator::union(...$this->valueTypes),
297: );
298:
299: if (count($this->optionalKeys) < $keyTypesCount) {
300: $array = TypeCombinator::intersect($array, new NonEmptyArrayType());
301: }
302:
303: if ($this->oversized) {
304: $array = TypeCombinator::intersect($array, new OversizedArrayType());
305: }
306:
307: if ($this->isList->yes()) {
308: $array = TypeCombinator::intersect($array, new AccessoryArrayListType());
309: }
310:
311: return $array;
312: }
313:
314: public function isList(): bool
315: {
316: return $this->isList->yes();
317: }
318:
319: }
320: