|
11 | 11 |
|
12 | 12 | use PHP_CodeSniffer\Files\File;
|
13 | 13 | use PHP_CodeSniffer\Sniffs\Sniff;
|
14 |
| -use PHP_CodeSniffer\Util\Tokens; |
15 | 14 |
|
16 | 15 | class TodoSniff implements Sniff
|
17 | 16 | {
|
@@ -55,27 +54,40 @@ public function register()
|
55 | 54 | */
|
56 | 55 | public function process(File $phpcsFile, $stackPtr)
|
57 | 56 | {
|
58 |
| - $tokens = $phpcsFile->getTokens(); |
59 |
| - |
| 57 | + $tokens = $phpcsFile->getTokens(); |
60 | 58 | $content = $tokens[$stackPtr]['content'];
|
61 | 59 | $matches = [];
|
62 |
| - preg_match('/(?:\A|[^\p{L}]+)todo([^\p{L}]+(.*)|\Z)/ui', $content, $matches); |
63 |
| - if (empty($matches) === false) { |
64 |
| - // Clear whitespace and some common characters not required at |
65 |
| - // the end of a to-do message to make the warning more informative. |
66 |
| - $type = 'CommentFound'; |
67 |
| - $todoMessage = trim($matches[1]); |
68 |
| - $todoMessage = trim($todoMessage, '-:[](). '); |
69 |
| - $error = 'Comment refers to a TODO task'; |
70 |
| - $data = [$todoMessage]; |
71 |
| - if ($todoMessage !== '') { |
72 |
| - $type = 'TaskFound'; |
73 |
| - $error .= ' "%s"'; |
| 60 | + |
| 61 | + if (preg_match('/(?:\A|[^\p{L}]+)todo([^\p{L}]+(.*)|\Z)/ui', $content, $matches) !== 1) { |
| 62 | + return; |
| 63 | + } |
| 64 | + |
| 65 | + $todoMessage = trim($matches[1]); |
| 66 | + // Clear whitespace and some common characters not required at |
| 67 | + // the end of a to-do message to make the warning more informative. |
| 68 | + $todoMessage = trim($todoMessage, '-:[](). '); |
| 69 | + |
| 70 | + if ($tokens[$stackPtr]['code'] === T_DOC_COMMENT_TAG |
| 71 | + && $todoMessage === '' |
| 72 | + ) { |
| 73 | + $nextNonEmpty = $phpcsFile->findNext(T_DOC_COMMENT_WHITESPACE, ($stackPtr + 1), null, true); |
| 74 | + if ($nextNonEmpty !== false |
| 75 | + && $tokens[$nextNonEmpty]['code'] === T_DOC_COMMENT_STRING |
| 76 | + ) { |
| 77 | + $todoMessage = trim($tokens[$nextNonEmpty]['content'], '-:[](). '); |
74 | 78 | }
|
| 79 | + } |
75 | 80 |
|
76 |
| - $phpcsFile->addWarning($error, $stackPtr, $type, $data); |
| 81 | + $error = 'Comment refers to a TODO task'; |
| 82 | + $type = 'CommentFound'; |
| 83 | + $data = [$todoMessage]; |
| 84 | + if ($todoMessage !== '') { |
| 85 | + $error .= ' "%s"'; |
| 86 | + $type = 'TaskFound'; |
77 | 87 | }
|
78 | 88 |
|
| 89 | + $phpcsFile->addWarning($error, $stackPtr, $type, $data); |
| 90 | + |
79 | 91 | }//end process()
|
80 | 92 |
|
81 | 93 |
|
|
0 commit comments