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

Breadcrumb

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

function Helpers::getFunctionIndexForFunctionCallArgument

* Find the index of the function keyword for a token in a function call's arguments * * For the variable `$foo` in the expression `doSomething($foo)`, this will * return the index of the `doSomething` token. * *

Parameters

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

3 calls to Helpers::getFunctionIndexForFunctionCallArgument()
Helpers::isTokenInsideFunctionCallArgument in vendor/sirbrillig/phpcs-variable-analysis/VariableAnalysis/Lib/Helpers.php
* Return true if the token is inside the arguments of a function call. * * For example, the variable `$foo` in `doSomething($foo)` is inside the * arguments to the call to `doSomething()`. * *
Helpers::isVariableInsideIssetOrEmpty in vendor/sirbrillig/phpcs-variable-analysis/VariableAnalysis/Lib/Helpers.php
*
Helpers::isVariableInsideUnset in vendor/sirbrillig/phpcs-variable-analysis/VariableAnalysis/Lib/Helpers.php
*

File

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

Class

Helpers

Namespace

VariableAnalysis\Lib

Code

public static function getFunctionIndexForFunctionCallArgument(File $phpcsFile, $stackPtr) {
    $tokens = $phpcsFile->getTokens();
    $token = $tokens[$stackPtr];
    if (empty($token['nested_parenthesis'])) {
        return null;
    }
    
    /**
     * @var array<int|string|null>
     */
    $startingParenthesis = array_keys($token['nested_parenthesis']);
    $startOfArguments = end($startingParenthesis);
    if (!is_int($startOfArguments)) {
        return null;
    }
    $nonFunctionTokenTypes = Tokens::$emptyTokens;
    $functionPtr = self::getIntOrNull($phpcsFile->findPrevious($nonFunctionTokenTypes, $startOfArguments - 1, null, true, null, true));
    if (!is_int($functionPtr) || !isset($tokens[$functionPtr]['code'])) {
        return null;
    }
    if ($tokens[$functionPtr]['content'] === 'function' || $tokens[$functionPtr]['content'] === 'fn' && self::isArrowFunction($phpcsFile, $functionPtr)) {
        // If there is a function/fn keyword before the beginning of the parens,
        // this is a function definition and not a function call.
        return null;
    }
    if (!empty($tokens[$functionPtr]['scope_opener'])) {
        // If the alleged function name has a scope, this is not a function call.
        return null;
    }
    $functionNameType = $tokens[$functionPtr]['code'];
    if (!in_array($functionNameType, Tokens::$functionNameTokens, true)) {
        // If the alleged function name is not a variable or a string, this is
        // not a function call.
        return null;
    }
    if ($tokens[$functionPtr]['level'] !== $tokens[$stackPtr]['level']) {
        // If the variable is inside a different scope than the function name,
        // the function call doesn't apply to the variable.
        return null;
    }
    return $functionPtr;
}
RSS feed
Powered by Drupal