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

Unused global variable warning when global variable is written to #81

Closed
aeisenberg opened this issue Apr 18, 2019 · 10 comments · Fixed by #83
Closed

Unused global variable warning when global variable is written to #81

aeisenberg opened this issue Apr 18, 2019 · 10 comments · Fixed by #83
Labels

Comments

@aeisenberg
Copy link

Say I have this function:

function updateGlobal($newVal) {
  global $myGlobal;
  $myGlobal = $newVal;
}

I will get the following message:

Unused global variable $myGlobal.

It seems like this is an erroneous message. Or at least it should be configurable.

Currently, I'm able to get around the warning this way:

function updateGlobal($newVal) {
  global $myGlobal;
  $myGlobal;
  $myGlobal = $newVal;
}

But this is not ideal.

@sirbrillig
Copy link
Owner

Wow, that is a bug. I actually don't think we handle the global keyword at all; it should count as a variable definition for the purposes of checking a variable's scope. Thanks for finding it!

@sirbrillig sirbrillig added the bug label Apr 20, 2019
@aeisenberg
Copy link
Author

I'm not entirely familiar with the code, but it looks like if you added a call to markVariableRead on line 590 after the markVariableDeclaration, that should fix the problem. I can submit a PR if you like.

@aeisenberg
Copy link
Author

Nope....that's not gonna work.

@sirbrillig
Copy link
Owner

Yeah, that's looking for the variable declaration. I was wrong, we are checking for the global keyword, so there's something wrong with that. 🤔

@aeisenberg
Copy link
Author

Looks like in checkForAssignment, you need to check if the variable being assigned is a global variable and if so also mark it as read.

How do you check if the variable is global?

@aeisenberg
Copy link
Author

Or is this something that isn't kept track of yet?

@sirbrillig
Copy link
Owner

Global variables are a little complicated (see #37) but I am tracking their declaration with the scopeType property of VariableInfo. In this case, that variable declaration is being found and recorded correctly.

Really we just want to see if the variable has been declared within the scope (processScopeCloseForVariable(), which uses getStackPtrIfVariableIsUnused()). I think that getStackPtrIfVariableIsUnused() might be doing something wrong or I'm misunderstanding what that function was supposed to do. Still looking...

@sirbrillig
Copy link
Owner

sirbrillig commented Apr 20, 2019

Gah, jetlag is clouding my brain. The issue as I understand it is that for some reason the assignment $myGlobal = $newVal isn't being recorded as a use of $myGlobal, although as far as I can tell myGlobal is being saved in a VariableInfo object when it is declared, and its firstInitialized property is being set correctly when it is being assigned. The warning happens at the end of the scope in processScopeCloseForVariable() so something there must not be operating as it should be. Or maybe there's some confusion about what variables should be reported there.

Possibly helpful note to self: getStackPtrIfVariableIsUnused is first defined here: https://github.com/sirbrillig/phpcs-variable-analysis/pull/32/files

There's a test to recreate the issue here: https://github.com/sirbrillig/phpcs-variable-analysis/pull/83/files

Debug mode can be triggered by doing this: ./vendor/bin/phpcs -vvvv --standard=VariableAnalysis VariableAnalysis/Tests/CodeAnalysis/fixtures/FunctionWithGlobalVarFixture.php

@sirbrillig
Copy link
Owner

Found it. Global imports just need to be treated like pass-by-reference.

@aeisenberg
Copy link
Author

aeisenberg commented Apr 22, 2019 via email

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