function ForLoopShouldBeWhileLoopSniff::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/ ForLoopShouldBeWhileLoopSniff.php, line 54
Class
Namespace
PHP_CodeSniffer\Standards\Generic\Sniffs\CodeAnalysisCode
public function process(File $phpcsFile, $stackPtr) {
$tokens = $phpcsFile->getTokens();
$token = $tokens[$stackPtr];
// Skip invalid statement.
if (isset($token['parenthesis_opener'], $token['parenthesis_closer']) === false) {
return;
}
$next = ++$token['parenthesis_opener'];
$end = --$token['parenthesis_closer'];
$parts = [
0,
0,
0,
];
$index = 0;
for (; $next <= $end; ++$next) {
$code = $tokens[$next]['code'];
if ($code === T_SEMICOLON) {
++$index;
}
else {
if (isset(Tokens::$emptyTokens[$code]) === false) {
++$parts[$index];
}
}
}
if ($parts[0] === 0 && $parts[2] === 0 && $parts[1] > 0) {
$error = 'This FOR loop can be simplified to a WHILE loop';
$phpcsFile->addWarning($error, $stackPtr, 'CanSimplify');
}
}