Skip to content

Commit

Permalink
Implement areFollowingArgumentsUsed
Browse files Browse the repository at this point in the history
  • Loading branch information
sirbrillig committed Dec 21, 2018
1 parent 558343a commit 067a149
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
1 change: 1 addition & 0 deletions VariableAnalysis/Lib/Helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace VariableAnalysis\Lib;

use PHP_CodeSniffer\Files\File;
use VariableAnalysis\Lib\VariableInfo;

class Helpers {
public static function findContainingOpeningSquareBracket(File $phpcsFile, $stackPtr) {
Expand Down
24 changes: 22 additions & 2 deletions VariableAnalysis/Sniffs/CodeAnalysis/VariableAnalysisSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,23 @@ protected function getOrCreateVariableInfo($varName, $currScope) {
return $scopeInfo->variables[$varName];
}

protected function areFollowingArgumentsUsed($varInfo, $scopeInfo) {
$foundVarPosition = false;
foreach ($scopeInfo->variables as $variable) {
if ($variable === $varInfo) {
$foundVarPosition = true;
continue;
}
if (! $foundVarPosition) {
continue;
}
if ($variable->firstRead) {
return true;
}
}
return false;
}

protected function markVariableAssignment($varName, $stackPtr, $currScope) {
$varInfo = $this->getOrCreateVariableInfo($varName, $currScope);
if (!isset($varInfo->scopeType)) {
Expand Down Expand Up @@ -926,17 +943,20 @@ protected function processScopeClose(File $phpcsFile, $stackPtr) {
return;
}
foreach ($scopeInfo->variables as $varInfo) {
$this->processScopeCloseForVariable($phpcsFile, $varInfo);
$this->processScopeCloseForVariable($phpcsFile, $varInfo, $scopeInfo);
}
}

protected function processScopeCloseForVariable($phpcsFile, $varInfo) {
protected function processScopeCloseForVariable($phpcsFile, $varInfo, $scopeInfo) {
if ($varInfo->ignoreUnused || isset($varInfo->firstRead)) {
return;
}
if ($this->allowUnusedFunctionParameters && $varInfo->scopeType === 'param') {
return;
}
if ($this->ignoreUnusedArgsBeforeUsed && $varInfo->scopeType === 'param' && $this->areFollowingArgumentsUsed($varInfo, $scopeInfo)) {
return;
}
if ($varInfo->passByReference && isset($varInfo->firstInitialized)) {
// If we're pass-by-reference then it's a common pattern to
// use the variable to return data to the caller, so any
Expand Down

0 comments on commit 067a149

Please sign in to comment.