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

Breadcrumb

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

function RequireExplicitBooleanOperatorPrecedenceSniff::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/RequireExplicitBooleanOperatorPrecedenceSniff.php, line 74

Class

RequireExplicitBooleanOperatorPrecedenceSniff

Namespace

PHP_CodeSniffer\Standards\Generic\Sniffs\CodeAnalysis

Code

public function process(File $phpcsFile, $stackPtr) {
    $tokens = $phpcsFile->getTokens();
    $start = $phpcsFile->findStartOfStatement($stackPtr);
    $previous = $phpcsFile->findPrevious($this->searchTargets, $stackPtr - 1, $start, false, null, true);
    if ($previous === false) {
        // No token found.
        return;
    }
    if ($tokens[$previous]['code'] === $tokens[$stackPtr]['code']) {
        // Identical operator found.
        return;
    }
    if (in_array($tokens[$previous]['code'], [
        T_INLINE_THEN,
        T_INLINE_ELSE,
    ], true) === true) {
        // Beginning of the expression found for the ternary conditional operator.
        return;
    }
    // We found a mismatching operator, thus we must report the error.
    $error = 'Mixing different binary boolean operators within an expression';
    $error .= ' without using parentheses to clarify precedence is not allowed.';
    $phpcsFile->addError($error, $stackPtr, 'MissingParentheses');
}
RSS feed
Powered by Drupal