From ef4afd72bca50a0aff61599d3e433c9ee64287ac Mon Sep 17 00:00:00 2001 From: Matt Brown Date: Wed, 13 Jan 2021 18:10:59 -0500 Subject: [PATCH] Unset should change array with keyed offsets --- src/Psalm/Internal/Analyzer/Statements/UnsetAnalyzer.php | 4 +++- src/Psalm/Type/Atomic/TKeyedArray.php | 4 ++-- tests/TypeReconciliation/RedundantConditionTest.php | 8 ++++++++ 3 files changed, 13 insertions(+), 3 deletions(-) diff --git a/src/Psalm/Internal/Analyzer/Statements/UnsetAnalyzer.php b/src/Psalm/Internal/Analyzer/Statements/UnsetAnalyzer.php index 7ecd878eb..d414bfe90 100644 --- a/src/Psalm/Internal/Analyzer/Statements/UnsetAnalyzer.php +++ b/src/Psalm/Internal/Analyzer/Statements/UnsetAnalyzer.php @@ -81,8 +81,10 @@ class UnsetAnalyzer $atomic_root_type->sealed = false; $root_type->addType( - $atomic_root_type->getGenericArrayType() + $atomic_root_type->getGenericArrayType(false) ); + + $atomic_root_type->is_list = false; } } elseif ($atomic_root_type instanceof Type\Atomic\TNonEmptyArray) { $root_type->addType( diff --git a/src/Psalm/Type/Atomic/TKeyedArray.php b/src/Psalm/Type/Atomic/TKeyedArray.php index 9424d4aab..88a40285f 100644 --- a/src/Psalm/Type/Atomic/TKeyedArray.php +++ b/src/Psalm/Type/Atomic/TKeyedArray.php @@ -246,7 +246,7 @@ class TKeyedArray extends \Psalm\Type\Atomic return $value_type; } - public function getGenericArrayType(): TArray + public function getGenericArrayType(bool $allow_non_empty = true): TArray { $key_types = []; $value_type = null; @@ -285,7 +285,7 @@ class TKeyedArray extends \Psalm\Type\Atomic $value_type->possibly_undefined = false; - if ($this->previous_value_type || $has_defined_keys) { + if ($allow_non_empty && ($this->previous_value_type || $has_defined_keys)) { $array_type = new TNonEmptyArray([$key_type, $value_type]); } else { $array_type = new TArray([$key_type, $value_type]); diff --git a/tests/TypeReconciliation/RedundantConditionTest.php b/tests/TypeReconciliation/RedundantConditionTest.php index f1de64ba5..01f5120b7 100644 --- a/tests/TypeReconciliation/RedundantConditionTest.php +++ b/tests/TypeReconciliation/RedundantConditionTest.php @@ -797,6 +797,14 @@ class RedundantConditionTest extends \Psalm\Tests\TestCase return (int) (hexdec($x) + 1); }', ], + 'unsetArrayWithKnownOffset' => [ + '