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

Breadcrumb

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

function VariableAnalysisSniff::processScopeCloseForVariable

* Warn about an unused variable if it has not been used within a scope. * *

Parameters

File $phpcsFile: * @param VariableInfo $varInfo * @param ScopeInfo $scopeInfo * * @return void

2 calls to VariableAnalysisSniff::processScopeCloseForVariable()
VariableAnalysisSniff::processScopeClose in vendor/sirbrillig/phpcs-variable-analysis/VariableAnalysis/Sniffs/CodeAnalysis/VariableAnalysisSniff.php
* Called to process the end of a scope. * * Note that although triggered by the closing curly brace of the scope, * $stackPtr is the scope conditional, not the closing curly brace. * *
VariableAnalysisSniff::processVariableAsAssignment in vendor/sirbrillig/phpcs-variable-analysis/VariableAnalysis/Sniffs/CodeAnalysis/VariableAnalysisSniff.php
* Process a variable that is being assigned. * * This will record that the variable has been defined within a scope so that * later we can determine if it it unused and we can guarantee that any * future uses of the variable are not using an…

File

vendor/sirbrillig/phpcs-variable-analysis/VariableAnalysis/Sniffs/CodeAnalysis/VariableAnalysisSniff.php, line 1927

Class

VariableAnalysisSniff

Namespace

VariableAnalysis\Sniffs\CodeAnalysis

Code

protected function processScopeCloseForVariable(File $phpcsFile, VariableInfo $varInfo, ScopeInfo $scopeInfo) {
    Helpers::debug('processScopeCloseForVariable', $varInfo);
    if ($varInfo->ignoreUnused || isset($varInfo->firstRead)) {
        return;
    }
    if ($this->allowUnusedFunctionParameters && $varInfo->scopeType === ScopeType::PARAM) {
        return;
    }
    if ($this->allowUnusedParametersBeforeUsed && $varInfo->scopeType === ScopeType::PARAM && Helpers::areFollowingArgumentsUsed($varInfo, $scopeInfo)) {
        Helpers::debug("variable '{$varInfo->name}' at end of scope has unused following args");
        return;
    }
    if ($this->allowUnusedForeachVariables && $varInfo->isForeachLoopAssociativeValue) {
        return;
    }
    if ($varInfo->referencedVariableScope !== null && isset($varInfo->firstInitialized)) {
        Helpers::debug("variable '{$varInfo->name}' at end of scope is a reference and so counts as used");
        // If we're pass-by-reference then it's a common pattern to
        // use the variable to return data to the caller, so any
        // assignment also counts as "variable use" for the purposes
        // of "unused variable" warnings.
        return;
    }
    if ($varInfo->scopeType === ScopeType::GLOBALSCOPE && isset($varInfo->firstInitialized)) {
        Helpers::debug("variable '{$varInfo->name}' at end of scope is a global and so counts as used");
        // If we imported this variable from the global scope, any further use of
        // the variable, including assignment, should count as "variable use" for
        // the purposes of "unused variable" warnings.
        return;
    }
    if (empty($varInfo->firstDeclared) && empty($varInfo->firstInitialized)) {
        return;
    }
    if ($this->allowUnusedVariablesBeforeRequire && Helpers::isRequireInScopeAfter($phpcsFile, $varInfo, $scopeInfo)) {
        return;
    }
    if ($scopeInfo->scopeStartIndex === 0 && $this->allowUnusedVariablesInFileScope) {
        return;
    }
    if (!empty($varInfo->firstDeclared) && $varInfo->scopeType === ScopeType::PARAM && Helpers::isInAbstractClass($phpcsFile, Helpers::getFunctionIndexForFunctionParameter($phpcsFile, $varInfo->firstDeclared) ?: 0) && Helpers::isFunctionBodyEmpty($phpcsFile, Helpers::getFunctionIndexForFunctionParameter($phpcsFile, $varInfo->firstDeclared) ?: 0)) {
        // Allow non-abstract methods inside an abstract class to have unused
        // parameters if the method body does nothing. Such methods are
        // effectively optional abstract methods so their unused parameters
        // should be ignored as we do with abstract method parameters.
        return;
    }
    $this->warnAboutUnusedVariable($phpcsFile, $varInfo);
}

API Navigation

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