-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Recognize that variable vars are not assignments (#88)
* Tests: Add tests for variable variables * Recognize that variable vars are not assignments When a variable variable is used as the left side of an assignment, there are actually two variables being checked: there is the current one, and the variable one whose name is the value of the current one. In this case, we must not treat the current one as an assignment; it is actually a read. This change updates the logic so that this is the case.
- Loading branch information
1 parent
00d3158
commit fd217f2
Showing
3 changed files
with
102 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
VariableAnalysis/Tests/CodeAnalysis/fixtures/VariableVariablesFixture.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
<?php | ||
|
||
function usedVariableVariableInReturn() { | ||
$foo = true; // this is used but it requires execution to know that | ||
$varName = 'foo'; | ||
return $$varName; | ||
} | ||
|
||
function usedVariableVariableInEcho() { | ||
$foo = true; // this is used but it requires execution to know that | ||
$varName = 'foo'; | ||
echo $$varName; | ||
} | ||
|
||
function usedVariableVariableInLeftAssignment() { | ||
$foo = true; // the below is assignment, not a read, so this should be a warning | ||
$marName = 'foo'; | ||
$$marName = false; | ||
} | ||
|
||
function usedVariableVariableInRightAssignment() { | ||
$foo = true; // this is used but it requires execution to know that | ||
$varName = 'foo'; | ||
$bar = $$varName; | ||
echo $bar; | ||
} | ||
|
||
function usedVariableVariableInEchoWithBraces() { | ||
$foo = true; // this is used but it requires execution to know that | ||
$varName = 'foo'; | ||
echo ${$varName}; | ||
} | ||
|
||
function usedVariableVariableInLeftAssignmentWithBraces() { | ||
$foo = true; // this is used but it requires execution to know that | ||
$varName = 'foo'; | ||
${$varName} = false; | ||
} | ||
|
||
function usedVariableVariableInEchoWithBracesInString() { | ||
$foo = true; // this is used but it requires execution to know that | ||
$varName = 'foo'; | ||
echo "${$varName}"; | ||
} | ||
|
||
function undefinedVariableVariableInEcho() { | ||
$foo = true; // this should be a warning | ||
echo $$varName; // this should be a warning | ||
} | ||
|
||
function usedVariableVariableTwoLevels() { | ||
$foo = 'hello'; // this is used but it requires execution to know that | ||
$varNameOne = 'foo'; // this is used but it requires execution to know that | ||
$varNameTwo = 'varNameOne'; | ||
return $$$varNameTwo; | ||
} |