Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
Expand Down
23 changes: 23 additions & 0 deletions Tests/VariableAnalysisSniff/GlobalScopeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
4 changes: 4 additions & 0 deletions Tests/VariableAnalysisSniff/fixtures/GlobalScopeFixture.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,7 @@

echo $name;
echo $activity; // undefined variable $activity

function thisIsAFunction() {
echo $whatever; // undefined variable $whatever
}
4 changes: 4 additions & 0 deletions VariableAnalysis/Lib/ScopeInfo.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [];
Expand Down
12 changes: 11 additions & 1 deletion VariableAnalysis/Sniffs/CodeAnalysis/VariableAnalysisSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
}
Expand Down