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

Breadcrumb

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

function PHP::processAdditional

Performs additional processing after main tokenizing.

This additional processing checks for CASE statements that are using curly braces for scope openers and closers. It also turns some T_FUNCTION tokens into T_CLOSURE when they are not standard function definitions. It also detects short array syntax and converts those square brackets into new tokens. It also corrects some usage of the static and class keywords. It also assigns tokens to function return types.

Return value

void

Overrides Tokenizer::processAdditional

1 method overrides PHP::processAdditional()
CSS::processAdditional in vendor/squizlabs/php_codesniffer/src/Tokenizers/CSS.php
Performs additional processing after main tokenizing.

File

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

Class

PHP

Namespace

PHP_CodeSniffer\Tokenizers

Code

protected function processAdditional() {
    if (PHP_CODESNIFFER_VERBOSITY > 1) {
        echo "\t*** START ADDITIONAL PHP PROCESSING ***" . PHP_EOL;
    }
    $this->createAttributesNestingMap();
    $numTokens = count($this->tokens);
    $lastSeenTypeToken = $numTokens;
    for ($i = $numTokens - 1; $i >= 0; $i--) {
        // Check for any unset scope conditions due to alternate IF/ENDIF syntax.
        if (isset($this->tokens[$i]['scope_opener']) === true && isset($this->tokens[$i]['scope_condition']) === false) {
            $this->tokens[$i]['scope_condition'] = $this->tokens[$this->tokens[$i]['scope_opener']]['scope_condition'];
        }
        if ($this->tokens[$i]['code'] === T_FUNCTION) {
            
            /*
                Detect functions that are actually closures and
                assign them a different token.
            */
            if (isset($this->tokens[$i]['scope_opener']) === true) {
                for ($x = $i + 1; $x < $numTokens; $x++) {
                    if (isset(Tokens::$emptyTokens[$this->tokens[$x]['code']]) === false && $this->tokens[$x]['code'] !== T_BITWISE_AND) {
                        break;
                    }
                }
                if ($this->tokens[$x]['code'] === T_OPEN_PARENTHESIS) {
                    $this->tokens[$i]['code'] = T_CLOSURE;
                    $this->tokens[$i]['type'] = 'T_CLOSURE';
                    if (PHP_CODESNIFFER_VERBOSITY > 1) {
                        $line = $this->tokens[$i]['line'];
                        echo "\t* token {$i} on line {$line} changed from T_FUNCTION to T_CLOSURE" . PHP_EOL;
                    }
                    for ($x = $this->tokens[$i]['scope_opener'] + 1; $x < $this->tokens[$i]['scope_closer']; $x++) {
                        if (isset($this->tokens[$x]['conditions'][$i]) === false) {
                            continue;
                        }
                        $this->tokens[$x]['conditions'][$i] = T_CLOSURE;
                        if (PHP_CODESNIFFER_VERBOSITY > 1) {
                            $type = $this->tokens[$x]['type'];
                            echo "\t\t* cleaned {$x} ({$type}) *" . PHP_EOL;
                        }
                    }
                }
            }
            
            //end if
            continue;
        }
        else {
            if ($this->tokens[$i]['code'] === T_CLASS && isset($this->tokens[$i]['scope_opener']) === true) {
                
                /*
                    Detect anonymous classes and assign them a different token.
                */
                for ($x = $i + 1; $x < $numTokens; $x++) {
                    if (isset(Tokens::$emptyTokens[$this->tokens[$x]['code']]) === false) {
                        break;
                    }
                }
                if ($this->tokens[$x]['code'] === T_OPEN_PARENTHESIS || $this->tokens[$x]['code'] === T_OPEN_CURLY_BRACKET || $this->tokens[$x]['code'] === T_EXTENDS || $this->tokens[$x]['code'] === T_IMPLEMENTS) {
                    $this->tokens[$i]['code'] = T_ANON_CLASS;
                    $this->tokens[$i]['type'] = 'T_ANON_CLASS';
                    if (PHP_CODESNIFFER_VERBOSITY > 1) {
                        $line = $this->tokens[$i]['line'];
                        echo "\t* token {$i} on line {$line} changed from T_CLASS to T_ANON_CLASS" . PHP_EOL;
                    }
                    if ($this->tokens[$x]['code'] === T_OPEN_PARENTHESIS && isset($this->tokens[$x]['parenthesis_closer']) === true) {
                        $closer = $this->tokens[$x]['parenthesis_closer'];
                        $this->tokens[$i]['parenthesis_opener'] = $x;
                        $this->tokens[$i]['parenthesis_closer'] = $closer;
                        $this->tokens[$i]['parenthesis_owner'] = $i;
                        $this->tokens[$x]['parenthesis_owner'] = $i;
                        $this->tokens[$closer]['parenthesis_owner'] = $i;
                        if (PHP_CODESNIFFER_VERBOSITY > 1) {
                            $line = $this->tokens[$i]['line'];
                            echo "\t\t* added parenthesis keys to T_ANON_CLASS token {$i} on line {$line}" . PHP_EOL;
                        }
                    }
                    for ($x = $this->tokens[$i]['scope_opener'] + 1; $x < $this->tokens[$i]['scope_closer']; $x++) {
                        if (isset($this->tokens[$x]['conditions'][$i]) === false) {
                            continue;
                        }
                        $this->tokens[$x]['conditions'][$i] = T_ANON_CLASS;
                        if (PHP_CODESNIFFER_VERBOSITY > 1) {
                            $type = $this->tokens[$x]['type'];
                            echo "\t\t* cleaned {$x} ({$type}) *" . PHP_EOL;
                        }
                    }
                }
                
                //end if
                continue;
            }
            else {
                if ($this->tokens[$i]['code'] === T_FN && isset($this->tokens[$i + 1]) === true) {
                    // Possible arrow function.
                    for ($x = $i + 1; $x < $numTokens; $x++) {
                        if (isset(Tokens::$emptyTokens[$this->tokens[$x]['code']]) === false && $this->tokens[$x]['code'] !== T_BITWISE_AND) {
                            // Non-whitespace content.
                            break;
                        }
                    }
                    if (isset($this->tokens[$x]) === true && $this->tokens[$x]['code'] === T_OPEN_PARENTHESIS) {
                        $ignore = Tokens::$emptyTokens;
                        $ignore += [
                            T_ARRAY => T_ARRAY,
                            T_CALLABLE => T_CALLABLE,
                            T_COLON => T_COLON,
                            T_NAMESPACE => T_NAMESPACE,
                            T_NS_SEPARATOR => T_NS_SEPARATOR,
                            T_NULL => T_NULL,
                            T_TRUE => T_TRUE,
                            T_FALSE => T_FALSE,
                            T_NULLABLE => T_NULLABLE,
                            T_PARENT => T_PARENT,
                            T_SELF => T_SELF,
                            T_STATIC => T_STATIC,
                            T_STRING => T_STRING,
                            T_TYPE_UNION => T_TYPE_UNION,
                            T_TYPE_INTERSECTION => T_TYPE_INTERSECTION,
                            T_TYPE_OPEN_PARENTHESIS => T_TYPE_OPEN_PARENTHESIS,
                            T_TYPE_CLOSE_PARENTHESIS => T_TYPE_CLOSE_PARENTHESIS,
                        ];
                        $closer = $this->tokens[$x]['parenthesis_closer'];
                        for ($arrow = $closer + 1; $arrow < $numTokens; $arrow++) {
                            if (isset($ignore[$this->tokens[$arrow]['code']]) === false) {
                                break;
                            }
                        }
                        if ($this->tokens[$arrow]['code'] === T_DOUBLE_ARROW) {
                            $endTokens = [
                                T_COLON => true,
                                T_COMMA => 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,
                            ];
                            $inTernary = false;
                            $lastEndToken = null;
                            for ($scopeCloser = $arrow + 1; $scopeCloser < $numTokens; $scopeCloser++) {
                                // Arrow function closer should never be shared with the closer of a match
                                // control structure.
                                if (isset($this->tokens[$scopeCloser]['scope_closer'], $this->tokens[$scopeCloser]['scope_condition']) === true && $scopeCloser === $this->tokens[$scopeCloser]['scope_closer'] && $this->tokens[$this->tokens[$scopeCloser]['scope_condition']]['code'] === T_MATCH) {
                                    if ($arrow < $this->tokens[$scopeCloser]['scope_condition']) {
                                        // Match in return value of arrow function. Move on to the next token.
                                        continue;
                                    }
                                    // Arrow function as return value for the last match case without trailing comma.
                                    if ($lastEndToken !== null) {
                                        $scopeCloser = $lastEndToken;
                                        break;
                                    }
                                    for ($lastNonEmpty = $scopeCloser - 1; $lastNonEmpty > $arrow; $lastNonEmpty--) {
                                        if (isset(Tokens::$emptyTokens[$this->tokens[$lastNonEmpty]['code']]) === false) {
                                            $scopeCloser = $lastNonEmpty;
                                            break 2;
                                        }
                                    }
                                }
                                if (isset($endTokens[$this->tokens[$scopeCloser]['code']]) === true) {
                                    if ($lastEndToken !== null && (isset($this->tokens[$scopeCloser]['parenthesis_opener']) === true && $this->tokens[$scopeCloser]['parenthesis_opener'] < $arrow || isset($this->tokens[$scopeCloser]['bracket_opener']) === true && $this->tokens[$scopeCloser]['bracket_opener'] < $arrow)) {
                                        for ($lastNonEmpty = $scopeCloser - 1; $lastNonEmpty > $arrow; $lastNonEmpty--) {
                                            if (isset(Tokens::$emptyTokens[$this->tokens[$lastNonEmpty]['code']]) === false) {
                                                $scopeCloser = $lastNonEmpty;
                                                break;
                                            }
                                        }
                                    }
                                    break;
                                }
                                if ($inTernary === false && isset($this->tokens[$scopeCloser]['scope_closer'], $this->tokens[$scopeCloser]['scope_condition']) === true && $scopeCloser === $this->tokens[$scopeCloser]['scope_closer'] && $this->tokens[$this->tokens[$scopeCloser]['scope_condition']]['code'] === T_FN) {
                                    // Found a nested arrow function that already has the closer set and is in
                                    // the same scope as us, so we can use its closer.
                                    break;
                                }
                                if (isset($this->tokens[$scopeCloser]['scope_closer']) === true && $this->tokens[$scopeCloser]['code'] !== T_INLINE_ELSE && $this->tokens[$scopeCloser]['code'] !== T_END_HEREDOC && $this->tokens[$scopeCloser]['code'] !== T_END_NOWDOC) {
                                    // We minus 1 here in case the closer can be shared with us.
                                    $scopeCloser = $this->tokens[$scopeCloser]['scope_closer'] - 1;
                                    continue;
                                }
                                if (isset($this->tokens[$scopeCloser]['parenthesis_closer']) === true) {
                                    $scopeCloser = $this->tokens[$scopeCloser]['parenthesis_closer'];
                                    $lastEndToken = $scopeCloser;
                                    continue;
                                }
                                if (isset($this->tokens[$scopeCloser]['bracket_closer']) === true) {
                                    $scopeCloser = $this->tokens[$scopeCloser]['bracket_closer'];
                                    $lastEndToken = $scopeCloser;
                                    continue;
                                }
                                if ($this->tokens[$scopeCloser]['code'] === T_INLINE_THEN) {
                                    $inTernary = true;
                                    continue;
                                }
                                if ($this->tokens[$scopeCloser]['code'] === T_INLINE_ELSE) {
                                    if ($inTernary === false) {
                                        break;
                                    }
                                    $inTernary = false;
                                    continue;
                                }
                            }
                            
                            //end for
                            if ($scopeCloser !== $numTokens) {
                                if (PHP_CODESNIFFER_VERBOSITY > 1) {
                                    $line = $this->tokens[$i]['line'];
                                    echo "\t=> token {$i} on line {$line} processed as arrow function" . PHP_EOL;
                                    echo "\t\t* scope opener set to {$arrow} *" . PHP_EOL;
                                    echo "\t\t* scope closer set to {$scopeCloser} *" . PHP_EOL;
                                    echo "\t\t* parenthesis opener set to {$x} *" . PHP_EOL;
                                    echo "\t\t* parenthesis closer set to {$closer} *" . PHP_EOL;
                                }
                                $this->tokens[$i]['code'] = T_FN;
                                $this->tokens[$i]['type'] = 'T_FN';
                                $this->tokens[$i]['scope_condition'] = $i;
                                $this->tokens[$i]['scope_opener'] = $arrow;
                                $this->tokens[$i]['scope_closer'] = $scopeCloser;
                                $this->tokens[$i]['parenthesis_owner'] = $i;
                                $this->tokens[$i]['parenthesis_opener'] = $x;
                                $this->tokens[$i]['parenthesis_closer'] = $closer;
                                $this->tokens[$arrow]['code'] = T_FN_ARROW;
                                $this->tokens[$arrow]['type'] = 'T_FN_ARROW';
                                $this->tokens[$arrow]['scope_condition'] = $i;
                                $this->tokens[$arrow]['scope_opener'] = $arrow;
                                $this->tokens[$arrow]['scope_closer'] = $scopeCloser;
                                $this->tokens[$scopeCloser]['scope_condition'] = $i;
                                $this->tokens[$scopeCloser]['scope_opener'] = $arrow;
                                $this->tokens[$scopeCloser]['scope_closer'] = $scopeCloser;
                                $opener = $this->tokens[$i]['parenthesis_opener'];
                                $closer = $this->tokens[$i]['parenthesis_closer'];
                                $this->tokens[$opener]['parenthesis_owner'] = $i;
                                $this->tokens[$closer]['parenthesis_owner'] = $i;
                                if (PHP_CODESNIFFER_VERBOSITY > 1) {
                                    $line = $this->tokens[$arrow]['line'];
                                    echo "\t\t* token {$arrow} on line {$line} changed from T_DOUBLE_ARROW to T_FN_ARROW" . PHP_EOL;
                                }
                            }
                            
                            //end if
                        }
                        
                        //end if
                    }
                    
                    //end if
                    // If after all that, the extra tokens are not set, this is not an arrow function.
                    if (isset($this->tokens[$i]['scope_closer']) === false) {
                        if (PHP_CODESNIFFER_VERBOSITY > 1) {
                            $line = $this->tokens[$i]['line'];
                            echo "\t=> token {$i} on line {$line} is not an arrow function" . PHP_EOL;
                            echo "\t\t* token changed from T_FN to T_STRING" . PHP_EOL;
                        }
                        $this->tokens[$i]['code'] = T_STRING;
                        $this->tokens[$i]['type'] = 'T_STRING';
                    }
                }
                else {
                    if ($this->tokens[$i]['code'] === T_OPEN_SQUARE_BRACKET) {
                        if (isset($this->tokens[$i]['bracket_closer']) === false) {
                            continue;
                        }
                        // Unless there is a variable or a bracket before this token,
                        // it is the start of an array being defined using the short syntax.
                        $isShortArray = false;
                        $allowed = [
                            T_CLOSE_SQUARE_BRACKET => T_CLOSE_SQUARE_BRACKET,
                            T_CLOSE_CURLY_BRACKET => T_CLOSE_CURLY_BRACKET,
                            T_CLOSE_PARENTHESIS => T_CLOSE_PARENTHESIS,
                            T_VARIABLE => T_VARIABLE,
                            T_OBJECT_OPERATOR => T_OBJECT_OPERATOR,
                            T_NULLSAFE_OBJECT_OPERATOR => T_NULLSAFE_OBJECT_OPERATOR,
                            T_STRING => T_STRING,
                            T_CONSTANT_ENCAPSED_STRING => T_CONSTANT_ENCAPSED_STRING,
                            T_DOUBLE_QUOTED_STRING => T_DOUBLE_QUOTED_STRING,
                        ];
                        $allowed += Tokens::$magicConstants;
                        for ($x = $i - 1; $x >= 0; $x--) {
                            // If we hit a scope opener, the statement has ended
                            // without finding anything, so it's probably an array
                            // using PHP 7.1 short list syntax.
                            if (isset($this->tokens[$x]['scope_opener']) === true) {
                                $isShortArray = true;
                                break;
                            }
                            if (isset(Tokens::$emptyTokens[$this->tokens[$x]['code']]) === false) {
                                // Allow for control structures without braces.
                                if ($this->tokens[$x]['code'] === T_CLOSE_PARENTHESIS && isset($this->tokens[$x]['parenthesis_owner']) === true && isset(Tokens::$scopeOpeners[$this->tokens[$this->tokens[$x]['parenthesis_owner']]['code']]) === true || isset($allowed[$this->tokens[$x]['code']]) === false) {
                                    $isShortArray = true;
                                }
                                break;
                            }
                        }
                        
                        //end for
                        if ($isShortArray === true) {
                            $this->tokens[$i]['code'] = T_OPEN_SHORT_ARRAY;
                            $this->tokens[$i]['type'] = 'T_OPEN_SHORT_ARRAY';
                            $closer = $this->tokens[$i]['bracket_closer'];
                            $this->tokens[$closer]['code'] = T_CLOSE_SHORT_ARRAY;
                            $this->tokens[$closer]['type'] = 'T_CLOSE_SHORT_ARRAY';
                            if (PHP_CODESNIFFER_VERBOSITY > 1) {
                                $line = $this->tokens[$i]['line'];
                                echo "\t* token {$i} on line {$line} changed from T_OPEN_SQUARE_BRACKET to T_OPEN_SHORT_ARRAY" . PHP_EOL;
                                $line = $this->tokens[$closer]['line'];
                                echo "\t* token {$closer} on line {$line} changed from T_CLOSE_SQUARE_BRACKET to T_CLOSE_SHORT_ARRAY" . PHP_EOL;
                            }
                        }
                        continue;
                    }
                    else {
                        if ($this->tokens[$i]['code'] === T_MATCH) {
                            if (isset($this->tokens[$i]['scope_opener'], $this->tokens[$i]['scope_closer']) === false) {
                                // Not a match expression after all.
                                $this->tokens[$i]['code'] = T_STRING;
                                $this->tokens[$i]['type'] = 'T_STRING';
                                if (PHP_CODESNIFFER_VERBOSITY > 1) {
                                    echo "\t\t* token {$i} changed from T_MATCH to T_STRING" . PHP_EOL;
                                }
                                if (isset($this->tokens[$i]['parenthesis_opener'], $this->tokens[$i]['parenthesis_closer']) === true) {
                                    $opener = $this->tokens[$i]['parenthesis_opener'];
                                    $closer = $this->tokens[$i]['parenthesis_closer'];
                                    unset($this->tokens[$opener]['parenthesis_owner'], $this->tokens[$closer]['parenthesis_owner']);
                                    unset($this->tokens[$i]['parenthesis_opener'], $this->tokens[$i]['parenthesis_closer'], $this->tokens[$i]['parenthesis_owner']);
                                    if (PHP_CODESNIFFER_VERBOSITY > 1) {
                                        echo "\t\t* cleaned parenthesis of token {$i} *" . PHP_EOL;
                                    }
                                }
                            }
                            else {
                                // Retokenize the double arrows for match expression cases to `T_MATCH_ARROW`.
                                $searchFor = [
                                    T_OPEN_CURLY_BRACKET => T_OPEN_CURLY_BRACKET,
                                    T_OPEN_SQUARE_BRACKET => T_OPEN_SQUARE_BRACKET,
                                    T_OPEN_PARENTHESIS => T_OPEN_PARENTHESIS,
                                    T_OPEN_SHORT_ARRAY => T_OPEN_SHORT_ARRAY,
                                    T_DOUBLE_ARROW => T_DOUBLE_ARROW,
                                ];
                                $searchFor += Tokens::$scopeOpeners;
                                for ($x = $this->tokens[$i]['scope_opener'] + 1; $x < $this->tokens[$i]['scope_closer']; $x++) {
                                    if (isset($searchFor[$this->tokens[$x]['code']]) === false) {
                                        continue;
                                    }
                                    if (isset($this->tokens[$x]['scope_closer']) === true) {
                                        $x = $this->tokens[$x]['scope_closer'];
                                        continue;
                                    }
                                    if (isset($this->tokens[$x]['parenthesis_closer']) === true) {
                                        $x = $this->tokens[$x]['parenthesis_closer'];
                                        continue;
                                    }
                                    if (isset($this->tokens[$x]['bracket_closer']) === true) {
                                        $x = $this->tokens[$x]['bracket_closer'];
                                        continue;
                                    }
                                    // This must be a double arrow, but make sure anyhow.
                                    if ($this->tokens[$x]['code'] === T_DOUBLE_ARROW) {
                                        $this->tokens[$x]['code'] = T_MATCH_ARROW;
                                        $this->tokens[$x]['type'] = 'T_MATCH_ARROW';
                                        if (PHP_CODESNIFFER_VERBOSITY > 1) {
                                            echo "\t\t* token {$x} changed from T_DOUBLE_ARROW to T_MATCH_ARROW" . PHP_EOL;
                                        }
                                    }
                                }
                                
                                //end for
                            }
                            
                            //end if
                            continue;
                        }
                        else {
                            if ($this->tokens[$i]['code'] === T_BITWISE_OR || $this->tokens[$i]['code'] === T_BITWISE_AND || $this->tokens[$i]['code'] === T_CLOSE_PARENTHESIS) {
                                if ($lastSeenTypeToken < $i) {
                                    // We've already examined this code to check if it is a type declaration and concluded it wasn't.
                                    // No need to do it again.
                                    continue;
                                }
                                
                                /*
                                    Convert "|" to T_TYPE_UNION or leave as T_BITWISE_OR.
                                    Convert "&" to T_TYPE_INTERSECTION or leave as T_BITWISE_AND.
                                    Convert "(" and ")" to T_TYPE_(OPEN|CLOSE)_PARENTHESIS or leave as T_(OPEN|CLOSE)_PARENTHESIS.
                                
                                    All type related tokens will be converted in one go as soon as this section is hit.
                                */
                                $allowed = [
                                    T_STRING => T_STRING,
                                    T_CALLABLE => T_CALLABLE,
                                    T_SELF => T_SELF,
                                    T_PARENT => T_PARENT,
                                    T_STATIC => T_STATIC,
                                    T_FALSE => T_FALSE,
                                    T_TRUE => T_TRUE,
                                    T_NULL => T_NULL,
                                    T_NAMESPACE => T_NAMESPACE,
                                    T_NS_SEPARATOR => T_NS_SEPARATOR,
                                ];
                                $suspectedType = null;
                                $typeTokenCountAfter = 0;
                                for ($x = $i + 1; $x < $numTokens; $x++) {
                                    if (isset(Tokens::$emptyTokens[$this->tokens[$x]['code']]) === true) {
                                        continue;
                                    }
                                    if (isset($allowed[$this->tokens[$x]['code']]) === true) {
                                        ++$typeTokenCountAfter;
                                        continue;
                                    }
                                    if (($typeTokenCountAfter > 0 || $this->tokens[$i]['code'] === T_CLOSE_PARENTHESIS && isset($this->tokens[$i]['parenthesis_owner']) === false) && ($this->tokens[$x]['code'] === T_BITWISE_AND || $this->tokens[$x]['code'] === T_ELLIPSIS)) {
                                        // Skip past reference and variadic indicators for parameter types.
                                        continue;
                                    }
                                    if ($this->tokens[$x]['code'] === T_VARIABLE) {
                                        // Parameter/Property defaults can not contain variables, so this could be a type.
                                        $suspectedType = 'property or parameter';
                                        break;
                                    }
                                    if ($this->tokens[$x]['code'] === T_DOUBLE_ARROW) {
                                        // Possible arrow function.
                                        $suspectedType = 'return';
                                        break;
                                    }
                                    if ($this->tokens[$x]['code'] === T_SEMICOLON) {
                                        // Possible abstract method or interface method.
                                        $suspectedType = 'return';
                                        break;
                                    }
                                    if ($this->tokens[$x]['code'] === T_OPEN_CURLY_BRACKET && isset($this->tokens[$x]['scope_condition']) === true && $this->tokens[$this->tokens[$x]['scope_condition']]['code'] === T_FUNCTION) {
                                        $suspectedType = 'return';
                                        break;
                                    }
                                    if ($this->tokens[$x]['code'] === T_EQUAL) {
                                        // Possible constant declaration, the `T_STRING` name will have been skipped over already.
                                        $suspectedType = 'constant';
                                        break;
                                    }
                                    break;
                                }
                                
                                //end for
                                if ($typeTokenCountAfter === 0 && ($this->tokens[$i]['code'] !== T_CLOSE_PARENTHESIS || isset($this->tokens[$i]['parenthesis_owner']) === true) || isset($suspectedType) === false) {
                                    // Definitely not a union, intersection or DNF type, move on.
                                    continue;
                                }
                                if ($suspectedType === 'property or parameter') {
                                    unset($allowed[T_STATIC]);
                                }
                                $typeTokenCountBefore = 0;
                                $typeOperators = [
                                    $i,
                                ];
                                $parenthesesCount = 0;
                                $confirmed = false;
                                $maybeNullable = null;
                                if ($this->tokens[$i]['code'] === T_OPEN_PARENTHESIS || $this->tokens[$i]['code'] === T_CLOSE_PARENTHESIS) {
                                    ++$parenthesesCount;
                                }
                                for ($x = $i - 1; $x >= 0; $x--) {
                                    if (isset(Tokens::$emptyTokens[$this->tokens[$x]['code']]) === true) {
                                        continue;
                                    }
                                    if ($suspectedType === 'property or parameter' && $this->tokens[$x]['code'] === T_STRING && strtolower($this->tokens[$x]['content']) === 'static') {
                                        // Static keyword followed directly by an open parenthesis for a DNF type.
                                        // This token should be T_STATIC and was incorrectly identified as a function call before.
                                        $this->tokens[$x]['code'] = T_STATIC;
                                        $this->tokens[$x]['type'] = 'T_STATIC';
                                        if (PHP_CODESNIFFER_VERBOSITY > 1) {
                                            $line = $this->tokens[$x]['line'];
                                            echo "\t* token {$x} on line {$line} changed back from T_STRING to T_STATIC" . PHP_EOL;
                                        }
                                    }
                                    if ($suspectedType === 'property or parameter' && $this->tokens[$x]['code'] === T_OPEN_PARENTHESIS) {
                                        // We need to prevent the open parenthesis for a function/fn declaration from being retokenized
                                        // to T_TYPE_OPEN_PARENTHESIS if this is the first parameter in the declaration.
                                        if (isset($this->tokens[$x]['parenthesis_owner']) === true && $this->tokens[$this->tokens[$x]['parenthesis_owner']]['code'] === T_FUNCTION) {
                                            $confirmed = true;
                                            break;
                                        }
                                        else {
                                            // This may still be an arrow function which hasn't been handled yet.
                                            for ($y = $x - 1; $y > 0; $y--) {
                                                if (isset(Tokens::$emptyTokens[$this->tokens[$y]['code']]) === false && $this->tokens[$y]['code'] !== T_BITWISE_AND) {
                                                    // Non-whitespace content.
                                                    break;
                                                }
                                            }
                                            if ($this->tokens[$y]['code'] === T_FN) {
                                                $confirmed = true;
                                                break;
                                            }
                                        }
                                    }
                                    
                                    //end if
                                    if (isset($allowed[$this->tokens[$x]['code']]) === true) {
                                        ++$typeTokenCountBefore;
                                        continue;
                                    }
                                    // Union, intersection and DNF types can't use the nullable operator, but be tolerant to parse errors.
                                    if (($typeTokenCountBefore > 0 || $this->tokens[$x]['code'] === T_OPEN_PARENTHESIS && isset($this->tokens[$x]['parenthesis_owner']) === false) && ($this->tokens[$x]['code'] === T_NULLABLE || $this->tokens[$x]['code'] === T_INLINE_THEN)) {
                                        if ($this->tokens[$x]['code'] === T_INLINE_THEN) {
                                            $maybeNullable = $x;
                                        }
                                        continue;
                                    }
                                    if ($this->tokens[$x]['code'] === T_BITWISE_OR || $this->tokens[$x]['code'] === T_BITWISE_AND) {
                                        $typeOperators[] = $x;
                                        continue;
                                    }
                                    if ($this->tokens[$x]['code'] === T_OPEN_PARENTHESIS || $this->tokens[$x]['code'] === T_CLOSE_PARENTHESIS) {
                                        ++$parenthesesCount;
                                        $typeOperators[] = $x;
                                        continue;
                                    }
                                    if ($suspectedType === 'return' && $this->tokens[$x]['code'] === T_COLON) {
                                        // Make sure this is the colon for a return type.
                                        for ($y = $x - 1; $y > 0; $y--) {
                                            if (isset(Tokens::$emptyTokens[$this->tokens[$y]['code']]) === false) {
                                                break;
                                            }
                                        }
                                        if ($this->tokens[$y]['code'] !== T_CLOSE_PARENTHESIS) {
                                            // Definitely not a union, intersection or DNF return type, move on.
                                            continue 2;
                                        }
                                        if (isset($this->tokens[$y]['parenthesis_owner']) === true) {
                                            if ($this->tokens[$this->tokens[$y]['parenthesis_owner']]['code'] === T_FUNCTION || $this->tokens[$this->tokens[$y]['parenthesis_owner']]['code'] === T_CLOSURE || $this->tokens[$this->tokens[$y]['parenthesis_owner']]['code'] === T_FN) {
                                                $confirmed = true;
                                            }
                                            break;
                                        }
                                        // Arrow functions may not have the parenthesis_owner set correctly yet.
                                        // Closure use tokens won't be parentheses owners until PHPCS 4.0.
                                        if (isset($this->tokens[$y]['parenthesis_opener']) === true) {
                                            for ($z = $this->tokens[$y]['parenthesis_opener'] - 1; $z > 0; $z--) {
                                                if (isset(Tokens::$emptyTokens[$this->tokens[$z]['code']]) === false) {
                                                    break;
                                                }
                                            }
                                            if ($this->tokens[$z]['code'] === T_FN || $this->tokens[$z]['code'] === T_USE) {
                                                $confirmed = true;
                                            }
                                        }
                                        break;
                                    }
                                    
                                    //end if
                                    if ($suspectedType === 'constant' && $this->tokens[$x]['code'] === T_CONST) {
                                        $confirmed = true;
                                        break;
                                    }
                                    if ($suspectedType === 'property or parameter' && (isset(Tokens::$scopeModifiers[$this->tokens[$x]['code']]) === true || $this->tokens[$x]['code'] === T_VAR || $this->tokens[$x]['code'] === T_STATIC || $this->tokens[$x]['code'] === T_READONLY)) {
                                        // This will also confirm constructor property promotion parameters, but that's fine.
                                        $confirmed = true;
                                    }
                                    break;
                                }
                                
                                //end for
                                // Remember the last token we examined as part of the (non-)"type declaration".
                                $lastSeenTypeToken = $x;
                                if ($confirmed === false && $suspectedType === 'property or parameter' && isset($this->tokens[$i]['nested_parenthesis']) === true) {
                                    $parens = $this->tokens[$i]['nested_parenthesis'];
                                    $last = end($parens);
                                    if (isset($this->tokens[$last]['parenthesis_owner']) === true && $this->tokens[$this->tokens[$last]['parenthesis_owner']]['code'] === T_FUNCTION) {
                                        $confirmed = true;
                                    }
                                    else {
                                        // No parenthesis owner set, this may be an arrow function which has not yet
                                        // had additional processing done.
                                        if (isset($this->tokens[$last]['parenthesis_opener']) === true) {
                                            for ($x = $this->tokens[$last]['parenthesis_opener'] - 1; $x >= 0; $x--) {
                                                if (isset(Tokens::$emptyTokens[$this->tokens[$x]['code']]) === true) {
                                                    continue;
                                                }
                                                break;
                                            }
                                            if ($this->tokens[$x]['code'] === T_FN) {
                                                for (--$x; $x >= 0; $x--) {
                                                    if (isset(Tokens::$emptyTokens[$this->tokens[$x]['code']]) === true || $this->tokens[$x]['code'] === T_BITWISE_AND) {
                                                        continue;
                                                    }
                                                    break;
                                                }
                                                if ($this->tokens[$x]['code'] !== T_FUNCTION) {
                                                    $confirmed = true;
                                                }
                                            }
                                        }
                                        
                                        //end if
                                    }
                                    
                                    //end if
                                    unset($parens, $last);
                                }
                                
                                //end if
                                if ($confirmed === false || $parenthesesCount % 2 !== 0) {
                                    // Not a (valid) union, intersection or DNF type after all, move on.
                                    continue;
                                }
                                foreach ($typeOperators as $x) {
                                    if ($this->tokens[$x]['code'] === T_BITWISE_OR) {
                                        $this->tokens[$x]['code'] = T_TYPE_UNION;
                                        $this->tokens[$x]['type'] = 'T_TYPE_UNION';
                                        if (PHP_CODESNIFFER_VERBOSITY > 1) {
                                            $line = $this->tokens[$x]['line'];
                                            echo "\t* token {$x} on line {$line} changed from T_BITWISE_OR to T_TYPE_UNION" . PHP_EOL;
                                        }
                                    }
                                    else {
                                        if ($this->tokens[$x]['code'] === T_BITWISE_AND) {
                                            $this->tokens[$x]['code'] = T_TYPE_INTERSECTION;
                                            $this->tokens[$x]['type'] = 'T_TYPE_INTERSECTION';
                                            if (PHP_CODESNIFFER_VERBOSITY > 1) {
                                                $line = $this->tokens[$x]['line'];
                                                echo "\t* token {$x} on line {$line} changed from T_BITWISE_AND to T_TYPE_INTERSECTION" . PHP_EOL;
                                            }
                                        }
                                        else {
                                            if ($this->tokens[$x]['code'] === T_OPEN_PARENTHESIS) {
                                                $this->tokens[$x]['code'] = T_TYPE_OPEN_PARENTHESIS;
                                                $this->tokens[$x]['type'] = 'T_TYPE_OPEN_PARENTHESIS';
                                                if (PHP_CODESNIFFER_VERBOSITY > 1) {
                                                    $line = $this->tokens[$x]['line'];
                                                    echo "\t* token {$x} on line {$line} changed from T_OPEN_PARENTHESIS to T_TYPE_OPEN_PARENTHESIS" . PHP_EOL;
                                                }
                                            }
                                            else {
                                                if ($this->tokens[$x]['code'] === T_CLOSE_PARENTHESIS) {
                                                    $this->tokens[$x]['code'] = T_TYPE_CLOSE_PARENTHESIS;
                                                    $this->tokens[$x]['type'] = 'T_TYPE_CLOSE_PARENTHESIS';
                                                    if (PHP_CODESNIFFER_VERBOSITY > 1) {
                                                        $line = $this->tokens[$x]['line'];
                                                        echo "\t* token {$x} on line {$line} changed from T_CLOSE_PARENTHESIS to T_TYPE_CLOSE_PARENTHESIS" . PHP_EOL;
                                                    }
                                                }
                                            }
                                        }
                                    }
                                    
                                    //end if
                                }
                                
                                //end foreach
                                if (isset($maybeNullable) === true) {
                                    $this->tokens[$maybeNullable]['code'] = T_NULLABLE;
                                    $this->tokens[$maybeNullable]['type'] = 'T_NULLABLE';
                                    if (PHP_CODESNIFFER_VERBOSITY > 1) {
                                        $line = $this->tokens[$maybeNullable]['line'];
                                        echo "\t* token {$maybeNullable} on line {$line} changed from T_INLINE_THEN to T_NULLABLE" . PHP_EOL;
                                    }
                                }
                                continue;
                            }
                            else {
                                if ($this->tokens[$i]['code'] === T_STATIC) {
                                    for ($x = $i - 1; $x > 0; $x--) {
                                        if (isset(Tokens::$emptyTokens[$this->tokens[$x]['code']]) === false) {
                                            break;
                                        }
                                    }
                                    if ($this->tokens[$x]['code'] === T_INSTANCEOF) {
                                        $this->tokens[$i]['code'] = T_STRING;
                                        $this->tokens[$i]['type'] = 'T_STRING';
                                        if (PHP_CODESNIFFER_VERBOSITY > 1) {
                                            $line = $this->tokens[$i]['line'];
                                            echo "\t* token {$i} on line {$line} changed from T_STATIC to T_STRING" . PHP_EOL;
                                        }
                                    }
                                    continue;
                                }
                                else {
                                    if ($this->tokens[$i]['code'] === T_TRUE || $this->tokens[$i]['code'] === T_FALSE || $this->tokens[$i]['code'] === T_NULL) {
                                        for ($x = $i + 1; $x < $numTokens; $x++) {
                                            if (isset(Tokens::$emptyTokens[$this->tokens[$x]['code']]) === false) {
                                                // Non-whitespace content.
                                                break;
                                            }
                                        }
                                        if ($x !== $numTokens && isset($this->tstringContexts[$this->tokens[$x]['code']]) === true) {
                                            if (PHP_CODESNIFFER_VERBOSITY > 1) {
                                                $line = $this->tokens[$i]['line'];
                                                $type = $this->tokens[$i]['type'];
                                                echo "\t* token {$i} on line {$line} changed from {$type} to T_STRING" . PHP_EOL;
                                            }
                                            $this->tokens[$i]['code'] = T_STRING;
                                            $this->tokens[$i]['type'] = 'T_STRING';
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
        
        //end if
        if ($this->tokens[$i]['code'] !== T_CASE && $this->tokens[$i]['code'] !== T_DEFAULT || isset($this->tokens[$i]['scope_opener']) === false) {
            // Only interested in CASE and DEFAULT statements from here on in.
            continue;
        }
        $scopeOpener = $this->tokens[$i]['scope_opener'];
        $scopeCloser = $this->tokens[$i]['scope_closer'];
        // If the first char after the opener is a curly brace
        // and that brace has been ignored, it is actually
        // opening this case statement and the opener and closer are
        // probably set incorrectly.
        for ($x = $scopeOpener + 1; $x < $numTokens; $x++) {
            if (isset(Tokens::$emptyTokens[$this->tokens[$x]['code']]) === false) {
                // Non-whitespace content.
                break;
            }
        }
        if ($this->tokens[$x]['code'] === T_CASE || $this->tokens[$x]['code'] === T_DEFAULT) {
            // Special case for multiple CASE statements that share the same
            // closer. Because we are going backwards through the file, this next
            // CASE statement is already fixed, so just use its closer and don't
            // worry about fixing anything.
            $newCloser = $this->tokens[$x]['scope_closer'];
            $this->tokens[$i]['scope_closer'] = $newCloser;
            if (PHP_CODESNIFFER_VERBOSITY > 1) {
                $oldType = $this->tokens[$scopeCloser]['type'];
                $newType = $this->tokens[$newCloser]['type'];
                $line = $this->tokens[$i]['line'];
                echo "\t* token {$i} (T_CASE) on line {$line} closer changed from {$scopeCloser} ({$oldType}) to {$newCloser} ({$newType})" . PHP_EOL;
            }
            continue;
        }
        if ($this->tokens[$x]['code'] !== T_OPEN_CURLY_BRACKET || isset($this->tokens[$x]['scope_condition']) === true) {
            // Not a CASE/DEFAULT with a curly brace opener.
            continue;
        }
        // The closer for this CASE/DEFAULT should be the closing curly brace and
        // not whatever it already is. The opener needs to be the opening curly
        // brace so everything matches up.
        $newCloser = $this->tokens[$x]['bracket_closer'];
        foreach ([
            $i,
            $x,
            $newCloser,
        ] as $index) {
            $this->tokens[$index]['scope_condition'] = $i;
            $this->tokens[$index]['scope_opener'] = $x;
            $this->tokens[$index]['scope_closer'] = $newCloser;
        }
        if (PHP_CODESNIFFER_VERBOSITY > 1) {
            $line = $this->tokens[$i]['line'];
            $tokenType = $this->tokens[$i]['type'];
            $oldType = $this->tokens[$scopeOpener]['type'];
            $newType = $this->tokens[$x]['type'];
            echo "\t* token {$i} ({$tokenType}) on line {$line} opener changed from {$scopeOpener} ({$oldType}) to {$x} ({$newType})" . PHP_EOL;
            $oldType = $this->tokens[$scopeCloser]['type'];
            $newType = $this->tokens[$newCloser]['type'];
            echo "\t* token {$i} ({$tokenType}) on line {$line} closer changed from {$scopeCloser} ({$oldType}) to {$newCloser} ({$newType})" . PHP_EOL;
        }
        if ($this->tokens[$scopeOpener]['scope_condition'] === $i) {
            unset($this->tokens[$scopeOpener]['scope_condition']);
            unset($this->tokens[$scopeOpener]['scope_opener']);
            unset($this->tokens[$scopeOpener]['scope_closer']);
        }
        if ($this->tokens[$scopeCloser]['scope_condition'] === $i) {
            unset($this->tokens[$scopeCloser]['scope_condition']);
            unset($this->tokens[$scopeCloser]['scope_opener']);
            unset($this->tokens[$scopeCloser]['scope_closer']);
        }
        else {
            // We were using a shared closer. All tokens that were
            // sharing this closer with us, except for the scope condition
            // and it's opener, need to now point to the new closer.
            $condition = $this->tokens[$scopeCloser]['scope_condition'];
            $start = $this->tokens[$condition]['scope_opener'] + 1;
            for ($y = $start; $y < $scopeCloser; $y++) {
                if (isset($this->tokens[$y]['scope_closer']) === true && $this->tokens[$y]['scope_closer'] === $scopeCloser) {
                    $this->tokens[$y]['scope_closer'] = $newCloser;
                    if (PHP_CODESNIFFER_VERBOSITY > 1) {
                        $line = $this->tokens[$y]['line'];
                        $tokenType = $this->tokens[$y]['type'];
                        $oldType = $this->tokens[$scopeCloser]['type'];
                        $newType = $this->tokens[$newCloser]['type'];
                        echo "\t\t* token {$y} ({$tokenType}) on line {$line} closer changed from {$scopeCloser} ({$oldType}) to {$newCloser} ({$newType})" . PHP_EOL;
                    }
                }
            }
        }
        
        //end if
        unset($this->tokens[$x]['bracket_opener']);
        unset($this->tokens[$x]['bracket_closer']);
        unset($this->tokens[$newCloser]['bracket_opener']);
        unset($this->tokens[$newCloser]['bracket_closer']);
        $this->tokens[$scopeCloser]['conditions'][] = $i;
        // Now fix up all the tokens that think they are
        // inside the CASE/DEFAULT statement when they are really outside.
        for ($x = $newCloser; $x < $scopeCloser; $x++) {
            foreach ($this->tokens[$x]['conditions'] as $num => $oldCond) {
                if ($oldCond === $this->tokens[$i]['code']) {
                    $oldConditions = $this->tokens[$x]['conditions'];
                    unset($this->tokens[$x]['conditions'][$num]);
                    if (PHP_CODESNIFFER_VERBOSITY > 1) {
                        $type = $this->tokens[$x]['type'];
                        $oldConds = '';
                        foreach ($oldConditions as $condition) {
                            $oldConds .= Tokens::tokenName($condition) . ',';
                        }
                        $oldConds = rtrim($oldConds, ',');
                        $newConds = '';
                        foreach ($this->tokens[$x]['conditions'] as $condition) {
                            $newConds .= Tokens::tokenName($condition) . ',';
                        }
                        $newConds = rtrim($newConds, ',');
                        echo "\t\t* cleaned {$x} ({$type}) *" . PHP_EOL;
                        echo "\t\t\t=> conditions changed from {$oldConds} to {$newConds}" . PHP_EOL;
                    }
                    break;
                }
                
                //end if
            }
            
            //end foreach
        }
        
        //end for
    }
    
    //end for
    if (PHP_CODESNIFFER_VERBOSITY > 1) {
        echo "\t*** END ADDITIONAL PHP PROCESSING ***" . PHP_EOL;
    }
}

API Navigation

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