PHP_CodeSniffer_File::findEndOfStatement PHP Method

findEndOfStatement() public method

Returns the position of the last non-whitespace token in a statement.
public findEndOfStatement ( integer $start, integer | array $ignore = null ) : integer
$start integer The position to start searching from in the token stack.
$ignore integer | array Token types that should not be considered stop points.
return integer
    public function findEndOfStatement($start, $ignore = null)
    {
        $endTokens = array(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) {
                if (isset($endTokens[$code]) === true) {
                    unset($endTokens[$code]);
                }
            }
        }
        $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'])) {
                $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'];
                    }
                }
            }
            if (isset(PHP_CodeSniffer_Tokens::$emptyTokens[$this->_tokens[$i]['code']]) === false) {
                $lastNotEmpty = $i;
            }
        }
        //end for
        return $this->numTokens - 1;
    }

Usage Example

 /**
  * Processes single-line calls.
  *
  * @param PHP_CodeSniffer_File $phpcsFile   The file being scanned.
  * @param int                  $stackPtr    The position of the current token
  *                                          in the stack passed in $tokens.
  * @param int                  $openBracket The position of the opening bracket
  *                                          in the stack passed in $tokens.
  * @param array                $tokens      The stack of tokens that make up
  *                                          the file.
  *
  * @return void
  */
 public function isMultiLineCall(PHP_CodeSniffer_File $phpcsFile, $stackPtr, $openBracket, $tokens)
 {
     // If the first argument is on a new line, this is a multi-line
     // function call, even if there is only one argument.
     $next = $phpcsFile->findNext(PHP_CodeSniffer_Tokens::$emptyTokens, $openBracket + 1, null, true);
     if ($tokens[$next]['line'] !== $tokens[$stackPtr]['line']) {
         return true;
     }
     $closeBracket = $tokens[$openBracket]['parenthesis_closer'];
     $end = $phpcsFile->findEndOfStatement($openBracket + 1);
     while ($tokens[$end]['code'] === T_COMMA) {
         // If the next bit of code is not on the same line, this is a
         // multi-line function call.
         $next = $phpcsFile->findNext(PHP_CodeSniffer_Tokens::$emptyTokens, $end + 1, $closeBracket, true);
         if ($next === false) {
             return false;
         }
         if ($tokens[$next]['line'] !== $tokens[$end]['line']) {
             return true;
         }
         $end = $phpcsFile->findEndOfStatement($next);
     }
     // We've reached the last argument, so see if the next content
     // (should be the close bracket) is also on the same line.
     $next = $phpcsFile->findNext(PHP_CodeSniffer_Tokens::$emptyTokens, $end + 1, $closeBracket, true);
     if ($next !== false && $tokens[$next]['line'] !== $tokens[$end]['line']) {
         return true;
     }
     return false;
 }
All Usage Examples Of PHP_CodeSniffer_File::findEndOfStatement