From 355c2789a7971dc9adc74854b407e67e56647f10 Mon Sep 17 00:00:00 2001 From: Payton Swick Date: Tue, 14 Jul 2020 16:30:22 -0400 Subject: [PATCH 1/4] Add test for allowUndefinedVariablesInFileScope --- .../VariableAnalysisSniff/GlobalScopeTest.php | 23 +++++++++++++++++++ .../fixtures/GlobalScopeFixture.php | 4 ++++ 2 files changed, 27 insertions(+) diff --git a/Tests/VariableAnalysisSniff/GlobalScopeTest.php b/Tests/VariableAnalysisSniff/GlobalScopeTest.php index eb34b11b..e72d3319 100644 --- a/Tests/VariableAnalysisSniff/GlobalScopeTest.php +++ b/Tests/VariableAnalysisSniff/GlobalScopeTest.php @@ -7,11 +7,34 @@ class GlobalScopeTest extends BaseTestCase { public function testGlobalScopeWarnings() { $fixtureFile = $this->getFixture('GlobalScopeFixture.php'); $phpcsFile = $this->prepareLocalFileForSniffs($fixtureFile); + $phpcsFile->ruleset->setSniffProperty( + 'VariableAnalysis\Sniffs\CodeAnalysis\VariableAnalysisSniff', + 'allowUndefinedVariablesInFileScope', + 'false' + ); $phpcsFile->process(); $lines = $this->getWarningLineNumbersFromFile($phpcsFile); $expectedErrors = [ 4, 7, + 10, + ]; + $this->assertEquals($expectedErrors, $lines); + } + + public function testGlobalScopeWarningsWithAllowUndefinedVariablesInFileScope() { + $fixtureFile = $this->getFixture('GlobalScopeFixture.php'); + $phpcsFile = $this->prepareLocalFileForSniffs($fixtureFile); + $phpcsFile->ruleset->setSniffProperty( + 'VariableAnalysis\Sniffs\CodeAnalysis\VariableAnalysisSniff', + 'allowUndefinedVariablesInFileScope', + 'true' + ); + $phpcsFile->process(); + $lines = $this->getWarningLineNumbersFromFile($phpcsFile); + $expectedErrors = [ + 4, + 10, ]; $this->assertEquals($expectedErrors, $lines); } diff --git a/Tests/VariableAnalysisSniff/fixtures/GlobalScopeFixture.php b/Tests/VariableAnalysisSniff/fixtures/GlobalScopeFixture.php index 98958af0..d9e2f6de 100644 --- a/Tests/VariableAnalysisSniff/fixtures/GlobalScopeFixture.php +++ b/Tests/VariableAnalysisSniff/fixtures/GlobalScopeFixture.php @@ -5,3 +5,7 @@ echo $name; echo $activity; // undefined variable $activity + +function thisIsAFunction() { + echo $whatever; // undefined variable $whatever +} From 6f69fde1c74f66ceb12d84f32ad158e5f596e8d3 Mon Sep 17 00:00:00 2001 From: Payton Swick Date: Tue, 14 Jul 2020 16:30:44 -0400 Subject: [PATCH 2/4] Add descriptions for ScopeInfo properties --- VariableAnalysis/Lib/ScopeInfo.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/VariableAnalysis/Lib/ScopeInfo.php b/VariableAnalysis/Lib/ScopeInfo.php index 04b435bd..1a82f8ef 100644 --- a/VariableAnalysis/Lib/ScopeInfo.php +++ b/VariableAnalysis/Lib/ScopeInfo.php @@ -7,11 +7,15 @@ */ class ScopeInfo { /** + * The token index of the start of this scope. + * * @var int */ public $owner; /** + * The variables defined in this scope. + * * @var VariableInfo[] */ public $variables = []; From 625952f0c17a045c161cd65df77b665b5adea881 Mon Sep 17 00:00:00 2001 From: Payton Swick Date: Tue, 14 Jul 2020 16:31:14 -0400 Subject: [PATCH 3/4] Add allowUndefinedVariablesInFileScope to sniff --- .../Sniffs/CodeAnalysis/VariableAnalysisSniff.php | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/VariableAnalysis/Sniffs/CodeAnalysis/VariableAnalysisSniff.php b/VariableAnalysis/Sniffs/CodeAnalysis/VariableAnalysisSniff.php index c6550703..21e5a757 100644 --- a/VariableAnalysis/Sniffs/CodeAnalysis/VariableAnalysisSniff.php +++ b/VariableAnalysis/Sniffs/CodeAnalysis/VariableAnalysisSniff.php @@ -66,12 +66,19 @@ class VariableAnalysisSniff implements Sniff { /** * Allow function parameters to be unused without provoking unused-var warning. - * Set generic.codeanalysis.variableanalysis.allowUnusedFunctionParameters to a true value. * * @var bool */ public $allowUnusedFunctionParameters = false; + /** + * If set, ignores undefined variables in the file scope (the top-level + * scope of a file). + * + * @var bool + */ + public $allowUndefinedVariablesInFileScope = false; + /** * A space-separated list of names of placeholder variables that you want to * ignore from unused variable warnings. For example, to ignore the variables @@ -316,6 +323,9 @@ protected function getOrCreateVariableInfo($varName, $currScope) { if (isset($this->ignoreUnusedRegexp) && preg_match($this->ignoreUnusedRegexp, $varName) === 1) { $scopeInfo->variables[$varName]->ignoreUnused = true; } + if ($scopeInfo->owner === 0 && $this->allowUndefinedVariablesInFileScope) { + $scopeInfo->variables[$varName]->ignoreUndefined = true; + } if (in_array($varName, $validUndefinedVariableNames)) { $scopeInfo->variables[$varName]->ignoreUndefined = true; } From 0166f4d8e6e64f8cd258910f661a24365c1ec6a9 Mon Sep 17 00:00:00 2001 From: Payton Swick Date: Tue, 14 Jul 2020 16:32:48 -0400 Subject: [PATCH 4/4] Add allowUndefinedVariablesInFileScope to README --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index f209dbdf..71bcf486 100644 --- a/README.md +++ b/README.md @@ -67,6 +67,7 @@ The available options are as follows: - `allowUnusedFunctionParameters` (bool, default `false`): if set to true, function arguments will never be marked as unused. - `allowUnusedCaughtExceptions` (bool, default `true`): if set to true, caught Exception variables will never be marked as unused. - `allowUnusedParametersBeforeUsed` (bool, default `true`): if set to true, unused function arguments will be ignored if they are followed by used function arguments. +- `allowUndefinedVariablesInFileScope` (bool, default `false`): if set to true, undefined variables in the file's top-level scope will never be marked as undefined. - `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'`. - `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 `'/^_/'`. - `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`.