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: /** @api */
26: class ConstantArrayTypeBuilder
27: {
28:
29: public const ARRAY_COUNT_LIMIT = 256;
30:
31: private bool $degradeToGeneralArray = false;
32:
33: private bool $oversized = false;
34:
35: /**
36: * @param array<int, Type> $keyTypes
37: * @param array<int, Type> $valueTypes
38: * @param non-empty-list<int> $nextAutoIndexes
39: * @param array<int> $optionalKeys
40: */
41: private function __construct(
42: private array $keyTypes,
43: private array $valueTypes,
44: private array $nextAutoIndexes,
45: private array $optionalKeys,
46: private TrinaryLogic $isList,
47: )
48: {
49: }
50:
51: public static function createEmpty(): self
52: {
53: return new self([], [], [0], [], TrinaryLogic::createYes());
54: }
55:
56: public static function createFromConstantArray(ConstantArrayType $startArrayType): self
57: {
58: $builder = new self(
59: $startArrayType->getKeyTypes(),
60: $startArrayType->getValueTypes(),
61: $startArrayType->getNextAutoIndexes(),
62: $startArrayType->getOptionalKeys(),
63: $startArrayType->isList(),
64: );
65:
66: if (count($startArrayType->getKeyTypes()) > self::ARRAY_COUNT_LIMIT) {
67: $builder->degradeToGeneralArray(true);
68: }
69:
70: return $builder;
71: }
72:
73: public function setOffsetValueType(?Type $offsetType, Type $valueType, bool $optional = false): void
74: {
75: if ($offsetType !== null) {
76: $offsetType = $offsetType->toArrayKey();
77: }
78:
79: if (!$this->degradeToGeneralArray) {
80: if ($offsetType === null) {
81: $newAutoIndexes = $optional ? $this->nextAutoIndexes : [];
82: $hasOptional = false;
83: foreach ($this->keyTypes as $i => $keyType) {
84: if (!$keyType instanceof ConstantIntegerType) {
85: continue;
86: }
87:
88: if (!in_array($keyType->getValue(), $this->nextAutoIndexes, true)) {
89: continue;
90: }
91:
92: $this->valueTypes[$i] = TypeCombinator::union($this->valueTypes[$i], $valueType);
93:
94: if (!$hasOptional && !$optional) {
95: $this->optionalKeys = array_values(array_filter($this->optionalKeys, static fn (int $index): bool => $index !== $i));
96: }
97:
98: /** @var int|float $newAutoIndex */
99: $newAutoIndex = $keyType->getValue() + 1;
100: if (is_float($newAutoIndex)) {
101: $newAutoIndex = $keyType->getValue();
102: }
103:
104: $newAutoIndexes[] = $newAutoIndex;
105: $hasOptional = true;
106: }
107:
108: $max = max($this->nextAutoIndexes);
109:
110: $this->keyTypes[] = new ConstantIntegerType($max);
111: $this->valueTypes[] = $valueType;
112:
113: /** @var int|float $newAutoIndex */
114: $newAutoIndex = $max + 1;
115: if (is_float($newAutoIndex)) {
116: $newAutoIndex = $max;
117: }
118:
119: $newAutoIndexes[] = $newAutoIndex;
120: $this->nextAutoIndexes = array_values(array_unique($newAutoIndexes));
121:
122: if ($optional || $hasOptional) {
123: $this->optionalKeys[] = count($this->keyTypes) - 1;
124: }
125:
126: if (count($this->keyTypes) > self::ARRAY_COUNT_LIMIT) {
127: $this->degradeToGeneralArray = true;
128: }
129:
130: return;
131: }
132:
133: if ($offsetType instanceof ConstantIntegerType || $offsetType instanceof ConstantStringType) {
134: /** @var ConstantIntegerType|ConstantStringType $keyType */
135: foreach ($this->keyTypes as $i => $keyType) {
136: if ($keyType->getValue() !== $offsetType->getValue()) {
137: continue;
138: }
139:
140: if ($optional) {
141: $valueType = TypeCombinator::union($valueType, $this->valueTypes[$i]);
142: }
143:
144: $this->valueTypes[$i] = $valueType;
145:
146: if (!$optional) {
147: $this->optionalKeys = array_values(array_filter($this->optionalKeys, static fn (int $index): bool => $index !== $i));
148: if ($keyType instanceof ConstantIntegerType) {
149: $nextAutoIndexes = array_values(array_filter($this->nextAutoIndexes, static fn (int $index) => $index > $keyType->getValue()));
150: if (count($nextAutoIndexes) === 0) {
151: throw new ShouldNotHappenException();
152: }
153: $this->nextAutoIndexes = $nextAutoIndexes;
154: }
155: }
156: return;
157: }
158:
159: $this->keyTypes[] = $offsetType;
160: $this->valueTypes[] = $valueType;
161:
162: if ($offsetType instanceof ConstantIntegerType) {
163: $min = min($this->nextAutoIndexes);
164: $max = max($this->nextAutoIndexes);
165: if ($offsetType->getValue() > $min) {
166: if ($offsetType->getValue() <= $max) {
167: $this->isList = $this->isList->and(TrinaryLogic::createMaybe());
168: } else {
169: $this->isList = TrinaryLogic::createNo();
170: }
171: }
172: if ($offsetType->getValue() >= $max) {
173: /** @var int|float $newAutoIndex */
174: $newAutoIndex = $offsetType->getValue() + 1;
175: if (is_float($newAutoIndex)) {
176: $newAutoIndex = $max;
177: }
178: if (!$optional) {
179: $this->nextAutoIndexes = [$newAutoIndex];
180: } else {
181: $this->nextAutoIndexes[] = $newAutoIndex;
182: }
183: }
184: } else {
185: $this->isList = TrinaryLogic::createNo();
186: }
187:
188: if ($optional) {
189: $this->optionalKeys[] = count($this->keyTypes) - 1;
190: }
191:
192: if (count($this->keyTypes) > self::ARRAY_COUNT_LIMIT) {
193: $this->degradeToGeneralArray = true;
194: }
195:
196: return;
197: }
198:
199: $this->isList = TrinaryLogic::createNo();
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:
259: if ($offsetType === null) {
260: $offsetType = TypeCombinator::union(...array_map(static fn (int $index) => new ConstantIntegerType($index), $this->nextAutoIndexes));
261: } else {
262: $this->isList = TrinaryLogic::createNo();
263: }
264:
265: $this->keyTypes[] = $offsetType;
266: $this->valueTypes[] = $valueType;
267: if ($optional) {
268: $this->optionalKeys[] = count($this->keyTypes) - 1;
269: }
270: $this->degradeToGeneralArray = true;
271: }
272:
273: public function degradeToGeneralArray(bool $oversized = false): void
274: {
275: $this->degradeToGeneralArray = true;
276: $this->oversized = $this->oversized || $oversized;
277: }
278:
279: public function getArray(): Type
280: {
281: $keyTypesCount = count($this->keyTypes);
282: if ($keyTypesCount === 0) {
283: return new ConstantArrayType([], []);
284: }
285:
286: if (!$this->degradeToGeneralArray) {
287: /** @var array<int, ConstantIntegerType|ConstantStringType> $keyTypes */
288: $keyTypes = $this->keyTypes;
289: return new ConstantArrayType($keyTypes, $this->valueTypes, $this->nextAutoIndexes, $this->optionalKeys, $this->isList);
290: }
291:
292: $array = new ArrayType(
293: TypeCombinator::union(...$this->keyTypes),
294: TypeCombinator::union(...$this->valueTypes),
295: );
296:
297: if (count($this->optionalKeys) < $keyTypesCount) {
298: $array = TypeCombinator::intersect($array, new NonEmptyArrayType());
299: }
300:
301: if ($this->oversized) {
302: $array = TypeCombinator::intersect($array, new OversizedArrayType());
303: }
304:
305: if ($this->isList->yes()) {
306: $array = AccessoryArrayListType::intersectWith($array);
307: }
308:
309: return $array;
310: }
311:
312: public function isList(): bool
313: {
314: return $this->isList->yes();
315: }
316:
317: }
318: