From 0d477221950c802179fb775475f8ab3f47b9c827 Mon Sep 17 00:00:00 2001 From: orklah Date: Mon, 22 Nov 2021 21:13:25 +0100 Subject: [PATCH] fix counting array properties when some are Never --- .../Call/FunctionCallReturnTypeFetcher.php | 12 ++++++++++-- .../TypeReconciliation/RedundantConditionTest.php | 14 +++++++++++++- 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/src/Psalm/Internal/Analyzer/Statements/Expression/Call/FunctionCallReturnTypeFetcher.php b/src/Psalm/Internal/Analyzer/Statements/Expression/Call/FunctionCallReturnTypeFetcher.php index c6f2f9198bf..8b6e6c54dcf 100644 --- a/src/Psalm/Internal/Analyzer/Statements/Expression/Call/FunctionCallReturnTypeFetcher.php +++ b/src/Psalm/Internal/Analyzer/Statements/Expression/Call/FunctionCallReturnTypeFetcher.php @@ -341,10 +341,18 @@ private static function getReturnTypeFromCallMapWithArgs( $min = 0; $max = 0; foreach ($atomic_types['array']->properties as $property) { - if (!$property->possibly_undefined) { + // empty, never and possibly undefined can't count for min value + if (!$property->possibly_undefined + && !$property->isEmpty() + && !$property->isNever() + ) { $min++; } - $max++; + + //empty and never can't count for max value because we know keys are undefined + if (!$property->isEmpty() && !$property->isNever()) { + $max++; + } } if ($atomic_types['array']->sealed) { diff --git a/tests/TypeReconciliation/RedundantConditionTest.php b/tests/TypeReconciliation/RedundantConditionTest.php index e0e003595fb..18bfc9cae35 100644 --- a/tests/TypeReconciliation/RedundantConditionTest.php +++ b/tests/TypeReconciliation/RedundantConditionTest.php @@ -867,7 +867,19 @@ public function get(): ?stdClass{ return new stdClass;} } ' - ] + ], + 'countWithNeverValuesInKeyedArray' => [ + '