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

fix false positives for partially incalid phpdoc

This commit is contained in:
kkmuffme 2022-06-05 12:03:57 +02:00
parent f47b4180fb
commit 74671e3a3c
2 changed files with 46 additions and 10 deletions

View File

@ -3,12 +3,15 @@
namespace Psalm\Internal\PhpVisitor\Reflector; namespace Psalm\Internal\PhpVisitor\Reflector;
use PhpParser; use PhpParser;
use Psalm\CodeLocation;
use Psalm\DocComment; use Psalm\DocComment;
use Psalm\Exception\DocblockParseException; use Psalm\Exception\DocblockParseException;
use Psalm\Exception\IncorrectDocblockException; use Psalm\Exception\IncorrectDocblockException;
use Psalm\Internal\Analyzer\CommentAnalyzer; use Psalm\Internal\Analyzer\CommentAnalyzer;
use Psalm\Internal\Scanner\FunctionDocblockComment; use Psalm\Internal\Scanner\FunctionDocblockComment;
use Psalm\Internal\Scanner\ParsedDocblock; use Psalm\Internal\Scanner\ParsedDocblock;
use Psalm\Issue\InvalidDocblock;
use Psalm\IssueBuffer;
use function array_shift; use function array_shift;
use function array_unique; use function array_unique;
@ -34,8 +37,11 @@ class FunctionLikeDocblockParser
/** /**
* @throws DocblockParseException if there was a problem parsing the docblock * @throws DocblockParseException if there was a problem parsing the docblock
*/ */
public static function parse(PhpParser\Comment\Doc $comment): FunctionDocblockComment public static function parse(
{ PhpParser\Comment\Doc $comment,
CodeLocation $code_location,
string $cased_function_id
): FunctionDocblockComment {
$parsed_docblock = DocComment::parsePreservingLength($comment); $parsed_docblock = DocComment::parsePreservingLength($comment);
$comment_text = $comment->getText(); $comment_text = $comment->getText();
@ -49,7 +55,9 @@ class FunctionLikeDocblockParser
self::extractReturnType( self::extractReturnType(
$comment, $comment,
$parsed_docblock->combined_tags['return'], $parsed_docblock->combined_tags['return'],
$info $info,
$code_location,
$cased_function_id
); );
} }
@ -107,7 +115,12 @@ class FunctionLikeDocblockParser
$info->params[] = $info_param; $info->params[] = $info_param;
} }
} else { } else {
throw new DocblockParseException('Badly-formatted @param'); IssueBuffer::maybeAdd(
new InvalidDocblock(
'Badly-formatted @param in docblock for ' . $cased_function_id,
$code_location
)
);
} }
} }
} }
@ -152,7 +165,12 @@ class FunctionLikeDocblockParser
]; ];
} }
} else { } else {
throw new DocblockParseException('Badly-formatted @param'); IssueBuffer::maybeAdd(
new InvalidDocblock(
'Badly-formatted @param in docblock for ' . $cased_function_id,
$code_location
)
);
} }
} }
} }
@ -335,7 +353,12 @@ class FunctionLikeDocblockParser
]; ];
} }
} else { } else {
throw new DocblockParseException('Badly-formatted @param'); IssueBuffer::maybeAdd(
new InvalidDocblock(
'Badly-formatted @param in docblock for ' . $cased_function_id,
$code_location
)
);
} }
} }
} }
@ -542,7 +565,9 @@ class FunctionLikeDocblockParser
private static function extractReturnType( private static function extractReturnType(
PhpParser\Comment\Doc $comment, PhpParser\Comment\Doc $comment,
array $return_specials, array $return_specials,
FunctionDocblockComment $info FunctionDocblockComment $info,
CodeLocation $code_location,
string $cased_function_id
): void { ): void {
foreach ($return_specials as $offset => $return_block) { foreach ($return_specials as $offset => $return_block) {
$return_lines = explode("\n", $return_block); $return_lines = explode("\n", $return_block);
@ -581,7 +606,12 @@ class FunctionLikeDocblockParser
$info->return_type_start = $offset; $info->return_type_start = $offset;
$info->return_type_end = $end; $info->return_type_end = $end;
} else { } else {
throw new DocblockParseException('Badly-formatted @return type'); IssueBuffer::maybeAdd(
new InvalidDocblock(
'Badly-formatted @param in docblock for ' . $cased_function_id,
$code_location
)
);
} }
break; break;

View File

@ -467,7 +467,8 @@ class FunctionLikeNodeScanner
if ($doc_comment) { if ($doc_comment) {
try { try {
$docblock_info = FunctionLikeDocblockParser::parse($doc_comment); $code_location = new CodeLocation($this->file_scanner, $stmt, null, true);
$docblock_info = FunctionLikeDocblockParser::parse($doc_comment, $code_location, $cased_function_id);
} catch (IncorrectDocblockException $e) { } catch (IncorrectDocblockException $e) {
$storage->docblock_issues[] = new MissingDocblockType( $storage->docblock_issues[] = new MissingDocblockType(
$e->getMessage() . ' in docblock for ' . $cased_function_id, $e->getMessage() . ' in docblock for ' . $cased_function_id,
@ -1041,7 +1042,12 @@ class FunctionLikeNodeScanner
if ($doc_comment) { if ($doc_comment) {
$docblock_info = null; $docblock_info = null;
try { try {
$docblock_info = FunctionLikeDocblockParser::parse($doc_comment); $code_location = new CodeLocation($this->file_scanner, $stmt, null, true);
$docblock_info = FunctionLikeDocblockParser::parse(
$doc_comment,
$code_location,
$cased_function_id
);
} catch (IncorrectDocblockException|DocblockParseException $e) { } catch (IncorrectDocblockException|DocblockParseException $e) {
} }
if ($docblock_info) { if ($docblock_info) {