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

Breadcrumb

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

function VariableAnalysisSniff::processVariableAsUseImportDefinition

* Process a variable definition if it is inside a function's "use" import. * *

Parameters

File $phpcsFile: * @param int $stackPtr * @param string $varName * @param int $outerScope The start of the scope outside the function definition * * @return void

1 call to VariableAnalysisSniff::processVariableAsUseImportDefinition()
VariableAnalysisSniff::processVariable in vendor/sirbrillig/phpcs-variable-analysis/VariableAnalysis/Sniffs/CodeAnalysis/VariableAnalysisSniff.php
* Process a normal variable in the code. * * Most importantly, this function determines if the variable use is a "read" * (using the variable for something) or a "write" (an assignment) or, * sometimes, both at once. * …

File

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

Class

VariableAnalysisSniff

Namespace

VariableAnalysis\Sniffs\CodeAnalysis

Code

protected function processVariableAsUseImportDefinition(File $phpcsFile, $stackPtr, $varName, $outerScope) {
    $tokens = $phpcsFile->getTokens();
    Helpers::debug('processVariableAsUseImportDefinition', $stackPtr, $varName, $outerScope);
    $endOfArgsPtr = $phpcsFile->findPrevious([
        T_CLOSE_PARENTHESIS,
    ], $stackPtr - 1, null);
    if (!is_int($endOfArgsPtr)) {
        throw new \Exception("Arguments index not found for function use index {$stackPtr} when processing variable {$varName}");
    }
    $functionPtr = Helpers::getFunctionIndexForFunctionParameter($phpcsFile, $endOfArgsPtr);
    if (!is_int($functionPtr)) {
        throw new \Exception("Function index not found for function use index {$stackPtr} (using {$endOfArgsPtr}) when processing variable {$varName}");
    }
    // Use is both a read (in the enclosing scope) and a define (in the function scope)
    $this->markVariableRead($varName, $stackPtr, $outerScope);
    // If it's undefined in the enclosing scope, the use is wrong
    if ($this->isVariableUndefined($varName, $stackPtr, $outerScope) === true) {
        Helpers::debug("variable '{$varName}' in function definition looks undefined in scope", $outerScope);
        $this->warnAboutUndefinedVariable($phpcsFile, $varName, $stackPtr);
        return;
    }
    $this->markVariableDeclaration($varName, ScopeType::BOUND, null, $stackPtr, $functionPtr);
    $this->markVariableAssignment($varName, $stackPtr, $functionPtr);
    // Are we pass-by-reference? If so, then any assignment to the variable in
    // the function scope also should count for the enclosing scope.
    $referencePtr = $phpcsFile->findPrevious(Tokens::$emptyTokens, $stackPtr - 1, null, true, null, true);
    if (is_int($referencePtr) && $tokens[$referencePtr]['code'] === T_BITWISE_AND) {
        Helpers::debug("variable '{$varName}' in function definition looks passed by reference");
        $varInfo = $this->getOrCreateVariableInfo($varName, $functionPtr);
        $varInfo->referencedVariableScope = $outerScope;
    }
}

API Navigation

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