Skip to content

Commit

Permalink
Infer never return type in closure only with missing or void native r…
Browse files Browse the repository at this point in the history
…eturn typehint
  • Loading branch information
ondrejmirtes committed Jan 29, 2021
1 parent 136db04 commit 809807a
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
9 changes: 8 additions & 1 deletion src/Type/TypehintHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,14 @@ public static function decideType(
}
}

$resultType = $type->isSuperTypeOf(TemplateTypeHelper::resolveToBounds($phpDocType))->yes() ? $phpDocType : $type;
if (
(!$phpDocType instanceof NeverType || ($type instanceof MixedType && !$type->isExplicitMixed()))
&& $type->isSuperTypeOf(TemplateTypeHelper::resolveToBounds($phpDocType))->yes()
) {
$resultType = $phpDocType;
} else {
$resultType = $type;
}

if ($type instanceof UnionType) {
$addToUnionTypes = [];
Expand Down
5 changes: 5 additions & 0 deletions tests/PHPStan/Analyser/data/closure-return-type.php
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,11 @@ public function doBaz(): void
}
};
assertType('*NEVER*', $f());

$f = function (): \stdClass {
throw new \Exception();
};
assertType('stdClass', $f());
}

}

0 comments on commit 809807a

Please sign in to comment.