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

Breadcrumb

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

class UnconditionalIfStatementSniff

Hierarchy

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

Expanded class hierarchy of UnconditionalIfStatementSniff

File

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

Namespace

PHP_CodeSniffer\Standards\Generic\Sniffs\CodeAnalysis
View source
class UnconditionalIfStatementSniff implements Sniff {
    
    /**
     * Registers the tokens that this sniff wants to listen for.
     *
     * @return array<int|string>
     */
    public function register() {
        return [
            T_IF,
            T_ELSEIF,
        ];
    }
    
    //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 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');
        }
    }
    
    //end process()

}

Members

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