Skip to main content
Drupal API
User account menu
  • Log in

Breadcrumb

  1. Drupal Core 11.1.x
  2. ForLoopWithTestFunctionCallSniff.php

class ForLoopWithTestFunctionCallSniff

Hierarchy

  • class \PHP_CodeSniffer\Standards\Generic\Sniffs\CodeAnalysis\ForLoopWithTestFunctionCallSniff implements \PHP_CodeSniffer\Sniffs\Sniff

Expanded class hierarchy of ForLoopWithTestFunctionCallSniff

File

vendor/squizlabs/php_codesniffer/src/Standards/Generic/Sniffs/CodeAnalysis/ForLoopWithTestFunctionCallSniff.php, line 33

Namespace

PHP_CodeSniffer\Standards\Generic\Sniffs\CodeAnalysis
View source
class ForLoopWithTestFunctionCallSniff 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'];
        $position = 0;
        for (; $next <= $end; ++$next) {
            $code = $tokens[$next]['code'];
            if ($code === T_SEMICOLON) {
                ++$position;
            }
            if ($position < 1) {
                continue;
            }
            else {
                if ($position > 1) {
                    break;
                }
                else {
                    if ($code !== T_VARIABLE && $code !== T_STRING) {
                        continue;
                    }
                }
            }
            // Find next non empty token, if it is a open parenthesis we have a
            // function call.
            $index = $phpcsFile->findNext(Tokens::$emptyTokens, $next + 1, null, true);
            if ($tokens[$index]['code'] === T_OPEN_PARENTHESIS) {
                $error = 'Avoid function calls in a FOR loop test part';
                $phpcsFile->addWarning($error, $stackPtr, 'NotAllowed');
                break;
            }
        }
        
        //end for
    }
    
    //end process()

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title
ForLoopWithTestFunctionCallSniff::process public function Processes this test, when one of its tokens is encountered. Overrides Sniff::process
ForLoopWithTestFunctionCallSniff::register public function Registers the tokens that this sniff wants to listen for. Overrides Sniff::register
RSS feed
Powered by Drupal