Skip to content

Commit

Permalink
ArrayType::setOffsetValueType() - generalize key type if it goes over…
Browse files Browse the repository at this point in the history
… a threshold
  • Loading branch information
ondrejmirtes committed Aug 11, 2022
1 parent 519d705 commit 24aee60
Showing 1 changed file with 7 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/Type/ArrayType.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
use PHPStan\Type\Traits\UndecidedBooleanTypeTrait;
use PHPStan\Type\Traits\UndecidedComparisonTypeTrait;
use function array_merge;
use function count;
use function is_float;
use function is_int;
use function key;
Expand Down Expand Up @@ -279,8 +280,13 @@ public function setOffsetValueType(?Type $offsetType, Type $valueType, bool $uni
return $builder->getArray();
}

$keyType = TypeCombinator::union($this->keyType, $offsetType);
if (count(TypeUtils::getConstantScalars($keyType)) > ConstantArrayTypeBuilder::ARRAY_COUNT_LIMIT) {
$keyType = $keyType->generalize(GeneralizePrecision::moreSpecific());
}

$array = new self(
TypeCombinator::union($this->keyType, $offsetType),
$keyType,
$unionValues ? TypeCombinator::union($this->itemType, $valueType) : $valueType,
);
if ($offsetType instanceof ConstantIntegerType || $offsetType instanceof ConstantStringType) {
Expand Down

0 comments on commit 24aee60

Please sign in to comment.