diff --git a/src/Type/Constant/ConstantArrayType.php b/src/Type/Constant/ConstantArrayType.php index 3853794348..38cbd0c7cb 100644 --- a/src/Type/Constant/ConstantArrayType.php +++ b/src/Type/Constant/ConstantArrayType.php @@ -734,7 +734,9 @@ public function unsetOffset(Type $offsetType): Type $k++; } - return new self($newKeyTypes, $newValueTypes, $this->nextAutoIndexes, $newOptionalKeys, TrinaryLogic::createNo()); + $isList = $this->isList()->and(TrinaryLogic::createFromBoolean($i === count($this->keyTypes) - 1)); + + return new self($newKeyTypes, $newValueTypes, $this->nextAutoIndexes, $newOptionalKeys, $isList); } return $this; diff --git a/src/Type/Constant/ConstantArrayTypeBuilder.php b/src/Type/Constant/ConstantArrayTypeBuilder.php index a306833e8d..7604ad5b41 100644 --- a/src/Type/Constant/ConstantArrayTypeBuilder.php +++ b/src/Type/Constant/ConstantArrayTypeBuilder.php @@ -109,6 +109,9 @@ public function setOffsetValueType(?Type $offsetType, Type $valueType, bool $opt } $max = max($this->nextAutoIndexes); + if ($max !== count($this->keyTypes)) { + $this->isList = TrinaryLogic::createNo(); + } $this->keyTypes[] = new ConstantIntegerType($max); $this->valueTypes[] = $valueType; diff --git a/tests/PHPStan/Analyser/nsrt/array-is-list-unset.php b/tests/PHPStan/Analyser/nsrt/array-is-list-unset.php index b14b8c97df..4c67982461 100644 --- a/tests/PHPStan/Analyser/nsrt/array-is-list-unset.php +++ b/tests/PHPStan/Analyser/nsrt/array-is-list-unset.php @@ -19,8 +19,11 @@ function () { assertType('true', array_is_list($a)); unset($a[2]); assertType('array{1, 2}', $a); - assertType('false', array_is_list($a)); + assertType('true', array_is_list($a)); $a[] = 4; assertType('array{0: 1, 1: 2, 3: 4}', $a); assertType('false', array_is_list($a)); + unset($a[3]); + assertType('array{0: 1, 1: 2}', $a); + assertType('true', array_is_list($a)); };