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

Variable variables are marked as unused #87

Open
sirbrillig opened this issue May 10, 2019 · 0 comments
Open

Variable variables are marked as unused #87

sirbrillig opened this issue May 10, 2019 · 0 comments

Comments

@sirbrillig
Copy link
Owner

Originally described in #84.

Variables in PHP may have a dynamic symbol name, and are called variable variables. For example, the following function prints hello, which is a string stored in the variable $greeting, even though that variable is never explicitly mentioned:

function usedVariableVariable() {
  $greeting = 'hello';
  $varName = 'greeting';
  echo $$varName;
}

Here's the process I would imagine using to identify such a variable for the purposes of this sniff:

  1. Is the current variable defined? Good.
  2. Is the current variable preceded by a T_DOLLAR or a T_DOLLAR and a T_OPEN_CURLY_BRACKET? If so, it's a variable variable.
  3. Resolve the value of the current variable (this is extremely hard).
  4. Process the T_DOLLAR as though it were a variable with the symbol name of the resolved current variable.
  5. Start again at 1 since there may be several levels.

Step 3 is very difficult because it requires executing the PHP code or a deep static analysis. Maybe there's something else we can do, though?

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

1 participant