From 29bd519477dcf22f51ba7200e3f7d40460129360 Mon Sep 17 00:00:00 2001 From: Adam Zimmermann Date: Thu, 10 Sep 2020 16:49:01 -0500 Subject: [PATCH 01/19] Create @todo format sniff. --- .../Sniffs/Commenting/TodoCommentSniff.php | 65 +++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 coder_sniffer/Drupal/Sniffs/Commenting/TodoCommentSniff.php diff --git a/coder_sniffer/Drupal/Sniffs/Commenting/TodoCommentSniff.php b/coder_sniffer/Drupal/Sniffs/Commenting/TodoCommentSniff.php new file mode 100644 index 00000000..05688696 --- /dev/null +++ b/coder_sniffer/Drupal/Sniffs/Commenting/TodoCommentSniff.php @@ -0,0 +1,65 @@ + + */ + public function register() + { + return [ + T_COMMENT, + T_DOC_COMMENT_TAG, + ]; + + }//end register() + + + /** + * Processes this test, when one of its tokens is encountered. + * + * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned. + * @param int $stackPtr The position of the current token + * in the stack passed in $tokens. + * + * @return void + */ + public function process(File $phpcsFile, $stackPtr) + { + $tokens = $phpcsFile->getTokens(); + $comment = $tokens[$stackPtr]['content']; + if ($tokens[$stackPtr]['code'] == T_DOC_COMMENT_TAG) { + $comment = $phpcsFile->findNext(T_DOC_COMMENT_STRING, ($stackPtr + 1)); + } + $expression = '/(?i)(?=(@to(-|\s|)+do))(?-i)(?!@todo\s\w)/m'; + if ((bool) preg_match($expression, $comment) === true) { + $phpcsFile->addError('@todo comments should be in the "@todo Some task." format.', $stackPtr, 'TodoFormat'); + } + + }//end process() + + +}//end class From edf13c7de9eb427e14373a21acc3911ed67d3540 Mon Sep 17 00:00:00 2001 From: Adam Zimmermann Date: Thu, 10 Sep 2020 16:55:39 -0500 Subject: [PATCH 02/19] Fix code standards and add comments. --- .../Drupal/Sniffs/Commenting/TodoCommentSniff.php | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/coder_sniffer/Drupal/Sniffs/Commenting/TodoCommentSniff.php b/coder_sniffer/Drupal/Sniffs/Commenting/TodoCommentSniff.php index 05688696..da23194f 100644 --- a/coder_sniffer/Drupal/Sniffs/Commenting/TodoCommentSniff.php +++ b/coder_sniffer/Drupal/Sniffs/Commenting/TodoCommentSniff.php @@ -49,11 +49,14 @@ public function register() */ public function process(File $phpcsFile, $stackPtr) { - $tokens = $phpcsFile->getTokens(); + $tokens = $phpcsFile->getTokens(); + // Use the comment text by default. $comment = $tokens[$stackPtr]['content']; - if ($tokens[$stackPtr]['code'] == T_DOC_COMMENT_TAG) { + // Use the next line for multi-line comments. + if ($tokens[$stackPtr]['code'] === T_DOC_COMMENT_TAG) { $comment = $phpcsFile->findNext(T_DOC_COMMENT_STRING, ($stackPtr + 1)); } + $expression = '/(?i)(?=(@to(-|\s|)+do))(?-i)(?!@todo\s\w)/m'; if ((bool) preg_match($expression, $comment) === true) { $phpcsFile->addError('@todo comments should be in the "@todo Some task." format.', $stackPtr, 'TodoFormat'); From bf855821f088a0e77d58c5cf68a2197f50e37e4a Mon Sep 17 00:00:00 2001 From: Adam Zimmermann Date: Thu, 10 Sep 2020 17:27:54 -0500 Subject: [PATCH 03/19] Catch todo without @ symbol at the start. --- coder_sniffer/Drupal/Sniffs/Commenting/TodoCommentSniff.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/coder_sniffer/Drupal/Sniffs/Commenting/TodoCommentSniff.php b/coder_sniffer/Drupal/Sniffs/Commenting/TodoCommentSniff.php index da23194f..011e2d0a 100644 --- a/coder_sniffer/Drupal/Sniffs/Commenting/TodoCommentSniff.php +++ b/coder_sniffer/Drupal/Sniffs/Commenting/TodoCommentSniff.php @@ -57,7 +57,7 @@ public function process(File $phpcsFile, $stackPtr) $comment = $phpcsFile->findNext(T_DOC_COMMENT_STRING, ($stackPtr + 1)); } - $expression = '/(?i)(?=(@to(-|\s|)+do))(?-i)(?!@todo\s\w)/m'; + $expression = '/^\s*(?i)(?=(@*to(-|\s|)+do))(?-i)(?!@todo\s\w)/m'; if ((bool) preg_match($expression, $comment) === true) { $phpcsFile->addError('@todo comments should be in the "@todo Some task." format.', $stackPtr, 'TodoFormat'); } From 44b397f36b7c35246138bb227131097095246c25 Mon Sep 17 00:00:00 2001 From: Adam Zimmermann Date: Thu, 10 Sep 2020 17:30:16 -0500 Subject: [PATCH 04/19] Fix code styles. --- coder_sniffer/Drupal/Sniffs/Commenting/TodoCommentSniff.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/coder_sniffer/Drupal/Sniffs/Commenting/TodoCommentSniff.php b/coder_sniffer/Drupal/Sniffs/Commenting/TodoCommentSniff.php index 011e2d0a..33a3be20 100644 --- a/coder_sniffer/Drupal/Sniffs/Commenting/TodoCommentSniff.php +++ b/coder_sniffer/Drupal/Sniffs/Commenting/TodoCommentSniff.php @@ -49,7 +49,7 @@ public function register() */ public function process(File $phpcsFile, $stackPtr) { - $tokens = $phpcsFile->getTokens(); + $tokens = $phpcsFile->getTokens(); // Use the comment text by default. $comment = $tokens[$stackPtr]['content']; // Use the next line for multi-line comments. From 5eef8dce175ce5faaaebb44eb3f0f36df838314d Mon Sep 17 00:00:00 2001 From: Adam Zimmermann Date: Thu, 10 Sep 2020 17:42:47 -0500 Subject: [PATCH 05/19] Add check for a single space and capital letter. --- coder_sniffer/Drupal/Sniffs/Commenting/TodoCommentSniff.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/coder_sniffer/Drupal/Sniffs/Commenting/TodoCommentSniff.php b/coder_sniffer/Drupal/Sniffs/Commenting/TodoCommentSniff.php index 33a3be20..e529f925 100644 --- a/coder_sniffer/Drupal/Sniffs/Commenting/TodoCommentSniff.php +++ b/coder_sniffer/Drupal/Sniffs/Commenting/TodoCommentSniff.php @@ -57,7 +57,7 @@ public function process(File $phpcsFile, $stackPtr) $comment = $phpcsFile->findNext(T_DOC_COMMENT_STRING, ($stackPtr + 1)); } - $expression = '/^\s*(?i)(?=(@*to(-|\s|)+do))(?-i)(?!@todo\s\w)/m'; + $expression = '/^\s*(?i)(?=(@*to(-|\s|)+do))(?-i)(?!@todo\s{1}[A-Z])/m'; if ((bool) preg_match($expression, $comment) === true) { $phpcsFile->addError('@todo comments should be in the "@todo Some task." format.', $stackPtr, 'TodoFormat'); } From 1acde9e9b95a4112ac7a77b87910b06dc0d773ca Mon Sep 17 00:00:00 2001 From: Adam Zimmermann Date: Fri, 25 Sep 2020 13:08:56 -0500 Subject: [PATCH 06/19] Fix regex for "//" format comments. --- coder_sniffer/Drupal/Sniffs/Commenting/TodoCommentSniff.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/coder_sniffer/Drupal/Sniffs/Commenting/TodoCommentSniff.php b/coder_sniffer/Drupal/Sniffs/Commenting/TodoCommentSniff.php index e529f925..0176ff78 100644 --- a/coder_sniffer/Drupal/Sniffs/Commenting/TodoCommentSniff.php +++ b/coder_sniffer/Drupal/Sniffs/Commenting/TodoCommentSniff.php @@ -57,7 +57,7 @@ public function process(File $phpcsFile, $stackPtr) $comment = $phpcsFile->findNext(T_DOC_COMMENT_STRING, ($stackPtr + 1)); } - $expression = '/^\s*(?i)(?=(@*to(-|\s|)+do))(?-i)(?!@todo\s{1}[A-Z])/m'; + $expression = '/^(\/\/)*\s*(?i)(?=(@*to(-|\s|)+do))(?-i)(?!@todo\s{1}[A-Z])/m'; if ((bool) preg_match($expression, $comment) === true) { $phpcsFile->addError('@todo comments should be in the "@todo Some task." format.', $stackPtr, 'TodoFormat'); } From ac29748ad607c4a92f9ca5f08e63eee8489db494 Mon Sep 17 00:00:00 2001 From: Adam Zimmermann Date: Fri, 25 Sep 2020 13:25:44 -0500 Subject: [PATCH 07/19] Remove capital letter requirement. --- coder_sniffer/Drupal/Sniffs/Commenting/TodoCommentSniff.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/coder_sniffer/Drupal/Sniffs/Commenting/TodoCommentSniff.php b/coder_sniffer/Drupal/Sniffs/Commenting/TodoCommentSniff.php index 0176ff78..d98ac6f0 100644 --- a/coder_sniffer/Drupal/Sniffs/Commenting/TodoCommentSniff.php +++ b/coder_sniffer/Drupal/Sniffs/Commenting/TodoCommentSniff.php @@ -57,7 +57,7 @@ public function process(File $phpcsFile, $stackPtr) $comment = $phpcsFile->findNext(T_DOC_COMMENT_STRING, ($stackPtr + 1)); } - $expression = '/^(\/\/)*\s*(?i)(?=(@*to(-|\s|)+do))(?-i)(?!@todo\s{1}[A-Z])/m'; + $expression = '/^(\/\/)*\s*(?i)(?=(@*to(-|\s|)+do))(?-i)(?!@todo\s{1}\S)/m'; if ((bool) preg_match($expression, $comment) === true) { $phpcsFile->addError('@todo comments should be in the "@todo Some task." format.', $stackPtr, 'TodoFormat'); } From 3eefdc64576303a835fd00d2e2119367e38292ea Mon Sep 17 00:00:00 2001 From: Adam Zimmermann Date: Sat, 26 Sep 2020 08:03:14 -0500 Subject: [PATCH 08/19] Fix docblock comment sniff. --- .../Sniffs/Commenting/TodoCommentSniff.php | 52 ++++++++++++++----- 1 file changed, 40 insertions(+), 12 deletions(-) diff --git a/coder_sniffer/Drupal/Sniffs/Commenting/TodoCommentSniff.php b/coder_sniffer/Drupal/Sniffs/Commenting/TodoCommentSniff.php index d98ac6f0..71e10c74 100644 --- a/coder_sniffer/Drupal/Sniffs/Commenting/TodoCommentSniff.php +++ b/coder_sniffer/Drupal/Sniffs/Commenting/TodoCommentSniff.php @@ -33,6 +33,7 @@ public function register() return [ T_COMMENT, T_DOC_COMMENT_TAG, + T_DOC_COMMENT_STRING, ]; }//end register() @@ -47,22 +48,49 @@ public function register() * * @return void */ - public function process(File $phpcsFile, $stackPtr) - { - $tokens = $phpcsFile->getTokens(); - // Use the comment text by default. + public function process(File $phpcsFile, $stackPtr) { + $tokens = $phpcsFile->getTokens(); + // Standard comments and multi-line comments where the "@" is missing so + // it does not register as a T_DOC_COMMENT_TAG. + if (in_array($tokens[$stackPtr]['code'], [T_COMMENT, T_DOC_COMMENT_STRING])) { $comment = $tokens[$stackPtr]['content']; - // Use the next line for multi-line comments. - if ($tokens[$stackPtr]['code'] === T_DOC_COMMENT_TAG) { - $comment = $phpcsFile->findNext(T_DOC_COMMENT_STRING, ($stackPtr + 1)); - } - - $expression = '/^(\/\/)*\s*(?i)(?=(@*to(-|\s|)+do))(?-i)(?!@todo\s{1}\S)/m'; - if ((bool) preg_match($expression, $comment) === true) { - $phpcsFile->addError('@todo comments should be in the "@todo Some task." format.', $stackPtr, 'TodoFormat'); + $this->checkTodoFormat($phpcsFile, $stackPtr, $comment); + } + // Document comment tag (i.e. comments that begin with "@"). + elseif ($tokens[$stackPtr]['code'] == T_DOC_COMMENT_TAG) { + // Determine if this is related at all and build the full comment line + // from the various segments that the line is parsed into. + $expression = '/^@to/i'; + if ((bool) preg_match($expression, $tokens[$stackPtr]['content']) === true) { + $index = $stackPtr; + $comment = ''; + while ($tokens[$index]['line'] == $tokens[$stackPtr]['line']) { + $comment .= $tokens[$index]['content']; + $index++; + } + $this->checkTodoFormat($phpcsFile, $stackPtr, $comment); } + } }//end process() + /** + * Checks a comment string for the correct syntax. + * + * @param \PHP_CodeSniffer\Files\File $phpcsFile + * The file being scanned. + * @param int $stackPtr + * The position of the current token in the stack passed in $tokens. + * @param string $comment + * The comment text. + * + * @return void + */ + private function checkTodoFormat(File $phpcsFile, $stackPtr, string $comment) { + $expression = '/^(\/\/|\*)*\s*(?i)(?=(@*to(-|\s|)+do))(?-i)(?!@todo\s(?!-|:))/m'; + if ((bool) preg_match($expression, $comment) === true) { + $phpcsFile->addError('@todo comments should be in the "@todo Some task." format.', $stackPtr, 'TodoFormat'); + } + } }//end class From b39ad5d57929bb60a9b71a2b8eba2b30c38a761f Mon Sep 17 00:00:00 2001 From: Adam Zimmermann Date: Sat, 26 Sep 2020 09:45:14 -0500 Subject: [PATCH 09/19] Code style fixes. --- .../Sniffs/Commenting/TodoCommentSniff.php | 64 ++++++++++--------- 1 file changed, 34 insertions(+), 30 deletions(-) diff --git a/coder_sniffer/Drupal/Sniffs/Commenting/TodoCommentSniff.php b/coder_sniffer/Drupal/Sniffs/Commenting/TodoCommentSniff.php index 71e10c74..1de6f008 100644 --- a/coder_sniffer/Drupal/Sniffs/Commenting/TodoCommentSniff.php +++ b/coder_sniffer/Drupal/Sniffs/Commenting/TodoCommentSniff.php @@ -48,49 +48,53 @@ public function register() * * @return void */ - public function process(File $phpcsFile, $stackPtr) { - $tokens = $phpcsFile->getTokens(); - // Standard comments and multi-line comments where the "@" is missing so - // it does not register as a T_DOC_COMMENT_TAG. - if (in_array($tokens[$stackPtr]['code'], [T_COMMENT, T_DOC_COMMENT_STRING])) { - $comment = $tokens[$stackPtr]['content']; - $this->checkTodoFormat($phpcsFile, $stackPtr, $comment); - } - // Document comment tag (i.e. comments that begin with "@"). - elseif ($tokens[$stackPtr]['code'] == T_DOC_COMMENT_TAG) { - // Determine if this is related at all and build the full comment line - // from the various segments that the line is parsed into. - $expression = '/^@to/i'; - if ((bool) preg_match($expression, $tokens[$stackPtr]['content']) === true) { - $index = $stackPtr; - $comment = ''; - while ($tokens[$index]['line'] == $tokens[$stackPtr]['line']) { - $comment .= $tokens[$index]['content']; - $index++; - } - $this->checkTodoFormat($phpcsFile, $stackPtr, $comment); + public function process(File $phpcsFile, $stackPtr) + { + $tokens = $phpcsFile->getTokens(); + // Standard comments and multi-line comments where the "@" is missing so + // it does not register as a T_DOC_COMMENT_TAG. + if (in_array($tokens[$stackPtr]['code'], [T_COMMENT, T_DOC_COMMENT_STRING])) { + $comment = $tokens[$stackPtr]['content']; + $this->checkTodoFormat($phpcsFile, $stackPtr, $comment); + } + // Document comment tag (i.e. comments that begin with "@"). + else if ($tokens[$stackPtr]['code'] === T_DOC_COMMENT_TAG) { + // Determine if this is related at all and build the full comment line + // from the various segments that the line is parsed into. + $expression = '/^@to/i'; + if ((bool) preg_match($expression, $tokens[$stackPtr]['content']) === true) { + $index = $stackPtr; + $comment = ''; + while ($tokens[$index]['line'] === $tokens[$stackPtr]['line']) { + $comment .= $tokens[$index]['content']; + $index++; + } + $this->checkTodoFormat($phpcsFile, $stackPtr, $comment); + } } - } }//end process() + /** * Checks a comment string for the correct syntax. * * @param \PHP_CodeSniffer\Files\File $phpcsFile * The file being scanned. - * @param int $stackPtr + * @param int $stackPtr * The position of the current token in the stack passed in $tokens. - * @param string $comment + * @param string $comment * The comment text. * * @return void */ - private function checkTodoFormat(File $phpcsFile, $stackPtr, string $comment) { - $expression = '/^(\/\/|\*)*\s*(?i)(?=(@*to(-|\s|)+do))(?-i)(?!@todo\s(?!-|:))/m'; - if ((bool) preg_match($expression, $comment) === true) { - $phpcsFile->addError('@todo comments should be in the "@todo Some task." format.', $stackPtr, 'TodoFormat'); - } - } + private function checkTodoFormat(File $phpcsFile, $stackPtr, string $comment) + { + $expression = '/^(\/\/|\*)*\s*(?i)(?=(@*to(-|\s|)+do))(?-i)(?!@todo\s(?!-|:))/m'; + if ((bool) preg_match($expression, $comment) === true) { + $phpcsFile->addError('@todo comments should be in the "@todo Some task." format.', $stackPtr, 'TodoFormat'); + } + }//end checkTodoFormat() + }//end class From a2248540b7bdb1905c114fb8a084e34604e4f28f Mon Sep 17 00:00:00 2001 From: Adam Zimmermann Date: Sat, 26 Sep 2020 09:46:51 -0500 Subject: [PATCH 10/19] Fix @todo syntax per new sniff. --- tests/Drupal/good/good.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/Drupal/good/good.php b/tests/Drupal/good/good.php index 0efcf91c..5c2a5fc8 100644 --- a/tests/Drupal/good/good.php +++ b/tests/Drupal/good/good.php @@ -1185,7 +1185,7 @@ function test6(array $names) { /** * Some short description. * - * @todo TODOs are allowed here. + * @@todo TODOs are allowed here. * * @param string $x * Some parameter. @@ -1205,8 +1205,8 @@ class ListContainsTest extends RulesIntegrationTestBase {} /** * Provides a 'Delete any path alias' action. * - * @todo: Add access callback information from Drupal 7. - * @todo: Add group information from Drupal 7. + * @todo Add access callback information from Drupal 7. + * @todo Add group information from Drupal 7. * * @Action( * id = "rules_path_alias_delete", From 14c4a4087b229ee2bc09fc325ab5eb7e2545bd13 Mon Sep 17 00:00:00 2001 From: Adam Zimmermann Date: Sat, 26 Sep 2020 10:17:54 -0500 Subject: [PATCH 11/19] Add unit test. --- .../Commenting/TodoCommentUnitTest.inc.php | 60 +++++++++++++++++++ .../Drupal/Commenting/TodoCommentUnitTest.php | 45 ++++++++++++++ 2 files changed, 105 insertions(+) create mode 100644 tests/Drupal/Commenting/TodoCommentUnitTest.inc.php create mode 100644 tests/Drupal/Commenting/TodoCommentUnitTest.php diff --git a/tests/Drupal/Commenting/TodoCommentUnitTest.inc.php b/tests/Drupal/Commenting/TodoCommentUnitTest.inc.php new file mode 100644 index 00000000..adbb9867 --- /dev/null +++ b/tests/Drupal/Commenting/TodoCommentUnitTest.inc.php @@ -0,0 +1,60 @@ + + */ + protected function getErrorList(string $testFile): array + { + return array_fill_keys(range(13, 30), 1) + array_fill_keys(range(41, 59), 1); + + }//end getErrorList() + + + /** + * Returns the lines where warnings should occur. + * + * The key of the array should represent the line number and the value + * should represent the number of warnings that should occur on that line. + * + * @param string $testFile The name of the file being tested. + * + * @return array + */ + protected function getWarningList(string $testFile): array + { + return []; + + }//end getWarningList() + + +}//end class From 1c2480925cbfa3e013d96494653b8629248625b3 Mon Sep 17 00:00:00 2001 From: Adam Zimmermann Date: Sat, 26 Sep 2020 10:23:20 -0500 Subject: [PATCH 12/19] Code style fixes. --- .../Sniffs/Commenting/TodoCommentSniff.php | 19 ++++++++++--------- tests/Drupal/good/good.php | 4 ++-- 2 files changed, 12 insertions(+), 11 deletions(-) diff --git a/coder_sniffer/Drupal/Sniffs/Commenting/TodoCommentSniff.php b/coder_sniffer/Drupal/Sniffs/Commenting/TodoCommentSniff.php index 1de6f008..62a3a995 100644 --- a/coder_sniffer/Drupal/Sniffs/Commenting/TodoCommentSniff.php +++ b/coder_sniffer/Drupal/Sniffs/Commenting/TodoCommentSniff.php @@ -57,18 +57,20 @@ public function process(File $phpcsFile, $stackPtr) $comment = $tokens[$stackPtr]['content']; $this->checkTodoFormat($phpcsFile, $stackPtr, $comment); } - // Document comment tag (i.e. comments that begin with "@"). + else if ($tokens[$stackPtr]['code'] === T_DOC_COMMENT_TAG) { + // Document comment tag (i.e. comments that begin with "@"). // Determine if this is related at all and build the full comment line // from the various segments that the line is parsed into. $expression = '/^@to/i'; if ((bool) preg_match($expression, $tokens[$stackPtr]['content']) === true) { - $index = $stackPtr; + $index = $stackPtr; $comment = ''; while ($tokens[$index]['line'] === $tokens[$stackPtr]['line']) { $comment .= $tokens[$index]['content']; $index++; } + $this->checkTodoFormat($phpcsFile, $stackPtr, $comment); } } @@ -79,12 +81,10 @@ public function process(File $phpcsFile, $stackPtr) /** * Checks a comment string for the correct syntax. * - * @param \PHP_CodeSniffer\Files\File $phpcsFile - * The file being scanned. - * @param int $stackPtr - * The position of the current token in the stack passed in $tokens. - * @param string $comment - * The comment text. + * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned. + * @param int $stackPtr The position of the current token + * in the stack passed in $tokens. + * @param string $comment The comment text. * * @return void */ @@ -92,8 +92,9 @@ private function checkTodoFormat(File $phpcsFile, $stackPtr, string $comment) { $expression = '/^(\/\/|\*)*\s*(?i)(?=(@*to(-|\s|)+do))(?-i)(?!@todo\s(?!-|:))/m'; if ((bool) preg_match($expression, $comment) === true) { - $phpcsFile->addError('@todo comments should be in the "@todo Some task." format.', $stackPtr, 'TodoFormat'); + $phpcsFile->addError('@todo comments should be in the "@todo Some task." format.', $stackPtr, 'TodoFormat'); } + }//end checkTodoFormat() diff --git a/tests/Drupal/good/good.php b/tests/Drupal/good/good.php index 5c2a5fc8..2f36e561 100644 --- a/tests/Drupal/good/good.php +++ b/tests/Drupal/good/good.php @@ -1185,10 +1185,10 @@ function test6(array $names) { /** * Some short description. * - * @@todo TODOs are allowed here. - * * @param string $x * Some parameter. + * + * @todo These are allowed here. */ function test7($x) { From b6eed65dce1816efef7ea7a651be713e9842cc47 Mon Sep 17 00:00:00 2001 From: Adam Zimmermann Date: Sat, 26 Sep 2020 10:42:34 -0500 Subject: [PATCH 13/19] Code style fixes. --- .../Sniffs/Commenting/TodoCommentSniff.php | 28 ++-- .../Commenting/TodoCommentUnitTest.inc.php | 157 ++++++++++++++---- .../Drupal/Commenting/TodoCommentUnitTest.php | 64 +++---- 3 files changed, 169 insertions(+), 80 deletions(-) diff --git a/coder_sniffer/Drupal/Sniffs/Commenting/TodoCommentSniff.php b/coder_sniffer/Drupal/Sniffs/Commenting/TodoCommentSniff.php index 62a3a995..802ae3a1 100644 --- a/coder_sniffer/Drupal/Sniffs/Commenting/TodoCommentSniff.php +++ b/coder_sniffer/Drupal/Sniffs/Commenting/TodoCommentSniff.php @@ -53,12 +53,10 @@ public function process(File $phpcsFile, $stackPtr) $tokens = $phpcsFile->getTokens(); // Standard comments and multi-line comments where the "@" is missing so // it does not register as a T_DOC_COMMENT_TAG. - if (in_array($tokens[$stackPtr]['code'], [T_COMMENT, T_DOC_COMMENT_STRING])) { + if ($tokens[$stackPtr]['code'] === T_COMMENT || $tokens[$stackPtr]['code'] === T_DOC_COMMENT_STRING) { $comment = $tokens[$stackPtr]['content']; $this->checkTodoFormat($phpcsFile, $stackPtr, $comment); - } - - else if ($tokens[$stackPtr]['code'] === T_DOC_COMMENT_TAG) { + } else if ($tokens[$stackPtr]['code'] === T_DOC_COMMENT_TAG) { // Document comment tag (i.e. comments that begin with "@"). // Determine if this is related at all and build the full comment line // from the various segments that the line is parsed into. @@ -72,22 +70,22 @@ public function process(File $phpcsFile, $stackPtr) } $this->checkTodoFormat($phpcsFile, $stackPtr, $comment); - } + }//end if } }//end process() - /** - * Checks a comment string for the correct syntax. - * - * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned. - * @param int $stackPtr The position of the current token - * in the stack passed in $tokens. - * @param string $comment The comment text. - * - * @return void - */ + /** + * Checks a comment string for the correct syntax. + * + * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned. + * @param int $stackPtr The position of the current token + * in the stack passed in $tokens. + * @param string $comment The comment text. + * + * @return void + */ private function checkTodoFormat(File $phpcsFile, $stackPtr, string $comment) { $expression = '/^(\/\/|\*)*\s*(?i)(?=(@*to(-|\s|)+do))(?-i)(?!@todo\s(?!-|:))/m'; diff --git a/tests/Drupal/Commenting/TodoCommentUnitTest.inc.php b/tests/Drupal/Commenting/TodoCommentUnitTest.inc.php index adbb9867..d67b812d 100644 --- a/tests/Drupal/Commenting/TodoCommentUnitTest.inc.php +++ b/tests/Drupal/Commenting/TodoCommentUnitTest.inc.php @@ -1,60 +1,151 @@ - */ - protected function getErrorList(string $testFile): array - { - return array_fill_keys(range(13, 30), 1) + array_fill_keys(range(41, 59), 1); - - }//end getErrorList() - - - /** - * Returns the lines where warnings should occur. - * - * The key of the array should represent the line number and the value - * should represent the number of warnings that should occur on that line. - * - * @param string $testFile The name of the file being tested. - * - * @return array - */ - protected function getWarningList(string $testFile): array - { - return []; - - }//end getWarningList() + /** + * Returns the lines where errors should occur. + * + * The key of the array should represent the line number and the value + * should represent the number of errors that should occur on that line. + * + * @param string $testFile The name of the file being tested. + * + * @return array + */ + protected function getErrorList(string $testFile): array + { + return array_fill_keys(range(13, 30), 1) + array_fill_keys(range(41, 59), 1); + + }//end getErrorList() + + + /** + * Returns the lines where warnings should occur. + * + * The key of the array should represent the line number and the value + * should represent the number of warnings that should occur on that line. + * + * @param string $testFile The name of the file being tested. + * + * @return array + */ + protected function getWarningList(string $testFile): array + { + return []; + + }//end getWarningList() }//end class From 0a68762aad139224136baa45445556a980a24a80 Mon Sep 17 00:00:00 2001 From: Adam Zimmermann Date: Sat, 26 Sep 2020 10:50:43 -0500 Subject: [PATCH 14/19] Include existing format in error message. --- coder_sniffer/Drupal/Sniffs/Commenting/TodoCommentSniff.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/coder_sniffer/Drupal/Sniffs/Commenting/TodoCommentSniff.php b/coder_sniffer/Drupal/Sniffs/Commenting/TodoCommentSniff.php index 802ae3a1..bc8a9aa1 100644 --- a/coder_sniffer/Drupal/Sniffs/Commenting/TodoCommentSniff.php +++ b/coder_sniffer/Drupal/Sniffs/Commenting/TodoCommentSniff.php @@ -90,7 +90,8 @@ private function checkTodoFormat(File $phpcsFile, $stackPtr, string $comment) { $expression = '/^(\/\/|\*)*\s*(?i)(?=(@*to(-|\s|)+do))(?-i)(?!@todo\s(?!-|:))/m'; if ((bool) preg_match($expression, $comment) === true) { - $phpcsFile->addError('@todo comments should be in the "@todo Some task." format.', $stackPtr, 'TodoFormat'); + $comment = trim($comment, " /\r\n"); + $phpcsFile->addError("'%s' should match the format '@todo Some task'", $stackPtr, 'TodoFormat', [$comment]); } }//end checkTodoFormat() From 3cf7c3902970f64cd33602dded27725c6ca93b48 Mon Sep 17 00:00:00 2001 From: Adam Zimmermann Date: Sat, 26 Sep 2020 11:02:51 -0500 Subject: [PATCH 15/19] Fix testing errors. --- .../Commenting/TodoCommentUnitTest.inc.php | 49 ++++++++++--------- .../Drupal/Commenting/TodoCommentUnitTest.php | 26 +++++++++- 2 files changed, 51 insertions(+), 24 deletions(-) diff --git a/tests/Drupal/Commenting/TodoCommentUnitTest.inc.php b/tests/Drupal/Commenting/TodoCommentUnitTest.inc.php index d67b812d..29ef2996 100644 --- a/tests/Drupal/Commenting/TodoCommentUnitTest.inc.php +++ b/tests/Drupal/Commenting/TodoCommentUnitTest.inc.php @@ -1,121 +1,122 @@ Date: Sat, 26 Sep 2020 11:15:32 -0500 Subject: [PATCH 16/19] More test fixes. --- .../Commenting/TodoCommentUnitTest.inc.php | 50 ++++++++++--------- .../Drupal/Commenting/TodoCommentUnitTest.php | 4 -- 2 files changed, 27 insertions(+), 27 deletions(-) diff --git a/tests/Drupal/Commenting/TodoCommentUnitTest.inc.php b/tests/Drupal/Commenting/TodoCommentUnitTest.inc.php index 29ef2996..6e4d1dbf 100644 --- a/tests/Drupal/Commenting/TodoCommentUnitTest.inc.php +++ b/tests/Drupal/Commenting/TodoCommentUnitTest.inc.php @@ -1,124 +1,128 @@ Date: Sat, 26 Sep 2020 11:20:40 -0500 Subject: [PATCH 17/19] Simplify error listing. --- .../Commenting/TodoCommentUnitTest.inc.php | 29 +++---------------- .../Drupal/Commenting/TodoCommentUnitTest.php | 22 ++------------ 2 files changed, 7 insertions(+), 44 deletions(-) diff --git a/tests/Drupal/Commenting/TodoCommentUnitTest.inc.php b/tests/Drupal/Commenting/TodoCommentUnitTest.inc.php index 6e4d1dbf..468bbc29 100644 --- a/tests/Drupal/Commenting/TodoCommentUnitTest.inc.php +++ b/tests/Drupal/Commenting/TodoCommentUnitTest.inc.php @@ -1,124 +1,103 @@ Date: Sun, 27 Sep 2020 15:56:00 -0500 Subject: [PATCH 18/19] Fix tests. --- .../Sniffs/Commenting/TodoCommentSniff.php | 2 +- .../Commenting/TodoCommentUnitTest.inc.php | 46 +++++++++---------- .../Drupal/Commenting/TodoCommentUnitTest.php | 4 +- 3 files changed, 26 insertions(+), 26 deletions(-) diff --git a/coder_sniffer/Drupal/Sniffs/Commenting/TodoCommentSniff.php b/coder_sniffer/Drupal/Sniffs/Commenting/TodoCommentSniff.php index bc8a9aa1..41193b68 100644 --- a/coder_sniffer/Drupal/Sniffs/Commenting/TodoCommentSniff.php +++ b/coder_sniffer/Drupal/Sniffs/Commenting/TodoCommentSniff.php @@ -88,7 +88,7 @@ public function process(File $phpcsFile, $stackPtr) */ private function checkTodoFormat(File $phpcsFile, $stackPtr, string $comment) { - $expression = '/^(\/\/|\*)*\s*(?i)(?=(@*to(-|\s|)+do))(?-i)(?!@todo\s(?!-|:))/m'; + $expression = '/^(\/\/|\*)*\s*(?i)(?=(@*to(-|\s|)+do))(?-i)(?!@todo\s(?!-|:)\S)/m'; if ((bool) preg_match($expression, $comment) === true) { $comment = trim($comment, " /\r\n"); $phpcsFile->addError("'%s' should match the format '@todo Some task'", $stackPtr, 'TodoFormat', [$comment]); diff --git a/tests/Drupal/Commenting/TodoCommentUnitTest.inc.php b/tests/Drupal/Commenting/TodoCommentUnitTest.inc.php index 468bbc29..6c50cd75 100644 --- a/tests/Drupal/Commenting/TodoCommentUnitTest.inc.php +++ b/tests/Drupal/Commenting/TodoCommentUnitTest.inc.php @@ -5,95 +5,95 @@ * Example task comments. */ -/* +/** * @todo Valid. */ -/* +/** * @todo Valid */ -/* +/** * @todo This is valid. */ -/* +/** * @todo this is valid. */ -/* +/** * @TODO Error */ -/* +/** * @ToDo Error */ -/* +/** * @TODo Error */ -/* +/** * @ToDO Error */ -/* +/** * @todo: Error */ -/* +/** * @to-do Error */ -/* +/** * @TO-DO Error */ -/* +/** * @To-Do Error */ -/* +/** * @TO do Error */ -/* +/** * @to do Error */ -/* +/** * @todo: Error */ -/* +/** * @todo : Error */ -/* +/** * @todo- Error */ -/* +/** * @todo - Error */ -/* +/** * @todoError */ -/* +/** * todo Error */ -/* +/** * TODO Error */ -/* +/** * ToDo Error */ -/* +/** * @todo Error */ diff --git a/tests/Drupal/Commenting/TodoCommentUnitTest.php b/tests/Drupal/Commenting/TodoCommentUnitTest.php index e2a76fd2..0f01bd35 100644 --- a/tests/Drupal/Commenting/TodoCommentUnitTest.php +++ b/tests/Drupal/Commenting/TodoCommentUnitTest.php @@ -20,8 +20,8 @@ class TodoCommentUnitTest extends CoderSniffUnitTest */ protected function getErrorList(string $testFile): array { - $errorList = array_fill_keys(range(133, 151), 1); - for ($i = 25; $i < 100; $i + 4) { + $errorList = array_fill_keys(range(116, 134), 1); + for ($i = 25; $i < 100; $i += 4) { $errorList[$i] = 1; } return $errorList; From 68b284a1e2ffad1e81216bd082d9013ab749eded Mon Sep 17 00:00:00 2001 From: Adam Zimmermann Date: Sun, 27 Sep 2020 16:39:28 -0500 Subject: [PATCH 19/19] Fix code style issues. --- .../Commenting/TodoCommentUnitTest.inc.php | 269 +++++++++++++++--- .../Drupal/Commenting/TodoCommentUnitTest.php | 5 +- 2 files changed, 229 insertions(+), 45 deletions(-) diff --git a/tests/Drupal/Commenting/TodoCommentUnitTest.inc.php b/tests/Drupal/Commenting/TodoCommentUnitTest.inc.php index 6c50cd75..39f16694 100644 --- a/tests/Drupal/Commenting/TodoCommentUnitTest.inc.php +++ b/tests/Drupal/Commenting/TodoCommentUnitTest.inc.php @@ -1,137 +1,320 @@