From c54416090f31be4ae7934c8dbc3ea1f74df728bb Mon Sep 17 00:00:00 2001 From: Matt Brown Date: Wed, 2 Dec 2020 14:49:30 -0500 Subject: [PATCH] =?UTF-8?q?Don=E2=80=99t=20suggest=20a=20potential=20value?= =?UTF-8?q?=20that=E2=80=99s=20undefined?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fixes #4754 --- .../Statements/Expression/AssertionFinder.php | 15 ++++++++++++++- tests/TypeReconciliation/ArrayKeyExistsTest.php | 13 +++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/src/Psalm/Internal/Analyzer/Statements/Expression/AssertionFinder.php b/src/Psalm/Internal/Analyzer/Statements/Expression/AssertionFinder.php index d17d5c3dc..0c32886ba 100644 --- a/src/Psalm/Internal/Analyzer/Statements/Expression/AssertionFinder.php +++ b/src/Psalm/Internal/Analyzer/Statements/Expression/AssertionFinder.php @@ -3733,12 +3733,25 @@ class AssertionFinder || $atomic_type instanceof Type\Atomic\TKeyedArray ) { if ($atomic_type instanceof Type\Atomic\TKeyedArray) { + $key_possibly_undefined = false; + + foreach ($atomic_type->properties as $property_type) { + if ($property_type->possibly_undefined) { + $key_possibly_undefined = true; + break; + } + } + $key_type = $atomic_type->getGenericKeyType(); + + if ($key_possibly_undefined) { + $key_type->possibly_undefined = true; + } } else { $key_type = $atomic_type->type_params[0]; } - if ($key_type->allStringLiterals()) { + if ($key_type->allStringLiterals() && !$key_type->possibly_undefined) { foreach ($key_type->getLiteralStrings() as $array_literal_type) { $literal_assertions[] = '=' . $array_literal_type->getId(); } diff --git a/tests/TypeReconciliation/ArrayKeyExistsTest.php b/tests/TypeReconciliation/ArrayKeyExistsTest.php index 7bbc8efbb..0268ea081 100644 --- a/tests/TypeReconciliation/ArrayKeyExistsTest.php +++ b/tests/TypeReconciliation/ArrayKeyExistsTest.php @@ -299,6 +299,19 @@ class ArrayKeyExistsTest extends \Psalm\Tests\TestCase 'MixedArgument', ], ], + 'arrayKeyExistsTwoVars' => [ + '