diff --git a/VariableAnalysis/Tests/CodeAnalysis/VariableAnalysisTest.php b/VariableAnalysis/Tests/CodeAnalysis/VariableAnalysisTest.php index b44ea5dc..6166b8bf 100644 --- a/VariableAnalysis/Tests/CodeAnalysis/VariableAnalysisTest.php +++ b/VariableAnalysis/Tests/CodeAnalysis/VariableAnalysisTest.php @@ -902,10 +902,31 @@ public function testGetDefinedVarsCountsAsRead() { $phpcsFile->process(); $lines = $this->getWarningLineNumbersFromFile($phpcsFile); $expectedWarnings = [ - 6, - 18, - 22, - 29, + 6, + 18, + 22, + 29, + ]; + $this->assertEquals($expectedWarnings, $lines); + } + + public function testReassignedVariablesAreWarnings() { + $fixtureFile = $this->getFixture('ReassignedVariableFixture.php'); + $phpcsFile = $this->prepareLocalFileForSniffs($this->getSniffFiles(), $fixtureFile); + $phpcsFile->process(); + $lines = $this->getWarningLineNumbersFromFile($phpcsFile); + $expectedWarnings = [ + 8, + 28, + 36, + 48, + 52, + 75, + 86, + 97, + 106, + 109, + 127, ]; $this->assertEquals($expectedWarnings, $lines); } diff --git a/VariableAnalysis/Tests/CodeAnalysis/fixtures/ReassignedVariableFixture.php b/VariableAnalysis/Tests/CodeAnalysis/fixtures/ReassignedVariableFixture.php new file mode 100644 index 00000000..2e962162 --- /dev/null +++ b/VariableAnalysis/Tests/CodeAnalysis/fixtures/ReassignedVariableFixture.php @@ -0,0 +1,133 @@ +id === $data->id) { + $name = $person->name; + } + $flavor = 'grape'; + $flavor = 'candy'; // Should be a warning + } + echo "The name is {$name}"; + echo $flavor; +} + +function reassignPlain($id) { + $hello = 'world'; + $hello = 'abc'; // Should be a warning + if ($id === 1) { + $hello = 'admin'; + } + echo $hello; +} + +function reassignInIfElse($user) { + $name = 'unknown'; + if ($user === 'admin') { + $name = 'administrator'; + $flavor = 'grape'; + $flavor = 'candy'; // Should be a warning + } else { + $name = 'user'; + $tea = 'green'; + $tea = 'oolong'; // Should be a warning + } + echo $name; + echo $tea; + echo $flavor; +} + +function reassignInIfElseWithoutBrackets($user) { + $name = 'unknown'; + if ($user === 'admin') + $name = 'administrator'; + else + $name = 'user'; + echo $name; +} + +function reassignInIfElseIf($user) { + $name = 'unknown'; + if ($user === 'admin') { + $name = 'administrator'; + } elseif ($user === 'bob') { + $name = 'user'; + $tea = 'green'; + $tea = 'oolong'; // Should be a warning + } + echo $name; + echo $tea; +} + +function reassignInWhile($user) { + $name = 'unknown'; + while ($user->isValid()) { + $name = 'someone'; + $tea = 'green'; + $tea = 'oolong'; // Should be a warning + } + echo $name; + echo $tea; +} + +function reassignInDoWhile($user) { + $name = 'unknown'; + do { + $name = 'someone'; + $tea = 'green'; + $tea = 'oolong'; // Should be a warning + } while ($user->isValid()); + echo $name; + echo $tea; +} + +function reassignInForInit() { + $name = 'unknown'; + $tea = 'green'; + for ($name = 1; $name++; $name < 5) { // Should be a warning + $tea = 'black'; + $flavor = 'grape'; + $flavor = 'candy'; // Should be a warning + echo 'hello'; + } + echo $tea; + echo $flavor; +} + +function reassignInSwitch($user) { + $name = 'unknown'; + switch ($user) { + case 'admin': + $name = 'administrator'; + $flavor = 'sweet'; + break; + case 'other': + $name = 'someone'; + $flavor = 'salty'; + $tea = 'oolong'; + $tea = 'puer'; // Should be a warning + break; + } + echo $name; + echo $tea; + echo $flavor; +}