mirror of
https://github.com/danog/psalm.git
synced 2025-01-21 21:31:13 +01:00
Fix #3419 - don’t add null to return type when template/conditional return is used
This commit is contained in:
parent
8632cdb3cd
commit
1b84fc2c12
@ -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());
|
||||
}
|
||||
|
@ -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
|
||||
*/
|
||||
|
@ -476,6 +476,18 @@ class ConditionalReturnTypeTest extends TestCase
|
||||
return $s;
|
||||
}',
|
||||
],
|
||||
'nullableReturnType' => [
|
||||
'<?php
|
||||
/**
|
||||
* @psalm-return ($name is "foo" ? string : null)
|
||||
*/
|
||||
function get(string $name) {
|
||||
if ($name === "foo") {
|
||||
return "hello";
|
||||
}
|
||||
return null;
|
||||
}'
|
||||
],
|
||||
];
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user