Skip to content

Commit d8135e3

Browse files
committed
Move areFollowingArgumentsUsed to Helpers
1 parent 2a535e8 commit d8135e3

File tree

2 files changed

+38
-27
lines changed

2 files changed

+38
-27
lines changed

VariableAnalysis/Lib/Helpers.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
namespace VariableAnalysis\Lib;
44

55
use PHP_CodeSniffer\Files\File;
6+
use VariableAnalysis\Lib\ScopeInfo;
7+
use VariableAnalysis\Lib\VariableInfo;
68
use PHP_CodeSniffer\Util\Tokens;
79

810
class Helpers {
@@ -341,4 +343,30 @@ public static function splitStringToArray($pattern, $value) {
341343
$result = preg_split($pattern, $value);
342344
return is_array($result) ? $result : [];
343345
}
346+
347+
/**
348+
* @param VariableInfo $varInfo
349+
* @param ScopeInfo $scopeInfo
350+
*
351+
* @return bool
352+
*/
353+
public static function areFollowingArgumentsUsed(VariableInfo $varInfo, ScopeInfo $scopeInfo) {
354+
$foundVarPosition = false;
355+
foreach ($scopeInfo->variables as $variable) {
356+
if ($variable === $varInfo) {
357+
$foundVarPosition = true;
358+
continue;
359+
}
360+
if (! $foundVarPosition) {
361+
continue;
362+
}
363+
if ($variable->scopeType !== 'param') {
364+
continue;
365+
}
366+
if ($variable->firstRead) {
367+
return true;
368+
}
369+
}
370+
return false;
371+
}
344372
}

VariableAnalysis/Sniffs/CodeAnalysis/VariableAnalysisSniff.php

Lines changed: 10 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,15 @@ class VariableAnalysisSniff implements Sniff {
112112
*/
113113
public $allowUnusedForeachVariables = true;
114114

115+
/**
116+
* If set to true, unused variables in a function before a require or import
117+
* statement will not be marked as unused because they may be used in the
118+
* required file.
119+
*
120+
* @var bool
121+
*/
122+
public $allowUnusedVariablesBeforeRequire = false;
123+
115124
/**
116125
* @return (int|string)[]
117126
*/
@@ -277,32 +286,6 @@ protected function getOrCreateVariableInfo($varName, $currScope) {
277286
return $scopeInfo->variables[$varName];
278287
}
279288

280-
/**
281-
* @param VariableInfo $varInfo
282-
* @param ScopeInfo $scopeInfo
283-
*
284-
* @return bool
285-
*/
286-
protected function areFollowingArgumentsUsed($varInfo, $scopeInfo) {
287-
$foundVarPosition = false;
288-
foreach ($scopeInfo->variables as $variable) {
289-
if ($variable === $varInfo) {
290-
$foundVarPosition = true;
291-
continue;
292-
}
293-
if (! $foundVarPosition) {
294-
continue;
295-
}
296-
if ($variable->scopeType !== 'param') {
297-
continue;
298-
}
299-
if ($variable->firstRead) {
300-
return true;
301-
}
302-
}
303-
return false;
304-
}
305-
306289
/**
307290
* @param string $varName
308291
* @param int $stackPtr
@@ -1408,7 +1391,7 @@ protected function processScopeCloseForVariable(File $phpcsFile, VariableInfo $v
14081391
if ($this->allowUnusedFunctionParameters && $varInfo->scopeType === 'param') {
14091392
return;
14101393
}
1411-
if ($this->allowUnusedParametersBeforeUsed && $varInfo->scopeType === 'param' && $this->areFollowingArgumentsUsed($varInfo, $scopeInfo)) {
1394+
if ($this->allowUnusedParametersBeforeUsed && $varInfo->scopeType === 'param' && Helpers::areFollowingArgumentsUsed($varInfo, $scopeInfo)) {
14121395
Helpers::debug("variable {$varInfo->name} at end of scope has unused following args");
14131396
return;
14141397
}

0 commit comments

Comments
 (0)