Skip to content

Commit

Permalink
Don’t suggest a potential value that’s undefined
Browse files Browse the repository at this point in the history
Fixes #4754
  • Loading branch information
muglug committed Dec 2, 2020
1 parent b64eb6d commit e2bb02e
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -3733,12 +3733,25 @@ private static function getArrayKeyExistsAssertions(
|| $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();
}
Expand Down
13 changes: 13 additions & 0 deletions tests/TypeReconciliation/ArrayKeyExistsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,19 @@ public function getPosters($commenter, $numToGet=10) {
'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;
}'
],
];
}

Expand Down

0 comments on commit e2bb02e

Please sign in to comment.