Skip to content

Commit

Permalink
feat(CaseSemicolon): Add sniff to check semicolons on case statements…
Browse files Browse the repository at this point in the history
… (#3487111)
  • Loading branch information
klausi authored Dec 31, 2024
1 parent bf0dc2f commit ce445b9
Show file tree
Hide file tree
Showing 4 changed files with 163 additions and 0 deletions.
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
24 changes: 24 additions & 0 deletions tests/Drupal/ControlStructures/CaseSemicolonUnitTest.inc
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 tests/Drupal/ControlStructures/CaseSemicolonUnitTest.inc.fixed
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;
}
48 changes: 48 additions & 0 deletions tests/Drupal/ControlStructures/CaseSemicolonUnitTest.php
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

0 comments on commit ce445b9

Please sign in to comment.