From 77c70c0ad2062e823abb0b5120af45a0cac085fc Mon Sep 17 00:00:00 2001 From: orklah Date: Tue, 5 Oct 2021 16:09:43 +0200 Subject: [PATCH 1/2] allow |false in unions as long as it's not alone and not with null --- src/Psalm/Type/Union.php | 12 ++++++++++++ 1 file changed, 12 insertions(+) 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)); } From ecb541ea51ca43f27385a205ad6488ee48eac83c Mon Sep 17 00:00:00 2001 From: orklah Date: Tue, 5 Oct 2021 16:11:10 +0200 Subject: [PATCH 2/2] add test --- .../ReturnTypeManipulationTest.php | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) 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' => [ + '