Skip to content

Commit ebc7599

Browse files
authored
Merge pull request #8713 from weirdan/fix-8712
Reject `@psalm-consistent-constructor` in function docblocks
2 parents 9d4c718 + 8e1f129 commit ebc7599

File tree

2 files changed

+40
-7
lines changed

2 files changed

+40
-7
lines changed

src/Psalm/Internal/PhpVisitor/Reflector/FunctionLikeDocblockParser.php

Lines changed: 35 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use Psalm\Issue\InvalidDocblock;
1616
use Psalm\IssueBuffer;
1717

18+
use function array_keys;
1819
use function array_shift;
1920
use function array_unique;
2021
use function count;
@@ -704,17 +705,44 @@ private static function checkUnexpectedTags(
704705
PhpParser\Comment\Doc $comment
705706
): void {
706707
if (isset($parsed_docblock->tags['psalm-import-type'])) {
707-
foreach ($parsed_docblock->tags['psalm-import-type'] as $offset => $_) {
708-
$info->unexpected_tags['psalm-import-type']['lines'][] = self::docblockLineNumber($comment, $offset);
709-
}
708+
$info->unexpected_tags['psalm-import-type']['lines'] = self::tagOffsetsToLines(
709+
array_keys($parsed_docblock->tags['psalm-import-type']),
710+
$comment
711+
);
710712
}
711713

712714
if (isset($parsed_docblock->combined_tags['var'])) {
713-
$info->unexpected_tags['var'] = ['lines' => [], 'suggested_replacement' => 'param'];
714-
foreach ($parsed_docblock->combined_tags['var'] as $offset => $_) {
715-
$info->unexpected_tags['var']['lines'][] = self::docblockLineNumber($comment, $offset);
716-
}
715+
$info->unexpected_tags['var'] = [
716+
'lines' => self::tagOffsetsToLines(
717+
array_keys($parsed_docblock->combined_tags['var']),
718+
$comment
719+
),
720+
'suggested_replacement' => 'param'
721+
];
722+
}
723+
724+
if (isset($parsed_docblock->tags['psalm-consistent-constructor'])) {
725+
$info->unexpected_tags['psalm-consistent-constructor'] = [
726+
'lines' => self::tagOffsetsToLines(
727+
array_keys($parsed_docblock->tags['psalm-consistent-constructor']),
728+
$comment
729+
),
730+
'suggested_replacement' => 'psalm-consistent-constructor on a class level',
731+
];
732+
}
733+
}
734+
735+
/**
736+
* @param list<int> $offsets
737+
* @return list<int>
738+
*/
739+
private static function tagOffsetsToLines(array $offsets, PhpParser\Comment\Doc $comment): array
740+
{
741+
$ret = [];
742+
foreach ($offsets as $offset) {
743+
$ret[] = self::docblockLineNumber($comment, $offset);
717744
}
745+
return $ret;
718746
}
719747

720748
private static function docblockLineNumber(PhpParser\Comment\Doc $comment, int $offset): int

tests/FunctionLikeDocblockParserTest.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@ public function testReturnsUnexpectedTags(): void
135135
$doc = '/**
136136
* @psalm-import-type abcd
137137
* @var int $p
138+
* @psalm-consistent-constructor
138139
*/
139140
';
140141
$php_parser_doc = new Doc($doc, 0);
@@ -147,6 +148,10 @@ public function testReturnsUnexpectedTags(): void
147148
[
148149
'psalm-import-type' => ['lines' => [1]],
149150
'var' => ['lines' => [2], 'suggested_replacement' => 'param'],
151+
'psalm-consistent-constructor' => [
152+
'lines' => [3],
153+
'suggested_replacement' => 'psalm-consistent-constructor on a class level'
154+
]
150155
],
151156
$function_docblock->unexpected_tags
152157
);

0 commit comments

Comments
 (0)