Initial Commit

This commit is contained in:
Riley Schneider
2025-12-03 16:38:10 +01:00
parent c5e26bf594
commit b732d8d4b5
17680 changed files with 5977495 additions and 2 deletions

View File

@@ -0,0 +1,69 @@
<?php
/**
* Verifies that control statements conform to their coding standards.
*
* PHP version 5
*
* @category PHP
* @package PHP_CodeSniffer
* @author Greg Sherwood <gsherwood@squiz.net>
* @author Marc McIntyre <mmcintyre@squiz.net>
* @copyright 2006-2011 Squiz Pty Ltd (ABN 77 084 670 600)
* @license http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence
* @link http://pear.php.net/package/PHP_CodeSniffer
*/
if (class_exists('PHP_CodeSniffer_Standards_AbstractPatternSniff', true) === false) {
throw new PHP_CodeSniffer_Exception('Class PHP_CodeSniffer_Standards_AbstractPatternSniff not found');
}
/**
* Verifies that control statements conform to their coding standards.
*
* @category PHP
* @package PHP_CodeSniffer
* @author Greg Sherwood <gsherwood@squiz.net>
* @author Marc McIntyre <mmcintyre@squiz.net>
* @copyright 2006-2011 Squiz Pty Ltd (ABN 77 084 670 600)
* @license http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence
* @version Release: 1.3.3
* @link http://pear.php.net/package/PHP_CodeSniffer
*/
class Squiz_Sniffs_ControlStructures_ControlSignatureSniff extends PHP_CodeSniffer_Standards_AbstractPatternSniff
{
/**
* A list of tokenizers this sniff supports.
*
* @var array
*/
public $supportedTokenizers = array(
'PHP',
'JS',
);
/**
* Returns the patterns that this test wishes to verify.
*
* @return array(string)
*/
protected function getPatterns()
{
return array(
'try {EOL...} catch (...) {EOL',
'do {EOL...} while (...);EOL',
'while (...) {EOL',
'for (...) {EOL',
'if (...) {EOL',
'foreach (...) {EOL',
'} else if (...) {EOL',
'} else {EOL',
);
}//end getPatterns()
}//end class
?>

View File

@@ -0,0 +1,67 @@
<?php
/**
* Squiz_Sniffs_ControlStructures_ElseIfDeclarationSniff.
*
* PHP version 5
*
* @category PHP
* @package PHP_CodeSniffer
* @author Greg Sherwood <gsherwood@squiz.net>
* @author Marc McIntyre <mmcintyre@squiz.net>
* @copyright 2006-2011 Squiz Pty Ltd (ABN 77 084 670 600)
* @license http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence
* @link http://pear.php.net/package/PHP_CodeSniffer
*/
/**
* Squiz_Sniffs_ControlStructures_ElseIfDeclarationSniff.
*
* Verifies that there are not elseif statements. The else and the if should
* be separated by a space.
*
* @category PHP
* @package PHP_CodeSniffer
* @author Greg Sherwood <gsherwood@squiz.net>
* @author Marc McIntyre <mmcintyre@squiz.net>
* @copyright 2006-2011 Squiz Pty Ltd (ABN 77 084 670 600)
* @license http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence
* @version Release: 1.3.3
* @link http://pear.php.net/package/PHP_CodeSniffer
*/
class Squiz_Sniffs_ControlStructures_ElseIfDeclarationSniff implements PHP_CodeSniffer_Sniff
{
/**
* Returns an array of tokens this test wants to listen for.
*
* @return array
*/
public function register()
{
return array(T_ELSEIF);
}//end register()
/**
* Processes this test, when one of its tokens is encountered.
*
* @param PHP_CodeSniffer_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(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
{
$error = 'Usage of ELSEIF not allowed; use ELSE IF instead';
$phpcsFile->addError($error, $stackPtr, 'NotAllowed');
}//end process()
}//end class
?>

View File

@@ -0,0 +1,144 @@
<?php
/**
* Squiz_Sniffs_ControlStructures_ForEachLoopDeclarationSniff.
*
* PHP version 5
*
* @category PHP
* @package PHP_CodeSniffer
* @author Greg Sherwood <gsherwood@squiz.net>
* @author Marc McIntyre <mmcintyre@squiz.net>
* @copyright 2006-2011 Squiz Pty Ltd (ABN 77 084 670 600)
* @license http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence
* @link http://pear.php.net/package/PHP_CodeSniffer
*/
/**
* Squiz_Sniffs_ControlStructures_ForEachLoopDeclarationSniff.
*
* Verifies that there is a space between each condition of foreach loops.
*
* @category PHP
* @package PHP_CodeSniffer
* @author Greg Sherwood <gsherwood@squiz.net>
* @author Marc McIntyre <mmcintyre@squiz.net>
* @copyright 2006-2011 Squiz Pty Ltd (ABN 77 084 670 600)
* @license http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence
* @version Release: 1.3.3
* @link http://pear.php.net/package/PHP_CodeSniffer
*/
class Squiz_Sniffs_ControlStructures_ForEachLoopDeclarationSniff implements PHP_CodeSniffer_Sniff
{
/**
* Returns an array of tokens this test wants to listen for.
*
* @return array
*/
public function register()
{
return array(T_FOREACH);
}//end register()
/**
* Processes this test, when one of its tokens is encountered.
*
* @param PHP_CodeSniffer_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(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
{
$tokens = $phpcsFile->getTokens();
$openingBracket = $phpcsFile->findNext(T_OPEN_PARENTHESIS, $stackPtr);
$closingBracket = $tokens[$openingBracket]['parenthesis_closer'];
if ($tokens[($openingBracket + 1)]['code'] === T_WHITESPACE) {
$error = 'Space found after opening bracket of FOREACH loop';
$phpcsFile->addError($error, $stackPtr, 'SpaceAfterOpen');
}
if ($tokens[($closingBracket - 1)]['code'] === T_WHITESPACE) {
$error = 'Space found before closing bracket of FOREACH loop';
$phpcsFile->addError($error, $stackPtr, 'SpaceBeforeClose');
}
$asToken = $phpcsFile->findNext(T_AS, $openingBracket);
$content = $tokens[$asToken]['content'];
if ($content !== strtolower($content)) {
$expected = strtolower($content);
$error = 'AS keyword must be lowercase; expected "%s" but found "%s"';
$data = array(
$expected,
$content,
);
$phpcsFile->addError($error, $stackPtr, 'AsNotLower', $data);
}
$doubleArrow = $phpcsFile->findNext(T_DOUBLE_ARROW, $openingBracket, $closingBracket);
if ($doubleArrow !== false) {
if ($tokens[($doubleArrow - 1)]['code'] !== T_WHITESPACE) {
$error = 'Expected 1 space before "=>"; 0 found';
$phpcsFile->addError($error, $stackPtr, 'NoSpaceBeforeArrow');
} else {
if (strlen($tokens[($doubleArrow - 1)]['content']) !== 1) {
$spaces = strlen($tokens[($doubleArrow - 1)]['content']);
$error = 'Expected 1 space before "=>"; %s found';
$data = array($spaces);
$phpcsFile->addError($error, $stackPtr, 'SpacingBeforeArrow', $data);
}
}
if ($tokens[($doubleArrow + 1)]['code'] !== T_WHITESPACE) {
$error = 'Expected 1 space after "=>"; 0 found';
$phpcsFile->addError($error, $stackPtr, 'NoSpaceAfterArrow');
} else {
if (strlen($tokens[($doubleArrow + 1)]['content']) !== 1) {
$spaces = strlen($tokens[($doubleArrow + 1)]['content']);
$error = 'Expected 1 space after "=>"; %s found';
$data = array($spaces);
$phpcsFile->addError($error, $stackPtr, 'SpacingAfterArrow', $data);
}
}
}//end if
if ($tokens[($asToken - 1)]['code'] !== T_WHITESPACE) {
$error = 'Expected 1 space before "as"; 0 found';
$phpcsFile->addError($error, $stackPtr, 'NoSpaceBeforeAs');
} else {
if (strlen($tokens[($asToken - 1)]['content']) !== 1) {
$spaces = strlen($tokens[($asToken - 1)]['content']);
$error = 'Expected 1 space before "as"; %s found';
$data = array($spaces);
$phpcsFile->addError($error, $stackPtr, 'SpacingBeforeAs', $data);
}
}
if ($tokens[($asToken + 1)]['code'] !== T_WHITESPACE) {
$error = 'Expected 1 space after "as"; 0 found';
$phpcsFile->addError($error, $stackPtr, 'NoSpaceAfterAs');
} else {
if (strlen($tokens[($asToken + 1)]['content']) !== 1) {
$spaces = strlen($tokens[($asToken + 1)]['content']);
$error = 'Expected 1 space after "as"; %s found';
$data = array($spaces);
$phpcsFile->addError($error, $stackPtr, 'SpacingAfterAs', $data);
}
}
}//end process()
}//end class
?>

View File

@@ -0,0 +1,136 @@
<?php
/**
* Squiz_Sniffs_ControlStructures_ForLoopDeclarationSniff.
*
* PHP version 5
*
* @category PHP
* @package PHP_CodeSniffer
* @author Greg Sherwood <gsherwood@squiz.net>
* @author Marc McIntyre <mmcintyre@squiz.net>
* @copyright 2006-2011 Squiz Pty Ltd (ABN 77 084 670 600)
* @license http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence
* @link http://pear.php.net/package/PHP_CodeSniffer
*/
/**
* Squiz_Sniffs_ControlStructures_ForLoopDeclarationSniff.
*
* Verifies that there is a space between each condition of for loops.
*
* @category PHP
* @package PHP_CodeSniffer
* @author Greg Sherwood <gsherwood@squiz.net>
* @author Marc McIntyre <mmcintyre@squiz.net>
* @copyright 2006-2011 Squiz Pty Ltd (ABN 77 084 670 600)
* @license http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence
* @version Release: 1.3.3
* @link http://pear.php.net/package/PHP_CodeSniffer
*/
class Squiz_Sniffs_ControlStructures_ForLoopDeclarationSniff implements PHP_CodeSniffer_Sniff
{
/**
* A list of tokenizers this sniff supports.
*
* @var array
*/
public $supportedTokenizers = array(
'PHP',
'JS',
);
/**
* Returns an array of tokens this test wants to listen for.
*
* @return array
*/
public function register()
{
return array(T_FOR);
}//end register()
/**
* Processes this test, when one of its tokens is encountered.
*
* @param PHP_CodeSniffer_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(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
{
$tokens = $phpcsFile->getTokens();
$openingBracket = $phpcsFile->findNext(T_OPEN_PARENTHESIS, $stackPtr);
if ($openingBracket === false) {
$error = 'Possible parse error: no opening parenthesis for FOR keyword';
$phpcsFile->addWarning($error, $stackPtr, 'NoOpenBracket');
return;
}
$closingBracket = $tokens[$openingBracket]['parenthesis_closer'];
if ($tokens[($openingBracket + 1)]['code'] === T_WHITESPACE) {
$error = 'Space found after opening bracket of FOR loop';
$phpcsFile->addError($error, $stackPtr, 'SpacingAfterOpen');
}
if ($tokens[($closingBracket - 1)]['code'] === T_WHITESPACE) {
$error = 'Space found before closing bracket of FOR loop';
$phpcsFile->addError($error, $stackPtr, 'SpacingBeforeClose');
}
$firstSemicolon = $phpcsFile->findNext(T_SEMICOLON, $openingBracket, $closingBracket);
// Check whitespace around each of the tokens.
if ($firstSemicolon !== false) {
if ($tokens[($firstSemicolon - 1)]['code'] === T_WHITESPACE) {
$error = 'Space found before first semicolon of FOR loop';
$phpcsFile->addError($error, $stackPtr, 'SpacingBeforeFirst');
}
if ($tokens[($firstSemicolon + 1)]['code'] !== T_WHITESPACE) {
$error = 'Expected 1 space after first semicolon of FOR loop; 0 found';
$phpcsFile->addError($error, $stackPtr, 'NoSpaceAfterFirst');
} else {
if (strlen($tokens[($firstSemicolon + 1)]['content']) !== 1) {
$spaces = strlen($tokens[($firstSemicolon + 1)]['content']);
$error = 'Expected 1 space after first semicolon of FOR loop; %s found';
$data = array($spaces);
$phpcsFile->addError($error, $stackPtr, 'SpacingAfterFirst', $data);
}
}
$secondSemicolon = $phpcsFile->findNext(T_SEMICOLON, ($firstSemicolon + 1));
if ($secondSemicolon !== false) {
if ($tokens[($secondSemicolon - 1)]['code'] === T_WHITESPACE) {
$error = 'Space found before second semicolon of FOR loop';
$phpcsFile->addError($error, $stackPtr, 'SpacingBeforeSecond');
}
if ($tokens[($secondSemicolon + 1)]['code'] !== T_WHITESPACE) {
$error = 'Expected 1 space after second semicolon of FOR loop; 0 found';
$phpcsFile->addError($error, $stackPtr, 'NoSpaceAfterSecond');
} else {
if (strlen($tokens[($secondSemicolon + 1)]['content']) !== 1) {
$spaces = strlen($tokens[($firstSemicolon + 1)]['content']);
$error = 'Expected 1 space after second semicolon of FOR loop; %s found';
$data = array($spaces);
$phpcsFile->addError($error, $stackPtr, 'SpacingAfterSecond', $data);
}
}
}//end if
}//end if
}//end process()
}//end class
?>

View File

@@ -0,0 +1,141 @@
<?php
/**
* Squiz_Sniffs_ControlStructures_InlineControlStructureSniff.
*
* PHP version 5
*
* @category PHP
* @package PHP_CodeSniffer
* @author Greg Sherwood <gsherwood@squiz.net>
* @author Marc McIntyre <mmcintyre@squiz.net>
* @copyright 2006-2011 Squiz Pty Ltd (ABN 77 084 670 600)
* @license http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence
* @link http://pear.php.net/package/PHP_CodeSniffer
*/
/**
* Squiz_Sniffs_ControlStructures_InlineIfDeclarationSniff.
*
* Tests the spacing of shorthand IF statements.
*
* @category PHP
* @package PHP_CodeSniffer
* @author Greg Sherwood <gsherwood@squiz.net>
* @author Marc McIntyre <mmcintyre@squiz.net>
* @copyright 2006-2011 Squiz Pty Ltd (ABN 77 084 670 600)
* @license http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence
* @version Release: 1.3.3
* @link http://pear.php.net/package/PHP_CodeSniffer
*/
class Squiz_Sniffs_ControlStructures_InlineIfDeclarationSniff implements PHP_CodeSniffer_Sniff
{
/**
* Returns an array of tokens this test wants to listen for.
*
* @return array
*/
public function register()
{
return array(T_INLINE_THEN);
}//end register()
/**
* Processes this sniff, when one of its tokens is encountered.
*
* @param PHP_CodeSniffer_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(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
{
$tokens = $phpcsFile->getTokens();
// Find the opening bracket of the inline IF.
for ($i = ($stackPtr - 1); $i > 0; $i--) {
if (isset($tokens[$i]['parenthesis_opener']) === true
&& $tokens[$i]['parenthesis_opener'] < $i
) {
$i = $tokens[$i]['parenthesis_opener'];
continue;
}
if ($tokens[$i]['code'] === T_OPEN_PARENTHESIS) {
break;
}
}
if ($i <= 0) {
// Could not find the begining of the statement. Probably not
// wrapped with brackets, so assume it ends with a semicolon.
$statementEnd = $phpcsFile->findNext(T_SEMICOLON, ($stackPtr + 1));
} else {
$statementEnd = $tokens[$i]['parenthesis_closer'];
}
// Make sure it's all on the same line.
if ($tokens[$statementEnd]['line'] !== $tokens[$stackPtr]['line']) {
$error = 'Inline shorthand IF statement must be declared on a single line';
$phpcsFile->addError($error, $stackPtr, 'NotSingleLine');
return;
}
// Make sure there are spaces around the question mark.
$contentBefore = $phpcsFile->findPrevious(T_WHITESPACE, ($stackPtr - 1), null, true);
$contentAfter = $phpcsFile->findNext(T_WHITESPACE, ($stackPtr + 1), null, true);
if ($tokens[$contentBefore]['code'] !== T_CLOSE_PARENTHESIS) {
$error = 'Inline shorthand IF statement requires brackets around comparison';
$phpcsFile->addError($error, $stackPtr, 'NoBrackets');
return;
}
$spaceBefore = ($tokens[$stackPtr]['column'] - ($tokens[$contentBefore]['column'] + strlen($tokens[$contentBefore]['content'])));
if ($spaceBefore !== 1) {
$error = 'Inline shorthand IF statement requires 1 space before THEN; %s found';
$data = array($spaceBefore);
$phpcsFile->addError($error, $stackPtr, 'SpacingBeforeThen', $data);
}
$spaceAfter = (($tokens[$contentAfter]['column']) - ($tokens[$stackPtr]['column'] + 1));
if ($spaceAfter !== 1) {
$error = 'Inline shorthand IF statement requires 1 space after THEN; %s found';
$data = array($spaceAfter);
$phpcsFile->addError($error, $stackPtr, 'SpacingAfterThen', $data);
}
// If there is an else in this condition, make sure it has correct spacing.
$inlineElse = $phpcsFile->findNext(T_COLON, ($stackPtr + 1), $statementEnd, false);
if ($inlineElse === false) {
// No else condition.
return;
}
$contentBefore = $phpcsFile->findPrevious(T_WHITESPACE, ($inlineElse - 1), null, true);
$contentAfter = $phpcsFile->findNext(T_WHITESPACE, ($inlineElse + 1), null, true);
$spaceBefore = ($tokens[$inlineElse]['column'] - ($tokens[$contentBefore]['column'] + strlen($tokens[$contentBefore]['content'])));
if ($spaceBefore !== 1) {
$error = 'Inline shorthand IF statement requires 1 space before ELSE; %s found';
$data = array($spaceBefore);
$phpcsFile->addError($error, $inlineElse, 'SpacingBeforeElse', $data);
}
$spaceAfter = (($tokens[$contentAfter]['column']) - ($tokens[$inlineElse]['column'] + 1));
if ($spaceAfter !== 1) {
$error = 'Inline shorthand IF statement requires 1 space after ELSE; %s found';
$data = array($spaceAfter);
$phpcsFile->addError($error, $inlineElse, 'SpacingAfterElse', $data);
}
}//end process()
}//end class
?>

View File

@@ -0,0 +1,86 @@
<?php
/**
* Squiz_Sniffs_ControlStructures_LowercaseDeclarationSniff.
*
* PHP version 5
*
* @category PHP
* @package PHP_CodeSniffer
* @author Greg Sherwood <gsherwood@squiz.net>
* @author Marc McIntyre <mmcintyre@squiz.net>
* @copyright 2006-2011 Squiz Pty Ltd (ABN 77 084 670 600)
* @license http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence
* @link http://pear.php.net/package/PHP_CodeSniffer
*/
/**
* Squiz_Sniffs_ControlStructures_LowercaseDeclarationSniff.
*
* Ensures all control structure keywords are lowercase.
*
* @category PHP
* @package PHP_CodeSniffer
* @author Greg Sherwood <gsherwood@squiz.net>
* @author Marc McIntyre <mmcintyre@squiz.net>
* @copyright 2006-2011 Squiz Pty Ltd (ABN 77 084 670 600)
* @license http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence
* @version Release: 1.3.3
* @link http://pear.php.net/package/PHP_CodeSniffer
*/
class Squiz_Sniffs_ControlStructures_LowercaseDeclarationSniff implements PHP_CodeSniffer_Sniff
{
/**
* Returns an array of tokens this test wants to listen for.
*
* @return array
*/
public function register()
{
return array(
T_IF,
T_ELSE,
T_ELSEIF,
T_FOREACH,
T_FOR,
T_DO,
T_SWITCH,
T_WHILE,
T_TRY,
T_CATCH,
);
}//end register()
/**
* Processes this test, when one of its tokens is encountered.
*
* @param PHP_CodeSniffer_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(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
{
$tokens = $phpcsFile->getTokens();
$content = $tokens[$stackPtr]['content'];
if ($content !== strtolower($content)) {
$error = '%s keyword must be lowercase; expected "%s" but found "%s"';
$data = array(
strtoupper($content),
strtolower($content),
$content,
);
$phpcsFile->addError($error, $stackPtr, 'FoundUppercase', $data);
}
}//end process()
}//end class
?>

View File

@@ -0,0 +1,255 @@
<?php
/**
* Squiz_Sniffs_ControlStructures_SwitchDeclarationSniff.
*
* PHP version 5
*
* @category PHP
* @package PHP_CodeSniffer
* @author Greg Sherwood <gsherwood@squiz.net>
* @author Marc McIntyre <mmcintyre@squiz.net>
* @copyright 2006-2011 Squiz Pty Ltd (ABN 77 084 670 600)
* @license http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence
* @link http://pear.php.net/package/PHP_CodeSniffer
*/
/**
* Squiz_Sniffs_ControlStructures_SwitchDeclarationSniff.
*
* Ensures all the breaks and cases are aligned correctly according to their
* parent switch's alignment and enforces other switch formatting.
*
* @category PHP
* @package PHP_CodeSniffer
* @author Greg Sherwood <gsherwood@squiz.net>
* @author Marc McIntyre <mmcintyre@squiz.net>
* @copyright 2006-2011 Squiz Pty Ltd (ABN 77 084 670 600)
* @license http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence
* @version Release: 1.3.3
* @link http://pear.php.net/package/PHP_CodeSniffer
*/
class Squiz_Sniffs_ControlStructures_SwitchDeclarationSniff implements PHP_CodeSniffer_Sniff
{
/**
* A list of tokenizers this sniff supports.
*
* @var array
*/
public $supportedTokenizers = array(
'PHP',
'JS',
);
/**
* Returns an array of tokens this test wants to listen for.
*
* @return array
*/
public function register()
{
return array(T_SWITCH);
}//end register()
/**
* Processes this test, when one of its tokens is encountered.
*
* @param PHP_CodeSniffer_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(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
{
$tokens = $phpcsFile->getTokens();
// We can't process SWITCH statements unless we know where they start and end.
if (isset($tokens[$stackPtr]['scope_opener']) === false
|| isset($tokens[$stackPtr]['scope_closer']) === false
) {
return;
}
$switch = $tokens[$stackPtr];
$nextCase = $stackPtr;
$caseAlignment = ($switch['column'] + 4);
$caseCount = 0;
$foundDefault = false;
while (($nextCase = $phpcsFile->findNext(array(T_CASE, T_DEFAULT, T_SWITCH), ($nextCase + 1), $switch['scope_closer'])) !== false) {
// Skip nested SWITCH statements; they are handled on their own.
if ($tokens[$nextCase]['code'] === T_SWITCH) {
$nextCase = $tokens[$nextCase]['scope_closer'];
continue;
}
if ($tokens[$nextCase]['code'] === T_DEFAULT) {
$type = 'Default';
$foundDefault = true;
} else {
$type = 'Case';
$caseCount++;
}
if ($tokens[$nextCase]['content'] !== strtolower($tokens[$nextCase]['content'])) {
$expected = strtolower($tokens[$nextCase]['content']);
$error = strtoupper($type).' keyword must be lowercase; expected "%s" but found "%s"';
$data = array(
$expected,
$tokens[$nextCase]['content'],
);
$phpcsFile->addError($error, $nextCase, $type.'NotLower', $data);
}
if ($tokens[$nextCase]['column'] !== $caseAlignment) {
$error = strtoupper($type).' keyword must be indented 4 spaces from SWITCH keyword';
$phpcsFile->addError($error, $nextCase, $type.'Indent');
}
if ($type === 'Case'
&& ($tokens[($nextCase + 1)]['type'] !== 'T_WHITESPACE'
|| $tokens[($nextCase + 1)]['content'] !== ' ')
) {
$error = 'CASE keyword must be followed by a single space';
$phpcsFile->addError($error, $nextCase, 'SpacingAfterCase');
}
$opener = $tokens[$nextCase]['scope_opener'];
if ($tokens[($opener - 1)]['type'] === 'T_WHITESPACE') {
$error = 'There must be no space before the colon in a '.strtoupper($type).' statement';
$phpcsFile->addError($error, $nextCase, 'SpaceBeforeColon'.$type);
}
$nextBreak = $tokens[$nextCase]['scope_closer'];
if ($tokens[$nextBreak]['code'] === T_BREAK) {
if ($tokens[$nextBreak]['scope_condition'] === $nextCase) {
// Only need to check a couple of things once, even if the
// break is shared between multiple case statements, or even
// the default case.
if ($tokens[$nextBreak]['column'] !== $caseAlignment) {
$error = 'BREAK statement must be indented 4 spaces from SWITCH keyword';
$phpcsFile->addError($error, $nextBreak, 'BreakIndent');
}
$breakLine = $tokens[$nextBreak]['line'];
$prevLine = 0;
for ($i = ($nextBreak - 1); $i > $stackPtr; $i--) {
if ($tokens[$i]['type'] !== 'T_WHITESPACE') {
$prevLine = $tokens[$i]['line'];
break;
}
}
if ($prevLine !== ($breakLine - 1)) {
$error = 'Blank lines are not allowed before BREAK statements';
$phpcsFile->addError($error, $nextBreak, 'SpacingBeforeBreak');
}
$breakLine = $tokens[$nextBreak]['line'];
$nextLine = $tokens[$tokens[$stackPtr]['scope_closer']]['line'];
$semicolon = $phpcsFile->findNext(T_SEMICOLON, $nextBreak);
for ($i = ($semicolon + 1); $i < $tokens[$stackPtr]['scope_closer']; $i++) {
if ($tokens[$i]['type'] !== 'T_WHITESPACE') {
$nextLine = $tokens[$i]['line'];
break;
}
}
if ($type === 'Case') {
// Ensure the BREAK statement is followed by
// a single blank line, or the end switch brace.
if ($nextLine !== ($breakLine + 2) && $i !== $tokens[$stackPtr]['scope_closer']) {
$error = 'BREAK statements must be followed by a single blank line';
$phpcsFile->addError($error, $nextBreak, 'SpacingAfterBreak');
}
} else {
// Ensure the BREAK statement is not followed by a blank line.
if ($nextLine !== ($breakLine + 1)) {
$error = 'Blank lines are not allowed after the DEFAULT case\'s BREAK statement';
$phpcsFile->addError($error, $nextBreak, 'SpacingAfterDefaultBreak');
}
}
$caseLine = $tokens[$nextCase]['line'];
$nextLine = $tokens[$nextBreak]['line'];
for ($i = ($opener + 1); $i < $nextBreak; $i++) {
if ($tokens[$i]['type'] !== 'T_WHITESPACE') {
$nextLine = $tokens[$i]['line'];
break;
}
}
if ($nextLine !== ($caseLine + 1)) {
$error = 'Blank lines are not allowed after '.strtoupper($type).' statements';
$phpcsFile->addError($error, $nextCase, 'SpacingAfter'.$type);
}
}//end if
if ($type === 'Case') {
// Ensure empty CASE statements are not allowed.
// They must have some code content in them. A comment is not enough.
$foundContent = false;
for ($i = ($tokens[$nextCase]['scope_opener'] + 1); $i < $nextBreak; $i++) {
if ($tokens[$i]['code'] === T_CASE) {
$i = $tokens[$i]['scope_opener'];
continue;
}
if (in_array($tokens[$i]['code'], PHP_CodeSniffer_Tokens::$emptyTokens) === false) {
$foundContent = true;
break;
}
}
if ($foundContent === false) {
$error = 'Empty CASE statements are not allowed';
$phpcsFile->addError($error, $nextCase, 'EmptyCase');
}
} else {
// Ensure empty DEFAULT statements are not allowed.
// They must (at least) have a comment describing why
// the default case is being ignored.
$foundContent = false;
for ($i = ($tokens[$nextCase]['scope_opener'] + 1); $i < $nextBreak; $i++) {
if ($tokens[$i]['type'] !== 'T_WHITESPACE') {
$foundContent = true;
break;
}
}
if ($foundContent === false) {
$error = 'Comment required for empty DEFAULT case';
$phpcsFile->addError($error, $nextCase, 'EmptyDefault');
}
}//end if
} else if ($type === 'Default') {
$error = 'DEFAULT case must have a BREAK statement';
$phpcsFile->addError($error, $nextCase, 'DefaultNoBreak');
}//end if
}//end while
if ($foundDefault === false) {
$error = 'All SWITCH statements must contain a DEFAULT case';
$phpcsFile->addError($error, $stackPtr, 'MissingDefault');
}
if ($tokens[$switch['scope_closer']]['column'] !== $switch['column']) {
$error = 'Closing brace of SWITCH statement must be aligned with SWITCH keyword';
$phpcsFile->addError($error, $switch['scope_closer'], 'CloseBraceAlign');
}
if ($caseCount === 0) {
$error = 'SWITCH statements must contain at least one CASE statement';
$phpcsFile->addError($error, $stackPtr, 'MissingCase');
}
}//end process()
}//end class
?>