function UnconditionalIfStatementSniff::process
Processes this test, when one of its tokens is encountered.
Parameters
\PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.:
int $stackPtr The position of the current token: in the stack passed in $tokens.
Return value
void
Overrides Sniff::process
File
-
vendor/
squizlabs/ php_codesniffer/ src/ Standards/ Generic/ Sniffs/ CodeAnalysis/ UnconditionalIfStatementSniff.php, line 61
Class
Namespace
PHP_CodeSniffer\Standards\Generic\Sniffs\CodeAnalysisCode
public function process(File $phpcsFile, $stackPtr) {
$tokens = $phpcsFile->getTokens();
$token = $tokens[$stackPtr];
// Skip if statement without body.
if (isset($token['parenthesis_opener'], $token['parenthesis_closer']) === false) {
return;
}
$next = ++$token['parenthesis_opener'];
$end = --$token['parenthesis_closer'];
$goodCondition = false;
for (; $next <= $end; ++$next) {
$code = $tokens[$next]['code'];
if (isset(Tokens::$emptyTokens[$code]) === true) {
continue;
}
else {
if ($code !== T_TRUE && $code !== T_FALSE) {
$goodCondition = true;
}
}
}
if ($goodCondition === false) {
$error = 'Avoid IF statements that are always true or false';
$phpcsFile->addWarning($error, $stackPtr, 'Found');
}
}