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

Breadcrumb

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

function Helpers::findVariableScope

*

Parameters

File $phpcsFile: * @param int $stackPtr * @param string $varName (optional) if it differs from the normalized 'content' of the token at $stackPtr * * @return ?int

4 calls to Helpers::findVariableScope()
VariableAnalysisSniff::markAllVariablesRead in vendor/sirbrillig/phpcs-variable-analysis/VariableAnalysis/Sniffs/CodeAnalysis/VariableAnalysisSniff.php
* Mark all variables within a scope as being used. * * This will prevent any of the variables in that scope from being reported * as unused. * *
VariableAnalysisSniff::processCompact in vendor/sirbrillig/phpcs-variable-analysis/VariableAnalysis/Sniffs/CodeAnalysis/VariableAnalysisSniff.php
* Called to process variables named in a call to compact(). * *
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. * …
VariableAnalysisSniff::processVariableInString in vendor/sirbrillig/phpcs-variable-analysis/VariableAnalysis/Sniffs/CodeAnalysis/VariableAnalysisSniff.php
* Called to process variables found in double quoted strings. * * Note that there may be more than one variable in the string, which will * result only in one call for the string. * *

File

vendor/sirbrillig/phpcs-variable-analysis/VariableAnalysis/Lib/Helpers.php, line 425

Class

Helpers

Namespace

VariableAnalysis\Lib

Code

public static function findVariableScope(File $phpcsFile, $stackPtr, $varName = null) {
    $tokens = $phpcsFile->getTokens();
    $token = $tokens[$stackPtr];
    $varName = isset($varName) ? $varName : self::normalizeVarName($token['content']);
    $enclosingScopeIndex = self::findVariableScopeExceptArrowFunctions($phpcsFile, $stackPtr);
    if ($enclosingScopeIndex) {
        $arrowFunctionIndex = self::getContainingArrowFunctionIndex($phpcsFile, $stackPtr, $enclosingScopeIndex);
        $isTokenInsideArrowFunctionBody = is_int($arrowFunctionIndex);
        if ($isTokenInsideArrowFunctionBody) {
            // Get the list of variables defined by the arrow function
            // If this matches any of them, the scope is the arrow function,
            // otherwise, it uses the enclosing scope.
            if ($arrowFunctionIndex) {
                $variableNames = self::getVariablesDefinedByArrowFunction($phpcsFile, $arrowFunctionIndex);
                self::debug('findVariableScope: looking for', $varName, 'in arrow function variables', $variableNames);
                if (in_array($varName, $variableNames, true)) {
                    return $arrowFunctionIndex;
                }
            }
        }
    }
    return self::findVariableScopeExceptArrowFunctions($phpcsFile, $stackPtr);
}

API Navigation

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