Open
Description
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:
- Is the current variable defined? Good.
- 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.
- Resolve the value of the current variable (this is extremely hard).
- Process the T_DOLLAR as though it were a variable with the symbol name of the resolved current variable.
- 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?