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

Breadcrumb

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

function DisallowYodaConditionsSniff::isArrayStatic

Determines if an array is a static definition.

Parameters

\PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.:

int $arrayToken The position of the array token.:

Return value

bool

1 call to DisallowYodaConditionsSniff::isArrayStatic()
DisallowYodaConditionsSniff::process in vendor/squizlabs/php_codesniffer/src/Standards/Generic/Sniffs/ControlStructures/DisallowYodaConditionsSniff.php
Processes this test, when one of its tokens is encountered.

File

vendor/squizlabs/php_codesniffer/src/Standards/Generic/Sniffs/ControlStructures/DisallowYodaConditionsSniff.php, line 137

Class

DisallowYodaConditionsSniff

Namespace

PHP_CodeSniffer\Standards\Generic\Sniffs\ControlStructures

Code

public function isArrayStatic(File $phpcsFile, $arrayToken) {
    $tokens = $phpcsFile->getTokens();
    if ($tokens[$arrayToken]['code'] === T_OPEN_SHORT_ARRAY) {
        $start = $arrayToken;
        $end = $tokens[$arrayToken]['bracket_closer'];
    }
    else {
        if ($tokens[$arrayToken]['code'] === T_ARRAY) {
            $start = $tokens[$arrayToken]['parenthesis_opener'];
            $end = $tokens[$arrayToken]['parenthesis_closer'];
        }
        else {
            // Shouldn't be possible but may happen if external sniffs are using this method.
            return true;
            // @codeCoverageIgnore
        }
    }
    $staticTokens = Tokens::$emptyTokens;
    $staticTokens += Tokens::$textStringTokens;
    $staticTokens += Tokens::$assignmentTokens;
    $staticTokens += Tokens::$equalityTokens;
    $staticTokens += Tokens::$comparisonTokens;
    $staticTokens += Tokens::$arithmeticTokens;
    $staticTokens += Tokens::$operators;
    $staticTokens += Tokens::$booleanOperators;
    $staticTokens += Tokens::$castTokens;
    $staticTokens += Tokens::$bracketTokens;
    $staticTokens += [
        T_DOUBLE_ARROW => T_DOUBLE_ARROW,
        T_COMMA => T_COMMA,
        T_TRUE => T_TRUE,
        T_FALSE => T_FALSE,
    ];
    for ($i = $start + 1; $i < $end; $i++) {
        if (isset($tokens[$i]['scope_closer']) === true) {
            $i = $tokens[$i]['scope_closer'];
            continue;
        }
        if (isset($staticTokens[$tokens[$i]['code']]) === false) {
            return false;
        }
    }
    return true;
}

API Navigation

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