Skip to content

Commit

Permalink
Enable allowUnusedForeachVariables and allowUnusedCaughtExceptions (#73)
Browse files Browse the repository at this point in the history
* Tests: change allowUnusedForeachVariables to true

* Tests: remove unused foreach vars from test

* Change allowUnusedForeachVariables to true

* Tests: change allowUnusedCaughtExceptions to true

* Change allowUnusedCaughtExceptions to true
  • Loading branch information
sirbrillig authored Feb 15, 2019
1 parent af81d64 commit f72e025
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 16 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,12 @@ There's a variety of options to customize the behaviour of VariableAnalysis, tak
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 `false`): if set to true, caught Exception variables 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.
- `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'`.
- `allowUnusedForeachVariables` (bool, default `false`): if set to true, unused keys or values created by the `as` statement in a `foreach` loop will never be marked as unused.
- `allowUnusedForeachVariables` (bool, default `true`): if set to true, unused keys or values created by the `as` statement in a `foreach` loop will never be marked as unused.
- `sitePassByRefFunctions` (string, default `null`): a list of custom functions which pass in variables to be initialized by reference (eg `preg_match()`) and therefore should not require those variables to be defined ahead of time. The list is space separated and each entry is of the form `functionName:1,2`. The function name comes first followed by a colon and a comma-separated list of argument numbers (starting from 1) which should be considered variable definitions. The special value `...` in the arguments list will cause all arguments after the last number to be considered variable definitions.
- `allowWordPressPassByRefFunctions` (bool, default `false`): if set to true, a list of common WordPress pass-by-reference functions will be added to the list of PHP ones so that passing undefined variables to these functions (to be initialized by reference) will be allowed.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,9 @@ class VariableAnalysisSniff implements Sniff {
public $allowWordPressPassByRefFunctions = false;

/**
* Allows exceptions in a catch block to be unused without provoking unused-var warning.
* Set generic.codeanalysis.variableanalysis.allowUnusedCaughtExceptions to a true value.
* Allow exceptions in a catch block to be unused without warning.
*/
public $allowUnusedCaughtExceptions = false;
public $allowUnusedCaughtExceptions = true;

/**
* Allow function parameters to be unused without provoking unused-var warning.
Expand Down Expand Up @@ -81,7 +80,7 @@ class VariableAnalysisSniff implements Sniff {
* If set to true, unused keys or values created by the `as` statement
* in a `foreach` loop will never be marked as unused.
*/
public $allowUnusedForeachVariables = false;
public $allowUnusedForeachVariables = true;

public function register() {
return [
Expand Down
57 changes: 47 additions & 10 deletions VariableAnalysis/Tests/CodeAnalysis/VariableAnalysisTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,10 +123,6 @@ public function testFunctionWithForeachWarnings() {
22,
24,
26,
48,
50,
52,
54,
// FIXME: this is an unused variable that needs to be fixed but for now
// we will ignore it. See
// https://github.com/sirbrillig/phpcs-variable-analysis/pull/36
Expand Down Expand Up @@ -560,7 +556,6 @@ public function testUnusedParamsAreReported() {
16,
27,
39,
66,
72,
73,
];
Expand All @@ -586,7 +581,6 @@ public function testValidUnusedVariableNamesIgnoresUnusedVariables() {
4,
16,
39,
66,
72,
73,
];
Expand All @@ -608,13 +602,37 @@ public function testAllowUnusedFunctionParametersIgnoresUnusedVariables() {
);
$phpcsFile->process();
$lines = $this->getWarningLineNumbersFromFile($phpcsFile);
$expectedWarnings = [];
$this->assertEquals($expectedWarnings, $lines);
}

public function testAllowUnusedCaughtExceptionsIgnoresUnusedVariablesIfSet() {
$fixtureFile = $this->getFixture('FunctionWithUnusedParamsFixture.php');
$phpcsFile = $this->prepareLocalFileForSniffs($this->getSniffFiles(), $fixtureFile);
$phpcsFile->ruleset->setSniffProperty(
'VariableAnalysis\Sniffs\CodeAnalysis\VariableAnalysisSniff',
'allowUnusedParametersBeforeUsed',
'false'
);
$phpcsFile->ruleset->setSniffProperty(
'VariableAnalysis\Sniffs\CodeAnalysis\VariableAnalysisSniff',
'allowUnusedCaughtExceptions',
'true'
);
$phpcsFile->process();
$lines = $this->getWarningLineNumbersFromFile($phpcsFile);
$expectedWarnings = [
66,
4,
16,
27,
39,
72,
73,
];
$this->assertEquals($expectedWarnings, $lines);
}

public function testAllowUnusedCaughtExceptionsIgnoresUnusedVariables() {
public function testAllowUnusedCaughtExceptionsDoesNotIgnoreUnusedVariablesIfFalse() {
$fixtureFile = $this->getFixture('FunctionWithUnusedParamsFixture.php');
$phpcsFile = $this->prepareLocalFileForSniffs($this->getSniffFiles(), $fixtureFile);
$phpcsFile->ruleset->setSniffProperty(
Expand All @@ -625,7 +643,7 @@ public function testAllowUnusedCaughtExceptionsIgnoresUnusedVariables() {
$phpcsFile->ruleset->setSniffProperty(
'VariableAnalysis\Sniffs\CodeAnalysis\VariableAnalysisSniff',
'allowUnusedCaughtExceptions',
'true'
'false'
);
$phpcsFile->process();
$lines = $this->getWarningLineNumbersFromFile($phpcsFile);
Expand All @@ -634,6 +652,7 @@ public function testAllowUnusedCaughtExceptionsIgnoresUnusedVariables() {
16,
27,
39,
66,
72,
73,
];
Expand Down Expand Up @@ -759,7 +778,25 @@ public function testPregReplaceIgnoresNumericVariables() {
$this->assertEquals($expectedWarnings, $lines);
}

public function testUnusedForeachVariablesAreNotIgnoredByDefault() {
public function testUnusedForeachVariablesAreIgnoredByDefault() {
$fixtureFile = $this->getFixture('UnusedForeachFixture.php');
$phpcsFile = $this->prepareLocalFileForSniffs($this->getSniffFiles(), $fixtureFile);
$phpcsFile->process();
$lines = $this->getWarningLineNumbersFromFile($phpcsFile);
$expectedWarnings = [
7,
8,
16,
17,
25,
26,
33,
34,
];
$this->assertEquals($expectedWarnings, $lines);
}

public function testUnusedForeachVariablesAreNotIgnoredIfDisabled() {
$fixtureFile = $this->getFixture('UnusedForeachFixture.php');
$phpcsFile = $this->prepareLocalFileForSniffs($this->getSniffFiles(), $fixtureFile);
$phpcsFile->ruleset->setSniffProperty(
Expand Down

0 comments on commit f72e025

Please sign in to comment.