Skip to content

Commit

Permalink
Merge pull request #8713 from weirdan/fix-8712
Browse files Browse the repository at this point in the history
Reject `@psalm-consistent-constructor` in function docblocks
  • Loading branch information
orklah committed Nov 18, 2022
2 parents 9d4c718 + 8e1f129 commit ebc7599
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 7 deletions.
Expand Up @@ -15,6 +15,7 @@
use Psalm\Issue\InvalidDocblock;
use Psalm\IssueBuffer;

use function array_keys;
use function array_shift;
use function array_unique;
use function count;
Expand Down Expand Up @@ -704,17 +705,44 @@ private static function checkUnexpectedTags(
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
Expand Down
5 changes: 5 additions & 0 deletions tests/FunctionLikeDocblockParserTest.php
Expand Up @@ -135,6 +135,7 @@ public function testReturnsUnexpectedTags(): void
$doc = '/**
* @psalm-import-type abcd
* @var int $p
* @psalm-consistent-constructor
*/
';
$php_parser_doc = new Doc($doc, 0);
Expand All @@ -147,6 +148,10 @@ public function testReturnsUnexpectedTags(): void
[
'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
);
Expand Down

0 comments on commit ebc7599

Please sign in to comment.