Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix array_key_exists() with all int literal keys #5197

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3770,7 +3770,7 @@ private static function getArrayKeyExistsAssertions(
}
} elseif ($key_type->allIntLiterals() && !$key_type->possibly_undefined) {
foreach ($key_type->getLiteralInts() as $array_literal_type) {
$literal_assertions[] = '=' . $array_literal_type->getAssertionString();
$literal_assertions[] = '~' . $array_literal_type->getAssertionString();
}
}
}
Expand Down
28 changes: 28 additions & 0 deletions tests/TypeReconciliation/ArrayKeyExistsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,34 @@ function checkArrayKeyExistsInt(array $arr, int $int): int
return 1;
}'
],
'comparesStringAndAllIntKeysCorrectly' => [
'<?php
/**
* @param array<1|2|3, string> $arr
* @return bool
*/
function checkArrayKeyExistsComparison(array $arr, string $key): bool
{
if (array_key_exists($key, $arr)) {
return true;
}
return false;
}'
],
'comparesStringAndAllIntKeysCorrectlyNegated' => [
'<?php
/**
* @param array<1|2|3, string> $arr
* @return bool
*/
function checkArrayKeyExistsComparisonNegated(array $arr, string $key): bool
{
if (!array_key_exists($key, $arr)) {
return false;
}
return true;
}'
],
];
}

Expand Down