1
0
mirror of https://github.com/danog/psalm.git synced 2024-11-30 04:39:00 +01:00

Reject @psalm-consistent-constructor in function docblocks

Fixes vimeo/psalm#8712
This commit is contained in:
Bruce Weirdan 2022-11-16 19:14:13 -04:00
parent 12f33faece
commit 8e1f129701
No known key found for this signature in database
GPG Key ID: CFC3AAB181751B0D
2 changed files with 40 additions and 7 deletions

View File

@ -15,6 +15,7 @@ use Psalm\Internal\Scanner\ParsedDocblock;
use Psalm\Issue\InvalidDocblock;
use Psalm\IssueBuffer;
use function array_keys;
use function array_shift;
use function array_unique;
use function count;
@ -704,17 +705,44 @@ class FunctionLikeDocblockParser
PhpParser\Comment\Doc $comment
): void {
if (isset($parsed_docblock->tags['psalm-import-type'])) {
foreach ($parsed_docblock->tags['psalm-import-type'] as $offset => $_) {
$info->unexpected_tags['psalm-import-type']['lines'][] = self::docblockLineNumber($comment, $offset);
}
$info->unexpected_tags['psalm-import-type']['lines'] = self::tagOffsetsToLines(
array_keys($parsed_docblock->tags['psalm-import-type']),
$comment
);
}
if (isset($parsed_docblock->combined_tags['var'])) {
$info->unexpected_tags['var'] = ['lines' => [], 'suggested_replacement' => 'param'];
foreach ($parsed_docblock->combined_tags['var'] as $offset => $_) {
$info->unexpected_tags['var']['lines'][] = self::docblockLineNumber($comment, $offset);
}
$info->unexpected_tags['var'] = [
'lines' => self::tagOffsetsToLines(
array_keys($parsed_docblock->combined_tags['var']),
$comment
),
'suggested_replacement' => 'param'
];
}
if (isset($parsed_docblock->tags['psalm-consistent-constructor'])) {
$info->unexpected_tags['psalm-consistent-constructor'] = [
'lines' => self::tagOffsetsToLines(
array_keys($parsed_docblock->tags['psalm-consistent-constructor']),
$comment
),
'suggested_replacement' => 'psalm-consistent-constructor on a class level',
];
}
}
/**
* @param list<int> $offsets
* @return list<int>
*/
private static function tagOffsetsToLines(array $offsets, PhpParser\Comment\Doc $comment): array
{
$ret = [];
foreach ($offsets as $offset) {
$ret[] = self::docblockLineNumber($comment, $offset);
}
return $ret;
}
private static function docblockLineNumber(PhpParser\Comment\Doc $comment, int $offset): int

View File

@ -135,6 +135,7 @@ class FunctionLikeDocblockParserTest extends BaseTestCase
$doc = '/**
* @psalm-import-type abcd
* @var int $p
* @psalm-consistent-constructor
*/
';
$php_parser_doc = new Doc($doc, 0);
@ -147,6 +148,10 @@ class FunctionLikeDocblockParserTest extends BaseTestCase
[
'psalm-import-type' => ['lines' => [1]],
'var' => ['lines' => [2], 'suggested_replacement' => 'param'],
'psalm-consistent-constructor' => [
'lines' => [3],
'suggested_replacement' => 'psalm-consistent-constructor on a class level'
]
],
$function_docblock->unexpected_tags
);