class ForLoopShouldBeWhileLoopSniff
Hierarchy
- class \PHP_CodeSniffer\Standards\Generic\Sniffs\CodeAnalysis\ForLoopShouldBeWhileLoopSniff implements \PHP_CodeSniffer\Sniffs\Sniff
Expanded class hierarchy of ForLoopShouldBeWhileLoopSniff
File
-
vendor/
squizlabs/ php_codesniffer/ src/ Standards/ Generic/ Sniffs/ CodeAnalysis/ ForLoopShouldBeWhileLoopSniff.php, line 29
Namespace
PHP_CodeSniffer\Standards\Generic\Sniffs\CodeAnalysisView source
class ForLoopShouldBeWhileLoopSniff implements Sniff {
/**
* Registers the tokens that this sniff wants to listen for.
*
* @return array<int|string>
*/
public function register() {
return [
T_FOR,
];
}
//end register()
/**
* Processes this test, when one of its tokens is encountered.
*
* @param \PHP_CodeSniffer\Files\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(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');
}
}
//end process()
}
Members
Title Sort descending | Modifiers | Object type | Summary | Overriden Title |
---|---|---|---|---|
ForLoopShouldBeWhileLoopSniff::process | public | function | Processes this test, when one of its tokens is encountered. | Overrides Sniff::process |
ForLoopShouldBeWhileLoopSniff::register | public | function | Registers the tokens that this sniff wants to listen for. | Overrides Sniff::register |