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

Undefined variables in function calls are not detected #233

Closed
GaryJones opened this issue Jun 17, 2021 · 2 comments
Closed

Undefined variables in function calls are not detected #233

GaryJones opened this issue Jun 17, 2021 · 2 comments

Comments

@GaryJones
Copy link
Contributor

GaryJones commented Jun 17, 2021

Snippet:

<?php

if ( ! empty( $this->vars['video-id'] ) ) {
	echo $this->vars['video-id'];
}

With a default configuration (phpcs --standard=VariableAnalysis -s phpcs-test.php), then "Variable $this is undefined. (VariableAnalysis.CodeAnalysis.VariableAnalysis.UndefinedVariable)" is reported for line 4 (the echo line), but not for the $this in the empty().

Likewise:

<?php

if ( empty( $this->vars['video-id'] ) ) {
	return;
}

...doesn't give any violation at all.

If I remove the empty(), then the violation is reported.

<?php

if ( ! $this->vars['video-id'] ) {
	return;
}

Tested in VA 2.11.0.

@sirbrillig
Copy link
Owner

sirbrillig commented Jun 18, 2021

If the only function you tested with is empty(), then this is not a bug but a feature since empty() is allowed to contain undefined variables (since #204). This feature was added because both empty() and isset() are language constructs that are often used to detect the existence of variables (for example, no warning or error is thrown doing empty( $foo['bar']['baz'] ) if $foo does not exist, but will throw warnings for any other function like do_something( $foo['bar']['baz'] )).

Do you find this occurs with other functions as well?

@GaryJones
Copy link
Contributor Author

That makes sense. I tried with current_user_can() as the wrapping function, and that correctly gave a violation, and then this was correctly silenced with the <property name="allowUndefinedVariablesInFileScope" value="true"/> line in the config file ✅

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

No branches or pull requests

2 participants