From 24aee609ebf91b0100e2f60b78cde5a785936b81 Mon Sep 17 00:00:00 2001 From: Ondrej Mirtes Date: Thu, 11 Aug 2022 13:33:06 +0200 Subject: [PATCH] ArrayType::setOffsetValueType() - generalize key type if it goes over a threshold --- src/Type/ArrayType.php | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/Type/ArrayType.php b/src/Type/ArrayType.php index 309e96bcc5..0dc7c828b8 100644 --- a/src/Type/ArrayType.php +++ b/src/Type/ArrayType.php @@ -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; @@ -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) {