Last active
August 29, 2015 14:02
-
-
Save lozcalver/d77f1107ff6fbda45298 to your computer and use it in GitHub Desktop.
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
<?php | |
if (class_exists('PHP_CodeSniffer_Standards_AbstractPatternSniff', true) === false) { | |
throw new PHP_CodeSniffer_Exception('Class PHP_CodeSniffer_Standards_AbstractPatternSniff not found'); | |
} | |
class SilverStripe_Sniffs_ControlStructures_ControlSignatureSniff 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_IF, | |
T_ELSE, | |
T_ELSEIF, | |
T_FOREACH, | |
T_WHILE, | |
T_DO, | |
T_SWITCH, | |
T_FOR, | |
T_TRY, | |
T_CATCH, | |
); | |
} | |
public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) { | |
$tokens = $phpcsFile->getTokens(); | |
// Ignore the ELSE in ELSE IF. We'll process the IF part later when the parser gets to it | |
if(($tokens[$stackPtr]['code'] === T_ELSE) && ($tokens[($stackPtr + 2)]['code'] === T_IF)) { | |
return; | |
} | |
// If the control structure has parenthesis, get the position of the opening one | |
// If not, we ain't interested, so return | |
if(isset($tokens[$stackPtr]['parenthesis_opener']) === true) { | |
$checkPosition = $tokens[$stackPtr]['parenthesis_opener']; | |
} else { | |
return; | |
} | |
if($tokens[$checkPosition-1]['code'] === T_WHITESPACE) { | |
$phpcsFile->addError('Control structures with whitespace before parenthesis are not allowed.', $stackPtr, 'NotAllowed'); | |
return; | |
} | |
return; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment