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

File-scope unused variable not detected when ?> is present #232

Closed
GaryJones opened this issue Jun 17, 2021 · 3 comments · Fixed by #234
Closed

File-scope unused variable not detected when ?> is present #232

GaryJones opened this issue Jun 17, 2021 · 3 comments · Fixed by #234
Labels

Comments

@GaryJones
Copy link
Contributor

GaryJones commented Jun 17, 2021

Snippet 1:

<?php

$unused_variable = 'foo';

With the default configuration (phpcs --standard=VariableAnalysis -s phpcs-test.php), this is detected as "Unused variable $unused_variable (VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable)". ✅

Snippet 2:

<?php

$unused_variable = 'foo';
?>

The addition of the ?> stops the violation from being reported, even though the variable is still unused. ❌

Tested in VA 2.11.0.

@GaryJones GaryJones changed the title File-scope unused variable not detected when ?> is present. File-scope unused variable not detected when ?> is present Jun 17, 2021
@sirbrillig
Copy link
Owner

That definitely looks like a bug. I hadn't considered files that use closing php tags (since they're generally discouraged) and I suspect that the sniff misidentifies them. We'll need to make it aware of that.

@sirbrillig sirbrillig added the bug label Jun 18, 2021
@GaryJones
Copy link
Contributor Author

Consider the case of a template file which is most likely to contain file-scope variables, that may include many ?> and <?php as it breaks from PHP to HTML and back, then accounting for the ?> seems like an important thing :-)

@sirbrillig
Copy link
Owner

100% agree. I'm honestly surprised this hasn't been brought up before! I'm working on a fix.

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

Successfully merging a pull request may close this issue.

2 participants