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

Breadcrumb

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

function UnusedVariableSniff::process

* @phpcsSuppress SlevomatCodingStandard.TypeHints.ParameterTypeHint.MissingNativeTypeHint *

Parameters

int $variablePointer:

Overrides Sniff::process

File

vendor/slevomat/coding-standard/SlevomatCodingStandard/Sniffs/Variables/UnusedVariableSniff.php, line 92

Class

UnusedVariableSniff

Namespace

SlevomatCodingStandard\Sniffs\Variables

Code

public function process(File $phpcsFile, $variablePointer) : void {
    if (!$this->isAssignment($phpcsFile, $variablePointer)) {
        return;
    }
    $tokens = $phpcsFile->getTokens();
    $variableName = $tokens[$variablePointer]['content'];
    if (in_array($variableName, [
        '$this',
        '$GLOBALS',
        '$_SERVER',
        '$_GET',
        '$_POST',
        '$_FILES',
        '$_COOKIE',
        '$_SESSION',
        '$_REQUEST',
        '$_ENV',
    ], true)) {
        return;
    }
    $previousPointer = TokenHelper::findPreviousEffective($phpcsFile, $variablePointer - 1);
    if (in_array($tokens[$previousPointer]['code'], [
        T_OBJECT_OPERATOR,
        T_DOUBLE_COLON,
    ], true)) {
        // Property
        return;
    }
    if (in_array($tokens[$previousPointer]['code'], Tokens::$castTokens, true)) {
        $previousPointer = TokenHelper::findPreviousEffective($phpcsFile, $previousPointer - 1);
    }
    if (in_array($tokens[$previousPointer]['code'], [
        T_EQUAL,
        T_PLUS_EQUAL,
        T_MINUS_EQUAL,
        T_MUL_EQUAL,
        T_DIV_EQUAL,
        T_POW_EQUAL,
        T_MOD_EQUAL,
        T_AND_EQUAL,
        T_OR_EQUAL,
        T_XOR_EQUAL,
        T_SL_EQUAL,
        T_SR_EQUAL,
        T_CONCAT_EQUAL,
        T_YIELD,
    ], true)) {
        return;
    }
    if ($this->isUsedAsParameter($phpcsFile, $variablePointer)) {
        return;
    }
    if ($this->isUsedInForLoopCondition($phpcsFile, $variablePointer, $variableName)) {
        return;
    }
    if ($this->isDefinedInDoConditionAndUsedInLoop($phpcsFile, $variablePointer, $variableName)) {
        return;
    }
    if ($this->isUsedInLoopCycle($phpcsFile, $variablePointer, $variableName)) {
        return;
    }
    if ($this->isUsedAsKeyOrValueInArray($phpcsFile, $variablePointer)) {
        return;
    }
    if ($this->isValueInForeachAndErrorIsIgnored($phpcsFile, $variablePointer)) {
        return;
    }
    $scopeOwnerPointer = ScopeHelper::getRootPointer($phpcsFile, $variablePointer - 1);
    foreach (array_reverse($tokens[$variablePointer]['conditions'], true) as $conditionPointer => $conditionTokenCode) {
        if (in_array($conditionTokenCode, TokenHelper::$functionTokenCodes, true)) {
            $scopeOwnerPointer = $conditionPointer;
            break;
        }
    }
    if (in_array($tokens[$scopeOwnerPointer]['code'], TokenHelper::$functionTokenCodes, true)) {
        if ($this->isStaticOrGlobalVariable($phpcsFile, $scopeOwnerPointer, $variableName)) {
            return;
        }
        if ($this->isParameterPassedByReference($phpcsFile, $scopeOwnerPointer, $variableName)) {
            return;
        }
        if ($tokens[$scopeOwnerPointer]['code'] === T_CLOSURE && $this->isInheritedVariablePassedByReference($phpcsFile, $scopeOwnerPointer, $variableName)) {
            return;
        }
    }
    if ($this->isReference($phpcsFile, $scopeOwnerPointer, $variablePointer)) {
        return;
    }
    if (VariableHelper::isUsedInScopeAfterPointer($phpcsFile, $scopeOwnerPointer, $variablePointer, $variablePointer + 1)) {
        return;
    }
    if ($this->isPartOfStatementAndWithIncrementOrDecrementOperator($phpcsFile, $variablePointer)) {
        return;
    }
    $phpcsFile->addError(sprintf('Unused variable %s.', $variableName), $variablePointer, self::CODE_UNUSED_VARIABLE);
}
RSS feed
Powered by Drupal