From 5b3531b5a50c63e54dbde1e27be90a43fe24c25f Mon Sep 17 00:00:00 2001 From: Matt Brown Date: Wed, 13 Sep 2017 14:35:42 -0400 Subject: [PATCH] Fix null coalescing on nullable output --- .../Checker/Statements/ExpressionChecker.php | 15 ++++++++++++++- tests/Php70Test.php | 11 +++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) 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' => [ '