Skip to content

Commit c4fb5ee

Browse files
authored
Add tests for if/loop condition undefined array (#211)
* Add tests for undefined arrays inside if conditions * Add test for while loop as well * Correct tests to allow undefined after array assign This actually matches the current behavior for non-array variables. It's sometimes wrong, but further determination would require runtime checking or static analysis.
1 parent a07fe65 commit c4fb5ee

File tree

3 files changed

+38
-0
lines changed

3 files changed

+38
-0
lines changed

Tests/VariableAnalysisSniff/IfConditionTest.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ public function testIfConditionWarnings() {
2828
87,
2929
98,
3030
101,
31+
159,
32+
166,
3133
];
3234
$this->assertEquals($expectedWarnings, $lines);
3335
}
@@ -56,6 +58,8 @@ public function testInlineIfConditionWarnings() {
5658
77,
5759
86,
5860
88,
61+
130,
62+
136,
5963
];
6064
$this->assertEquals($expectedWarnings, $lines);
6165
}

Tests/VariableAnalysisSniff/fixtures/FunctionWithIfConditionFixture.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,3 +145,25 @@ function ifConditionWithPossibleUse($first) {
145145
echo $name;
146146
}
147147
}
148+
149+
function ifConditionWithArrayAssignment($first) {
150+
$things = [];
151+
if ($first) {
152+
$things[] = 'person';
153+
}
154+
return $things;
155+
}
156+
157+
function ifConditionWithUndefinedArrayAssignment($first) {
158+
if ($first) {
159+
$things[] = 'person'; // undefined array variable
160+
}
161+
return $things;
162+
}
163+
164+
function loopAndPushWithUndefinedArray($parts) {
165+
while ($part = array_shift($parts)) {
166+
$suggestions[] = $part; // undefined array variable
167+
}
168+
return $suggestions;
169+
}

Tests/VariableAnalysisSniff/fixtures/FunctionWithInlineIfConditionFixture.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,3 +124,15 @@ function ifConditionWithPossibleUse($first) {
124124
if ($first)
125125
echo $name;
126126
}
127+
128+
function ifConditionWithUndefinedArrayAssignment($first) {
129+
if ($first)
130+
$things[] = 'person'; // undefined array variable
131+
return $things;
132+
}
133+
134+
function loopAndPushWithUndefinedArray($parts) {
135+
while ($part = array_shift($parts))
136+
$suggestions[] = $part; // undefined array variable
137+
return $suggestions;
138+
}

0 commit comments

Comments
 (0)