Skip to content

Commit 4b9da4f

Browse files
committed
Generic/Fixme: improve handling of "fixme" tags in docblocks
Essentially the same fix as applied in the sister-commit for the `Generic.Commenting.Todo` sniff. Until now, the sniff would only examine an individual comment token, while when a `@fixme` tag is used in a docblock, the "task description" is normally in the next `T_DOC_COMMENT_STRING` token. This commit fixes this and the sniff will now take docblock `@fixme` tags into account. Includes additional unit tests.
1 parent 40d5473 commit 4b9da4f

File tree

4 files changed

+61
-16
lines changed

4 files changed

+61
-16
lines changed

src/Standards/Generic/Sniffs/Commenting/FixmeSniff.php

+28-16
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212

1313
use PHP_CodeSniffer\Files\File;
1414
use PHP_CodeSniffer\Sniffs\Sniff;
15-
use PHP_CodeSniffer\Util\Tokens;
1615

1716
class FixmeSniff implements Sniff
1817
{
@@ -56,27 +55,40 @@ public function register()
5655
*/
5756
public function process(File $phpcsFile, $stackPtr)
5857
{
59-
$tokens = $phpcsFile->getTokens();
60-
58+
$tokens = $phpcsFile->getTokens();
6159
$content = $tokens[$stackPtr]['content'];
6260
$matches = [];
63-
preg_match('/(?:\A|[^\p{L}]+)fixme([^\p{L}]+(.*)|\Z)/ui', $content, $matches);
64-
if (empty($matches) === false) {
65-
// Clear whitespace and some common characters not required at
66-
// the end of a fixme message to make the error more informative.
67-
$type = 'CommentFound';
68-
$fixmeMessage = trim($matches[1]);
69-
$fixmeMessage = trim($fixmeMessage, '-:[](). ');
70-
$error = 'Comment refers to a FIXME task';
71-
$data = [$fixmeMessage];
72-
if ($fixmeMessage !== '') {
73-
$type = 'TaskFound';
74-
$error .= ' "%s"';
61+
62+
if (preg_match('/(?:\A|[^\p{L}]+)fixme([^\p{L}]+(.*)|\Z)/ui', $content, $matches) !== 1) {
63+
return;
64+
}
65+
66+
$fixmeMessage = trim($matches[1]);
67+
// Clear whitespace and some common characters not required at
68+
// the end of a to-do message to make the warning more informative.
69+
$fixmeMessage = trim($fixmeMessage, '-:[](). ');
70+
71+
if ($tokens[$stackPtr]['code'] === T_DOC_COMMENT_TAG
72+
&& $fixmeMessage === ''
73+
) {
74+
$nextNonEmpty = $phpcsFile->findNext(T_DOC_COMMENT_WHITESPACE, ($stackPtr + 1), null, true);
75+
if ($nextNonEmpty !== false
76+
&& $tokens[$nextNonEmpty]['code'] === T_DOC_COMMENT_STRING
77+
) {
78+
$fixmeMessage = trim($tokens[$nextNonEmpty]['content'], '-:[](). ');
7579
}
80+
}
7681

77-
$phpcsFile->addError($error, $stackPtr, $type, $data);
82+
$error = 'Comment refers to a FIXME task';
83+
$type = 'CommentFound';
84+
$data = [$fixmeMessage];
85+
if ($fixmeMessage !== '') {
86+
$error .= ' "%s"';
87+
$type = 'TaskFound';
7888
}
7989

90+
$phpcsFile->addError($error, $stackPtr, $type, $data);
91+
8092
}//end process()
8193

8294

src/Standards/Generic/Tests/Commenting/FixmeUnitTest.inc

+14
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,17 @@ Debug::bam('test');
2121
//FIXME.
2222
//éfixme
2323
//fixmeé
24+
25+
/**
26+
* While there is no official "fix me" tag, only `@todo`, let's support a tag for the purpose of this sniff anyway.
27+
*
28+
* @fixme This message should be picked up.
29+
* @fixme: This message should be picked up too.
30+
* @fixme - here is a message
31+
*
32+
* The below should not show a message as there is no description associated with the tag.
33+
* @fixme
34+
* @anothertag
35+
*
36+
* @param string $something FIXME: add description
37+
*/

src/Standards/Generic/Tests/Commenting/FixmeUnitTest.js

+14
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,17 @@ alert('test');
2121
//FIXME.
2222
//éfixme
2323
//fixmeé
24+
25+
/**
26+
* While there is no official "fix me" tag, only `@todo`, let's support a tag for the purpose of this sniff anyway.
27+
*
28+
* @fixme This message should be picked up.
29+
* @fixme: This message should be picked up too.
30+
* @fixme - here is a message
31+
*
32+
* The below should not show a message as there is no description associated with the tag.
33+
* @fixme
34+
* @anothertag
35+
*
36+
* @param string $something FIXME: add description
37+
*/

src/Standards/Generic/Tests/Commenting/FixmeUnitTest.php

+5
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,11 @@ public function getErrorList($testFile='FixmeUnitTest.inc')
3737
16 => 1,
3838
18 => 1,
3939
21 => 1,
40+
28 => 1,
41+
29 => 1,
42+
30 => 1,
43+
33 => 1,
44+
36 => 1,
4045
];
4146

4247
}//end getErrorList()

0 commit comments

Comments
 (0)