diff --git a/src/Psalm/Type/Union.php b/src/Psalm/Type/Union.php index a5ee23888..83df11bcd 100644 --- a/src/Psalm/Type/Union.php +++ b/src/Psalm/Type/Union.php @@ -497,6 +497,14 @@ class Union implements TypeNode $nullable = true; } + $falsable = false; + + if (isset($types['false']) && count($types) > 1) { + unset($types['false']); + + $falsable = true; + } + $php_types = []; foreach ($types as $atomic_type) { @@ -515,6 +523,10 @@ class Union implements TypeNode $php_types[] = $php_type; } + if ($falsable) { + return ($nullable ? '?' : '') . implode('|', array_unique($php_types)) . '|false'; + } + return ($nullable ? '?' : '') . implode('|', array_unique($php_types)); } diff --git a/tests/FileManipulation/ReturnTypeManipulationTest.php b/tests/FileManipulation/ReturnTypeManipulationTest.php index 5eafcffbe..3760cfae6 100644 --- a/tests/FileManipulation/ReturnTypeManipulationTest.php +++ b/tests/FileManipulation/ReturnTypeManipulationTest.php @@ -713,6 +713,24 @@ class ReturnTypeManipulationTest extends FileManipulationTestCase ['MissingReturnType'], false, true, + ], + 'OrFalseInReturn' => [ + '