From 97685b229b5721a6ca419d3cdbbdd08b316c0200 Mon Sep 17 00:00:00 2001 From: Vincent Langlet Date: Sun, 1 Sep 2024 15:05:12 +0200 Subject: [PATCH 1/2] Fix --- src/Type/Constant/ConstantArrayType.php | 4 +++- src/Type/Constant/ConstantArrayTypeBuilder.php | 3 +++ tests/PHPStan/Analyser/nsrt/array-is-list-unset.php | 2 +- 3 files changed, 7 insertions(+), 2 deletions(-) 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..49ba96215a 100644 --- a/tests/PHPStan/Analyser/nsrt/array-is-list-unset.php +++ b/tests/PHPStan/Analyser/nsrt/array-is-list-unset.php @@ -19,7 +19,7 @@ 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)); From 61b61607386c2e8707b2cbc889479b8689f122f8 Mon Sep 17 00:00:00 2001 From: Vincent Langlet Date: Wed, 4 Sep 2024 22:54:45 +0200 Subject: [PATCH 2/2] Add test --- tests/PHPStan/Analyser/nsrt/array-is-list-unset.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/tests/PHPStan/Analyser/nsrt/array-is-list-unset.php b/tests/PHPStan/Analyser/nsrt/array-is-list-unset.php index 49ba96215a..4c67982461 100644 --- a/tests/PHPStan/Analyser/nsrt/array-is-list-unset.php +++ b/tests/PHPStan/Analyser/nsrt/array-is-list-unset.php @@ -23,4 +23,7 @@ function () { $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)); };