-
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
array_walk with pass-by-reference causes false positive unused variable #215
Comments
This is supposed to be handled by the code here: phpcs-variable-analysis/VariableAnalysis/Sniffs/CodeAnalysis/VariableAnalysisSniff.php Lines 1675 to 1680 in 29b6bb4
but in this case, phpcs-variable-analysis/VariableAnalysis/Sniffs/CodeAnalysis/VariableAnalysisSniff.php Lines 620 to 625 in 29b6bb4
|
Actually, it looks like the variable is correctly being marked as pass-by-reference, but then when it reaches where the variable is used the code thinks that the variable is a new instance; more specifically, that it has a new scope. The original variable is defined as having a scope starting with the array_walk closure, but the new variable's scope starts at the enclosing function. The bug is happening because the scope detection code is getting confused about the difference between a bound reference variable and one which is going to be bound at runtime. For example: $foo = 'hello';
$bar = &$foo; // $bar is now a reference variable bound to $foo
$bar = 'bye'; // this writes to the bound variable; now $foo is 'bye' array_walk($data, function (&$foo) { // $foo is now a reference variable that will be bound at runtime
$foo = ucfirst($foo); // this writes to the bound variable, but we can't know anything about that variable since it does not exist in the code before runtime
}); |
This code produces
Unused variable $value
, when it is in fact used:Reported by @Levivb in #214
The text was updated successfully, but these errors were encountered: