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: 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: $this->oversized = true;
198: }
199:
200: return;
201: }
202:
203: $scalarTypes = $offsetType->getConstantScalarTypes();
204: if (count($scalarTypes) === 0) {
205: $integerRanges = TypeUtils::getIntegerRanges($offsetType);
206: if (count($integerRanges) > 0) {
207: foreach ($integerRanges as $integerRange) {
208: if ($integerRange->getMin() === null) {
209: break;
210: }
211: if ($integerRange->getMax() === null) {
212: break;
213: }
214:
215: $rangeLength = $integerRange->getMax() - $integerRange->getMin();
216: if ($rangeLength >= self::ARRAY_COUNT_LIMIT) {
217: $scalarTypes = [];
218: break;
219: }
220:
221: foreach (range($integerRange->getMin(), $integerRange->getMax()) as $rangeValue) {
222: $scalarTypes[] = new ConstantIntegerType($rangeValue);
223: }
224: }
225: }
226: }
227: if (count($scalarTypes) > 0 && count($scalarTypes) < self::ARRAY_COUNT_LIMIT) {
228: $match = true;
229: $valueTypes = $this->valueTypes;
230: foreach ($scalarTypes as $scalarType) {
231: $scalarOffsetType = $scalarType->toArrayKey();
232: if (!$scalarOffsetType instanceof ConstantIntegerType && !$scalarOffsetType instanceof ConstantStringType) {
233: throw new ShouldNotHappenException();
234: }
235: $offsetMatch = false;
236:
237: /** @var ConstantIntegerType|ConstantStringType $keyType */
238: foreach ($this->keyTypes as $i => $keyType) {
239: if ($keyType->getValue() !== $scalarOffsetType->getValue()) {
240: continue;
241: }
242:
243: $valueTypes[$i] = TypeCombinator::union($valueTypes[$i], $valueType);
244: $offsetMatch = true;
245: }
246:
247: if ($offsetMatch) {
248: continue;
249: }
250:
251: $match = false;
252: }
253:
254: if ($match) {
255: $this->valueTypes = $valueTypes;
256: return;
257: }
258: }
259:
260: $this->isList = TrinaryLogic::createNo();
261: }
262:
263: if ($offsetType === null) {
264: $offsetType = TypeCombinator::union(...array_map(static fn (int $index) => new ConstantIntegerType($index), $this->nextAutoIndexes));
265: } else {
266: $this->isList = TrinaryLogic::createNo();
267: }
268:
269: $this->keyTypes[] = $offsetType;
270: $this->valueTypes[] = $valueType;
271: if ($optional) {
272: $this->optionalKeys[] = count($this->keyTypes) - 1;
273: }
274: $this->degradeToGeneralArray = true;
275: }
276:
277: public function degradeToGeneralArray(bool $oversized = false): void
278: {
279: $this->degradeToGeneralArray = true;
280: $this->oversized = $this->oversized || $oversized;
281: }
282:
283: public function getArray(): Type
284: {
285: $keyTypesCount = count($this->keyTypes);
286: if ($keyTypesCount === 0) {
287: return new ConstantArrayType([], []);
288: }
289:
290: if (!$this->degradeToGeneralArray) {
291: /** @var array<int, ConstantIntegerType|ConstantStringType> $keyTypes */
292: $keyTypes = $this->keyTypes;
293: return new ConstantArrayType($keyTypes, $this->valueTypes, $this->nextAutoIndexes, $this->optionalKeys, $this->isList);
294: }
295:
296: $array = new ArrayType(
297: TypeCombinator::union(...$this->keyTypes),
298: TypeCombinator::union(...$this->valueTypes),
299: );
300:
301: if (count($this->optionalKeys) < $keyTypesCount) {
302: $array = TypeCombinator::intersect($array, new NonEmptyArrayType());
303: }
304:
305: if ($this->oversized) {
306: $array = TypeCombinator::intersect($array, new OversizedArrayType());
307: }
308:
309: if ($this->isList->yes()) {
310: $array = TypeCombinator::intersect($array, new AccessoryArrayListType());
311: }
312:
313: return $array;
314: }
315:
316: public function isList(): bool
317: {
318: return $this->isList->yes();
319: }
320:
321: }
322: