diff --git a/src/Type/ArrayType.php b/src/Type/ArrayType.php index 986081b68ea..a8dce08d738 100644 --- a/src/Type/ArrayType.php +++ b/src/Type/ArrayType.php @@ -275,26 +275,30 @@ public function setOffsetValueType(?Type $offsetType, Type $valueType, bool $uni $offsetType = self::castToArrayKeyType($offsetType); } - if ( - ($offsetType instanceof ConstantStringType || $offsetType instanceof ConstantIntegerType) - && $offsetType->isSuperTypeOf($this->keyType)->yes() - ) { - $builder = ConstantArrayTypeBuilder::createEmpty(); - $builder->setOffsetValueType($offsetType, $valueType); - return $builder->getArray(); - } - - $array = new self( - TypeCombinator::union($this->keyType, $offsetType), - $unionValues || $offsetType instanceof ConstantStringType || $offsetType instanceof ConstantIntegerType - ? TypeCombinator::union($this->itemType, $valueType) - : $valueType, - ); if ($offsetType instanceof ConstantStringType || $offsetType instanceof ConstantIntegerType) { - return TypeCombinator::intersect($array, new HasOffsetValueType($offsetType, $valueType), new NonEmptyArrayType()); + if ($offsetType->isSuperTypeOf($this->keyType)->yes()) { + $builder = ConstantArrayTypeBuilder::createEmpty(); + $builder->setOffsetValueType($offsetType, $valueType); + return $builder->getArray(); + } + + return TypeCombinator::intersect( + new self( + TypeCombinator::union($this->keyType, $offsetType), + TypeCombinator::union($this->itemType, $valueType), + ), + new HasOffsetValueType($offsetType, $valueType), + new NonEmptyArrayType() + ); } - return TypeCombinator::intersect($array, new NonEmptyArrayType()); + return TypeCombinator::intersect( + new self( + TypeCombinator::union($this->keyType, $offsetType), + $unionValues ? TypeCombinator::union($this->itemType, $valueType) : $valueType, + ), + new NonEmptyArrayType() + ); } public function unsetOffset(Type $offsetType): Type