Skip to content

Commit f72e025

Browse files
authored
Enable allowUnusedForeachVariables and allowUnusedCaughtExceptions (#73)
* 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
1 parent af81d64 commit f72e025

File tree

3 files changed

+52
-16
lines changed

3 files changed

+52
-16
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,12 +63,12 @@ There's a variety of options to customize the behaviour of VariableAnalysis, tak
6363
The available options are as follows:
6464

6565
- `allowUnusedFunctionParameters` (bool, default `false`): if set to true, function arguments will never be marked as unused.
66-
- `allowUnusedCaughtExceptions` (bool, default `false`): if set to true, caught Exception variables will never be marked as unused.
66+
- `allowUnusedCaughtExceptions` (bool, default `true`): if set to true, caught Exception variables will never be marked as unused.
6767
- `allowUnusedParametersBeforeUsed` (bool, default `true`): if set to true, unused function arguments will be ignored if they are followed by used function arguments.
6868
- `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'`.
6969
- `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 `'/^_/'`.
7070
- `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'`.
71-
- `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.
71+
- `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.
7272
- `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.
7373
- `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.
7474

VariableAnalysis/Sniffs/CodeAnalysis/VariableAnalysisSniff.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,9 @@ class VariableAnalysisSniff implements Sniff {
3939
public $allowWordPressPassByRefFunctions = false;
4040

4141
/**
42-
* Allows exceptions in a catch block to be unused without provoking unused-var warning.
43-
* Set generic.codeanalysis.variableanalysis.allowUnusedCaughtExceptions to a true value.
42+
* Allow exceptions in a catch block to be unused without warning.
4443
*/
45-
public $allowUnusedCaughtExceptions = false;
44+
public $allowUnusedCaughtExceptions = true;
4645

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

8685
public function register() {
8786
return [

VariableAnalysis/Tests/CodeAnalysis/VariableAnalysisTest.php

Lines changed: 47 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -123,10 +123,6 @@ public function testFunctionWithForeachWarnings() {
123123
22,
124124
24,
125125
26,
126-
48,
127-
50,
128-
52,
129-
54,
130126
// FIXME: this is an unused variable that needs to be fixed but for now
131127
// we will ignore it. See
132128
// https://github.com/sirbrillig/phpcs-variable-analysis/pull/36
@@ -560,7 +556,6 @@ public function testUnusedParamsAreReported() {
560556
16,
561557
27,
562558
39,
563-
66,
564559
72,
565560
73,
566561
];
@@ -586,7 +581,6 @@ public function testValidUnusedVariableNamesIgnoresUnusedVariables() {
586581
4,
587582
16,
588583
39,
589-
66,
590584
72,
591585
73,
592586
];
@@ -608,13 +602,37 @@ public function testAllowUnusedFunctionParametersIgnoresUnusedVariables() {
608602
);
609603
$phpcsFile->process();
610604
$lines = $this->getWarningLineNumbersFromFile($phpcsFile);
605+
$expectedWarnings = [];
606+
$this->assertEquals($expectedWarnings, $lines);
607+
}
608+
609+
public function testAllowUnusedCaughtExceptionsIgnoresUnusedVariablesIfSet() {
610+
$fixtureFile = $this->getFixture('FunctionWithUnusedParamsFixture.php');
611+
$phpcsFile = $this->prepareLocalFileForSniffs($this->getSniffFiles(), $fixtureFile);
612+
$phpcsFile->ruleset->setSniffProperty(
613+
'VariableAnalysis\Sniffs\CodeAnalysis\VariableAnalysisSniff',
614+
'allowUnusedParametersBeforeUsed',
615+
'false'
616+
);
617+
$phpcsFile->ruleset->setSniffProperty(
618+
'VariableAnalysis\Sniffs\CodeAnalysis\VariableAnalysisSniff',
619+
'allowUnusedCaughtExceptions',
620+
'true'
621+
);
622+
$phpcsFile->process();
623+
$lines = $this->getWarningLineNumbersFromFile($phpcsFile);
611624
$expectedWarnings = [
612-
66,
625+
4,
626+
16,
627+
27,
628+
39,
629+
72,
630+
73,
613631
];
614632
$this->assertEquals($expectedWarnings, $lines);
615633
}
616634

617-
public function testAllowUnusedCaughtExceptionsIgnoresUnusedVariables() {
635+
public function testAllowUnusedCaughtExceptionsDoesNotIgnoreUnusedVariablesIfFalse() {
618636
$fixtureFile = $this->getFixture('FunctionWithUnusedParamsFixture.php');
619637
$phpcsFile = $this->prepareLocalFileForSniffs($this->getSniffFiles(), $fixtureFile);
620638
$phpcsFile->ruleset->setSniffProperty(
@@ -625,7 +643,7 @@ public function testAllowUnusedCaughtExceptionsIgnoresUnusedVariables() {
625643
$phpcsFile->ruleset->setSniffProperty(
626644
'VariableAnalysis\Sniffs\CodeAnalysis\VariableAnalysisSniff',
627645
'allowUnusedCaughtExceptions',
628-
'true'
646+
'false'
629647
);
630648
$phpcsFile->process();
631649
$lines = $this->getWarningLineNumbersFromFile($phpcsFile);
@@ -634,6 +652,7 @@ public function testAllowUnusedCaughtExceptionsIgnoresUnusedVariables() {
634652
16,
635653
27,
636654
39,
655+
66,
637656
72,
638657
73,
639658
];
@@ -759,7 +778,25 @@ public function testPregReplaceIgnoresNumericVariables() {
759778
$this->assertEquals($expectedWarnings, $lines);
760779
}
761780

762-
public function testUnusedForeachVariablesAreNotIgnoredByDefault() {
781+
public function testUnusedForeachVariablesAreIgnoredByDefault() {
782+
$fixtureFile = $this->getFixture('UnusedForeachFixture.php');
783+
$phpcsFile = $this->prepareLocalFileForSniffs($this->getSniffFiles(), $fixtureFile);
784+
$phpcsFile->process();
785+
$lines = $this->getWarningLineNumbersFromFile($phpcsFile);
786+
$expectedWarnings = [
787+
7,
788+
8,
789+
16,
790+
17,
791+
25,
792+
26,
793+
33,
794+
34,
795+
];
796+
$this->assertEquals($expectedWarnings, $lines);
797+
}
798+
799+
public function testUnusedForeachVariablesAreNotIgnoredIfDisabled() {
763800
$fixtureFile = $this->getFixture('UnusedForeachFixture.php');
764801
$phpcsFile = $this->prepareLocalFileForSniffs($this->getSniffFiles(), $fixtureFile);
765802
$phpcsFile->ruleset->setSniffProperty(

0 commit comments

Comments
 (0)