From bd165206e4db14cd7c70d68c736df659040155f7 Mon Sep 17 00:00:00 2001 From: Payton Swick Date: Tue, 15 Sep 2020 16:13:19 -0400 Subject: [PATCH 1/3] Add tests for undefined arrays inside if conditions --- Tests/VariableAnalysisSniff/IfConditionTest.php | 2 ++ .../fixtures/FunctionWithIfConditionFixture.php | 15 +++++++++++++++ 2 files changed, 17 insertions(+) diff --git a/Tests/VariableAnalysisSniff/IfConditionTest.php b/Tests/VariableAnalysisSniff/IfConditionTest.php index 101db9ee..ddb3b4d9 100644 --- a/Tests/VariableAnalysisSniff/IfConditionTest.php +++ b/Tests/VariableAnalysisSniff/IfConditionTest.php @@ -28,6 +28,8 @@ public function testIfConditionWarnings() { 87, 98, 101, + 159, + 161, ]; $this->assertEquals($expectedWarnings, $lines); } diff --git a/Tests/VariableAnalysisSniff/fixtures/FunctionWithIfConditionFixture.php b/Tests/VariableAnalysisSniff/fixtures/FunctionWithIfConditionFixture.php index 0e90f494..efe24e1b 100644 --- a/Tests/VariableAnalysisSniff/fixtures/FunctionWithIfConditionFixture.php +++ b/Tests/VariableAnalysisSniff/fixtures/FunctionWithIfConditionFixture.php @@ -145,3 +145,18 @@ function ifConditionWithPossibleUse($first) { echo $name; } } + +function ifConditionWithArrayAssignment($first) { + $things = []; + if ($first) { + $things[] = 'person'; + } + return $things; +} + +function ifConditionWithUndefinedArrayAssignment($first) { + if ($first) { + $things[] = 'person'; // undefined array variable + } + return $things; // undefined variable +} From 8c71ca334dd29cdc0df33a26a798fd5f07b192e0 Mon Sep 17 00:00:00 2001 From: Payton Swick Date: Tue, 15 Sep 2020 16:23:11 -0400 Subject: [PATCH 2/3] Add test for while loop as well --- Tests/VariableAnalysisSniff/IfConditionTest.php | 6 ++++++ .../fixtures/FunctionWithIfConditionFixture.php | 7 +++++++ .../FunctionWithInlineIfConditionFixture.php | 12 ++++++++++++ 3 files changed, 25 insertions(+) diff --git a/Tests/VariableAnalysisSniff/IfConditionTest.php b/Tests/VariableAnalysisSniff/IfConditionTest.php index ddb3b4d9..6c22a424 100644 --- a/Tests/VariableAnalysisSniff/IfConditionTest.php +++ b/Tests/VariableAnalysisSniff/IfConditionTest.php @@ -30,6 +30,8 @@ public function testIfConditionWarnings() { 101, 159, 161, + 166, + 168, ]; $this->assertEquals($expectedWarnings, $lines); } @@ -58,6 +60,10 @@ public function testInlineIfConditionWarnings() { 77, 86, 88, + 130, + 131, + 136, + 137, ]; $this->assertEquals($expectedWarnings, $lines); } diff --git a/Tests/VariableAnalysisSniff/fixtures/FunctionWithIfConditionFixture.php b/Tests/VariableAnalysisSniff/fixtures/FunctionWithIfConditionFixture.php index efe24e1b..d55c26a4 100644 --- a/Tests/VariableAnalysisSniff/fixtures/FunctionWithIfConditionFixture.php +++ b/Tests/VariableAnalysisSniff/fixtures/FunctionWithIfConditionFixture.php @@ -160,3 +160,10 @@ function ifConditionWithUndefinedArrayAssignment($first) { } return $things; // undefined variable } + +function loopAndPushWithUndefinedArray($parts) { + while ($part = array_shift($parts)) { + $suggestions[] = $part; // undefined array variable + } + return $suggestions; // undefined variable +} diff --git a/Tests/VariableAnalysisSniff/fixtures/FunctionWithInlineIfConditionFixture.php b/Tests/VariableAnalysisSniff/fixtures/FunctionWithInlineIfConditionFixture.php index 3109dea9..fbce6adf 100644 --- a/Tests/VariableAnalysisSniff/fixtures/FunctionWithInlineIfConditionFixture.php +++ b/Tests/VariableAnalysisSniff/fixtures/FunctionWithInlineIfConditionFixture.php @@ -124,3 +124,15 @@ function ifConditionWithPossibleUse($first) { if ($first) echo $name; } + +function ifConditionWithUndefinedArrayAssignment($first) { + if ($first) + $things[] = 'person'; // undefined array variable + return $things; // undefined variable +} + +function loopAndPushWithUndefinedArray($parts) { + while ($part = array_shift($parts)) + $suggestions[] = $part; // undefined array variable + return $suggestions; // undefined variable +} From 661ff6f123aa5c26b17dc820bddd89d5fd09cae8 Mon Sep 17 00:00:00 2001 From: Payton Swick Date: Tue, 15 Sep 2020 16:40:48 -0400 Subject: [PATCH 3/3] 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. --- Tests/VariableAnalysisSniff/IfConditionTest.php | 4 ---- .../fixtures/FunctionWithIfConditionFixture.php | 4 ++-- .../fixtures/FunctionWithInlineIfConditionFixture.php | 4 ++-- 3 files changed, 4 insertions(+), 8 deletions(-) diff --git a/Tests/VariableAnalysisSniff/IfConditionTest.php b/Tests/VariableAnalysisSniff/IfConditionTest.php index 6c22a424..63ac330e 100644 --- a/Tests/VariableAnalysisSniff/IfConditionTest.php +++ b/Tests/VariableAnalysisSniff/IfConditionTest.php @@ -29,9 +29,7 @@ public function testIfConditionWarnings() { 98, 101, 159, - 161, 166, - 168, ]; $this->assertEquals($expectedWarnings, $lines); } @@ -61,9 +59,7 @@ public function testInlineIfConditionWarnings() { 86, 88, 130, - 131, 136, - 137, ]; $this->assertEquals($expectedWarnings, $lines); } diff --git a/Tests/VariableAnalysisSniff/fixtures/FunctionWithIfConditionFixture.php b/Tests/VariableAnalysisSniff/fixtures/FunctionWithIfConditionFixture.php index d55c26a4..eaa9841e 100644 --- a/Tests/VariableAnalysisSniff/fixtures/FunctionWithIfConditionFixture.php +++ b/Tests/VariableAnalysisSniff/fixtures/FunctionWithIfConditionFixture.php @@ -158,12 +158,12 @@ function ifConditionWithUndefinedArrayAssignment($first) { if ($first) { $things[] = 'person'; // undefined array variable } - return $things; // undefined variable + return $things; } function loopAndPushWithUndefinedArray($parts) { while ($part = array_shift($parts)) { $suggestions[] = $part; // undefined array variable } - return $suggestions; // undefined variable + return $suggestions; } diff --git a/Tests/VariableAnalysisSniff/fixtures/FunctionWithInlineIfConditionFixture.php b/Tests/VariableAnalysisSniff/fixtures/FunctionWithInlineIfConditionFixture.php index fbce6adf..aa69874e 100644 --- a/Tests/VariableAnalysisSniff/fixtures/FunctionWithInlineIfConditionFixture.php +++ b/Tests/VariableAnalysisSniff/fixtures/FunctionWithInlineIfConditionFixture.php @@ -128,11 +128,11 @@ function ifConditionWithPossibleUse($first) { function ifConditionWithUndefinedArrayAssignment($first) { if ($first) $things[] = 'person'; // undefined array variable - return $things; // undefined variable + return $things; } function loopAndPushWithUndefinedArray($parts) { while ($part = array_shift($parts)) $suggestions[] = $part; // undefined array variable - return $suggestions; // undefined variable + return $suggestions; }