Skip to content

Commit 10ba3c6

Browse files
committed
ArrayType::setOffsetValueType() - generalize item type if it goes over a threshold
1 parent 24aee60 commit 10ba3c6

File tree

1 file changed

+9
-4
lines changed

1 file changed

+9
-4
lines changed

src/Type/ArrayType.php

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -285,10 +285,15 @@ public function setOffsetValueType(?Type $offsetType, Type $valueType, bool $uni
285285
$keyType = $keyType->generalize(GeneralizePrecision::moreSpecific());
286286
}
287287

288-
$array = new self(
289-
$keyType,
290-
$unionValues ? TypeCombinator::union($this->itemType, $valueType) : $valueType,
291-
);
288+
$itemType = $valueType;
289+
if ($unionValues) {
290+
$itemType = TypeCombinator::union($this->itemType, $valueType);
291+
if (count(TypeUtils::getConstantScalars($itemType)) > ConstantArrayTypeBuilder::ARRAY_COUNT_LIMIT) {
292+
$itemType = $itemType->generalize(GeneralizePrecision::moreSpecific());
293+
}
294+
}
295+
296+
$array = new self($keyType, $itemType,);
292297
if ($offsetType instanceof ConstantIntegerType || $offsetType instanceof ConstantStringType) {
293298
return TypeCombinator::intersect($array, new HasOffsetValueType($offsetType, $valueType), new NonEmptyArrayType());
294299
}

0 commit comments

Comments
 (0)