Skip to content

Commit 0991ceb

Browse files
authored
Add allowUnusedVariablesInFileScope option (#229)
* Add test for allowUnusedVariablesInFileScope * Add allowUnusedVariablesInFileScope option * Add allowUnusedVariablesInFileScope to README
1 parent 0775e0c commit 0991ceb

File tree

4 files changed

+39
-1
lines changed

4 files changed

+39
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,8 @@ The available options are as follows:
6565
- `allowUnusedCaughtExceptions` (bool, default `true`): if set to true, caught Exception variables will never be marked as unused.
6666
- `allowUnusedParametersBeforeUsed` (bool, default `true`): if set to true, unused function arguments will be ignored if they are followed by used function arguments.
6767
- `allowUnusedVariablesBeforeRequire` (bool, default `false`): if set to true, variables defined before a `require`, `require_once`, `include`, or `include_once` will not be marked as unused. They may be intended for the required file.
68-
- `allowUndefinedVariablesInFileScope` (bool, default `false`): if set to true, undefined variables in the file's top-level scope will never be marked as undefined.
68+
- `allowUndefinedVariablesInFileScope` (bool, default `false`): if set to true, undefined variables in the file's top-level scope will never be marked as undefined. This can be useful for template files which use many global variables defined elsewhere.
69+
- `allowUnusedVariablesInFileScope` (bool, default `false`): if set to true, unused variables in the file's top-level scope will never be marked as unused. This can be helpful when defining a lot of global variables to be used elsewhere.
6970
- `validUnusedVariableNames` (string, default `null`): a space-separated list of names of placeholder variables that you want to ignore from unused variable warnings. For example, to ignore the variables `$junk` and `$unused`, this could be set to `'junk unused'`.
7071
- `ignoreUnusedRegexp` (string, default `null`): a PHP regexp string (note that this requires explicit delimiters) for variables that you want to ignore from unused variable warnings. For example, to ignore the variables `$_junk` and `$_unused`, this could be set to `'/^_/'`.
7172
- `validUndefinedVariableNames` (string, default `null`): a space-separated list of names of placeholder variables that you want to ignore from undefined variable warnings. For example, to ignore the variables `$post` and `$undefined`, this could be set to `'post undefined'`. This can be used in combination with `validUndefinedVariableRegexp`.

Tests/VariableAnalysisSniff/GlobalScopeTest.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ public function testGlobalScopeWarnings() {
1818
4,
1919
7,
2020
10,
21+
13,
2122
];
2223
$this->assertEquals($expectedErrors, $lines);
2324
}
@@ -35,6 +36,24 @@ public function testGlobalScopeWarningsWithAllowUndefinedVariablesInFileScope()
3536
$expectedErrors = [
3637
4,
3738
10,
39+
13,
40+
];
41+
$this->assertEquals($expectedErrors, $lines);
42+
}
43+
44+
public function testGlobalScopeWarningsWithAllowUnusedVariablesInFileScope() {
45+
$fixtureFile = $this->getFixture('GlobalScopeFixture.php');
46+
$phpcsFile = $this->prepareLocalFileForSniffs($fixtureFile);
47+
$phpcsFile->ruleset->setSniffProperty(
48+
'VariableAnalysis\Sniffs\CodeAnalysis\VariableAnalysisSniff',
49+
'allowUnusedVariablesInFileScope',
50+
'true'
51+
);
52+
$phpcsFile->process();
53+
$lines = $this->getWarningLineNumbersFromFile($phpcsFile);
54+
$expectedErrors = [
55+
7,
56+
10,
3857
];
3958
$this->assertEquals($expectedErrors, $lines);
4059
}

Tests/VariableAnalysisSniff/fixtures/GlobalScopeFixture.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,10 @@
99
function thisIsAFunction() {
1010
echo $whatever; // undefined variable $whatever
1111
}
12+
13+
$color = 'blue'; // used, but only by a global declaration
14+
15+
function anotherFunction() {
16+
global $color;
17+
echo $color;
18+
}

VariableAnalysis/Sniffs/CodeAnalysis/VariableAnalysisSniff.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,14 @@ class VariableAnalysisSniff implements Sniff {
8888
*/
8989
public $allowUndefinedVariablesInFileScope = false;
9090

91+
/**
92+
* If set, ignores unused variables in the file scope (the top-level
93+
* scope of a file).
94+
*
95+
* @var bool
96+
*/
97+
public $allowUnusedVariablesInFileScope = false;
98+
9199
/**
92100
* A space-separated list of names of placeholder variables that you want to
93101
* ignore from unused variable warnings. For example, to ignore the variables
@@ -1687,6 +1695,9 @@ protected function processScopeCloseForVariable(File $phpcsFile, VariableInfo $v
16871695
if ($this->allowUnusedVariablesBeforeRequire && Helpers::isRequireInScopeAfter($phpcsFile, $varInfo, $scopeInfo)) {
16881696
return;
16891697
}
1698+
if ($scopeInfo->scopeStartIndex === 0 && $this->allowUnusedVariablesInFileScope) {
1699+
return;
1700+
}
16901701

16911702
$this->warnAboutUnusedVariable($phpcsFile, $varInfo);
16921703
}

0 commit comments

Comments
 (0)