Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add reassigned variable warnings #102

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 25 additions & 4 deletions VariableAnalysis/Tests/CodeAnalysis/VariableAnalysisTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
<?php

function reassignInIf($user) {
$name = 'unknown';
if ($user === 'admin') {
$name = 'administrator';
$flavor = 'grape';
$flavor = 'candy'; // Should be a warning
}
echo $name;
echo $flavor;
}

function reassignInIfWithoutBrackets($user) {
$name = 'unknown';
if ($user === 'admin')
$name = 'administrator';
echo $name;
}

function reassignInForEach($people, $data) {
$name = 'unknown';
foreach ($people as $person) {
if ($person->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;
}