1
0
mirror of https://github.com/danog/psalm.git synced 2025-01-22 05:41:20 +01:00

Fix #3419 - don’t add null to return type when template/conditional return is used

This commit is contained in:
Brown 2020-05-22 12:44:19 -04:00
parent 8632cdb3cd
commit 1b84fc2c12
3 changed files with 27 additions and 0 deletions

View File

@ -2689,6 +2689,8 @@ class ReflectorVisitor extends PhpParser\NodeVisitorAbstract implements PhpParse
if ($storage->signature_return_type->isNullable() if ($storage->signature_return_type->isNullable()
&& !$storage->return_type->isNullable() && !$storage->return_type->isNullable()
&& !$storage->return_type->hasTemplate()
&& !$storage->return_type->hasConditional()
) { ) {
$storage->return_type->addType(new Type\Atomic\TNull()); $storage->return_type->addType(new Type\Atomic\TNull());
} }

View File

@ -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 * @return bool
*/ */

View File

@ -476,6 +476,18 @@ class ConditionalReturnTypeTest extends TestCase
return $s; return $s;
}', }',
], ],
'nullableReturnType' => [
'<?php
/**
* @psalm-return ($name is "foo" ? string : null)
*/
function get(string $name) {
if ($name === "foo") {
return "hello";
}
return null;
}'
],
]; ];
} }
} }