Skip to content

Commit fef613a

Browse files
jrfnlsirbrillig
authored andcommitted
VariableAnalysisSniff::checkForStaticMember(): fix comment tolerance [1] (#147)
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 0d01e04 commit fef613a

File tree

2 files changed

+7
-7
lines changed

2 files changed

+7
-7
lines changed

VariableAnalysis/Sniffs/CodeAnalysis/VariableAnalysisSniff.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -668,19 +668,19 @@ protected function checkForSuperGlobal(File $phpcsFile, $stackPtr, $varName) {
668668
protected function checkForStaticMember(File $phpcsFile, $stackPtr, $varName, $currScope) {
669669
$tokens = $phpcsFile->getTokens();
670670

671-
$doubleColonPtr = $stackPtr - 1;
672-
if ($tokens[$doubleColonPtr]['code'] !== T_DOUBLE_COLON) {
671+
$doubleColonPtr = $phpcsFile->findPrevious(Tokens::$emptyTokens, $stackPtr - 1, null, true);
672+
if ($doubleColonPtr === false || $tokens[$doubleColonPtr]['code'] !== T_DOUBLE_COLON) {
673673
return false;
674674
}
675-
$classNamePtr = $stackPtr - 2;
675+
$classNamePtr = $phpcsFile->findPrevious(Tokens::$emptyTokens, $doubleColonPtr - 1, null, true);
676676
$staticReferences = [
677677
T_STRING,
678678
T_SELF,
679679
T_PARENT,
680680
T_STATIC,
681681
T_VARIABLE,
682682
];
683-
if (! in_array($tokens[$classNamePtr]['code'], $staticReferences, true)) {
683+
if ($classNamePtr === false || ! in_array($tokens[$classNamePtr]['code'], $staticReferences, true)) {
684684
return false;
685685
}
686686
// "When calling static methods, the function call is stronger than the

VariableAnalysis/Tests/CodeAnalysis/fixtures/ClassWithMembersFixture.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ class ClassWithMembers {
5252
function method_with_member_var() {
5353
echo $this->member_var;
5454
echo $this->no_such_member_var;
55-
echo self::$static_member_var;
55+
echo self :: $static_member_var;
5656
echo self::$no_such_static_member_var;
5757
echo SomeOtherClass::$external_static_member_var;
5858
}
@@ -73,7 +73,7 @@ static function method_with_late_static_binding($param) {
7373
class ChildClassWithMembers extends ClassWithMembers {
7474
function method_with_parent_reference() {
7575
echo self::$static_member_var;
76-
echo parent::$no_such_static_member_var;
76+
echo parent /*comment*/ :: $no_such_static_member_var;
7777
}
7878
}
7979

@@ -87,7 +87,7 @@ function method_with_member_var() {
8787
echo $this->member_var;
8888
echo $this->no_such_member_var;
8989
echo self::$static_member_var;
90-
echo self::$no_such_static_member_var;
90+
echo self:: /*comment*/ $no_such_static_member_var;
9191
echo SomeOtherClass::$external_static_member_var;
9292
}
9393

0 commit comments

Comments
 (0)