-
Notifications
You must be signed in to change notification settings - Fork 15
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 warning #101
Comments
Hm... this is an interesting issue. I'd love to be able to catch things like this, but it's tricky. I guess this would be: if a variable is assigned, then assigned again before being used, show a warning. I wonder if there are legitimate cases where that would be a problem, though? What about this example? $name = 'unknown';
foreach ($people as $person) {
if ($person->id === $data->id) {
$name = $person->name;
}
}
echo "The name is {$name}"; That would cause a warning for While that example could be refactored to a single |
I actually just caught this since we moved to PHPStorm which also highlights these cases. Your foreach example is totally highlighting the issue - thus I would suggest to limit the scope in foreach/while/for to the loop itself, to avoid false positives. |
I thought of another one: $name = 'unknown';
if ($user === 'admin') {
$name = 'administrator';
}
echo $name; So: within the current scope or within the current Pretty complex logic. I'll see if I can write up some test cases. |
Oh, also, I suppose we do want to enter the statements themselves, just not their blocks: $name = 'unknown';
for ($name = 1; $name++; $name < 5) { // Should report a warning
echo 'hello';
} |
I added a draft with tests in #102 but it may take a while to work out the logic necessary for those tests to pass. Help is welcome! |
Just checked & it looks good. If you could merge it, I would run it through our whole code base to check for any false positives, but afaik there isn't anything that you haven't catered to in the tests I can think of either. |
hey, any update on this? would be great if it could be released |
Thank you for checking in. As I mentioned above, all I've added are (failing) tests. Actually writing the code to do this would be pretty complex and I haven't had time to work on it lately. If you'd like to give it a try, you'd be more than welcome. The tests in that PR can be used as a way to track your progress. |
If there is code like:
the first $hello declaration is not reported as "unused" even though it should, since it isn't used & this line is useless.
This case often happens when refactoring (of course in a more complicated way, with more code between the lines)
The text was updated successfully, but these errors were encountered: