Skip to content

Commit e44cab1

Browse files
committed
fix(InlineComment): Allow custom docblock tags (#3367776)
Previously, the InlineComment sniff flagged inline DocBlocks as long as they did not contain a '@var' tag. As noted in #3367776, it is desirable to also allow other (possibly custom) docblock tags. This commit updates InlineCommentSniff.php to allow any inline doc blocks that start with a doc comment tag, e.g. /** @var */. Any trailing content is allowed, however the docblock tag must be first. This commit also adds appropriate test cases to InlineCommentSniff.php.
1 parent 6b2edff commit e44cab1

File tree

4 files changed

+102
-15
lines changed

4 files changed

+102
-15
lines changed

coder_sniffer/Drupal/Sniffs/Commenting/InlineCommentSniff.php

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -111,10 +111,18 @@ public function process(File $phpcsFile, $stackPtr)
111111
}
112112

113113
if ($tokens[$stackPtr]['content'] === '/**') {
114-
// The only exception to inline doc blocks is the /** @var */
115-
// declaration. Allow that in any form.
116-
$varTag = $phpcsFile->findNext([T_DOC_COMMENT_TAG], ($stackPtr + 1), $tokens[$stackPtr]['comment_closer'], false, '@var');
117-
if ($varTag === false) {
114+
// The only exception are inline doc blocks that start with a doc comment tag, e.g. /** @var */.
115+
// Any trailing content after the comment tag is fine.
116+
$anyTag = $phpcsFile->findNext([T_DOC_COMMENT_TAG], ($stackPtr + 1), $tokens[$stackPtr]['comment_closer'], false);
117+
118+
// Ensure that there is nothing but stars and whitespace before the tag starts.
119+
// In particular, preceding text is not allowed.
120+
$beforeTag = false;
121+
if ($anyTag !== false) {
122+
$beforeTag = $phpcsFile->findNext([T_DOC_COMMENT_STAR, T_DOC_COMMENT_WHITESPACE], ($stackPtr + 1), $anyTag, true);
123+
}
124+
125+
if ($anyTag === false || $beforeTag !== false) {
118126
$error = 'Inline doc block comments are not allowed; use "/* Comment */" or "// Comment" instead';
119127
$phpcsFile->addError($error, $stackPtr, 'DocBlock');
120128
}

tests/Drupal/Commenting/InlineCommentUnitTest.inc

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,44 @@ function test2() {
117117
return $x;
118118
}
119119

120+
/**
121+
* Check more inline doc block cases.
122+
*/
123+
function test3() {
124+
/** I am not allowed. */
125+
something();
126+
127+
/**
128+
* I am also not allowed.
129+
* Even though I go on multiple lines.
130+
*/
131+
something();
132+
133+
/** @some-unknown-tag I am allowed. */
134+
something();
135+
136+
/**
137+
*
138+
* @some-tag-on-the-third-line */
139+
something();
140+
141+
/**
142+
* @some-tag-here
143+
*
144+
* Additonal text after is fine.
145+
*/
146+
something();
147+
148+
/**
149+
* There is some text here before the doc tag.
150+
*
151+
* This should not be allowed.
152+
*
153+
* @some-other-tag
154+
*/
155+
something();
156+
}
157+
120158
// Allow all the cspell comment variants.
121159
// cspell:ignore bananarama
122160
$x = 1;

tests/Drupal/Commenting/InlineCommentUnitTest.inc.fixed

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,44 @@ function test2() {
116116
return $x;
117117
}
118118

119+
/**
120+
* Check more inline doc block cases.
121+
*/
122+
function test3() {
123+
/** I am not allowed. */
124+
something();
125+
126+
/**
127+
* I am also not allowed.
128+
* Even though I go on multiple lines.
129+
*/
130+
something();
131+
132+
/** @some-unknown-tag I am allowed. */
133+
something();
134+
135+
/**
136+
*
137+
* @some-tag-on-the-third-line */
138+
something();
139+
140+
/**
141+
* @some-tag-here
142+
*
143+
* Additonal text after is fine.
144+
*/
145+
something();
146+
147+
/**
148+
* There is some text here before the doc tag.
149+
*
150+
* This should not be allowed.
151+
*
152+
* @some-other-tag
153+
*/
154+
something();
155+
}
156+
119157
// Allow all the cspell comment variants.
120158
// cspell:ignore bananarama
121159
$x = 1;

tests/Drupal/Commenting/InlineCommentUnitTest.php

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,20 @@ class InlineCommentUnitTest extends CoderSniffUnitTest
2121
protected function getErrorList(string $testFile): array
2222
{
2323
return [
24-
8 => 1,
25-
10 => 1,
26-
13 => 1,
27-
15 => 1,
28-
20 => 1,
29-
24 => 1,
30-
44 => 1,
31-
47 => 1,
32-
62 => 1,
33-
84 => 1,
34-
86 => 1,
24+
8 => 1,
25+
10 => 1,
26+
13 => 1,
27+
15 => 1,
28+
20 => 1,
29+
24 => 1,
30+
44 => 1,
31+
47 => 1,
32+
62 => 1,
33+
84 => 1,
34+
86 => 1,
35+
124 => 1,
36+
127 => 1,
37+
148 => 1,
3538
];
3639

3740
}//end getErrorList()

0 commit comments

Comments
 (0)