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

Breadcrumb

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

function PHP::tokenize

Creates an array of tokens when given some PHP code.

Starts by using token_get_all() but does a lot of extra processing to insert information about the context of the token.

Parameters

string $string The string to tokenize.:

Return value

array

Overrides Tokenizer::tokenize

2 calls to PHP::tokenize()
CSS::tokenize in vendor/squizlabs/php_codesniffer/src/Tokenizers/CSS.php
Creates an array of tokens when given some CSS code.
CSS::tokenize in vendor/squizlabs/php_codesniffer/src/Tokenizers/CSS.php
Creates an array of tokens when given some CSS code.
1 method overrides PHP::tokenize()
CSS::tokenize in vendor/squizlabs/php_codesniffer/src/Tokenizers/CSS.php
Creates an array of tokens when given some CSS code.

File

vendor/squizlabs/php_codesniffer/src/Tokenizers/PHP.php, line 515

Class

PHP

Namespace

PHP_CodeSniffer\Tokenizers

Code

protected function tokenize($string) {
    if (PHP_CODESNIFFER_VERBOSITY > 1) {
        echo "\t*** START PHP TOKENIZING ***" . PHP_EOL;
        $isWin = false;
        if (stripos(PHP_OS, 'WIN') === 0) {
            $isWin = true;
        }
    }
    $tokens = @token_get_all($string);
    $finalTokens = [];
    $newStackPtr = 0;
    $numTokens = count($tokens);
    $lastNotEmptyToken = 0;
    $insideInlineIf = [];
    $insideUseGroup = false;
    $insideConstDeclaration = false;
    $commentTokenizer = new Comment();
    for ($stackPtr = 0; $stackPtr < $numTokens; $stackPtr++) {
        // Special case for tokens we have needed to blank out.
        if ($tokens[$stackPtr] === null) {
            continue;
        }
        $token = (array) $tokens[$stackPtr];
        $tokenIsArray = isset($token[1]);
        if (PHP_CODESNIFFER_VERBOSITY > 1) {
            if ($tokenIsArray === true) {
                $type = Tokens::tokenName($token[0]);
                $content = Common::prepareForOutput($token[1]);
            }
            else {
                $newToken = self::resolveSimpleToken($token[0]);
                $type = $newToken['type'];
                $content = Common::prepareForOutput($token[0]);
            }
            echo "\tProcess token ";
            if ($tokenIsArray === true) {
                echo "[{$stackPtr}]";
            }
            else {
                echo " {$stackPtr} ";
            }
            echo ": {$type} => {$content}";
        }
        
        //end if
        if ($newStackPtr > 0 && isset(Tokens::$emptyTokens[$finalTokens[$newStackPtr - 1]['code']]) === false) {
            $lastNotEmptyToken = $newStackPtr - 1;
        }
        
        /*
            If we are using \r\n newline characters, the \r and \n are sometimes
            split over two tokens. This normally occurs after comments. We need
            to merge these two characters together so that our line endings are
            consistent for all lines.
        */
        if ($tokenIsArray === true && substr($token[1], -1) === "\r") {
            if (isset($tokens[$stackPtr + 1]) === true && is_array($tokens[$stackPtr + 1]) === true && $tokens[$stackPtr + 1][1][0] === "\n") {
                $token[1] .= "\n";
                if (PHP_CODESNIFFER_VERBOSITY > 1) {
                    if ($isWin === true) {
                        echo '\\n';
                    }
                    else {
                        echo "\x1b[30;1m\\n\x1b[0m";
                    }
                }
                if ($tokens[$stackPtr + 1][1] === "\n") {
                    // This token's content has been merged into the previous,
                    // so we can skip it.
                    $tokens[$stackPtr + 1] = '';
                }
                else {
                    $tokens[$stackPtr + 1][1] = substr($tokens[$stackPtr + 1][1], 1);
                }
            }
        }
        
        //end if
        if (PHP_CODESNIFFER_VERBOSITY > 1) {
            echo PHP_EOL;
        }
        
        /*
            Before PHP 5.5, the yield keyword was tokenized as
            T_STRING. So look for and change this token in
            earlier versions.
        */
        if (PHP_VERSION_ID < 50500 && $tokenIsArray === true && $token[0] === T_STRING && strtolower($token[1]) === 'yield' && isset($this->tstringContexts[$finalTokens[$lastNotEmptyToken]['code']]) === false) {
            // Could still be a context sensitive keyword or "yield from" and potentially multi-line,
            // so adjust the token stack in place.
            $token[0] = T_YIELD;
            if (PHP_CODESNIFFER_VERBOSITY > 1) {
                echo "\t\t* token {$stackPtr} changed from T_STRING to T_YIELD" . PHP_EOL;
            }
        }
        
        /*
            Tokenize context sensitive keyword as string when it should be string.
        */
        if ($tokenIsArray === true && isset(Tokens::$contextSensitiveKeywords[$token[0]]) === true && (isset($this->tstringContexts[$finalTokens[$lastNotEmptyToken]['code']]) === true || $finalTokens[$lastNotEmptyToken]['content'] === '&' || $insideConstDeclaration === true)) {
            if (isset($this->tstringContexts[$finalTokens[$lastNotEmptyToken]['code']]) === true) {
                $preserveKeyword = false;
                // `new class`, and `new static` should be preserved.
                if ($finalTokens[$lastNotEmptyToken]['code'] === T_NEW && ($token[0] === T_CLASS || $token[0] === T_STATIC)) {
                    $preserveKeyword = true;
                }
                // `new readonly class` should be preserved.
                if ($finalTokens[$lastNotEmptyToken]['code'] === T_NEW && strtolower($token[1]) === 'readonly') {
                    for ($i = $stackPtr + 1; $i < $numTokens; $i++) {
                        if (is_array($tokens[$i]) === false || isset(Tokens::$emptyTokens[$tokens[$i][0]]) === false) {
                            break;
                        }
                    }
                    if (is_array($tokens[$i]) === true && $tokens[$i][0] === T_CLASS) {
                        $preserveKeyword = true;
                    }
                }
                // `new class extends` `new class implements` should be preserved
                if (($token[0] === T_EXTENDS || $token[0] === T_IMPLEMENTS) && $finalTokens[$lastNotEmptyToken]['code'] === T_CLASS) {
                    $preserveKeyword = true;
                }
                // `namespace\` should be preserved
                if ($token[0] === T_NAMESPACE) {
                    for ($i = $stackPtr + 1; $i < $numTokens; $i++) {
                        if (is_array($tokens[$i]) === false) {
                            break;
                        }
                        if (isset(Tokens::$emptyTokens[$tokens[$i][0]]) === true) {
                            continue;
                        }
                        if ($tokens[$i][0] === T_NS_SEPARATOR) {
                            $preserveKeyword = true;
                        }
                        break;
                    }
                }
            }
            
            //end if
            // Types in typed constants should not be touched, but the constant name should be.
            if (isset($this->tstringContexts[$finalTokens[$lastNotEmptyToken]['code']]) === true && $finalTokens[$lastNotEmptyToken]['code'] === T_CONST || $insideConstDeclaration === true) {
                $preserveKeyword = true;
                // Find the next non-empty token.
                for ($i = $stackPtr + 1; $i < $numTokens; $i++) {
                    if (is_array($tokens[$i]) === true && isset(Tokens::$emptyTokens[$tokens[$i][0]]) === true) {
                        continue;
                    }
                    break;
                }
                if ($tokens[$i] === '=' || $tokens[$i] === ';') {
                    $preserveKeyword = false;
                    $insideConstDeclaration = false;
                }
            }
            
            //end if
            if ($finalTokens[$lastNotEmptyToken]['content'] === '&') {
                $preserveKeyword = true;
                for ($i = $lastNotEmptyToken - 1; $i >= 0; $i--) {
                    if (isset(Tokens::$emptyTokens[$finalTokens[$i]['code']]) === true) {
                        continue;
                    }
                    if ($finalTokens[$i]['code'] === T_FUNCTION) {
                        $preserveKeyword = false;
                    }
                    break;
                }
            }
            if ($preserveKeyword === false) {
                if (PHP_CODESNIFFER_VERBOSITY > 1) {
                    $type = Tokens::tokenName($token[0]);
                    echo "\t\t* token {$stackPtr} changed from {$type} to T_STRING" . PHP_EOL;
                }
                $finalTokens[$newStackPtr] = [
                    'code' => T_STRING,
                    'type' => 'T_STRING',
                    'content' => $token[1],
                ];
                $newStackPtr++;
                continue;
            }
        }
        
        //end if
        
        /*
            Mark the start of a constant declaration to allow for handling keyword to T_STRING
            convertion for constant names using reserved keywords.
        */
        if ($tokenIsArray === true && $token[0] === T_CONST) {
            $insideConstDeclaration = true;
        }
        
        /*
            Close an open "inside constant declaration" marker when no keyword conversion was needed.
        */
        if ($insideConstDeclaration === true && $tokenIsArray === false && ($token[0] === '=' || $token[0] === ';')) {
            $insideConstDeclaration = false;
        }
        
        /*
            Special case for `static` used as a function name, i.e. `static()`.
        
            Note: this may incorrectly change the static keyword directly before a DNF property type.
            If so, this will be caught and corrected for in the additional processing.
        */
        if ($tokenIsArray === true && $token[0] === T_STATIC && $finalTokens[$lastNotEmptyToken]['code'] !== T_NEW) {
            for ($i = $stackPtr + 1; $i < $numTokens; $i++) {
                if (is_array($tokens[$i]) === true && isset(Tokens::$emptyTokens[$tokens[$i][0]]) === true) {
                    continue;
                }
                if ($tokens[$i][0] === '(') {
                    $finalTokens[$newStackPtr] = [
                        'code' => T_STRING,
                        'type' => 'T_STRING',
                        'content' => $token[1],
                    ];
                    $newStackPtr++;
                    continue 2;
                }
                break;
            }
        }
        
        //end if
        
        /*
            Parse doc blocks into something that can be easily iterated over.
        */
        if ($tokenIsArray === true && ($token[0] === T_DOC_COMMENT || $token[0] === T_COMMENT && strpos($token[1], '/**') === 0 && $token[1] !== '/**/')) {
            $commentTokens = $commentTokenizer->tokenizeString($token[1], $this->eolChar, $newStackPtr);
            foreach ($commentTokens as $commentToken) {
                $finalTokens[$newStackPtr] = $commentToken;
                $newStackPtr++;
            }
            continue;
        }
        
        /*
            PHP 8 tokenizes a new line after a slash and hash comment to the next whitespace token.
        */
        if (PHP_VERSION_ID >= 80000 && $tokenIsArray === true && ($token[0] === T_COMMENT && (strpos($token[1], '//') === 0 || strpos($token[1], '#') === 0)) && isset($tokens[$stackPtr + 1]) === true && is_array($tokens[$stackPtr + 1]) === true && $tokens[$stackPtr + 1][0] === T_WHITESPACE) {
            $nextToken = $tokens[$stackPtr + 1];
            // If the next token is a single new line, merge it into the comment token
            // and set to it up to be skipped.
            if ($nextToken[1] === "\n" || $nextToken[1] === "\r\n" || $nextToken[1] === "\n\r") {
                $token[1] .= $nextToken[1];
                $tokens[$stackPtr + 1] = null;
                if (PHP_CODESNIFFER_VERBOSITY > 1) {
                    echo "\t\t* merged newline after comment into comment token {$stackPtr}" . PHP_EOL;
                }
            }
            else {
                // This may be a whitespace token consisting of multiple new lines.
                if (strpos($nextToken[1], "\r\n") === 0) {
                    $token[1] .= "\r\n";
                    $tokens[$stackPtr + 1][1] = substr($nextToken[1], 2);
                }
                else {
                    if (strpos($nextToken[1], "\n\r") === 0) {
                        $token[1] .= "\n\r";
                        $tokens[$stackPtr + 1][1] = substr($nextToken[1], 2);
                    }
                    else {
                        if (strpos($nextToken[1], "\n") === 0) {
                            $token[1] .= "\n";
                            $tokens[$stackPtr + 1][1] = substr($nextToken[1], 1);
                        }
                    }
                }
                if (PHP_CODESNIFFER_VERBOSITY > 1) {
                    echo "\t\t* stripped first newline after comment and added it to comment token {$stackPtr}" . PHP_EOL;
                }
            }
            
            //end if
        }
        
        //end if
        
        /*
            For Explicit Octal Notation prior to PHP 8.1 we need to combine the
            T_LNUMBER and T_STRING token values into a single token value, and
            then ignore the T_STRING token.
        */
        if (PHP_VERSION_ID < 80100 && $tokenIsArray === true && $token[1] === '0' && (isset($tokens[$stackPtr + 1]) === true && is_array($tokens[$stackPtr + 1]) === true && $tokens[$stackPtr + 1][0] === T_STRING && isset($tokens[$stackPtr + 1][1][0], $tokens[$stackPtr + 1][1][1]) === true && strtolower($tokens[$stackPtr + 1][1][0]) === 'o' && $tokens[$stackPtr + 1][1][1] !== '_') && preg_match('`^(o[0-7]+(?:_[0-7]+)?)([0-9_]*)$`i', $tokens[$stackPtr + 1][1], $matches) === 1) {
            $finalTokens[$newStackPtr] = [
                'code' => T_LNUMBER,
                'type' => 'T_LNUMBER',
                'content' => $token[1] .= $matches[1],
            ];
            $newStackPtr++;
            if (isset($matches[2]) === true && $matches[2] !== '') {
                $type = 'T_LNUMBER';
                if ($matches[2][0] === '_') {
                    $type = 'T_STRING';
                }
                $finalTokens[$newStackPtr] = [
                    'code' => constant($type),
                    'type' => $type,
                    'content' => $matches[2],
                ];
                $newStackPtr++;
            }
            $stackPtr++;
            continue;
        }
        
        //end if
        
        /*
            PHP 8.1 introduced two dedicated tokens for the & character.
            Retokenizing both of these to T_BITWISE_AND, which is the
            token PHPCS already tokenized them as.
        */
        if ($tokenIsArray === true && ($token[0] === T_AMPERSAND_FOLLOWED_BY_VAR_OR_VARARG || $token[0] === T_AMPERSAND_NOT_FOLLOWED_BY_VAR_OR_VARARG)) {
            $finalTokens[$newStackPtr] = [
                'code' => T_BITWISE_AND,
                'type' => 'T_BITWISE_AND',
                'content' => $token[1],
            ];
            $newStackPtr++;
            continue;
        }
        
        /*
            If this is a double quoted string, PHP will tokenize the whole
            thing which causes problems with the scope map when braces are
            within the string. So we need to merge the tokens together to
            provide a single string.
        */
        if ($tokenIsArray === false && ($token[0] === '"' || $token[0] === 'b"')) {
            // Binary casts need a special token.
            if ($token[0] === 'b"') {
                $finalTokens[$newStackPtr] = [
                    'code' => T_BINARY_CAST,
                    'type' => 'T_BINARY_CAST',
                    'content' => 'b',
                ];
                $newStackPtr++;
            }
            $tokenContent = '"';
            $nestedVars = [];
            for ($i = $stackPtr + 1; $i < $numTokens; $i++) {
                $subToken = (array) $tokens[$i];
                $subTokenIsArray = isset($subToken[1]);
                if ($subTokenIsArray === true) {
                    $tokenContent .= $subToken[1];
                    if (($subToken[1] === '{' || $subToken[1] === '${') && $subToken[0] !== T_ENCAPSED_AND_WHITESPACE) {
                        $nestedVars[] = $i;
                    }
                }
                else {
                    $tokenContent .= $subToken[0];
                    if ($subToken[0] === '}') {
                        array_pop($nestedVars);
                    }
                }
                if ($subTokenIsArray === false && $subToken[0] === '"' && empty($nestedVars) === true) {
                    // We found the other end of the double quoted string.
                    break;
                }
            }
            
            //end for
            $stackPtr = $i;
            // Convert each line within the double quoted string to a
            // new token, so it conforms with other multiple line tokens.
            $tokenLines = explode($this->eolChar, $tokenContent);
            $numLines = count($tokenLines);
            $newToken = [];
            for ($j = 0; $j < $numLines; $j++) {
                $newToken['content'] = $tokenLines[$j];
                if ($j === $numLines - 1) {
                    if ($tokenLines[$j] === '') {
                        break;
                    }
                }
                else {
                    $newToken['content'] .= $this->eolChar;
                }
                $newToken['code'] = T_DOUBLE_QUOTED_STRING;
                $newToken['type'] = 'T_DOUBLE_QUOTED_STRING';
                $finalTokens[$newStackPtr] = $newToken;
                $newStackPtr++;
            }
            // Continue, as we're done with this token.
            continue;
        }
        
        //end if
        
        /*
            Detect binary casting and assign the casts their own token.
        */
        if ($tokenIsArray === true && $token[0] === T_CONSTANT_ENCAPSED_STRING && (substr($token[1], 0, 2) === 'b"' || substr($token[1], 0, 2) === "b'")) {
            $finalTokens[$newStackPtr] = [
                'code' => T_BINARY_CAST,
                'type' => 'T_BINARY_CAST',
                'content' => 'b',
            ];
            $newStackPtr++;
            $token[1] = substr($token[1], 1);
        }
        if ($tokenIsArray === true && $token[0] === T_STRING_CAST && preg_match('`^\\(\\s*binary\\s*\\)$`i', $token[1]) === 1) {
            $finalTokens[$newStackPtr] = [
                'code' => T_BINARY_CAST,
                'type' => 'T_BINARY_CAST',
                'content' => $token[1],
            ];
            $newStackPtr++;
            continue;
        }
        
        /*
            If this is a heredoc, PHP will tokenize the whole
            thing which causes problems when heredocs don't
            contain real PHP code, which is almost never.
            We want to leave the start and end heredoc tokens
            alone though.
        */
        if ($tokenIsArray === true && $token[0] === T_START_HEREDOC) {
            // Add the start heredoc token to the final array.
            $finalTokens[$newStackPtr] = self::standardiseToken($token);
            // Check if this is actually a nowdoc and use a different token
            // to help the sniffs.
            $nowdoc = false;
            if (strpos($token[1], "'") !== false) {
                $finalTokens[$newStackPtr]['code'] = T_START_NOWDOC;
                $finalTokens[$newStackPtr]['type'] = 'T_START_NOWDOC';
                $nowdoc = true;
            }
            $tokenContent = '';
            for ($i = $stackPtr + 1; $i < $numTokens; $i++) {
                $subTokenIsArray = is_array($tokens[$i]);
                if ($subTokenIsArray === true && $tokens[$i][0] === T_END_HEREDOC) {
                    // We found the other end of the heredoc.
                    break;
                }
                if ($subTokenIsArray === true) {
                    $tokenContent .= $tokens[$i][1];
                }
                else {
                    $tokenContent .= $tokens[$i];
                }
            }
            if ($i === $numTokens) {
                // We got to the end of the file and never
                // found the closing token, so this probably wasn't
                // a heredoc.
                if (PHP_CODESNIFFER_VERBOSITY > 1) {
                    $type = $finalTokens[$newStackPtr]['type'];
                    echo "\t\t* failed to find the end of the here/nowdoc" . PHP_EOL;
                    echo "\t\t* token {$stackPtr} changed from {$type} to T_STRING" . PHP_EOL;
                }
                $finalTokens[$newStackPtr]['code'] = T_STRING;
                $finalTokens[$newStackPtr]['type'] = 'T_STRING';
                $newStackPtr++;
                continue;
            }
            $stackPtr = $i;
            $newStackPtr++;
            // Convert each line within the heredoc to a
            // new token, so it conforms with other multiple line tokens.
            $tokenLines = explode($this->eolChar, $tokenContent);
            $numLines = count($tokenLines);
            $newToken = [];
            for ($j = 0; $j < $numLines; $j++) {
                $newToken['content'] = $tokenLines[$j];
                if ($j === $numLines - 1) {
                    if ($tokenLines[$j] === '') {
                        break;
                    }
                }
                else {
                    $newToken['content'] .= $this->eolChar;
                }
                if ($nowdoc === true) {
                    $newToken['code'] = T_NOWDOC;
                    $newToken['type'] = 'T_NOWDOC';
                }
                else {
                    $newToken['code'] = T_HEREDOC;
                    $newToken['type'] = 'T_HEREDOC';
                }
                $finalTokens[$newStackPtr] = $newToken;
                $newStackPtr++;
            }
            
            //end for
            // Add the end heredoc token to the final array.
            $finalTokens[$newStackPtr] = self::standardiseToken($tokens[$stackPtr]);
            if ($nowdoc === true) {
                $finalTokens[$newStackPtr]['code'] = T_END_NOWDOC;
                $finalTokens[$newStackPtr]['type'] = 'T_END_NOWDOC';
            }
            $newStackPtr++;
            // Continue, as we're done with this token.
            continue;
        }
        
        //end if
        
        /*
            Enum keyword for PHP < 8.1
        */
        if ($tokenIsArray === true && $token[0] === T_STRING && strtolower($token[1]) === 'enum') {
            // Get the next non-empty token.
            for ($i = $stackPtr + 1; $i < $numTokens; $i++) {
                if (is_array($tokens[$i]) === false || isset(Tokens::$emptyTokens[$tokens[$i][0]]) === false) {
                    break;
                }
            }
            if (isset($tokens[$i]) === true && is_array($tokens[$i]) === true && $tokens[$i][0] === T_STRING) {
                // Modify $tokens directly so we can use it later when converting enum "case".
                $tokens[$stackPtr][0] = T_ENUM;
                $newToken = [];
                $newToken['code'] = T_ENUM;
                $newToken['type'] = 'T_ENUM';
                $newToken['content'] = $token[1];
                $finalTokens[$newStackPtr] = $newToken;
                if (PHP_CODESNIFFER_VERBOSITY > 1) {
                    echo "\t\t* token {$stackPtr} changed from T_STRING to T_ENUM" . PHP_EOL;
                }
                $newStackPtr++;
                continue;
            }
        }
        
        //end if
        
        /*
            Convert enum "case" to T_ENUM_CASE
        */
        if ($tokenIsArray === true && $token[0] === T_CASE && isset($this->tstringContexts[$finalTokens[$lastNotEmptyToken]['code']]) === false) {
            $isEnumCase = false;
            $scope = 1;
            for ($i = $stackPtr - 1; $i > 0; $i--) {
                if ($tokens[$i] === '}') {
                    $scope++;
                    continue;
                }
                if ($tokens[$i] === '{') {
                    $scope--;
                    continue;
                }
                if (is_array($tokens[$i]) === false) {
                    continue;
                }
                if ($scope !== 0) {
                    continue;
                }
                if ($tokens[$i][0] === T_SWITCH) {
                    break;
                }
                if ($tokens[$i][0] === T_ENUM || $tokens[$i][0] === T_ENUM_CASE) {
                    $isEnumCase = true;
                    break;
                }
            }
            
            //end for
            if ($isEnumCase === true) {
                // Modify $tokens directly so we can use it as optimisation for other enum "case".
                $tokens[$stackPtr][0] = T_ENUM_CASE;
                $newToken = [];
                $newToken['code'] = T_ENUM_CASE;
                $newToken['type'] = 'T_ENUM_CASE';
                $newToken['content'] = $token[1];
                $finalTokens[$newStackPtr] = $newToken;
                if (PHP_CODESNIFFER_VERBOSITY > 1) {
                    echo "\t\t* token {$stackPtr} changed from T_CASE to T_ENUM_CASE" . PHP_EOL;
                }
                $newStackPtr++;
                continue;
            }
        }
        
        //end if
        
        /*
            As of PHP 8.0 fully qualified, partially qualified and namespace relative
            identifier names are tokenized differently.
            This "undoes" the new tokenization so the tokenization will be the same in
            in PHP 5, 7 and 8.
        */
        if (PHP_VERSION_ID >= 80000 && $tokenIsArray === true && ($token[0] === T_NAME_QUALIFIED || $token[0] === T_NAME_FULLY_QUALIFIED || $token[0] === T_NAME_RELATIVE)) {
            $name = $token[1];
            if ($token[0] === T_NAME_FULLY_QUALIFIED) {
                $newToken = [];
                $newToken['code'] = T_NS_SEPARATOR;
                $newToken['type'] = 'T_NS_SEPARATOR';
                $newToken['content'] = '\\';
                $finalTokens[$newStackPtr] = $newToken;
                ++$newStackPtr;
                $name = ltrim($name, '\\');
            }
            if ($token[0] === T_NAME_RELATIVE) {
                $newToken = [];
                $newToken['code'] = T_NAMESPACE;
                $newToken['type'] = 'T_NAMESPACE';
                $newToken['content'] = substr($name, 0, 9);
                $finalTokens[$newStackPtr] = $newToken;
                ++$newStackPtr;
                $newToken = [];
                $newToken['code'] = T_NS_SEPARATOR;
                $newToken['type'] = 'T_NS_SEPARATOR';
                $newToken['content'] = '\\';
                $finalTokens[$newStackPtr] = $newToken;
                ++$newStackPtr;
                $name = substr($name, 10);
            }
            $parts = explode('\\', $name);
            $partCount = count($parts);
            $lastPart = $partCount - 1;
            foreach ($parts as $i => $part) {
                $newToken = [];
                $newToken['code'] = T_STRING;
                $newToken['type'] = 'T_STRING';
                $newToken['content'] = $part;
                $finalTokens[$newStackPtr] = $newToken;
                ++$newStackPtr;
                if ($i !== $lastPart) {
                    $newToken = [];
                    $newToken['code'] = T_NS_SEPARATOR;
                    $newToken['type'] = 'T_NS_SEPARATOR';
                    $newToken['content'] = '\\';
                    $finalTokens[$newStackPtr] = $newToken;
                    ++$newStackPtr;
                }
            }
            if (PHP_CODESNIFFER_VERBOSITY > 1) {
                $type = Tokens::tokenName($token[0]);
                $content = Common::prepareForOutput($token[1]);
                echo "\t\t* token {$stackPtr} split into individual tokens; was: {$type} => {$content}" . PHP_EOL;
            }
            continue;
        }
        
        //end if
        
        /*
            PHP 8.0 Attributes
        */
        if (PHP_VERSION_ID < 80000 && $token[0] === T_COMMENT && strpos($token[1], '#[') === 0) {
            $subTokens = $this->parsePhpAttribute($tokens, $stackPtr);
            if ($subTokens !== null) {
                array_splice($tokens, $stackPtr, 1, $subTokens);
                $numTokens = count($tokens);
                $tokenIsArray = true;
                $token = $tokens[$stackPtr];
            }
            else {
                $token[0] = T_ATTRIBUTE;
            }
        }
        if ($tokenIsArray === true && $token[0] === T_ATTRIBUTE) {
            // Go looking for the close bracket.
            $bracketCloser = $this->findCloser($tokens, $stackPtr + 1, [
                '[',
                '#[',
            ], ']');
            $newToken = [];
            $newToken['code'] = T_ATTRIBUTE;
            $newToken['type'] = 'T_ATTRIBUTE';
            $newToken['content'] = '#[';
            $finalTokens[$newStackPtr] = $newToken;
            $tokens[$bracketCloser] = [];
            $tokens[$bracketCloser][0] = T_ATTRIBUTE_END;
            $tokens[$bracketCloser][1] = ']';
            if (PHP_CODESNIFFER_VERBOSITY > 1) {
                echo "\t\t* token {$bracketCloser} changed from T_CLOSE_SQUARE_BRACKET to T_ATTRIBUTE_END" . PHP_EOL;
            }
            $newStackPtr++;
            continue;
        }
        
        //end if
        
        /*
            Tokenize the parameter labels for PHP 8.0 named parameters as a special T_PARAM_NAME
            token and ensures that the colon after it is always T_COLON.
        */
        if ($tokenIsArray === true && ($token[0] === T_STRING || preg_match('`^[a-zA-Z_\\x80-\\xff]`', $token[1]) === 1)) {
            // Get the next non-empty token.
            for ($i = $stackPtr + 1; $i < $numTokens; $i++) {
                if (is_array($tokens[$i]) === false || isset(Tokens::$emptyTokens[$tokens[$i][0]]) === false) {
                    break;
                }
            }
            if (isset($tokens[$i]) === true && is_array($tokens[$i]) === false && $tokens[$i] === ':') {
                // Get the previous non-empty token.
                for ($j = $stackPtr - 1; $j > 0; $j--) {
                    if (is_array($tokens[$j]) === false || isset(Tokens::$emptyTokens[$tokens[$j][0]]) === false) {
                        break;
                    }
                }
                if (is_array($tokens[$j]) === false && ($tokens[$j] === '(' || $tokens[$j] === ',')) {
                    $newToken = [];
                    $newToken['code'] = T_PARAM_NAME;
                    $newToken['type'] = 'T_PARAM_NAME';
                    $newToken['content'] = $token[1];
                    $finalTokens[$newStackPtr] = $newToken;
                    $newStackPtr++;
                    // Modify the original token stack so that future checks, like
                    // determining T_COLON vs T_INLINE_ELSE can handle this correctly.
                    $tokens[$stackPtr][0] = T_PARAM_NAME;
                    if (PHP_CODESNIFFER_VERBOSITY > 1) {
                        $type = Tokens::tokenName($token[0]);
                        echo "\t\t* token {$stackPtr} changed from {$type} to T_PARAM_NAME" . PHP_EOL;
                    }
                    continue;
                }
            }
            
            //end if
        }
        
        //end if
        
        /*
            "readonly" keyword for PHP < 8.1
        */
        if ($tokenIsArray === true && strtolower($token[1]) === 'readonly' && (isset($this->tstringContexts[$finalTokens[$lastNotEmptyToken]['code']]) === false || $finalTokens[$lastNotEmptyToken]['code'] === T_NEW)) {
            // Get the next non-whitespace token.
            for ($i = $stackPtr + 1; $i < $numTokens; $i++) {
                if (is_array($tokens[$i]) === false || isset(Tokens::$emptyTokens[$tokens[$i][0]]) === false) {
                    break;
                }
            }
            $isReadonlyKeyword = false;
            if (isset($tokens[$i]) === false || $tokens[$i] !== '(') {
                $isReadonlyKeyword = true;
            }
            else {
                if ($tokens[$i] === '(') {
                    
                    /*
                     * Skip over tokens which can be used in type declarations.
                     * At this point, the only token types which need to be taken into consideration
                     * as potential type declarations are identifier names, T_ARRAY, T_CALLABLE and T_NS_SEPARATOR
                     * and the union/intersection/dnf parentheses.
                     */
                    $foundDNFParens = 1;
                    $foundDNFPipe = 0;
                    for (++$i; $i < $numTokens; $i++) {
                        if (is_array($tokens[$i]) === true) {
                            $tokenType = $tokens[$i][0];
                        }
                        else {
                            $tokenType = $tokens[$i];
                        }
                        if (isset(Tokens::$emptyTokens[$tokenType]) === true) {
                            continue;
                        }
                        if ($tokenType === '|') {
                            ++$foundDNFPipe;
                            continue;
                        }
                        if ($tokenType === ')') {
                            ++$foundDNFParens;
                            continue;
                        }
                        if ($tokenType === '(') {
                            ++$foundDNFParens;
                            continue;
                        }
                        if ($tokenType === T_STRING || $tokenType === T_NAME_FULLY_QUALIFIED || $tokenType === T_NAME_RELATIVE || $tokenType === T_NAME_QUALIFIED || $tokenType === T_ARRAY || $tokenType === T_NAMESPACE || $tokenType === T_NS_SEPARATOR || $tokenType === T_AMPERSAND_NOT_FOLLOWED_BY_VAR_OR_VARARG || $tokenType === '&') {
                            continue;
                        }
                        // Reached the next token after.
                        if ($foundDNFParens % 2 === 0 && $foundDNFPipe >= 1 && ($tokenType === T_VARIABLE || $tokenType === T_AMPERSAND_FOLLOWED_BY_VAR_OR_VARARG)) {
                            $isReadonlyKeyword = true;
                        }
                        break;
                    }
                    
                    //end for
                }
            }
            
            //end if
            if ($isReadonlyKeyword === true) {
                $finalTokens[$newStackPtr] = [
                    'code' => T_READONLY,
                    'type' => 'T_READONLY',
                    'content' => $token[1],
                ];
                $newStackPtr++;
                if (PHP_CODESNIFFER_VERBOSITY > 1 && $type !== T_READONLY) {
                    echo "\t\t* token {$stackPtr} changed from {$type} to T_READONLY" . PHP_EOL;
                }
            }
            else {
                $finalTokens[$newStackPtr] = [
                    'code' => T_STRING,
                    'type' => 'T_STRING',
                    'content' => $token[1],
                ];
                $newStackPtr++;
                if (PHP_CODESNIFFER_VERBOSITY > 1 && $type !== T_STRING) {
                    echo "\t\t* token {$stackPtr} changed from {$type} to T_STRING" . PHP_EOL;
                }
            }
            
            //end if
            continue;
        }
        
        //end if
        
        /*
            Before PHP 7.0, "yield from" was tokenized as
            T_YIELD, T_WHITESPACE and T_STRING. So look for
            and change this token in earlier versions.
        */
        if (PHP_VERSION_ID < 70000 && $tokenIsArray === true && $token[0] === T_YIELD && isset($tokens[$stackPtr + 1]) === true && isset($tokens[$stackPtr + 2]) === true && $tokens[$stackPtr + 1][0] === T_WHITESPACE && strpos($tokens[$stackPtr + 1][1], $this->eolChar) === false && $tokens[$stackPtr + 2][0] === T_STRING && strtolower($tokens[$stackPtr + 2][1]) === 'from') {
            // Single-line "yield from" with only whitespace between.
            $finalTokens[$newStackPtr] = [
                'code' => T_YIELD_FROM,
                'type' => 'T_YIELD_FROM',
                'content' => $token[1] . $tokens[$stackPtr + 1][1] . $tokens[$stackPtr + 2][1],
            ];
            if (PHP_CODESNIFFER_VERBOSITY > 1) {
                for ($i = $stackPtr + 1; $i <= $stackPtr + 2; $i++) {
                    $type = Tokens::tokenName($tokens[$i][0]);
                    $content = Common::prepareForOutput($tokens[$i][1]);
                    echo "\t\t* token {$i} merged into T_YIELD_FROM; was: {$type} => {$content}" . PHP_EOL;
                }
            }
            $newStackPtr++;
            $stackPtr += 2;
            continue;
        }
        else {
            if (PHP_VERSION_ID < 80300 && $tokenIsArray === true && $token[0] === T_STRING && strtolower($token[1]) === 'from' && $finalTokens[$lastNotEmptyToken]['code'] === T_YIELD) {
                
                /*
                    Before PHP 8.3, if there was a comment between the "yield" and "from" keywords,
                    it was tokenized as T_YIELD, T_WHITESPACE, T_COMMENT... and T_STRING.
                    We want to keep the tokenization of the tokens between, but need to change the
                    `T_YIELD` and `T_STRING` (from) keywords to `T_YIELD_FROM.
                */
                $finalTokens[$lastNotEmptyToken]['code'] = T_YIELD_FROM;
                $finalTokens[$lastNotEmptyToken]['type'] = 'T_YIELD_FROM';
                $finalTokens[$newStackPtr] = [
                    'code' => T_YIELD_FROM,
                    'type' => 'T_YIELD_FROM',
                    'content' => $token[1],
                ];
                $newStackPtr++;
                if (PHP_CODESNIFFER_VERBOSITY > 1) {
                    echo "\t\t* token {$lastNotEmptyToken} (new stack) changed into T_YIELD_FROM; was: T_YIELD" . PHP_EOL;
                    echo "\t\t* token {$stackPtr} changed into T_YIELD_FROM; was: T_STRING" . PHP_EOL;
                }
                continue;
            }
            else {
                if (PHP_VERSION_ID >= 70000 && $tokenIsArray === true && $token[0] === T_YIELD_FROM && strpos($token[1], $this->eolChar) !== false && preg_match('`^yield\\s+from$`i', $token[1]) === 1) {
                    
                    /*
                        In PHP 7.0+, a multi-line "yield from" (without comment) tokenizes as a single
                        T_YIELD_FROM token, but we want to split it and tokenize the whitespace
                        separately for consistency.
                    */
                    $finalTokens[$newStackPtr] = [
                        'code' => T_YIELD_FROM,
                        'type' => 'T_YIELD_FROM',
                        'content' => substr($token[1], 0, 5),
                    ];
                    $newStackPtr++;
                    $tokenLines = explode($this->eolChar, substr($token[1], 5, -4));
                    $numLines = count($tokenLines);
                    $newToken = [
                        'type' => 'T_WHITESPACE',
                        'code' => T_WHITESPACE,
                        'content' => '',
                    ];
                    foreach ($tokenLines as $i => $line) {
                        $newToken['content'] = $line;
                        if ($i === $numLines - 1) {
                            if ($line === '') {
                                break;
                            }
                        }
                        else {
                            $newToken['content'] .= $this->eolChar;
                        }
                        $finalTokens[$newStackPtr] = $newToken;
                        $newStackPtr++;
                    }
                    $finalTokens[$newStackPtr] = [
                        'code' => T_YIELD_FROM,
                        'type' => 'T_YIELD_FROM',
                        'content' => substr($token[1], -4),
                    ];
                    $newStackPtr++;
                    if (PHP_CODESNIFFER_VERBOSITY > 1) {
                        echo "\t\t* token {$stackPtr} split into 'yield', one or more whitespace tokens and 'from'" . PHP_EOL;
                    }
                    continue;
                }
                else {
                    if (PHP_VERSION_ID >= 80300 && $tokenIsArray === true && $token[0] === T_YIELD_FROM && preg_match('`^yield[ \\t]+from$`i', $token[1]) !== 1 && stripos($token[1], 'yield') === 0) {
                        
                        /*
                            Since PHP 8.3, "yield from" allows for comments and will
                            swallow the comment in the `T_YIELD_FROM` token.
                            We need to split this up to allow for sniffs handling comments.
                        */
                        $finalTokens[$newStackPtr] = [
                            'code' => T_YIELD_FROM,
                            'type' => 'T_YIELD_FROM',
                            'content' => substr($token[1], 0, 5),
                        ];
                        $newStackPtr++;
                        $yieldFromSubtokens = @token_get_all("<?php\n" . substr($token[1], 5, -4));
                        // Remove the PHP open tag token.
                        array_shift($yieldFromSubtokens);
                        // Add the "from" keyword.
                        $yieldFromSubtokens[] = [
                            0 => T_YIELD_FROM,
                            1 => substr($token[1], -4),
                        ];
                        // Inject the new tokens into the token stack.
                        array_splice($tokens, $stackPtr + 1, 0, $yieldFromSubtokens);
                        $numTokens = count($tokens);
                        if (PHP_CODESNIFFER_VERBOSITY > 1) {
                            echo "\t\t* token {$stackPtr} split into parts (yield from with comment)" . PHP_EOL;
                        }
                        unset($yieldFromSubtokens);
                        continue;
                    }
                }
            }
        }
        
        //end if
        
        /*
            Before PHP 5.6, the ... operator was tokenized as three
            T_STRING_CONCAT tokens in a row. So look for and combine
            these tokens in earlier versions.
        */
        if ($tokenIsArray === false && $token[0] === '.' && isset($tokens[$stackPtr + 1]) === true && isset($tokens[$stackPtr + 2]) === true && $tokens[$stackPtr + 1] === '.' && $tokens[$stackPtr + 2] === '.') {
            $newToken = [];
            $newToken['code'] = T_ELLIPSIS;
            $newToken['type'] = 'T_ELLIPSIS';
            $newToken['content'] = '...';
            $finalTokens[$newStackPtr] = $newToken;
            $newStackPtr++;
            $stackPtr += 2;
            continue;
        }
        
        /*
            Before PHP 5.6, the ** operator was tokenized as two
            T_MULTIPLY tokens in a row. So look for and combine
            these tokens in earlier versions.
        */
        if ($tokenIsArray === false && $token[0] === '*' && isset($tokens[$stackPtr + 1]) === true && $tokens[$stackPtr + 1] === '*') {
            $newToken = [];
            $newToken['code'] = T_POW;
            $newToken['type'] = 'T_POW';
            $newToken['content'] = '**';
            $finalTokens[$newStackPtr] = $newToken;
            $newStackPtr++;
            $stackPtr++;
            continue;
        }
        
        /*
            Before PHP 5.6, the **= operator was tokenized as
            T_MULTIPLY followed by T_MUL_EQUAL. So look for and combine
            these tokens in earlier versions.
        */
        if ($tokenIsArray === false && $token[0] === '*' && isset($tokens[$stackPtr + 1]) === true && is_array($tokens[$stackPtr + 1]) === true && $tokens[$stackPtr + 1][1] === '*=') {
            $newToken = [];
            $newToken['code'] = T_POW_EQUAL;
            $newToken['type'] = 'T_POW_EQUAL';
            $newToken['content'] = '**=';
            $finalTokens[$newStackPtr] = $newToken;
            $newStackPtr++;
            $stackPtr++;
            continue;
        }
        
        /*
            Before PHP 7, the ??= operator was tokenized as
            T_INLINE_THEN, T_INLINE_THEN, T_EQUAL.
            Between PHP 7.0 and 7.3, the ??= operator was tokenized as
            T_COALESCE, T_EQUAL.
            So look for and combine these tokens in earlier versions.
        */
        if ($tokenIsArray === false && $token[0] === '?' && isset($tokens[$stackPtr + 1]) === true && $tokens[$stackPtr + 1][0] === '?' && isset($tokens[$stackPtr + 2]) === true && $tokens[$stackPtr + 2][0] === '=' || $tokenIsArray === true && $token[0] === T_COALESCE && isset($tokens[$stackPtr + 1]) === true && $tokens[$stackPtr + 1][0] === '=') {
            $newToken = [];
            $newToken['code'] = T_COALESCE_EQUAL;
            $newToken['type'] = 'T_COALESCE_EQUAL';
            $newToken['content'] = '??=';
            $finalTokens[$newStackPtr] = $newToken;
            $newStackPtr++;
            $stackPtr++;
            if ($tokenIsArray === false) {
                // Pre PHP 7.
                $stackPtr++;
            }
            continue;
        }
        
        /*
            Before PHP 7, the ?? operator was tokenized as
            T_INLINE_THEN followed by T_INLINE_THEN.
            So look for and combine these tokens in earlier versions.
        */
        if ($tokenIsArray === false && $token[0] === '?' && isset($tokens[$stackPtr + 1]) === true && $tokens[$stackPtr + 1][0] === '?') {
            $newToken = [];
            $newToken['code'] = T_COALESCE;
            $newToken['type'] = 'T_COALESCE';
            $newToken['content'] = '??';
            $finalTokens[$newStackPtr] = $newToken;
            $newStackPtr++;
            $stackPtr++;
            continue;
        }
        
        /*
            Before PHP 8, the ?-> operator was tokenized as
            T_INLINE_THEN followed by T_OBJECT_OPERATOR.
            So look for and combine these tokens in earlier versions.
        */
        if ($tokenIsArray === false && $token[0] === '?' && isset($tokens[$stackPtr + 1]) === true && is_array($tokens[$stackPtr + 1]) === true && $tokens[$stackPtr + 1][0] === T_OBJECT_OPERATOR) {
            $newToken = [];
            $newToken['code'] = T_NULLSAFE_OBJECT_OPERATOR;
            $newToken['type'] = 'T_NULLSAFE_OBJECT_OPERATOR';
            $newToken['content'] = '?->';
            $finalTokens[$newStackPtr] = $newToken;
            $newStackPtr++;
            $stackPtr++;
            continue;
        }
        
        /*
            Before PHP 7.4, underscores inside T_LNUMBER and T_DNUMBER
            tokens split the token with a T_STRING. So look for
            and change these tokens in earlier versions.
        */
        if (PHP_VERSION_ID < 70400 && ($tokenIsArray === true && ($token[0] === T_LNUMBER || $token[0] === T_DNUMBER) && isset($tokens[$stackPtr + 1]) === true && is_array($tokens[$stackPtr + 1]) === true && $tokens[$stackPtr + 1][0] === T_STRING && $tokens[$stackPtr + 1][1][0] === '_')) {
            $newContent = $token[1];
            $newType = $token[0];
            for ($i = $stackPtr + 1; $i < $numTokens; $i++) {
                if (is_array($tokens[$i]) === false) {
                    break;
                }
                if ($tokens[$i][0] === T_LNUMBER || $tokens[$i][0] === T_DNUMBER) {
                    $newContent .= $tokens[$i][1];
                    continue;
                }
                if ($tokens[$i][0] === T_STRING && $tokens[$i][1][0] === '_' && (strpos($newContent, '0x') === 0 && preg_match('`^((?<!\\.)_[0-9A-F][0-9A-F\\.]*)+$`iD', $tokens[$i][1]) === 1 || strpos($newContent, '0x') !== 0 && substr($newContent, -1) !== '.' && substr(strtolower($newContent), -1) !== 'e' && preg_match('`^(?:(?<![\\.e])_[0-9][0-9e\\.]*)+$`iD', $tokens[$i][1]) === 1)) {
                    $newContent .= $tokens[$i][1];
                    // Support floats.
                    if (substr(strtolower($tokens[$i][1]), -1) === 'e' && ($tokens[$i + 1] === '-' || $tokens[$i + 1] === '+')) {
                        $newContent .= $tokens[$i + 1];
                        $i++;
                    }
                    continue;
                }
                
                //end if
                break;
            }
            
            //end for
            if ($newType === T_LNUMBER && (stripos($newContent, '0x') === 0 && hexdec(str_replace('_', '', $newContent)) > PHP_INT_MAX || stripos($newContent, '0b') === 0 && bindec(str_replace('_', '', $newContent)) > PHP_INT_MAX || stripos($newContent, '0o') === 0 && octdec(str_replace('_', '', $newContent)) > PHP_INT_MAX || stripos($newContent, '0x') !== 0 && (stripos($newContent, 'e') !== false || strpos($newContent, '.') !== false) || strpos($newContent, '0') === 0 && stripos($newContent, '0x') !== 0 && stripos($newContent, '0b') !== 0 && octdec(str_replace('_', '', $newContent)) > PHP_INT_MAX || strpos($newContent, '0') !== 0 && str_replace('_', '', $newContent) > PHP_INT_MAX)) {
                $newType = T_DNUMBER;
            }
            $newToken = [];
            $newToken['code'] = $newType;
            $newToken['type'] = Tokens::tokenName($newType);
            $newToken['content'] = $newContent;
            $finalTokens[$newStackPtr] = $newToken;
            $newStackPtr++;
            $stackPtr = $i - 1;
            continue;
        }
        
        //end if
        
        /*
            Backfill the T_MATCH token for PHP versions < 8.0 and
            do initial correction for non-match expression T_MATCH tokens
            to T_STRING for PHP >= 8.0.
            A final check for non-match expression T_MATCH tokens is done
            in PHP::processAdditional().
        */
        if ($tokenIsArray === true && ($token[0] === T_STRING && strtolower($token[1]) === 'match' || $token[0] === T_MATCH)) {
            $isMatch = false;
            for ($x = $stackPtr + 1; $x < $numTokens; $x++) {
                if (isset($tokens[$x][0], Tokens::$emptyTokens[$tokens[$x][0]]) === true) {
                    continue;
                }
                if ($tokens[$x] !== '(') {
                    // This is not a match expression.
                    break;
                }
                if (isset($this->tstringContexts[$finalTokens[$lastNotEmptyToken]['code']]) === true) {
                    // Also not a match expression.
                    break;
                }
                $isMatch = true;
                break;
            }
            
            //end for
            if ($isMatch === true && $token[0] === T_STRING) {
                $newToken = [];
                $newToken['code'] = T_MATCH;
                $newToken['type'] = 'T_MATCH';
                $newToken['content'] = $token[1];
                if (PHP_CODESNIFFER_VERBOSITY > 1) {
                    echo "\t\t* token {$stackPtr} changed from T_STRING to T_MATCH" . PHP_EOL;
                }
                $finalTokens[$newStackPtr] = $newToken;
                $newStackPtr++;
                continue;
            }
            else {
                if ($isMatch === false && $token[0] === T_MATCH) {
                    // PHP 8.0, match keyword, but not a match expression.
                    $newToken = [];
                    $newToken['code'] = T_STRING;
                    $newToken['type'] = 'T_STRING';
                    $newToken['content'] = $token[1];
                    if (PHP_CODESNIFFER_VERBOSITY > 1) {
                        echo "\t\t* token {$stackPtr} changed from T_MATCH to T_STRING" . PHP_EOL;
                    }
                    $finalTokens[$newStackPtr] = $newToken;
                    $newStackPtr++;
                    continue;
                }
            }
            
            //end if
        }
        
        //end if
        
        /*
            Retokenize the T_DEFAULT in match control structures as T_MATCH_DEFAULT
            to prevent scope being set and the scope for switch default statements
            breaking.
        */
        if ($tokenIsArray === true && $token[0] === T_DEFAULT && isset($this->tstringContexts[$finalTokens[$lastNotEmptyToken]['code']]) === false) {
            for ($x = $stackPtr + 1; $x < $numTokens; $x++) {
                if ($tokens[$x] === ',') {
                    // Skip over potential trailing comma (supported in PHP).
                    continue;
                }
                if (is_array($tokens[$x]) === false || isset(Tokens::$emptyTokens[$tokens[$x][0]]) === false) {
                    // Non-empty, non-comma content.
                    break;
                }
            }
            if (isset($tokens[$x]) === true && is_array($tokens[$x]) === true && $tokens[$x][0] === T_DOUBLE_ARROW) {
                // Modify the original token stack for the double arrow so that
                // future checks can disregard the double arrow token more easily.
                // For match expression "case" statements, this is handled
                // in PHP::processAdditional().
                $tokens[$x][0] = T_MATCH_ARROW;
                if (PHP_CODESNIFFER_VERBOSITY > 1) {
                    echo "\t\t* token {$x} changed from T_DOUBLE_ARROW to T_MATCH_ARROW" . PHP_EOL;
                }
                $newToken = [];
                $newToken['code'] = T_MATCH_DEFAULT;
                $newToken['type'] = 'T_MATCH_DEFAULT';
                $newToken['content'] = $token[1];
                if (PHP_CODESNIFFER_VERBOSITY > 1) {
                    echo "\t\t* token {$stackPtr} changed from T_DEFAULT to T_MATCH_DEFAULT" . PHP_EOL;
                }
                $finalTokens[$newStackPtr] = $newToken;
                $newStackPtr++;
                continue;
            }
            
            //end if
        }
        
        //end if
        
        /*
            Convert ? to T_NULLABLE OR T_INLINE_THEN
        */
        if ($tokenIsArray === false && $token[0] === '?') {
            $newToken = [];
            $newToken['content'] = '?';
            // For typed constants, we only need to check the token before the ? to be sure.
            if ($finalTokens[$lastNotEmptyToken]['code'] === T_CONST) {
                $newToken['code'] = T_NULLABLE;
                $newToken['type'] = 'T_NULLABLE';
                if (PHP_CODESNIFFER_VERBOSITY > 1) {
                    echo "\t\t* token {$stackPtr} changed from ? to T_NULLABLE" . PHP_EOL;
                }
                $finalTokens[$newStackPtr] = $newToken;
                $newStackPtr++;
                continue;
            }
            
            /*
             * Check if the next non-empty token is one of the tokens which can be used
             * in type declarations. If not, it's definitely a ternary.
             * At this point, the only token types which need to be taken into consideration
             * as potential type declarations are identifier names, T_ARRAY, T_CALLABLE and T_NS_SEPARATOR.
             */
            $lastRelevantNonEmpty = null;
            for ($i = $stackPtr + 1; $i < $numTokens; $i++) {
                if (is_array($tokens[$i]) === true) {
                    $tokenType = $tokens[$i][0];
                }
                else {
                    $tokenType = $tokens[$i];
                }
                if (isset(Tokens::$emptyTokens[$tokenType]) === true) {
                    continue;
                }
                if ($tokenType === T_STRING || $tokenType === T_NAME_FULLY_QUALIFIED || $tokenType === T_NAME_RELATIVE || $tokenType === T_NAME_QUALIFIED || $tokenType === T_ARRAY || $tokenType === T_NAMESPACE || $tokenType === T_NS_SEPARATOR) {
                    $lastRelevantNonEmpty = $tokenType;
                    continue;
                }
                if ($tokenType !== T_CALLABLE && isset($lastRelevantNonEmpty) === false || $lastRelevantNonEmpty === T_ARRAY && $tokenType === '(' || ($lastRelevantNonEmpty === T_STRING || $lastRelevantNonEmpty === T_NAME_FULLY_QUALIFIED || $lastRelevantNonEmpty === T_NAME_RELATIVE || $lastRelevantNonEmpty === T_NAME_QUALIFIED) && ($tokenType === T_DOUBLE_COLON || $tokenType === '(' || $tokenType === ':')) {
                    if (PHP_CODESNIFFER_VERBOSITY > 1) {
                        echo "\t\t* token {$stackPtr} changed from ? to T_INLINE_THEN" . PHP_EOL;
                    }
                    $newToken['code'] = T_INLINE_THEN;
                    $newToken['type'] = 'T_INLINE_THEN';
                    $insideInlineIf[] = $stackPtr;
                    $finalTokens[$newStackPtr] = $newToken;
                    $newStackPtr++;
                    continue 2;
                }
                break;
            }
            
            //end for
            
            /*
             * This can still be a nullable type or a ternary.
             * Do additional checking.
             */
            $prevNonEmpty = null;
            $lastSeenNonEmpty = null;
            for ($i = $stackPtr - 1; $i >= 0; $i--) {
                if (is_array($tokens[$i]) === true) {
                    $tokenType = $tokens[$i][0];
                }
                else {
                    $tokenType = $tokens[$i];
                }
                if ($tokenType === T_STATIC && ($lastSeenNonEmpty === T_DOUBLE_COLON || $lastSeenNonEmpty === '(')) {
                    $lastSeenNonEmpty = $tokenType;
                    continue;
                }
                if ($prevNonEmpty === null && isset(Tokens::$emptyTokens[$tokenType]) === false) {
                    // Found the previous non-empty token.
                    if ($tokenType === ':' || $tokenType === ',' || $tokenType === T_ATTRIBUTE_END) {
                        $newToken['code'] = T_NULLABLE;
                        $newToken['type'] = 'T_NULLABLE';
                        if (PHP_CODESNIFFER_VERBOSITY > 1) {
                            echo "\t\t* token {$stackPtr} changed from ? to T_NULLABLE" . PHP_EOL;
                        }
                        break;
                    }
                    $prevNonEmpty = $tokenType;
                }
                if ($tokenType === T_FUNCTION || $tokenType === T_FN || isset(Tokens::$methodPrefixes[$tokenType]) === true || $tokenType === T_VAR) {
                    if (PHP_CODESNIFFER_VERBOSITY > 1) {
                        echo "\t\t* token {$stackPtr} changed from ? to T_NULLABLE" . PHP_EOL;
                    }
                    $newToken['code'] = T_NULLABLE;
                    $newToken['type'] = 'T_NULLABLE';
                    break;
                }
                else {
                    if (in_array($tokenType, [
                        T_DOUBLE_ARROW,
                        T_OPEN_TAG,
                        T_OPEN_TAG_WITH_ECHO,
                        '=',
                        '{',
                        ';',
                    ], true) === true) {
                        if (PHP_CODESNIFFER_VERBOSITY > 1) {
                            echo "\t\t* token {$stackPtr} changed from ? to T_INLINE_THEN" . PHP_EOL;
                        }
                        $newToken['code'] = T_INLINE_THEN;
                        $newToken['type'] = 'T_INLINE_THEN';
                        $insideInlineIf[] = $stackPtr;
                        break;
                    }
                }
                if (isset(Tokens::$emptyTokens[$tokenType]) === false) {
                    $lastSeenNonEmpty = $tokenType;
                }
            }
            
            //end for
            $finalTokens[$newStackPtr] = $newToken;
            $newStackPtr++;
            continue;
        }
        
        //end if
        
        /*
            Tokens after a double colon may look like scope openers,
            such as when writing code like Foo::NAMESPACE, but they are
            only ever variables or strings.
        */
        if ($stackPtr > 1 && (is_array($tokens[$stackPtr - 1]) === true && $tokens[$stackPtr - 1][0] === T_PAAMAYIM_NEKUDOTAYIM) && $tokenIsArray === true && $token[0] !== T_STRING && $token[0] !== T_VARIABLE && $token[0] !== T_DOLLAR && isset(Tokens::$emptyTokens[$token[0]]) === false) {
            $newToken = [];
            $newToken['code'] = T_STRING;
            $newToken['type'] = 'T_STRING';
            $newToken['content'] = $token[1];
            $finalTokens[$newStackPtr] = $newToken;
            $newStackPtr++;
            continue;
        }
        
        /*
            Backfill the T_FN token for PHP versions < 7.4.
        */
        if ($tokenIsArray === true && $token[0] === T_STRING && strtolower($token[1]) === 'fn') {
            // Modify the original token stack so that
            // future checks (like looking for T_NULLABLE) can
            // detect the T_FN token more easily.
            $tokens[$stackPtr][0] = T_FN;
            $token[0] = T_FN;
            if (PHP_CODESNIFFER_VERBOSITY > 1) {
                echo "\t\t* token {$stackPtr} changed from T_STRING to T_FN" . PHP_EOL;
            }
        }
        
        /*
            This is a special condition for T_ARRAY tokens used for
            function return types. We want to keep the parenthesis map clean,
            so let's tag these tokens as T_STRING.
        */
        if ($tokenIsArray === true && ($token[0] === T_FUNCTION || $token[0] === T_FN) && $finalTokens[$lastNotEmptyToken]['code'] !== T_USE) {
            // Go looking for the colon to start the return type hint.
            // Start by finding the closing parenthesis of the function.
            $parenthesisStack = [];
            $parenthesisCloser = false;
            for ($x = $stackPtr + 1; $x < $numTokens; $x++) {
                if (is_array($tokens[$x]) === false && $tokens[$x] === '(') {
                    $parenthesisStack[] = $x;
                }
                else {
                    if (is_array($tokens[$x]) === false && $tokens[$x] === ')') {
                        array_pop($parenthesisStack);
                        if (empty($parenthesisStack) === true) {
                            $parenthesisCloser = $x;
                            break;
                        }
                    }
                }
            }
            if ($parenthesisCloser !== false) {
                for ($x = $parenthesisCloser + 1; $x < $numTokens; $x++) {
                    if (is_array($tokens[$x]) === false || isset(Tokens::$emptyTokens[$tokens[$x][0]]) === false) {
                        // Non-empty content.
                        if (is_array($tokens[$x]) === true && $tokens[$x][0] === T_USE) {
                            // Found a use statements, so search ahead for the closing parenthesis.
                            for ($x += 1; $x < $numTokens; $x++) {
                                if (is_array($tokens[$x]) === false && $tokens[$x] === ')') {
                                    continue 2;
                                }
                            }
                        }
                        break;
                    }
                }
                if (isset($tokens[$x]) === true && is_array($tokens[$x]) === false && $tokens[$x] === ':') {
                    // Find the start of the return type.
                    for ($x += 1; $x < $numTokens; $x++) {
                        if (is_array($tokens[$x]) === true && isset(Tokens::$emptyTokens[$tokens[$x][0]]) === true) {
                            // Whitespace or comments before the return type.
                            continue;
                        }
                        if (is_array($tokens[$x]) === false && $tokens[$x] === '?') {
                            // Found a nullable operator, so skip it.
                            // But also convert the token to save the tokenizer
                            // a bit of time later on.
                            $tokens[$x] = [
                                T_NULLABLE,
                                '?',
                            ];
                            if (PHP_CODESNIFFER_VERBOSITY > 1) {
                                echo "\t\t* token {$x} changed from ? to T_NULLABLE" . PHP_EOL;
                            }
                            continue;
                        }
                        break;
                    }
                    
                    //end for
                }
                
                //end if
            }
            
            //end if
        }
        
        //end if
        
        /*
            Before PHP 7, the <=> operator was tokenized as
            T_IS_SMALLER_OR_EQUAL followed by T_GREATER_THAN.
            So look for and combine these tokens in earlier versions.
        */
        if ($tokenIsArray === true && $token[0] === T_IS_SMALLER_OR_EQUAL && isset($tokens[$stackPtr + 1]) === true && $tokens[$stackPtr + 1][0] === '>') {
            $newToken = [];
            $newToken['code'] = T_SPACESHIP;
            $newToken['type'] = 'T_SPACESHIP';
            $newToken['content'] = '<=>';
            $finalTokens[$newStackPtr] = $newToken;
            $newStackPtr++;
            $stackPtr++;
            continue;
        }
        
        /*
            PHP doesn't assign a token to goto labels, so we have to.
            These are just string tokens with a single colon after them. Double
            colons are already tokenized and so don't interfere with this check.
            But we do have to account for CASE statements, that look just like
            goto labels.
        */
        if ($tokenIsArray === true && $token[0] === T_STRING && isset($tokens[$stackPtr + 1]) === true && $tokens[$stackPtr + 1] === ':' && (is_array($tokens[$stackPtr - 1]) === false || $tokens[$stackPtr - 1][0] !== T_PAAMAYIM_NEKUDOTAYIM)) {
            $stopTokens = [
                T_CASE => true,
                T_SEMICOLON => true,
                T_OPEN_TAG => true,
                T_OPEN_CURLY_BRACKET => true,
                T_INLINE_THEN => true,
                T_ENUM => true,
            ];
            for ($x = $newStackPtr - 1; $x > 0; $x--) {
                if (isset($stopTokens[$finalTokens[$x]['code']]) === true) {
                    break;
                }
            }
            if ($finalTokens[$x]['code'] !== T_CASE && $finalTokens[$x]['code'] !== T_INLINE_THEN && $finalTokens[$x]['code'] !== T_ENUM) {
                $finalTokens[$newStackPtr] = [
                    'content' => $token[1] . ':',
                    'code' => T_GOTO_LABEL,
                    'type' => 'T_GOTO_LABEL',
                ];
                if (PHP_CODESNIFFER_VERBOSITY > 1) {
                    echo "\t\t* token {$stackPtr} changed from T_STRING to T_GOTO_LABEL" . PHP_EOL;
                    echo "\t\t* skipping T_COLON token " . ($stackPtr + 1) . PHP_EOL;
                }
                $newStackPtr++;
                $stackPtr++;
                continue;
            }
        }
        
        //end if
        
        /*
            If this token has newlines in its content, split each line up
            and create a new token for each line. We do this so it's easier
            to ascertain where errors occur on a line.
            Note that $token[1] is the token's content.
        */
        if ($tokenIsArray === true && strpos($token[1], $this->eolChar) !== false) {
            $tokenLines = explode($this->eolChar, $token[1]);
            $numLines = count($tokenLines);
            $newToken = [
                'type' => Tokens::tokenName($token[0]),
                'code' => $token[0],
                'content' => '',
            ];
            for ($i = 0; $i < $numLines; $i++) {
                $newToken['content'] = $tokenLines[$i];
                if ($i === $numLines - 1) {
                    if ($tokenLines[$i] === '') {
                        break;
                    }
                }
                else {
                    $newToken['content'] .= $this->eolChar;
                }
                $finalTokens[$newStackPtr] = $newToken;
                $newStackPtr++;
            }
        }
        else {
            // Some T_STRING tokens should remain that way due to their context.
            if ($tokenIsArray === true && $token[0] === T_STRING) {
                $preserveTstring = false;
                // True/false/parent/self/static in typed constants should be fixed to their own token,
                // but the constant name should not be.
                if (isset($this->tstringContexts[$finalTokens[$lastNotEmptyToken]['code']]) === true && $finalTokens[$lastNotEmptyToken]['code'] === T_CONST || $insideConstDeclaration === true) {
                    // Find the next non-empty token.
                    for ($i = $stackPtr + 1; $i < $numTokens; $i++) {
                        if (is_array($tokens[$i]) === true && isset(Tokens::$emptyTokens[$tokens[$i][0]]) === true) {
                            continue;
                        }
                        break;
                    }
                    if ($tokens[$i] === '=') {
                        $preserveTstring = true;
                        $insideConstDeclaration = false;
                    }
                }
                else {
                    if (isset($this->tstringContexts[$finalTokens[$lastNotEmptyToken]['code']]) === true && $finalTokens[$lastNotEmptyToken]['code'] !== T_CONST) {
                        $preserveTstring = true;
                        // Special case for syntax like: return new self/new parent
                        // where self/parent should not be a string.
                        $tokenContentLower = strtolower($token[1]);
                        if ($finalTokens[$lastNotEmptyToken]['code'] === T_NEW && ($tokenContentLower === 'self' || $tokenContentLower === 'parent')) {
                            $preserveTstring = false;
                        }
                    }
                    else {
                        if ($finalTokens[$lastNotEmptyToken]['content'] === '&') {
                            // Function names for functions declared to return by reference.
                            for ($i = $lastNotEmptyToken - 1; $i >= 0; $i--) {
                                if (isset(Tokens::$emptyTokens[$finalTokens[$i]['code']]) === true) {
                                    continue;
                                }
                                if ($finalTokens[$i]['code'] === T_FUNCTION) {
                                    $preserveTstring = true;
                                }
                                break;
                            }
                        }
                        else {
                            // Keywords with special PHPCS token when used as a function call.
                            for ($i = $stackPtr + 1; $i < $numTokens; $i++) {
                                if (is_array($tokens[$i]) === true && isset(Tokens::$emptyTokens[$tokens[$i][0]]) === true) {
                                    continue;
                                }
                                if ($tokens[$i][0] === '(') {
                                    $preserveTstring = true;
                                }
                                break;
                            }
                        }
                    }
                }
                
                //end if
                if ($preserveTstring === true) {
                    $finalTokens[$newStackPtr] = [
                        'code' => T_STRING,
                        'type' => 'T_STRING',
                        'content' => $token[1],
                    ];
                    $newStackPtr++;
                    continue;
                }
            }
            
            //end if
            $newToken = null;
            if ($tokenIsArray === false) {
                if (isset(self::$resolveTokenCache[$token[0]]) === true) {
                    $newToken = self::$resolveTokenCache[$token[0]];
                }
            }
            else {
                $cacheKey = null;
                if ($token[0] === T_STRING) {
                    $cacheKey = strtolower($token[1]);
                }
                else {
                    if ($token[0] !== T_CURLY_OPEN) {
                        $cacheKey = $token[0];
                    }
                }
                if ($cacheKey !== null && isset(self::$resolveTokenCache[$cacheKey]) === true) {
                    $newToken = self::$resolveTokenCache[$cacheKey];
                    $newToken['content'] = $token[1];
                }
            }
            if ($newToken === null) {
                $newToken = self::standardiseToken($token);
            }
            // Convert colons that are actually the ELSE component of an
            // inline IF statement.
            if (empty($insideInlineIf) === false && $newToken['code'] === T_COLON) {
                $isInlineIf = true;
                // Make sure this isn't a named parameter label.
                // Get the previous non-empty token.
                for ($i = $stackPtr - 1; $i > 0; $i--) {
                    if (is_array($tokens[$i]) === false || isset(Tokens::$emptyTokens[$tokens[$i][0]]) === false) {
                        break;
                    }
                }
                if ($tokens[$i][0] === T_PARAM_NAME) {
                    $isInlineIf = false;
                    if (PHP_CODESNIFFER_VERBOSITY > 1) {
                        echo "\t\t* token is parameter label, not T_INLINE_ELSE" . PHP_EOL;
                    }
                }
                if ($isInlineIf === true) {
                    // Make sure this isn't a return type separator.
                    for ($i = $stackPtr - 1; $i > 0; $i--) {
                        if (is_array($tokens[$i]) === false || $tokens[$i][0] !== T_DOC_COMMENT && $tokens[$i][0] !== T_COMMENT && $tokens[$i][0] !== T_WHITESPACE) {
                            break;
                        }
                    }
                    if ($tokens[$i] === ')') {
                        $parenCount = 1;
                        for ($i--; $i > 0; $i--) {
                            if ($tokens[$i] === '(') {
                                $parenCount--;
                                if ($parenCount === 0) {
                                    break;
                                }
                            }
                            else {
                                if ($tokens[$i] === ')') {
                                    $parenCount++;
                                }
                            }
                        }
                        // We've found the open parenthesis, so if the previous
                        // non-empty token is FUNCTION or USE, this is a return type.
                        // Note that we need to skip T_STRING tokens here as these
                        // can be function names.
                        for ($i--; $i > 0; $i--) {
                            if (is_array($tokens[$i]) === false || $tokens[$i][0] !== T_DOC_COMMENT && $tokens[$i][0] !== T_COMMENT && $tokens[$i][0] !== T_WHITESPACE && $tokens[$i][0] !== T_STRING) {
                                break;
                            }
                        }
                        if ($tokens[$i][0] === T_FUNCTION || $tokens[$i][0] === T_FN || $tokens[$i][0] === T_USE) {
                            $isInlineIf = false;
                            if (PHP_CODESNIFFER_VERBOSITY > 1) {
                                echo "\t\t* token is return type, not T_INLINE_ELSE" . PHP_EOL;
                            }
                        }
                    }
                    
                    //end if
                }
                
                //end if
                // Check to see if this is a CASE or DEFAULT opener.
                if ($isInlineIf === true) {
                    $inlineIfToken = $insideInlineIf[count($insideInlineIf) - 1];
                    for ($i = $stackPtr; $i > $inlineIfToken; $i--) {
                        if (is_array($tokens[$i]) === true && ($tokens[$i][0] === T_CASE || $tokens[$i][0] === T_DEFAULT)) {
                            $isInlineIf = false;
                            if (PHP_CODESNIFFER_VERBOSITY > 1) {
                                echo "\t\t* token is T_CASE or T_DEFAULT opener, not T_INLINE_ELSE" . PHP_EOL;
                            }
                            break;
                        }
                        if (is_array($tokens[$i]) === false && ($tokens[$i] === ';' || $tokens[$i] === '{' || $tokens[$i] === '}')) {
                            break;
                        }
                    }
                    
                    //end for
                }
                
                //end if
                if ($isInlineIf === true) {
                    array_pop($insideInlineIf);
                    $newToken['code'] = T_INLINE_ELSE;
                    $newToken['type'] = 'T_INLINE_ELSE';
                    if (PHP_CODESNIFFER_VERBOSITY > 1) {
                        echo "\t\t* token changed from T_COLON to T_INLINE_ELSE" . PHP_EOL;
                    }
                }
            }
            
            //end if
            // This is a special condition for T_ARRAY tokens used for anything else
            // but array declarations, like type hinting function arguments as
            // being arrays.
            // We want to keep the parenthesis map clean, so let's tag these tokens as
            // T_STRING.
            if ($newToken['code'] === T_ARRAY) {
                for ($i = $stackPtr + 1; $i < $numTokens; $i++) {
                    if (is_array($tokens[$i]) === false || isset(Tokens::$emptyTokens[$tokens[$i][0]]) === false) {
                        // Non-empty content.
                        break;
                    }
                }
                if ($i !== $numTokens && $tokens[$i] !== '(') {
                    $newToken['code'] = T_STRING;
                    $newToken['type'] = 'T_STRING';
                }
            }
            // This is a special case when checking PHP 5.5+ code in PHP < 5.5
            // where "finally" should be T_FINALLY instead of T_STRING.
            if ($newToken['code'] === T_STRING && strtolower($newToken['content']) === 'finally' && $finalTokens[$lastNotEmptyToken]['code'] === T_CLOSE_CURLY_BRACKET) {
                $newToken['code'] = T_FINALLY;
                $newToken['type'] = 'T_FINALLY';
            }
            // This is a special case for PHP 5.6 use function and use const
            // where "function" and "const" should be T_STRING instead of T_FUNCTION
            // and T_CONST.
            if (($newToken['code'] === T_FUNCTION || $newToken['code'] === T_CONST) && ($finalTokens[$lastNotEmptyToken]['code'] === T_USE || $insideUseGroup === true)) {
                $newToken['code'] = T_STRING;
                $newToken['type'] = 'T_STRING';
            }
            // This is a special case for use groups in PHP 7+ where leaving
            // the curly braces as their normal tokens would confuse
            // the scope map and sniffs.
            if ($newToken['code'] === T_OPEN_CURLY_BRACKET && $finalTokens[$lastNotEmptyToken]['code'] === T_NS_SEPARATOR) {
                $newToken['code'] = T_OPEN_USE_GROUP;
                $newToken['type'] = 'T_OPEN_USE_GROUP';
                $insideUseGroup = true;
            }
            if ($insideUseGroup === true && $newToken['code'] === T_CLOSE_CURLY_BRACKET) {
                $newToken['code'] = T_CLOSE_USE_GROUP;
                $newToken['type'] = 'T_CLOSE_USE_GROUP';
                $insideUseGroup = false;
            }
            $finalTokens[$newStackPtr] = $newToken;
            $newStackPtr++;
        }
        
        //end if
    }
    
    //end for
    if (PHP_CODESNIFFER_VERBOSITY > 1) {
        echo "\t*** END PHP TOKENIZING ***" . PHP_EOL;
    }
    return $finalTokens;
}

API Navigation

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