-
Notifications
You must be signed in to change notification settings - Fork 51
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(CaseSemicolon): Add sniff to check semicolons on case statements…
… (#3487111)
- Loading branch information
Showing
4 changed files
with
163 additions
and
0 deletions.
There are no files selected for viewing
63 changes: 63 additions & 0 deletions
63
coder_sniffer/Drupal/Sniffs/ControlStructures/CaseSemicolonSniff.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
<?php | ||
/** | ||
* Verifies that case statements conform to their coding standards. | ||
* | ||
* @category PHP | ||
* @package PHP_CodeSniffer | ||
* @link http://pear.php.net/package/PHP_CodeSniffer | ||
*/ | ||
|
||
namespace Drupal\Sniffs\ControlStructures; | ||
|
||
use PHP_CodeSniffer\Files\File; | ||
use PHP_CodeSniffer\Sniffs\Sniff; | ||
|
||
/** | ||
* Checks that a colon ":" is used instead of ";" on case statements. | ||
* | ||
* @category PHP | ||
* @package PHP_CodeSniffer | ||
* @link http://pear.php.net/package/PHP_CodeSniffer | ||
*/ | ||
class CaseSemicolonSniff implements Sniff | ||
{ | ||
|
||
|
||
/** | ||
* Returns an array of tokens this test wants to listen for. | ||
* | ||
* @return array<int|string> | ||
*/ | ||
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<?php | ||
|
||
switch ($key) { | ||
case 'field_name': | ||
$checked_value = $field_storage->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; | ||
} |
28 changes: 28 additions & 0 deletions
28
tests/Drupal/ControlStructures/CaseSemicolonUnitTest.inc.fixed
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<?php | ||
|
||
/** | ||
* @file | ||
*/ | ||
|
||
switch ($key) { | ||
case 'field_name': | ||
$checked_value = $field_storage->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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
<?php | ||
|
||
namespace Drupal\Test\ControlStructures; | ||
|
||
use Drupal\Test\CoderSniffUnitTest; | ||
|
||
class CaseSemicolonUnitTest extends CoderSniffUnitTest | ||
{ | ||
|
||
|
||
/** | ||
* 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<int, int> | ||
*/ | ||
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<int, int> | ||
*/ | ||
protected function getWarningList(string $testFile): array | ||
{ | ||
return []; | ||
|
||
}//end getWarningList() | ||
|
||
|
||
}//end class |