phpcs-variable-analysis
phpcs-variable-analysis copied to clipboard
and, or, & xor logical operators aren't checked against variable-setting
Code speaks a thousand words. Replace and with the other logical operators (or, xor), and the same error will yield.
$get_something_and_do_stuff = true;
$get_something_and_do_stuff
and $something = get_something()
and $something === 'something' // VariableAnalysis.CodeAnalysis.VariableAnalysis.UndefinedVariable
and do_stuff();
Variable $something is undefined.
(VariableAnalysis.CodeAnalysis.VariableAnalysis.UndefinedVariable)
This is tricky, however, because the logical operators may prevent or bypass assigning variables; especially when the or-operator is used in sequence.
Oh, interesting. I hadn't considered a single statement with multiple expressions that define and use a variable. Good find.
I don't think it's feasible to create a fix for this, however. Consider these scenarios, where every instance of true/false may be a function or variable call which you can't test.
Positives:
false
and $something = true
or 'value' === $something; // warning
true
and $something = true
or 'value' === $something; // warning, never runs.
false
or $something = false
and 'value' === $something; // warning, never runs.
False positives:
false
or $something = true
and 'value' === $something; // warning that's incorrect
False negatives:
false
and $something = true;
if ( $something ) { // no warning, but it's undefined...
return false;
}