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

Breadcrumb

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

function DisallowYodaConditionsSniff::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/ControlStructures/DisallowYodaConditionsSniff.php, line 45

Class

DisallowYodaConditionsSniff

Namespace

PHP_CodeSniffer\Standards\Generic\Sniffs\ControlStructures

Code

public function process(File $phpcsFile, $stackPtr) {
    $tokens = $phpcsFile->getTokens();
    $previousIndex = $phpcsFile->findPrevious(Tokens::$emptyTokens, $stackPtr - 1, null, true);
    $relevantTokens = [
        T_CLOSE_SHORT_ARRAY,
        T_CLOSE_PARENTHESIS,
        T_TRUE,
        T_FALSE,
        T_NULL,
        T_LNUMBER,
        T_DNUMBER,
        T_CONSTANT_ENCAPSED_STRING,
    ];
    if (in_array($tokens[$previousIndex]['code'], $relevantTokens, true) === false) {
        return;
    }
    if ($tokens[$previousIndex]['code'] === T_CLOSE_SHORT_ARRAY) {
        $previousIndex = $tokens[$previousIndex]['bracket_opener'];
        if ($this->isArrayStatic($phpcsFile, $previousIndex) === false) {
            return;
        }
    }
    $prevIndex = $phpcsFile->findPrevious(Tokens::$emptyTokens, $previousIndex - 1, null, true);
    if (in_array($tokens[$prevIndex]['code'], Tokens::$arithmeticTokens, true) === true) {
        return;
    }
    if ($tokens[$prevIndex]['code'] === T_STRING_CONCAT) {
        return;
    }
    // Is it a parenthesis.
    if ($tokens[$previousIndex]['code'] === T_CLOSE_PARENTHESIS) {
        $beforeOpeningParenthesisIndex = $phpcsFile->findPrevious(Tokens::$emptyTokens, $tokens[$previousIndex]['parenthesis_opener'] - 1, null, true);
        if ($beforeOpeningParenthesisIndex === false || $tokens[$beforeOpeningParenthesisIndex]['code'] !== T_ARRAY) {
            if ($tokens[$beforeOpeningParenthesisIndex]['code'] === T_STRING) {
                return;
            }
            // If it is not an array check what is inside.
            $found = $phpcsFile->findPrevious(T_VARIABLE, $previousIndex - 1, $tokens[$previousIndex]['parenthesis_opener']);
            // If a variable exists, it is not Yoda.
            if ($found !== false) {
                return;
            }
            // If there is nothing inside the parenthesis, it is not a Yoda condition.
            $opener = $tokens[$previousIndex]['parenthesis_opener'];
            $prev = $phpcsFile->findPrevious(Tokens::$emptyTokens, $previousIndex - 1, $opener + 1, true);
            if ($prev === false) {
                return;
            }
        }
        else {
            if ($tokens[$beforeOpeningParenthesisIndex]['code'] === T_ARRAY && $this->isArrayStatic($phpcsFile, $beforeOpeningParenthesisIndex) === false) {
                return;
            }
        }
        
        //end if
    }
    
    //end if
    $phpcsFile->addError('Usage of Yoda conditions is not allowed; switch the expression order', $stackPtr, 'Found');
}

API Navigation

  • Drupal Core 11.1.x
  • Topics
  • Classes
  • Functions
  • Constants
  • Globals
  • Files
  • Namespaces
  • Deprecated
  • Services
RSS feed
Powered by Drupal