1
0
mirror of https://github.com/danog/psalm.git synced 2024-11-26 20:34:47 +01:00

Fix null coalescing on nullable output

This commit is contained in:
Matt Brown 2017-09-13 14:35:42 -04:00
parent eee8442af6
commit 5b3531b5a5
2 changed files with 25 additions and 1 deletions

View File

@ -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)) {

View File

@ -58,6 +58,17 @@ class Php70Test extends TestCase
'$a' => 'string|null',
],
],
'nullCoalesceWithNullableOnLeft' => [
'<?php
/** @return ?string */
function foo() {
return rand(0, 10) > 5 ? "hello" : null;
}
$a = foo() ?? "goodbye";',
'assertions' => [
'$a' => 'string',
],
],
'nullCoalesceWithReference' => [
'<?php
$var = 0;