1
0
mirror of https://github.com/danog/psalm.git synced 2024-11-30 04:39:00 +01:00

Don’t suggest a potential value that’s undefined

Fixes #4754
This commit is contained in:
Matt Brown 2020-12-02 14:49:30 -05:00 committed by Daniil Gentili
parent f8e22ab7ac
commit c54416090f
Signed by: danog
GPG Key ID: 8C1BE3B34B230CA7
2 changed files with 27 additions and 1 deletions

View File

@ -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();
}

View File

@ -299,6 +299,19 @@ class ArrayKeyExistsTest extends \Psalm\Tests\TestCase
'MixedArgument',
],
],
'arrayKeyExistsTwoVars' => [
'<?php
/**
* @param array{a: string, b: string, c?: string} $info
*/
function getReason(array $info, string $key, string $value): bool {
if (array_key_exists($key, $info) && $info[$key] === $value) {
return true;
}
return false;
}'
],
];
}