diff --git a/src/Psalm/Checker/Statements/ExpressionChecker.php b/src/Psalm/Checker/Statements/ExpressionChecker.php index 29c7ea1de..bf8d97a62 100644 --- a/src/Psalm/Checker/Statements/ExpressionChecker.php +++ b/src/Psalm/Checker/Statements/ExpressionChecker.php @@ -1026,7 +1026,20 @@ class ExpressionChecker $lhs_type = null; if (isset($stmt->left->inferredType)) { - $lhs_type = $stmt->left->inferredType; + $if_return_type_reconciled = TypeChecker::reconcileTypes( + '!null', + $stmt->left->inferredType, + '', + $statements_checker, + new CodeLocation($statements_checker->getSource(), $stmt), + $statements_checker->getSuppressedIssues() + ); + + if ($if_return_type_reconciled === false) { + return false; + } + + $lhs_type = $if_return_type_reconciled; } if (!$lhs_type || !isset($stmt->right->inferredType)) { diff --git a/tests/Php70Test.php b/tests/Php70Test.php index 067d03a38..bd8578403 100644 --- a/tests/Php70Test.php +++ b/tests/Php70Test.php @@ -58,6 +58,17 @@ class Php70Test extends TestCase '$a' => 'string|null', ], ], + 'nullCoalesceWithNullableOnLeft' => [ + ' 5 ? "hello" : null; + } + $a = foo() ?? "goodbye";', + 'assertions' => [ + '$a' => 'string', + ], + ], 'nullCoalesceWithReference' => [ '