function Helpers::findVariableScopeExceptArrowFunctions
* 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 "use" import list within a function definition, * this will return the enclosing scope, not the function keyword. This is * important to note because the "use" keyword performs double-duty, defining * variables for the function's scope, and consuming the variables in the * enclosing scope. Use `getUseIndexForUseImport` to determine if this * token needs to be treated as a "use". * * For a variable within an arrow function definition argument list, * this will return the arrow function's keyword index. * * For a variable in an arrow function body, this will return the enclosing * function's index, which may be incorrect. * * Since a variable in an arrow function's body may be imported from the * enclosing scope, it's important to test to see if the variable is in an * arrow function and also check its enclosing scope separately. * *
Parameters
File $phpcsFile: * @param int $stackPtr * * @return ?int
1 call to Helpers::findVariableScopeExceptArrowFunctions()
- Helpers::findVariableScope in vendor/
sirbrillig/ phpcs-variable-analysis/ VariableAnalysis/ Lib/ Helpers.php - *
File
-
vendor/
sirbrillig/ phpcs-variable-analysis/ VariableAnalysis/ Lib/ Helpers.php, line 542
Class
Namespace
VariableAnalysis\LibCode
public static function findVariableScopeExceptArrowFunctions(File $phpcsFile, $stackPtr) {
$tokens = $phpcsFile->getTokens();
$allowedTypes = [
T_VARIABLE,
T_DOUBLE_QUOTED_STRING,
T_HEREDOC,
T_STRING,
];
if (!in_array($tokens[$stackPtr]['code'], $allowedTypes, true)) {
throw new \Exception("Cannot find variable scope for non-variable {$tokens[$stackPtr]['type']}");
}
$startOfTokenScope = self::getStartOfTokenScope($phpcsFile, $stackPtr);
if (is_int($startOfTokenScope) && $startOfTokenScope > 0) {
return $startOfTokenScope;
}
// If there is no "conditions" array, this is a function definition argument.
if (self::isTokenFunctionParameter($phpcsFile, $stackPtr)) {
$functionPtr = self::getFunctionIndexForFunctionParameter($phpcsFile, $stackPtr);
if (!is_int($functionPtr)) {
throw new \Exception("Function index not found for function argument index {$stackPtr}");
}
return $functionPtr;
}
self::debug('Cannot find function scope for variable at', $stackPtr);
return $startOfTokenScope;
}