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

Merge pull request #8713 from weirdan/fix-8712

Reject `@psalm-consistent-constructor` in function docblocks
This commit is contained in:
orklah 2022-11-18 22:05:22 +01:00 committed by GitHub
commit ebc7599697
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
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
);