From 72d60249a93ab6156936c1eabbf2677cc2ddd492 Mon Sep 17 00:00:00 2001 From: orklah Date: Mon, 26 Jul 2021 23:03:00 +0200 Subject: [PATCH] Inaccurate error with different types in PHPDoc and typehint --- .../PhpVisitor/Reflector/FunctionLikeDocblockScanner.php | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/Psalm/Internal/PhpVisitor/Reflector/FunctionLikeDocblockScanner.php b/src/Psalm/Internal/PhpVisitor/Reflector/FunctionLikeDocblockScanner.php index 5a460441355..8c01914be31 100644 --- a/src/Psalm/Internal/PhpVisitor/Reflector/FunctionLikeDocblockScanner.php +++ b/src/Psalm/Internal/PhpVisitor/Reflector/FunctionLikeDocblockScanner.php @@ -11,6 +11,7 @@ use Psalm\Exception\TypeParseTreeException; use Psalm\Internal\Scanner\FileScanner; use Psalm\Internal\Scanner\FunctionDocblockComment; +use Psalm\Internal\Type\Comparator\UnionTypeComparator; use Psalm\Internal\Type\TypeAlias; use Psalm\Internal\Type\TypeParser; use Psalm\Internal\Type\TypeTokenizer; @@ -908,6 +909,12 @@ private static function handleReturn( && !$storage->return_type->isNullable() && !$storage->return_type->hasTemplate() && !$storage->return_type->hasConditional() + //don't add null to docblock type if it's not contained in signature type + && UnionTypeComparator::isContainedBy( + $codebase, + $storage->return_type, + $storage->signature_return_type + ) ) { $storage->return_type->addType(new Type\Atomic\TNull()); }