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

Breadcrumb

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

function VariableHelper::isUsedInScopeInternal

2 calls to VariableHelper::isUsedInScopeInternal()
VariableHelper::isUsedInScope in vendor/slevomat/coding-standard/SlevomatCodingStandard/Helpers/VariableHelper.php
VariableHelper::isUsedInScopeAfterPointer in vendor/slevomat/coding-standard/SlevomatCodingStandard/Helpers/VariableHelper.php

File

vendor/slevomat/coding-standard/SlevomatCodingStandard/Helpers/VariableHelper.php, line 103

Class

VariableHelper
@internal

Namespace

SlevomatCodingStandard\Helpers

Code

private static function isUsedInScopeInternal(File $phpcsFile, int $scopeOwnerPointer, int $variablePointer, ?int $startCheckPointer) : bool {
    $tokens = $phpcsFile->getTokens();
    if ($tokens[$scopeOwnerPointer]['code'] === T_OPEN_TAG) {
        $scopeCloserPointer = count($tokens) - 1;
    }
    elseif ($tokens[$scopeOwnerPointer]['code'] === T_FN) {
        $scopeCloserPointer = $tokens[$scopeOwnerPointer]['scope_closer'];
    }
    else {
        $scopeCloserPointer = $tokens[$scopeOwnerPointer]['scope_closer'] - 1;
    }
    if ($tokens[$scopeOwnerPointer]['code'] === T_OPEN_TAG) {
        $firstPointerInScope = $scopeOwnerPointer + 1;
    }
    elseif ($tokens[$scopeOwnerPointer]['code'] === T_FN) {
        $firstPointerInScope = $tokens[$scopeOwnerPointer]['scope_opener'];
    }
    else {
        $firstPointerInScope = $tokens[$scopeOwnerPointer]['scope_opener'] + 1;
    }
    if ($startCheckPointer === null) {
        $startCheckPointer = $firstPointerInScope;
    }
    for ($i = $startCheckPointer; $i <= $scopeCloserPointer; $i++) {
        if (!ScopeHelper::isInSameScope($phpcsFile, $i, $firstPointerInScope)) {
            continue;
        }
        if ($tokens[$i]['code'] === T_VARIABLE && self::isUsedAsVariable($phpcsFile, $variablePointer, $i)) {
            return true;
        }
        if ($tokens[$i]['code'] === T_STRING) {
            if (self::isGetDefinedVarsCall($phpcsFile, $i)) {
                return true;
            }
            if (self::isUsedInCompactFunction($phpcsFile, $variablePointer, $i)) {
                return true;
            }
        }
        if (in_array($tokens[$i]['code'], [
            T_DOUBLE_QUOTED_STRING,
            T_HEREDOC,
        ], true) && self::isUsedInScopeInString($phpcsFile, $tokens[$variablePointer]['content'], $i)) {
            return true;
        }
    }
    return false;
}
RSS feed
Powered by Drupal