Skip to content

Commit ce445b9

Browse files
authored
feat(CaseSemicolon): Add sniff to check semicolons on case statements (#3487111)
1 parent bf0dc2f commit ce445b9

File tree

4 files changed

+163
-0
lines changed

4 files changed

+163
-0
lines changed
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<?php
2+
/**
3+
* Verifies that case statements conform to their coding standards.
4+
*
5+
* @category PHP
6+
* @package PHP_CodeSniffer
7+
* @link http://pear.php.net/package/PHP_CodeSniffer
8+
*/
9+
10+
namespace Drupal\Sniffs\ControlStructures;
11+
12+
use PHP_CodeSniffer\Files\File;
13+
use PHP_CodeSniffer\Sniffs\Sniff;
14+
15+
/**
16+
* Checks that a colon ":" is used instead of ";" on case statements.
17+
*
18+
* @category PHP
19+
* @package PHP_CodeSniffer
20+
* @link http://pear.php.net/package/PHP_CodeSniffer
21+
*/
22+
class CaseSemicolonSniff implements Sniff
23+
{
24+
25+
26+
/**
27+
* Returns an array of tokens this test wants to listen for.
28+
*
29+
* @return array<int|string>
30+
*/
31+
public function register()
32+
{
33+
return [T_CASE];
34+
35+
}//end register()
36+
37+
38+
/**
39+
* Processes this test, when one of its tokens is encountered.
40+
*
41+
* @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
42+
* @param int $stackPtr The position of the current token in the
43+
* stack passed in $tokens.
44+
*
45+
* @return void
46+
*/
47+
public function process(File $phpcsFile, $stackPtr)
48+
{
49+
$tokens = $phpcsFile->getTokens();
50+
if (isset($tokens[$stackPtr]['scope_opener']) === true
51+
&& $tokens[$tokens[$stackPtr]['scope_opener']]['code'] === T_SEMICOLON
52+
) {
53+
$error = 'A colon ":" must be used to open a case statement, found ";"';
54+
$fix = $phpcsFile->addFixableError($error, $stackPtr, 'SemicolonNotAllowed');
55+
if ($fix === true) {
56+
$phpcsFile->fixer->replaceToken($tokens[$stackPtr]['scope_opener'], ':');
57+
}
58+
}
59+
60+
}//end process()
61+
62+
63+
}//end class
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
switch ($key) {
4+
case 'field_name':
5+
$checked_value = $field_storage->getName();
6+
break;
7+
8+
case 'field_id':
9+
case 'field_storage_uuid':
10+
$checked_value = $field_storage->uuid();
11+
break;
12+
13+
case 'uuid';
14+
$checked_value = $field->uuid();
15+
break;
16+
17+
case 'deleted';
18+
$checked_value = $field->isDeleted();
19+
break;
20+
21+
default:
22+
$checked_value = $field->get($key);
23+
break;
24+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
/**
4+
* @file
5+
*/
6+
7+
switch ($key) {
8+
case 'field_name':
9+
$checked_value = $field_storage->getName();
10+
break;
11+
12+
case 'field_id':
13+
case 'field_storage_uuid':
14+
$checked_value = $field_storage->uuid();
15+
break;
16+
17+
case 'uuid':
18+
$checked_value = $field->uuid();
19+
break;
20+
21+
case 'deleted':
22+
$checked_value = $field->isDeleted();
23+
break;
24+
25+
default:
26+
$checked_value = $field->get($key);
27+
break;
28+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
3+
namespace Drupal\Test\ControlStructures;
4+
5+
use Drupal\Test\CoderSniffUnitTest;
6+
7+
class CaseSemicolonUnitTest extends CoderSniffUnitTest
8+
{
9+
10+
11+
/**
12+
* Returns the lines where errors should occur.
13+
*
14+
* The key of the array should represent the line number and the value
15+
* should represent the number of errors that should occur on that line.
16+
*
17+
* @param string $testFile The name of the file being tested.
18+
*
19+
* @return array<int, int>
20+
*/
21+
protected function getErrorList(string $testFile): array
22+
{
23+
return [
24+
13 => 1,
25+
17 => 1,
26+
];
27+
28+
}//end getErrorList()
29+
30+
31+
/**
32+
* Returns the lines where warnings should occur.
33+
*
34+
* The key of the array should represent the line number and the value
35+
* should represent the number of warnings that should occur on that line.
36+
*
37+
* @param string $testFile The name of the file being tested.
38+
*
39+
* @return array<int, int>
40+
*/
41+
protected function getWarningList(string $testFile): array
42+
{
43+
return [];
44+
45+
}//end getWarningList()
46+
47+
48+
}//end class

0 commit comments

Comments
 (0)