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

Breadcrumb

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

function File::findEndOfStatement

Returns the position of the last non-whitespace token in a statement.

Parameters

int $start The position to start searching from in the token stack.:

int|string|array $ignore Token types that should not be considered stop points.:

Return value

int

File

vendor/squizlabs/php_codesniffer/src/Files/File.php, line 2600

Class

File

Namespace

PHP_CodeSniffer\Files

Code

public function findEndOfStatement($start, $ignore = null) {
    $endTokens = [
        T_COLON => true,
        T_COMMA => true,
        T_DOUBLE_ARROW => true,
        T_SEMICOLON => true,
        T_CLOSE_PARENTHESIS => true,
        T_CLOSE_SQUARE_BRACKET => true,
        T_CLOSE_CURLY_BRACKET => true,
        T_CLOSE_SHORT_ARRAY => true,
        T_OPEN_TAG => true,
        T_CLOSE_TAG => true,
    ];
    if ($ignore !== null) {
        $ignore = (array) $ignore;
        foreach ($ignore as $code) {
            unset($endTokens[$code]);
        }
    }
    // If the start token is inside the case part of a match expression,
    // advance to the match arrow and continue looking for the
    // end of the statement from there so that we skip over commas.
    if ($this->tokens[$start]['code'] !== T_MATCH_ARROW) {
        $matchExpression = $this->getCondition($start, T_MATCH);
        if ($matchExpression !== false) {
            $beforeArrow = true;
            $prevMatchArrow = $this->findPrevious(T_MATCH_ARROW, $start - 1, $this->tokens[$matchExpression]['scope_opener']);
            if ($prevMatchArrow !== false) {
                $prevComma = $this->findNext(T_COMMA, $prevMatchArrow + 1, $start);
                if ($prevComma === false) {
                    // No comma between this token and the last match arrow,
                    // so this token exists after the arrow and we can continue
                    // checking as normal.
                    $beforeArrow = false;
                }
            }
            if ($beforeArrow === true) {
                $nextMatchArrow = $this->findNext(T_MATCH_ARROW, $start + 1, $this->tokens[$matchExpression]['scope_closer']);
                if ($nextMatchArrow !== false) {
                    $start = $nextMatchArrow;
                }
            }
        }
        
        //end if
    }
    
    //end if
    $lastNotEmpty = $start;
    for ($i = $start; $i < $this->numTokens; $i++) {
        if ($i !== $start && isset($endTokens[$this->tokens[$i]['code']]) === true) {
            // Found the end of the statement.
            if ($this->tokens[$i]['code'] === T_CLOSE_PARENTHESIS || $this->tokens[$i]['code'] === T_CLOSE_SQUARE_BRACKET || $this->tokens[$i]['code'] === T_CLOSE_CURLY_BRACKET || $this->tokens[$i]['code'] === T_CLOSE_SHORT_ARRAY || $this->tokens[$i]['code'] === T_OPEN_TAG || $this->tokens[$i]['code'] === T_CLOSE_TAG) {
                return $lastNotEmpty;
            }
            return $i;
        }
        // Skip nested statements.
        if (isset($this->tokens[$i]['scope_closer']) === true && ($i === $this->tokens[$i]['scope_opener'] || $i === $this->tokens[$i]['scope_condition'])) {
            if ($this->tokens[$i]['code'] === T_FN) {
                $lastNotEmpty = $this->tokens[$i]['scope_closer'];
                $i = $this->tokens[$i]['scope_closer'] - 1;
                continue;
            }
            if ($i === $start && isset(Tokens::$scopeOpeners[$this->tokens[$i]['code']]) === true) {
                return $this->tokens[$i]['scope_closer'];
            }
            $i = $this->tokens[$i]['scope_closer'];
        }
        else {
            if (isset($this->tokens[$i]['bracket_closer']) === true && $i === $this->tokens[$i]['bracket_opener']) {
                $i = $this->tokens[$i]['bracket_closer'];
            }
            else {
                if (isset($this->tokens[$i]['parenthesis_closer']) === true && $i === $this->tokens[$i]['parenthesis_opener']) {
                    $i = $this->tokens[$i]['parenthesis_closer'];
                }
                else {
                    if ($this->tokens[$i]['code'] === T_OPEN_USE_GROUP) {
                        $end = $this->findNext(T_CLOSE_USE_GROUP, $i + 1);
                        if ($end !== false) {
                            $i = $end;
                        }
                    }
                }
            }
        }
        
        //end if
        if (isset(Tokens::$emptyTokens[$this->tokens[$i]['code']]) === false) {
            $lastNotEmpty = $i;
        }
    }
    
    //end for
    return $this->numTokens - 1;
}

API Navigation

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