Skip to content

Commit

Permalink
Fix #3419 - don’t add null to return type when template/conditional r…
Browse files Browse the repository at this point in the history
…eturn is used
  • Loading branch information
muglug committed May 22, 2020
1 parent 8632cdb commit 1b84fc2
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/Psalm/Internal/PhpVisitor/ReflectorVisitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -2689,6 +2689,8 @@ function (FunctionLikeParameter $p) {

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());
}
Expand Down
13 changes: 13 additions & 0 deletions src/Psalm/Type/Union.php
Original file line number Diff line number Diff line change
Expand Up @@ -956,6 +956,19 @@ function ($t) {
);
}

/**
* @return bool
*/
public function hasConditional()
{
return (bool) array_filter(
$this->types,
function (Atomic $type) : bool {
return $type instanceof Type\Atomic\TConditional;
}
);
}

/**
* @return bool
*/
Expand Down
12 changes: 12 additions & 0 deletions tests/Template/ConditionalReturnTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -476,6 +476,18 @@ function zeroArgsFalseOneArgString(string $s = "") {
return $s;
}',
],
'nullableReturnType' => [
'<?php
/**
* @psalm-return ($name is "foo" ? string : null)
*/
function get(string $name) {
if ($name === "foo") {
return "hello";
}
return null;
}'
],
];
}
}

0 comments on commit 1b84fc2

Please sign in to comment.