Skip to content

Commit 0ada835

Browse files
jrfnlsirbrillig
authored andcommitted
VariableAnalysisSniff::checkForStaticOutsideClass(): fix comment tolerance (#150)
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 c8241b9 commit 0ada835

File tree

2 files changed

+8
-5
lines changed

2 files changed

+8
-5
lines changed

VariableAnalysis/Sniffs/CodeAnalysis/VariableAnalysisSniff.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -706,11 +706,14 @@ protected function checkForStaticOutsideClass(File $phpcsFile, $stackPtr, $varNa
706706
$tokens = $phpcsFile->getTokens();
707707
$token = $tokens[$stackPtr];
708708

709-
$doubleColonPtr = $stackPtr - 1;
710-
if ($tokens[$doubleColonPtr]['code'] !== T_DOUBLE_COLON) {
709+
$doubleColonPtr = $phpcsFile->findPrevious(Tokens::$emptyTokens, $stackPtr - 1, null, true);
710+
if ($doubleColonPtr === false || $tokens[$doubleColonPtr]['code'] !== T_DOUBLE_COLON) {
711+
return false;
712+
}
713+
$classNamePtr = $phpcsFile->findPrevious(Tokens::$emptyTokens, $doubleColonPtr - 1, null, true);
714+
if ($classNamePtr === false) {
711715
return false;
712716
}
713-
$classNamePtr = $stackPtr - 2;
714717
$code = $tokens[$classNamePtr]['code'];
715718
$staticReferences = [
716719
T_SELF,

VariableAnalysis/Tests/CodeAnalysis/fixtures/FunctionWithClosureFixture.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ function method_with_self_inside_closure() {
5555

5656
function function_with_self_in_closure() {
5757
return function() {
58-
return self::$foobar; // should be an error
58+
return self /*comment*/::$foobar; // should be an error
5959
};
6060
}
6161

@@ -67,7 +67,7 @@ function function_with_this_in_closure() {
6767

6868
function function_with_static_in_closure() {
6969
return function() {
70-
return static::$foobar; // should be an error
70+
return static:: /*comment*/ $foobar; // should be an error
7171
};
7272
}
7373

0 commit comments

Comments
 (0)