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

Breadcrumb

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

function JS::processAdditional

Performs additional processing after main tokenizing.

This additional processing looks for properties, closures, labels and objects.

Return value

void

Overrides Tokenizer::processAdditional

File

vendor/squizlabs/php_codesniffer/src/Tokenizers/JS.php, line 1052

Class

JS

Namespace

PHP_CodeSniffer\Tokenizers

Code

public function processAdditional() {
    if (PHP_CODESNIFFER_VERBOSITY > 1) {
        echo "\t*** START ADDITIONAL JS PROCESSING ***" . PHP_EOL;
    }
    $numTokens = count($this->tokens);
    $classStack = [];
    for ($i = 0; $i < $numTokens; $i++) {
        if (PHP_CODESNIFFER_VERBOSITY > 1) {
            $type = $this->tokens[$i]['type'];
            $content = Util\Common::prepareForOutput($this->tokens[$i]['content']);
            echo str_repeat("\t", count($classStack));
            echo "\tProcess token {$i}: {$type} => {$content}" . PHP_EOL;
        }
        // Looking for functions that are actually closures.
        if ($this->tokens[$i]['code'] === T_FUNCTION && isset($this->tokens[$i]['scope_opener']) === true) {
            for ($x = $i + 1; $x < $numTokens; $x++) {
                if (isset(Util\Tokens::$emptyTokens[$this->tokens[$x]['code']]) === false) {
                    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 str_repeat("\t", count($classStack));
                    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 str_repeat("\t", count($classStack));
                        echo "\t\t* cleaned {$x} ({$type}) *" . PHP_EOL;
                    }
                }
            }
            
            //end if
            continue;
        }
        else {
            if ($this->tokens[$i]['code'] === T_OPEN_CURLY_BRACKET && isset($this->tokens[$i]['scope_condition']) === false && isset($this->tokens[$i]['bracket_closer']) === true) {
                $condition = $this->tokens[$i]['conditions'];
                $condition = end($condition);
                if ($condition === T_CLASS) {
                    // Possibly an ES6 method. To be classified as one, the previous
                    // non-empty tokens need to be a set of parenthesis, and then a string
                    // (the method name).
                    for ($parenCloser = $i - 1; $parenCloser > 0; $parenCloser--) {
                        if (isset(Util\Tokens::$emptyTokens[$this->tokens[$parenCloser]['code']]) === false) {
                            break;
                        }
                    }
                    if ($this->tokens[$parenCloser]['code'] === T_CLOSE_PARENTHESIS) {
                        $parenOpener = $this->tokens[$parenCloser]['parenthesis_opener'];
                        for ($name = $parenOpener - 1; $name > 0; $name--) {
                            if (isset(Util\Tokens::$emptyTokens[$this->tokens[$name]['code']]) === false) {
                                break;
                            }
                        }
                        if ($this->tokens[$name]['code'] === T_STRING) {
                            // We found a method name.
                            if (PHP_CODESNIFFER_VERBOSITY > 1) {
                                $line = $this->tokens[$name]['line'];
                                echo str_repeat("\t", count($classStack));
                                echo "\t* token {$name} on line {$line} changed from T_STRING to T_FUNCTION *" . PHP_EOL;
                            }
                            $closer = $this->tokens[$i]['bracket_closer'];
                            $this->tokens[$name]['code'] = T_FUNCTION;
                            $this->tokens[$name]['type'] = 'T_FUNCTION';
                            foreach ([
                                $name,
                                $i,
                                $closer,
                            ] as $token) {
                                $this->tokens[$token]['scope_condition'] = $name;
                                $this->tokens[$token]['scope_opener'] = $i;
                                $this->tokens[$token]['scope_closer'] = $closer;
                                $this->tokens[$token]['parenthesis_opener'] = $parenOpener;
                                $this->tokens[$token]['parenthesis_closer'] = $parenCloser;
                                $this->tokens[$token]['parenthesis_owner'] = $name;
                            }
                            $this->tokens[$parenOpener]['parenthesis_owner'] = $name;
                            $this->tokens[$parenCloser]['parenthesis_owner'] = $name;
                            for ($x = $i + 1; $x < $closer; $x++) {
                                $this->tokens[$x]['conditions'][$name] = T_FUNCTION;
                                ksort($this->tokens[$x]['conditions'], SORT_NUMERIC);
                                if (PHP_CODESNIFFER_VERBOSITY > 1) {
                                    $type = $this->tokens[$x]['type'];
                                    echo str_repeat("\t", count($classStack));
                                    echo "\t\t* added T_FUNCTION condition to {$x} ({$type}) *" . PHP_EOL;
                                }
                            }
                            continue;
                        }
                        
                        //end if
                    }
                    
                    //end if
                }
                
                //end if
                $classStack[] = $i;
                $closer = $this->tokens[$i]['bracket_closer'];
                $this->tokens[$i]['code'] = T_OBJECT;
                $this->tokens[$i]['type'] = 'T_OBJECT';
                $this->tokens[$closer]['code'] = T_CLOSE_OBJECT;
                $this->tokens[$closer]['type'] = 'T_CLOSE_OBJECT';
                if (PHP_CODESNIFFER_VERBOSITY > 1) {
                    echo str_repeat("\t", count($classStack));
                    echo "\t* token {$i} converted from T_OPEN_CURLY_BRACKET to T_OBJECT *" . PHP_EOL;
                    echo str_repeat("\t", count($classStack));
                    echo "\t* token {$closer} converted from T_CLOSE_CURLY_BRACKET to T_CLOSE_OBJECT *" . PHP_EOL;
                }
                for ($x = $i + 1; $x < $closer; $x++) {
                    $this->tokens[$x]['conditions'][$i] = T_OBJECT;
                    ksort($this->tokens[$x]['conditions'], SORT_NUMERIC);
                    if (PHP_CODESNIFFER_VERBOSITY > 1) {
                        $type = $this->tokens[$x]['type'];
                        echo str_repeat("\t", count($classStack));
                        echo "\t\t* added T_OBJECT condition to {$x} ({$type}) *" . PHP_EOL;
                    }
                }
            }
            else {
                if ($this->tokens[$i]['code'] === T_CLOSE_OBJECT) {
                    array_pop($classStack);
                }
                else {
                    if ($this->tokens[$i]['code'] === T_COLON) {
                        // If it is a scope opener, it belongs to a
                        // DEFAULT or CASE statement.
                        if (isset($this->tokens[$i]['scope_condition']) === true) {
                            continue;
                        }
                        // Make sure this is not part of an inline IF statement.
                        for ($x = $i - 1; $x >= 0; $x--) {
                            if ($this->tokens[$x]['code'] === T_INLINE_THEN) {
                                $this->tokens[$i]['code'] = T_INLINE_ELSE;
                                $this->tokens[$i]['type'] = 'T_INLINE_ELSE';
                                if (PHP_CODESNIFFER_VERBOSITY > 1) {
                                    echo str_repeat("\t", count($classStack));
                                    echo "\t* token {$i} converted from T_COLON to T_INLINE_THEN *" . PHP_EOL;
                                }
                                continue 2;
                            }
                            else {
                                if ($this->tokens[$x]['line'] < $this->tokens[$i]['line']) {
                                    break;
                                }
                            }
                        }
                        // The string to the left of the colon is either a property or label.
                        for ($label = $i - 1; $label >= 0; $label--) {
                            if (isset(Util\Tokens::$emptyTokens[$this->tokens[$label]['code']]) === false) {
                                break;
                            }
                        }
                        if ($this->tokens[$label]['code'] !== T_STRING && $this->tokens[$label]['code'] !== T_CONSTANT_ENCAPSED_STRING) {
                            continue;
                        }
                        if (empty($classStack) === false) {
                            $this->tokens[$label]['code'] = T_PROPERTY;
                            $this->tokens[$label]['type'] = 'T_PROPERTY';
                            if (PHP_CODESNIFFER_VERBOSITY > 1) {
                                echo str_repeat("\t", count($classStack));
                                echo "\t* token {$label} converted from T_STRING to T_PROPERTY *" . PHP_EOL;
                            }
                        }
                        else {
                            $this->tokens[$label]['code'] = T_LABEL;
                            $this->tokens[$label]['type'] = 'T_LABEL';
                            if (PHP_CODESNIFFER_VERBOSITY > 1) {
                                echo str_repeat("\t", count($classStack));
                                echo "\t* token {$label} converted from T_STRING to T_LABEL *" . PHP_EOL;
                            }
                        }
                        
                        //end if
                    }
                }
            }
        }
        
        //end if
    }
    
    //end for
    if (PHP_CODESNIFFER_VERBOSITY > 1) {
        echo "\t*** END ADDITIONAL JS PROCESSING ***" . PHP_EOL;
    }
}

API Navigation

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