diff --git a/coder_sniffer/Drupal/Sniffs/ControlStructures/CaseSemicolonSniff.php b/coder_sniffer/Drupal/Sniffs/ControlStructures/CaseSemicolonSniff.php new file mode 100644 index 00000000..301e13c3 --- /dev/null +++ b/coder_sniffer/Drupal/Sniffs/ControlStructures/CaseSemicolonSniff.php @@ -0,0 +1,63 @@ + + */ + public function register() + { + return [T_CASE]; + + }//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(); + if (isset($tokens[$stackPtr]['scope_opener']) === true + && $tokens[$tokens[$stackPtr]['scope_opener']]['code'] === T_SEMICOLON + ) { + $error = 'A colon ":" must be used to open a case statement, found ";"'; + $fix = $phpcsFile->addFixableError($error, $stackPtr, 'SemicolonNotAllowed'); + if ($fix === true) { + $phpcsFile->fixer->replaceToken($tokens[$stackPtr]['scope_opener'], ':'); + } + } + + }//end process() + + +}//end class diff --git a/tests/Drupal/ControlStructures/CaseSemicolonUnitTest.inc b/tests/Drupal/ControlStructures/CaseSemicolonUnitTest.inc new file mode 100644 index 00000000..7513913e --- /dev/null +++ b/tests/Drupal/ControlStructures/CaseSemicolonUnitTest.inc @@ -0,0 +1,24 @@ +getName(); + break; + + case 'field_id': + case 'field_storage_uuid': + $checked_value = $field_storage->uuid(); + break; + + case 'uuid'; + $checked_value = $field->uuid(); + break; + + case 'deleted'; + $checked_value = $field->isDeleted(); + break; + + default: + $checked_value = $field->get($key); + break; +} diff --git a/tests/Drupal/ControlStructures/CaseSemicolonUnitTest.inc.fixed b/tests/Drupal/ControlStructures/CaseSemicolonUnitTest.inc.fixed new file mode 100644 index 00000000..da6a995a --- /dev/null +++ b/tests/Drupal/ControlStructures/CaseSemicolonUnitTest.inc.fixed @@ -0,0 +1,28 @@ +getName(); + break; + + case 'field_id': + case 'field_storage_uuid': + $checked_value = $field_storage->uuid(); + break; + + case 'uuid': + $checked_value = $field->uuid(); + break; + + case 'deleted': + $checked_value = $field->isDeleted(); + break; + + default: + $checked_value = $field->get($key); + break; +} diff --git a/tests/Drupal/ControlStructures/CaseSemicolonUnitTest.php b/tests/Drupal/ControlStructures/CaseSemicolonUnitTest.php new file mode 100644 index 00000000..79dfe5df --- /dev/null +++ b/tests/Drupal/ControlStructures/CaseSemicolonUnitTest.php @@ -0,0 +1,48 @@ + + */ + protected function getErrorList(string $testFile): array + { + return [ + 13 => 1, + 17 => 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