for testing and deploying your application
for finding and fixing issues
for empowering human code reviews
<?php
/**
* PHPCompatibility_Sniffs_PHP_ForbiddenSwitchWithMultipleDefaultBlocksSniff.
*
* PHP version 7.0
* @category PHP
* @package PHPCompatibility
* @author Wim Godden <[email protected]>
*/
* Switch statements can not have multiple default blocks since PHP 7.0
class PHPCompatibility_Sniffs_PHP_ForbiddenSwitchWithMultipleDefaultBlocksSniff extends PHPCompatibility_Sniff
{
* If true, an error will be thrown; otherwise a warning.
* @var bool
protected $error = true;
* 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)
if ($this->supportsAbove('7.0')) {
$tokens = $phpcsFile->getTokens();
$defaultToken = $stackPtr;
$defaultCount = 0;
while ($defaultToken = $phpcsFile->findNext(T_DEFAULT, $defaultToken + 1, $tokens[$stackPtr]['scope_closer'])) {
$defaultCount++;
}
if ($defaultCount > 1) {
$phpcsFile->addError('Switch statements can not have multiple default blocks since PHP 7.0', $stackPtr);
}//end process()
}//end class