Skip to content

Commit

Permalink
Generic/Todo: improve handling of "todo" tags in docblocks
Browse files Browse the repository at this point in the history
Until now, the sniff would only examine an individual comment token, while when a `@todo` 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 `@todo` tags into account.

Includes additional unit tests.
  • Loading branch information
jrfnl committed Dec 7, 2023
1 parent 14eff3c commit 1c37da7
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 16 deletions.
48 changes: 32 additions & 16 deletions src/Standards/Generic/Sniffs/Commenting/TodoSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

use PHP_CodeSniffer\Files\File;
use PHP_CodeSniffer\Sniffs\Sniff;
use PHP_CodeSniffer\Util\Tokens;

class TodoSniff implements Sniff
{
Expand Down Expand Up @@ -55,27 +54,44 @@ public function register()
*/
public function process(File $phpcsFile, $stackPtr)
{
$tokens = $phpcsFile->getTokens();

$tokens = $phpcsFile->getTokens();
$content = $tokens[$stackPtr]['content'];
$matches = [];
preg_match('/(?:\A|[^\p{L}]+)todo([^\p{L}]+(.*)|\Z)/ui', $content, $matches);
if (empty($matches) === false) {
// Clear whitespace and some common characters not required at
// the end of a to-do message to make the warning more informative.
$type = 'CommentFound';
$todoMessage = trim($matches[1]);
$todoMessage = trim($todoMessage, '-:[](). ');
$error = 'Comment refers to a TODO task';
$data = [$todoMessage];
if ($todoMessage !== '') {
$type = 'TaskFound';
$error .= ' "%s"';

if (preg_match('/(?:\A|[^\p{L}]+)todo([^\p{L}]+(.*)|\Z)/ui', $content, $matches) !== 1) {
return;
}

// Clear whitespace and some common characters not required at
// the end of a to-do message to make the warning more informative.
$todoMessage = trim($matches[1]);
$todoMessage = trim($todoMessage, '-:[](). ');

if ($tokens[$stackPtr]['code'] === T_DOC_COMMENT_TAG
&& $todoMessage === ''
) {
$nextNonEmpty = $phpcsFile->findNext(T_DOC_COMMENT_WHITESPACE, ($stackPtr + 1), null, true);
if ($nextNonEmpty !== false
&& $tokens[$nextNonEmpty]['code'] === T_DOC_COMMENT_STRING
) {
$todoMessage = trim($tokens[$nextNonEmpty]['content'], '-:[](). ');
}
}

$error = 'Comment refers to a TODO task';
$type = 'CommentFound';
$data = [$todoMessage];
if ($todoMessage !== '') {
$error .= ' "%s"';
$type = 'TaskFound';
}

$phpcsFile->addWarning($error, $stackPtr, $type, $data);
if ($tokens[$stackPtr]['code'] === T_DOC_COMMENT_TAG) {
$type .= 'InDocblock';
}

$phpcsFile->addWarning($error, $stackPtr, $type, $data);

}//end process()


Expand Down
12 changes: 12 additions & 0 deletions src/Standards/Generic/Tests/Commenting/TodoUnitTest.inc
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,15 @@ Debug::bam('test');
//TODO.
//étodo
//todoé

/**
* @todo This message should be picked up.
* @todo: This message should be picked up too.
* @todo - here is a message
*
* The below should not show a message as there is no description associated with the tag.
* @todo
* @anothertag
*
* @param string $something TODO: add description
*/
12 changes: 12 additions & 0 deletions src/Standards/Generic/Tests/Commenting/TodoUnitTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,15 @@ alert('test');
//TODO.
//étodo
//todoé

/**
* @todo This message should be picked up.
* @todo: This message should be picked up too.
* @todo - here is a message
*
* The below should not show a message as there is no description associated with the tag.
* @todo
* @anothertag
*
* @param string $something TODO: add description
*/
5 changes: 5 additions & 0 deletions src/Standards/Generic/Tests/Commenting/TodoUnitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,11 @@ public function getWarningList($testFile='TodoUnitTest.inc')
16 => 1,
18 => 1,
21 => 1,
26 => 1,
27 => 1,
28 => 1,
31 => 1,
34 => 1,
];

}//end getWarningList()
Expand Down

0 comments on commit 1c37da7

Please sign in to comment.