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

Breadcrumb

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

function VariableAnalysisSniff::process

* Scan and process a token. * * This is the main processing function of the sniff. Will run on every token * for which `register()` returns true. * *

Parameters

File $phpcsFile: * @param int $stackPtr * * @return void

Overrides Sniff::process

File

vendor/sirbrillig/phpcs-variable-analysis/VariableAnalysis/Sniffs/CodeAnalysis/VariableAnalysisSniff.php, line 223

Class

VariableAnalysisSniff

Namespace

VariableAnalysis\Sniffs\CodeAnalysis

Code

public function process(File $phpcsFile, $stackPtr) {
    $tokens = $phpcsFile->getTokens();
    $scopeStartTokenTypes = [
        T_FUNCTION,
        T_CLOSURE,
    ];
    $token = $tokens[$stackPtr];
    // Cache the current PHPCS File in an instance variable so it can be more
    // easily accessed in other places which aren't passed the object.
    if ($this->currentFile !== $phpcsFile) {
        $this->currentFile = $phpcsFile;
        $this->forLoops = [];
        $this->enums = [];
    }
    // Add the global scope for the current file to our scope indexes.
    $scopesForFilename = $this->scopeManager
        ->getScopesForFilename($phpcsFile->getFilename());
    if (empty($scopesForFilename)) {
        $this->scopeManager
            ->recordScopeStartAndEnd($phpcsFile, 0);
    }
    // Find and process variables to perform two jobs: to record variable
    // definition or use, and to report variables as undefined if they are used
    // without having been first defined.
    if ($token['code'] === T_VARIABLE) {
        $this->processVariable($phpcsFile, $stackPtr);
    }
    // Report variables defined but not used in the current scope as unused
    // variables if the current token closes scopes.
    $this->searchForAndProcessClosingScopesAt($phpcsFile, $stackPtr);
    // Scan variables that were postponed because they exist in the increment
    // expression of a for loop if the current token closes a loop.
    $this->processClosingForLoopsAt($phpcsFile, $stackPtr);
    if ($token['code'] === T_VARIABLE) {
        return;
    }
    if ($token['code'] === T_DOUBLE_QUOTED_STRING || $token['code'] === T_HEREDOC) {
        $this->processVariableInString($phpcsFile, $stackPtr);
        return;
    }
    if ($token['code'] === T_STRING && $token['content'] === 'compact') {
        $this->processCompact($phpcsFile, $stackPtr);
        return;
    }
    // Record for loop boundaries so we can delay scanning the third for loop
    // expression until after the loop has been scanned.
    if ($token['code'] === T_FOR) {
        $this->recordForLoop($phpcsFile, $stackPtr);
        return;
    }
    // Record enums so we can detect them even before phpcs was able to.
    if ($token['content'] === 'enum') {
        $enumInfo = Helpers::makeEnumInfo($phpcsFile, $stackPtr);
        // The token might not actually be an enum so let's avoid returning if
        // it's not.
        if ($enumInfo) {
            $this->enums[$stackPtr] = $enumInfo;
            return;
        }
    }
    // If the current token is a call to `get_defined_vars()`, consider that a
    // usage of all variables in the current scope.
    if ($this->isGetDefinedVars($phpcsFile, $stackPtr)) {
        Helpers::debug('get_defined_vars is being called');
        $this->markAllVariablesRead($phpcsFile, $stackPtr);
        return;
    }
    // If the current token starts a scope, record that scope's start and end
    // indexes so that we can determine if variables in that scope are defined
    // and/or used.
    if (in_array($token['code'], $scopeStartTokenTypes, true) || Helpers::isArrowFunction($phpcsFile, $stackPtr)) {
        Helpers::debug('found scope condition', $token);
        $this->scopeManager
            ->recordScopeStartAndEnd($phpcsFile, $stackPtr);
        return;
    }
}
RSS feed
Powered by Drupal