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

Breadcrumb

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

function Helpers::getStartOfTokenScope

* Return the token index of the scope start for a variable token * * This will only work for a variable within a function's body. Otherwise, * see `findVariableScope`, which is more complex. * * Note that if used on a variable in an arrow function, it will return the * enclosing function's scope, which may be incorrect. * *

Parameters

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

1 call to Helpers::getStartOfTokenScope()
Helpers::findVariableScopeExceptArrowFunctions in vendor/sirbrillig/phpcs-variable-analysis/VariableAnalysis/Lib/Helpers.php
* Return the token index of the scope start for a token * * For a variable within a function body, or a variable within a function * definition argument list, this will return the function keyword's index. * * For a variable within a…

File

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

Class

Helpers

Namespace

VariableAnalysis\Lib

Code

private static function getStartOfTokenScope(File $phpcsFile, $stackPtr) {
    $tokens = $phpcsFile->getTokens();
    $token = $tokens[$stackPtr];
    $inClass = false;
    $conditions = isset($token['conditions']) ? $token['conditions'] : [];
    $functionTokenTypes = [
        T_FUNCTION,
        T_CLOSURE,
    ];
    foreach (array_reverse($conditions, true) as $scopePtr => $scopeCode) {
        if (in_array($scopeCode, $functionTokenTypes, true) || self::isArrowFunction($phpcsFile, $scopePtr)) {
            return $scopePtr;
        }
        if (isset(Tokens::$ooScopeTokens[$scopeCode]) === true) {
            $inClass = true;
        }
    }
    if ($inClass) {
        // If this is inside a class and not inside a function, this is either a
        // class member variable definition, or a function argument. If it is a
        // variable definition, it has no scope on its own (it can only be used
        // with an object reference). If it is a function argument, we need to do
        // more work (see `findVariableScopeExceptArrowFunctions`).
        return null;
    }
    // If we can't find a scope, let's use the first token of the file.
    return 0;
}

API Navigation

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