Skip to content

Commit 65dd500

Browse files
authored
feat(DocComment): Avoid misleading message when inheritdoc is missing the curly braces (#2904801)
1 parent f2bba4e commit 65dd500

File tree

4 files changed

+46
-1
lines changed

4 files changed

+46
-1
lines changed

coder_sniffer/Drupal/Sniffs/Commenting/DocCommentSniff.php

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,10 +139,23 @@ public function process(File $phpcsFile, $stackPtr)
139139
return;
140140
}
141141

142+
// If inheritDoc is found without curly braces it is identified as a T_DOC_COMMENT_TAG not a
143+
// T_DOC_COMMENT_STRING. It would be misleading to give the 'Missing short description' error
144+
// below, hence we give a more useful message and can fix it automatically.
145+
if (stripos($tokens[$short]['content'], '@inheritdoc') === 0) {
146+
$error = "{$tokens[$short]['content']} found. Did you mean {{$tokens[$short]['content']}}?";
147+
$fix = $phpcsFile->addFixableError($error, $short, 'InheritDocWithoutBraces');
148+
if ($fix === true) {
149+
$phpcsFile->fixer->replaceToken($short, "{{$tokens[$short]['content']}}");
150+
}
151+
152+
return;
153+
}
154+
142155
$error = 'Missing short description in doc comment';
143156
$phpcsFile->addError($error, $stackPtr, 'MissingShort');
144157
return;
145-
}
158+
}//end if
146159

147160
if (isset($fileShort) === true) {
148161
$start = $fileShort;

tests/Drupal/Commenting/DocCommentUnitTest.inc

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,3 +121,18 @@ function test13() {
121121
function test14(array $matches, array $sub_key, $to) {
122122

123123
}
124+
125+
/**
126+
* {@inheritdoc}
127+
*/
128+
function test15_this_inheritdoc_is_correct();
129+
130+
/**
131+
* @inheritdoc
132+
*/
133+
function test16_lower_case_fail_needs_braces();
134+
135+
/**
136+
* @inheritDoc
137+
*/
138+
function test17_camel_case_fail_needs_braces();

tests/Drupal/Commenting/DocCommentUnitTest.inc.fixed

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,3 +130,18 @@ function test13() {
130130
function test14(array $matches, array $sub_key, $to) {
131131

132132
}
133+
134+
/**
135+
* {@inheritdoc}
136+
*/
137+
function test15_this_inheritdoc_is_correct();
138+
139+
/**
140+
* {@inheritdoc}
141+
*/
142+
function test16_lower_case_fail_needs_braces();
143+
144+
/**
145+
* {@inheritDoc}
146+
*/
147+
function test17_camel_case_fail_needs_braces();

tests/Drupal/Commenting/DocCommentUnitTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ protected function getErrorList(string $testFile): array
3333
66 => 1,
3434
100 => 4,
3535
101 => 1,
36+
131 => 1,
37+
136 => 1,
3638
];
3739

3840
case 'DocCommentUnitTest.1.inc':

0 commit comments

Comments
 (0)