Skip to content

Commit 3fad284

Browse files
authored
Respect ignoring undefined variables in else blocks (#239)
* Add test for validUndefinedVariableNames in else block * Respect ignoreUndefined in processVaribleInsideElse
1 parent 9df9ade commit 3fad284

File tree

4 files changed

+94
-2
lines changed

4 files changed

+94
-2
lines changed

Tests/VariableAnalysisSniff/IfConditionTest.php

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,39 @@ public function testIfConditionWarnings() {
3030
101,
3131
159,
3232
166,
33+
176,
34+
179,
35+
];
36+
$this->assertEquals($expectedWarnings, $lines);
37+
}
38+
39+
public function testIfConditionWarningsWithValidUndefinedVariableNames() {
40+
$fixtureFile = $this->getFixture('FunctionWithIfConditionFixture.php');
41+
$phpcsFile = $this->prepareLocalFileForSniffs($fixtureFile);
42+
$phpcsFile->ruleset->setSniffProperty(
43+
'VariableAnalysis\Sniffs\CodeAnalysis\VariableAnalysisSniff',
44+
'validUndefinedVariableNames',
45+
'second'
46+
);
47+
$phpcsFile->ruleset->setSniffProperty(
48+
'VariableAnalysis\Sniffs\CodeAnalysis\VariableAnalysisSniff',
49+
'allowUnusedParametersBeforeUsed',
50+
'true'
51+
);
52+
$phpcsFile->process();
53+
$lines = $this->getWarningLineNumbersFromFile($phpcsFile);
54+
$expectedWarnings = [
55+
15,
56+
36,
57+
47,
58+
58,
59+
70,
60+
82,
61+
98,
62+
159,
63+
166,
64+
176,
65+
179,
3366
];
3467
$this->assertEquals($expectedWarnings, $lines);
3568
}
@@ -60,6 +93,39 @@ public function testInlineIfConditionWarnings() {
6093
88,
6194
130,
6295
136,
96+
152,
97+
154,
98+
];
99+
$this->assertEquals($expectedWarnings, $lines);
100+
}
101+
102+
public function testInlineIfConditionWarningsWithValidUndefinedVariableNames() {
103+
$fixtureFile = $this->getFixture('FunctionWithInlineIfConditionFixture.php');
104+
$phpcsFile = $this->prepareLocalFileForSniffs($fixtureFile);
105+
$phpcsFile->ruleset->setSniffProperty(
106+
'VariableAnalysis\Sniffs\CodeAnalysis\VariableAnalysisSniff',
107+
'validUndefinedVariableNames',
108+
'second'
109+
);
110+
$phpcsFile->ruleset->setSniffProperty(
111+
'VariableAnalysis\Sniffs\CodeAnalysis\VariableAnalysisSniff',
112+
'allowUnusedParametersBeforeUsed',
113+
'true'
114+
);
115+
$phpcsFile->process();
116+
$lines = $this->getWarningLineNumbersFromFile($phpcsFile);
117+
$expectedWarnings = [
118+
14,
119+
34,
120+
44,
121+
54,
122+
64,
123+
74,
124+
86,
125+
130,
126+
136,
127+
152,
128+
154,
63129
];
64130
$this->assertEquals($expectedWarnings, $lines);
65131
}

Tests/VariableAnalysisSniff/fixtures/FunctionWithIfConditionFixture.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,3 +167,16 @@ function loopAndPushWithUndefinedArray($parts) {
167167
}
168168
return $suggestions;
169169
}
170+
171+
function definedInsideElseIfBlockUndefinedInsideElseBlockDifferentName($first) {
172+
$name = 'human';
173+
if ($first) {
174+
$words = "hello {$name}";
175+
} elseif ($name) {
176+
$third = true; // unused variable $third
177+
} else {
178+
$words = "bye {$name}";
179+
echo $third; // undefined variable $third
180+
}
181+
echo $words;
182+
}

Tests/VariableAnalysisSniff/fixtures/FunctionWithInlineIfConditionFixture.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,3 +143,14 @@ function ifElseConditionWithInlineAssignAndUseInsideElse() {
143143
else
144144
echo $q;
145145
}
146+
147+
function definedInsideElseIfBlockUndefinedInsideElseBlockDifferentName($first) {
148+
$name = 'human';
149+
if ($first)
150+
$words = "hello {$name}";
151+
elseif ($name)
152+
$third = true; // unused variable $third
153+
else
154+
echo $third; // undefined variable $third
155+
echo $words;
156+
}

VariableAnalysis/Sniffs/CodeAnalysis/VariableAnalysisSniff.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1518,8 +1518,10 @@ protected function processVaribleInsideElse(File $phpcsFile, $stackPtr, $varName
15181518
}
15191519

15201520
if (count($assignmentsInsideAttachedBlocks) === count($allAssignmentIndices)) {
1521-
Helpers::debug("variable $varName inside else looks undefined");
1522-
$this->warnAboutUndefinedVariable($phpcsFile, $varName, $stackPtr);
1521+
if (! $varInfo->ignoreUndefined) {
1522+
Helpers::debug("variable $varName inside else looks undefined");
1523+
$this->warnAboutUndefinedVariable($phpcsFile, $varName, $stackPtr);
1524+
}
15231525
return;
15241526
}
15251527

0 commit comments

Comments
 (0)