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

Negated is_numeric narrows scalar type #9339

Merged
merged 2 commits into from
Feb 19, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/Psalm/Internal/Type/SimpleNegatedAssertionReconciler.php
Original file line number Diff line number Diff line change
Expand Up @@ -1292,6 +1292,10 @@ private static function reconcileNumeric(
} elseif ($type instanceof TArrayKey) {
$redundant = false;
$non_numeric_types[] = new TString();
} elseif ($type instanceof TScalar) {
$redundant = false;
$non_numeric_types[] = new TString();
$non_numeric_types[] = new TBool();
} elseif (!$type->isNumericType()) {
$non_numeric_types[] = $type;
} else {
Expand Down
14 changes: 14 additions & 0 deletions tests/TypeReconciliation/ConditionalTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,20 @@ function foo ($a) {
'$a' => 'string',
],
],
'typeRefinementonWithNegatedIsNumeric' => [
'code' => '<?php
/**
* @param scalar $v
* @return bool|string
*/
function toString($)
weirdan marked this conversation as resolved.
Show resolved Hide resolved
{
if (is_numeric($v)) {
return false;
}
return $v;
}',
],
'typeRefinementWithStringOrTrue' => [
'code' => '<?php
$a = rand(0, 5) > 4 ? "hello" : true;
Expand Down