diff --git a/src/Psalm/Internal/Analyzer/Statements/Expression/AssertionFinder.php b/src/Psalm/Internal/Analyzer/Statements/Expression/AssertionFinder.php index ff17fdd07..cb2075f9a 100644 --- a/src/Psalm/Internal/Analyzer/Statements/Expression/AssertionFinder.php +++ b/src/Psalm/Internal/Analyzer/Statements/Expression/AssertionFinder.php @@ -2262,19 +2262,24 @@ class AssertionFinder foreach ($second_arg_type->getAtomicTypes() as $atomic_type) { if ($atomic_type instanceof Type\Atomic\TArray || $atomic_type instanceof Type\Atomic\TKeyedArray + || $atomic_type instanceof Type\Atomic\TList ) { - if ($atomic_type instanceof Type\Atomic\TKeyedArray) { - $atomic_type = $atomic_type->getGenericArrayType(); + if ($atomic_type instanceof Type\Atomic\TList) { + $key_type = $atomic_type->type_param; + } elseif ($atomic_type instanceof Type\Atomic\TKeyedArray) { + $key_type = $atomic_type->getGenericKeyType(); + } else { + $key_type = $atomic_type->type_params[1]; } $array_literal_types = array_merge( - $atomic_type->type_params[1]->getLiteralStrings(), - $atomic_type->type_params[1]->getLiteralInts(), - $atomic_type->type_params[1]->getLiteralFloats() + $key_type->getLiteralStrings(), + $key_type->getLiteralInts(), + $key_type->getLiteralFloats() ); if ($array_literal_types - && count($atomic_type->type_params[1]->getAtomicTypes()) + && count($key_type->getAtomicTypes()) ) { $literal_assertions = []; @@ -2282,11 +2287,11 @@ class AssertionFinder $literal_assertions[] = '=' . $array_literal_type->getId(); } - if ($atomic_type->type_params[1]->isFalsable()) { + if ($key_type->isFalsable()) { $literal_assertions[] = 'false'; } - if ($atomic_type->type_params[1]->isNullable()) { + if ($key_type->isNullable()) { $literal_assertions[] = 'null'; } diff --git a/tests/TypeReconciliation/ValueTest.php b/tests/TypeReconciliation/ValueTest.php index 0af5e69e2..3229fc197 100644 --- a/tests/TypeReconciliation/ValueTest.php +++ b/tests/TypeReconciliation/ValueTest.php @@ -781,6 +781,26 @@ class ValueTest extends \Psalm\Tests\TestCase if (false === ($a > 1)){} }' ], + 'returnFromUnionLiteral' => [ + ' + */ + function getSupportedConsts() { + return ["a1", "a2"]; + } + + function foo(mixed $file) : string { + if (in_array($file, getSupportedConsts(), true)) { + return $file; + } + + return ""; + }', + [], + [], + '8.0' + ], ]; }