From 1b84fc2c1215cb28dbea2db495fc5b38e6c5851d Mon Sep 17 00:00:00 2001 From: Brown Date: Fri, 22 May 2020 12:44:19 -0400 Subject: [PATCH] =?UTF-8?q?Fix=20#3419=20-=20don=E2=80=99t=20add=20null=20?= =?UTF-8?q?to=20return=20type=20when=20template/conditional=20return=20is?= =?UTF-8?q?=20used?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Psalm/Internal/PhpVisitor/ReflectorVisitor.php | 2 ++ src/Psalm/Type/Union.php | 13 +++++++++++++ tests/Template/ConditionalReturnTypeTest.php | 12 ++++++++++++ 3 files changed, 27 insertions(+) diff --git a/src/Psalm/Internal/PhpVisitor/ReflectorVisitor.php b/src/Psalm/Internal/PhpVisitor/ReflectorVisitor.php index e99c6e52d..c2c4c6a99 100644 --- a/src/Psalm/Internal/PhpVisitor/ReflectorVisitor.php +++ b/src/Psalm/Internal/PhpVisitor/ReflectorVisitor.php @@ -2689,6 +2689,8 @@ class ReflectorVisitor extends PhpParser\NodeVisitorAbstract implements PhpParse if ($storage->signature_return_type->isNullable() && !$storage->return_type->isNullable() + && !$storage->return_type->hasTemplate() + && !$storage->return_type->hasConditional() ) { $storage->return_type->addType(new Type\Atomic\TNull()); } diff --git a/src/Psalm/Type/Union.php b/src/Psalm/Type/Union.php index 3f0f7726e..be4c8752c 100644 --- a/src/Psalm/Type/Union.php +++ b/src/Psalm/Type/Union.php @@ -956,6 +956,19 @@ class Union implements TypeNode ); } + /** + * @return bool + */ + public function hasConditional() + { + return (bool) array_filter( + $this->types, + function (Atomic $type) : bool { + return $type instanceof Type\Atomic\TConditional; + } + ); + } + /** * @return bool */ diff --git a/tests/Template/ConditionalReturnTypeTest.php b/tests/Template/ConditionalReturnTypeTest.php index 4f5b2f3fd..49c22624d 100644 --- a/tests/Template/ConditionalReturnTypeTest.php +++ b/tests/Template/ConditionalReturnTypeTest.php @@ -476,6 +476,18 @@ class ConditionalReturnTypeTest extends TestCase return $s; }', ], + 'nullableReturnType' => [ + '