Skip to content

Commit dfdac09

Browse files
jrfnlsirbrillig
authored andcommitted
VariableAnalysisSniff::checkForVariableVariable(): fix comment tolerance (#151)
PHP ignores comments in unexpected/unconventional places and so should the sniff. Includes adjusting an existing unit test. Without the fix, the adjusted unit test would cause test failures.
1 parent 0ada835 commit dfdac09

File tree

2 files changed

+10
-4
lines changed

2 files changed

+10
-4
lines changed

VariableAnalysis/Sniffs/CodeAnalysis/VariableAnalysisSniff.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -787,13 +787,19 @@ protected function checkForAssignment(File $phpcsFile, $stackPtr, $varName, $cur
787787
protected function checkForVariableVariable(File $phpcsFile, $stackPtr, $varName, $currScope) {
788788
$tokens = $phpcsFile->getTokens();
789789

790-
if (!isset($tokens[$stackPtr - 1]['code'])) {
790+
$prev = $phpcsFile->findPrevious(Tokens::$emptyTokens, ($stackPtr - 1), null, true);
791+
if ($prev === false) {
791792
return false;
792793
}
793-
if ($tokens[$stackPtr - 1]['code'] === T_DOLLAR) {
794+
if ($tokens[$prev]['code'] === T_DOLLAR) {
794795
return true;
795796
}
796-
if ($tokens[$stackPtr - 1]['code'] === T_OPEN_CURLY_BRACKET && isset($tokens[$stackPtr - 2]['code']) && $tokens[$stackPtr - 2]['code'] === T_DOLLAR) {
797+
if ($tokens[$prev]['code'] !== T_OPEN_CURLY_BRACKET) {
798+
return false;
799+
}
800+
801+
$prevPrev = $phpcsFile->findPrevious(Tokens::$emptyTokens, ($prev - 1), null, true);
802+
if ($prevPrev !== false && $tokens[$prevPrev]['code'] === T_DOLLAR) {
797803
return true;
798804
}
799805
return false;

VariableAnalysis/Tests/CodeAnalysis/fixtures/VariableVariablesFixture.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ function usedVariableVariableInEcho() {
1515
function usedVariableVariableInLeftAssignment() {
1616
$foo = true; // the below is assignment, not a read, so this should be a warning
1717
$marName = 'foo';
18-
$$marName = false;
18+
$ /* comment */ $marName = false;
1919
}
2020

2121
function usedVariableVariableInRightAssignment() {

0 commit comments

Comments
 (0)