From 2a92025737a02957b8076f99fa70e7452bd847de Mon Sep 17 00:00:00 2001 From: Matt Brown Date: Thu, 10 Dec 2020 01:40:20 -0500 Subject: [PATCH] Union types if stmt already has one --- .../Fetch/AtomicPropertyFetchAnalyzer.php | 9 ++++++++- tests/PropertyTypeTest.php | 13 ++++++++++--- 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/src/Psalm/Internal/Analyzer/Statements/Expression/Fetch/AtomicPropertyFetchAnalyzer.php b/src/Psalm/Internal/Analyzer/Statements/Expression/Fetch/AtomicPropertyFetchAnalyzer.php index c13fe92b2..459adeee4 100644 --- a/src/Psalm/Internal/Analyzer/Statements/Expression/Fetch/AtomicPropertyFetchAnalyzer.php +++ b/src/Psalm/Internal/Analyzer/Statements/Expression/Fetch/AtomicPropertyFetchAnalyzer.php @@ -566,7 +566,14 @@ class AtomicPropertyFetchAnalyzer $statements_analyzer->node_data = $old_data_provider; if ($fake_method_call_type) { - $statements_analyzer->node_data->setType($stmt, $fake_method_call_type); + if ($stmt_type = $statements_analyzer->node_data->getType($stmt)) { + $statements_analyzer->node_data->setType( + $stmt, + Type::combineUnionTypes($fake_method_call_type, $stmt_type) + ); + } else { + $statements_analyzer->node_data->setType($stmt, $fake_method_call_type); + } } else { $statements_analyzer->node_data->setType($stmt, Type::getMixed()); } diff --git a/tests/PropertyTypeTest.php b/tests/PropertyTypeTest.php index 772340c6b..93173f932 100644 --- a/tests/PropertyTypeTest.php +++ b/tests/PropertyTypeTest.php @@ -2349,9 +2349,16 @@ class PropertyTypeTest extends TestCase public string $name = "Dave"; } - function takesNullableUser(User|NullObject $user) : void { - echo $user->name; - }' + function takesNullableUser(User|NullObject $user) : ?string { + $name = $user->name; + + if ($name === null) {} + + return $name; + }', + [], + [], + '8.0' ], ]; }