Skip to content

Commit cc4fc9c

Browse files
authored
Add allowUndefinedVariablesInFileScope option (#193)
* Add test for allowUndefinedVariablesInFileScope * Add descriptions for ScopeInfo properties * Add allowUndefinedVariablesInFileScope to sniff * Add allowUndefinedVariablesInFileScope to README
1 parent b9caea0 commit cc4fc9c

File tree

5 files changed

+43
-1
lines changed

5 files changed

+43
-1
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ The available options are as follows:
6767
- `allowUnusedFunctionParameters` (bool, default `false`): if set to true, function arguments will never be marked as unused.
6868
- `allowUnusedCaughtExceptions` (bool, default `true`): if set to true, caught Exception variables will never be marked as unused.
6969
- `allowUnusedParametersBeforeUsed` (bool, default `true`): if set to true, unused function arguments will be ignored if they are followed by used function arguments.
70+
- `allowUndefinedVariablesInFileScope` (bool, default `false`): if set to true, undefined variables in the file's top-level scope will never be marked as undefined.
7071
- `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'`.
7172
- `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 `'/^_/'`.
7273
- `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: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,34 @@ class GlobalScopeTest extends BaseTestCase {
77
public function testGlobalScopeWarnings() {
88
$fixtureFile = $this->getFixture('GlobalScopeFixture.php');
99
$phpcsFile = $this->prepareLocalFileForSniffs($fixtureFile);
10+
$phpcsFile->ruleset->setSniffProperty(
11+
'VariableAnalysis\Sniffs\CodeAnalysis\VariableAnalysisSniff',
12+
'allowUndefinedVariablesInFileScope',
13+
'false'
14+
);
1015
$phpcsFile->process();
1116
$lines = $this->getWarningLineNumbersFromFile($phpcsFile);
1217
$expectedErrors = [
1318
4,
1419
7,
20+
10,
21+
];
22+
$this->assertEquals($expectedErrors, $lines);
23+
}
24+
25+
public function testGlobalScopeWarningsWithAllowUndefinedVariablesInFileScope() {
26+
$fixtureFile = $this->getFixture('GlobalScopeFixture.php');
27+
$phpcsFile = $this->prepareLocalFileForSniffs($fixtureFile);
28+
$phpcsFile->ruleset->setSniffProperty(
29+
'VariableAnalysis\Sniffs\CodeAnalysis\VariableAnalysisSniff',
30+
'allowUndefinedVariablesInFileScope',
31+
'true'
32+
);
33+
$phpcsFile->process();
34+
$lines = $this->getWarningLineNumbersFromFile($phpcsFile);
35+
$expectedErrors = [
36+
4,
37+
10,
1538
];
1639
$this->assertEquals($expectedErrors, $lines);
1740
}

Tests/VariableAnalysisSniff/fixtures/GlobalScopeFixture.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,7 @@
55

66
echo $name;
77
echo $activity; // undefined variable $activity
8+
9+
function thisIsAFunction() {
10+
echo $whatever; // undefined variable $whatever
11+
}

VariableAnalysis/Lib/ScopeInfo.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,15 @@
77
*/
88
class ScopeInfo {
99
/**
10+
* The token index of the start of this scope.
11+
*
1012
* @var int
1113
*/
1214
public $owner;
1315

1416
/**
17+
* The variables defined in this scope.
18+
*
1519
* @var VariableInfo[]
1620
*/
1721
public $variables = [];

VariableAnalysis/Sniffs/CodeAnalysis/VariableAnalysisSniff.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,12 +66,19 @@ class VariableAnalysisSniff implements Sniff {
6666

6767
/**
6868
* Allow function parameters to be unused without provoking unused-var warning.
69-
* Set generic.codeanalysis.variableanalysis.allowUnusedFunctionParameters to a true value.
7069
*
7170
* @var bool
7271
*/
7372
public $allowUnusedFunctionParameters = false;
7473

74+
/**
75+
* If set, ignores undefined variables in the file scope (the top-level
76+
* scope of a file).
77+
*
78+
* @var bool
79+
*/
80+
public $allowUndefinedVariablesInFileScope = false;
81+
7582
/**
7683
* A space-separated list of names of placeholder variables that you want to
7784
* ignore from unused variable warnings. For example, to ignore the variables
@@ -316,6 +323,9 @@ protected function getOrCreateVariableInfo($varName, $currScope) {
316323
if (isset($this->ignoreUnusedRegexp) && preg_match($this->ignoreUnusedRegexp, $varName) === 1) {
317324
$scopeInfo->variables[$varName]->ignoreUnused = true;
318325
}
326+
if ($scopeInfo->owner === 0 && $this->allowUndefinedVariablesInFileScope) {
327+
$scopeInfo->variables[$varName]->ignoreUndefined = true;
328+
}
319329
if (in_array($varName, $validUndefinedVariableNames)) {
320330
$scopeInfo->variables[$varName]->ignoreUndefined = true;
321331
}

0 commit comments

Comments
 (0)