-
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
Mixing concatenating assignment operator and assigment operation results in false positive. #183
Comments
Similarly $entry is reported as unused. foreach ($this->derivatives as &$entry) {
$entry += $base_plugin_definition;
}
return $this->derivatives; |
@alexpott Re the second example: IMO this is correct. |
That type of code is enough common, with latest PHP versions. It would be nice if the code would not be reported. |
@jrfnl thanks for looking at this issue. Re the second example - it's a tricky one because I think |
Thanks for the report! This might be worth reporting as two separate issues unless you see a connection that I'm missing (which is very possible). I haven't dug into it yet but I'm guessing that in the first case the array assignment (push) is masking the assignment to the single scalar. In the second case, there's probably a bug with how assignment by reference is being detected? Honestly it's hard for me to grok what's happening there. That code loops through an array of strings and appends another string to each one, modifying the original array items? EDIT: ah, yes, I just read your last comment. |
Another interesting bug I found when researching this: function concatAndAssignWithUndefinedShift() {
$suggestions = [];
while ($part = array_shift($parts)) { // undefined variable parts
$suggestions[] = $part;
}
return $suggestions;
}
|
Ok, I know why this is happening. In this example, function assignFunction($parts) {
$suggestion = 'block';
$suggestion .= '__';
return $parts;
} The reason is that even though function assignFunction() {
$suggestion = 'block';
$suggestion .= '__';
return $suggestion;
} In the case of your first example, the expression function causesBug() {
$suggestions = [];
$suggestion = 'block'; // marked incorrectly as unused
$suggestions[] = $suggestion .= '__'; // suggestion is marked as a write
return $suggestions;
}
function noBug() {
$suggestions = [];
$suggestion = 'block';
$suggestions[] = $suggestion; // suggestion is marked as a read
return $suggestions;
} |
I think a way to resolve this will be: when marking a variable as a write, also look to see if it's on the right-hand-side of an assignment or within a function call (essentially, if we're part of another expression). If so, also mark that variable as a read. The difficult thing will be how to determine if we're part of another expression. For the above example, it is immediately preceded by an equals sign, but it could also be |
Given the following code
$suggestion will be marked as unused.
The text was updated successfully, but these errors were encountered: