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
Namespace
VariableAnalysis\Sniffs\CodeAnalysisCode
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);
}