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