Skip to content

Commit 24aee60

Browse files
committed
ArrayType::setOffsetValueType() - generalize key type if it goes over a threshold
1 parent 519d705 commit 24aee60

File tree

1 file changed

+7
-1
lines changed

1 file changed

+7
-1
lines changed

src/Type/ArrayType.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
use PHPStan\Type\Traits\UndecidedBooleanTypeTrait;
2626
use PHPStan\Type\Traits\UndecidedComparisonTypeTrait;
2727
use function array_merge;
28+
use function count;
2829
use function is_float;
2930
use function is_int;
3031
use function key;
@@ -279,8 +280,13 @@ public function setOffsetValueType(?Type $offsetType, Type $valueType, bool $uni
279280
return $builder->getArray();
280281
}
281282

283+
$keyType = TypeCombinator::union($this->keyType, $offsetType);
284+
if (count(TypeUtils::getConstantScalars($keyType)) > ConstantArrayTypeBuilder::ARRAY_COUNT_LIMIT) {
285+
$keyType = $keyType->generalize(GeneralizePrecision::moreSpecific());
286+
}
287+
282288
$array = new self(
283-
TypeCombinator::union($this->keyType, $offsetType),
289+
$keyType,
284290
$unionValues ? TypeCombinator::union($this->itemType, $valueType) : $valueType,
285291
);
286292
if ($offsetType instanceof ConstantIntegerType || $offsetType instanceof ConstantStringType) {

0 commit comments

Comments
 (0)