From e529402e34172e5e46fbb74bc1d01b41b87344f3 Mon Sep 17 00:00:00 2001 From: Daniil Gentili Date: Thu, 1 Dec 2022 20:30:11 +0100 Subject: [PATCH] Fix --- .../Expression/Fetch/ArrayFetchAnalyzer.php | 50 ++++++++----------- 1 file changed, 20 insertions(+), 30 deletions(-) diff --git a/src/Psalm/Internal/Analyzer/Statements/Expression/Fetch/ArrayFetchAnalyzer.php b/src/Psalm/Internal/Analyzer/Statements/Expression/Fetch/ArrayFetchAnalyzer.php index 1c19e81a8..f71e98c75 100644 --- a/src/Psalm/Internal/Analyzer/Statements/Expression/Fetch/ArrayFetchAnalyzer.php +++ b/src/Psalm/Internal/Analyzer/Statements/Expression/Fetch/ArrayFetchAnalyzer.php @@ -1505,18 +1505,8 @@ class ArrayFetchAnalyzer ): void { $generic_key_type = $type->getGenericKeyType(); - if (!$stmt->dim && $type->fallback_params === null) { - if ($type->is_list) { - $key_values[] = new TLiteralInt(count($type->properties)); - } else { - $init = -1; - foreach ($type->properties as $k => $_) { - if (is_int($k)) { - $init = $k; - } - } - $key_values[] = new TLiteralInt($init+1); - } + if (!$stmt->dim && $type->fallback_params === null && $type->is_list) { + $key_values[] = new TLiteralInt(count($type->properties)); } if ($key_values) { @@ -1652,20 +1642,14 @@ class ArrayFetchAnalyzer $offset_type->isMixed() ? Type::getArrayKey() : $offset_type->freeze() ); - // Happens only if $type is an unsealed array or list if (!$stmt->dim) { - assert($type->fallback_params !== null); if ($type->is_list) { - if ($new_key_type !== $type->fallback_params[0] - || $generic_params !== $type->fallback_params[1] - ) { - $type = new TKeyedArray( - $type->properties, - null, - [$new_key_type, $generic_params], - true - ); - } + $type = new TKeyedArray( + $type->properties, + null, + [$new_key_type, $generic_params], + true + ); } else { $type = new TNonEmptyArray([ $new_key_type, @@ -1673,12 +1657,18 @@ class ArrayFetchAnalyzer ], null, $type->getMinCount()+1); } } else { - $type = new TKeyedArray( - $type->properties, - null, - [$new_key_type, $generic_params], - false - ); + $min_count = $type->getMinCount(); + if ($min_count) { + $type = new TNonEmptyArray([ + $new_key_type, + $generic_params, + ], null, $min_count); + } else { + $type = new TArray([ + $new_key_type, + $generic_params, + ]); + } } $array_access_type = Type::combineUnionTypes(