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

and, or, & xor logical operators aren't checked against variable-setting #99

Open
sybrew opened this issue Jul 21, 2019 · 2 comments
Open

Comments

@sybrew
Copy link

sybrew commented Jul 21, 2019

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.

@sirbrillig
Copy link
Owner

sirbrillig commented Jul 22, 2019

Oh, interesting. I hadn't considered a single statement with multiple expressions that define and use a variable. Good find.

@sybrew
Copy link
Author

sybrew commented Jul 23, 2019

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;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants