Skip to content

Commit 4372f50

Browse files
committed
Merge branch 'hotfix/static-this-usage-array-closure' of https://github.com/michalbundyra/PHP_CodeSniffer
2 parents 75ff420 + 10b6b2a commit 4372f50

File tree

3 files changed

+78
-14
lines changed

3 files changed

+78
-14
lines changed

src/Standards/Squiz/Sniffs/Scope/StaticThisUsageSniff.php

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -68,24 +68,45 @@ public function processTokenWithinScope(File $phpcsFile, $stackPtr, $currScope)
6868
$next = $stackPtr;
6969
$end = $tokens[$stackPtr]['scope_closer'];
7070

71+
$this->checkThisUsage($phpcsFile, $next, $end);
72+
73+
}//end processTokenWithinScope()
74+
75+
76+
/**
77+
* Check for $this variable usage between $next and $end tokens.
78+
*
79+
* @param File $phpcsFile The current file being scanned.
80+
* @param int $next The position of the next token to check.
81+
* @param int $end The position of the last token to check.
82+
*
83+
* @return void
84+
*/
85+
private function checkThisUsage(File $phpcsFile, $next, $end)
86+
{
87+
$tokens = $phpcsFile->getTokens();
88+
7189
do {
72-
$next = $phpcsFile->findNext([T_VARIABLE, T_CLOSURE, T_ANON_CLASS], ($next + 1), $end);
90+
$next = $phpcsFile->findNext([T_VARIABLE, T_ANON_CLASS], ($next + 1), $end);
7391
if ($next === false) {
7492
continue;
75-
} else if ($tokens[$next]['code'] === T_CLOSURE
76-
|| $tokens[$next]['code'] === T_ANON_CLASS
77-
) {
93+
}
94+
95+
if ($tokens[$next]['code'] === T_ANON_CLASS) {
96+
$this->checkThisUsage($phpcsFile, $next, $tokens[$next]['scope_opener']);
7897
$next = $tokens[$next]['scope_closer'];
7998
continue;
80-
} else if (strtolower($tokens[$next]['content']) !== '$this') {
99+
}
100+
101+
if ($tokens[$next]['content'] !== '$this') {
81102
continue;
82103
}
83104

84105
$error = 'Usage of "$this" in static methods will cause runtime errors';
85106
$phpcsFile->addError($error, $next, 'Found');
86107
} while ($next !== false);
87108

88-
}//end processTokenWithinScope()
109+
}//end checkThisUsage()
89110

90111

91112
/**

src/Standards/Squiz/Tests/Scope/StaticThisUsageUnitTest.inc

Lines changed: 47 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -57,22 +57,61 @@ class MyClass
5757

5858
public function getAnonymousClass() {
5959
return new class() {
60-
public static function something() {
61-
$this->doSomething();
60+
public static function something() {
61+
$this->doSomething();
6262
}
6363
};
6464
}
6565
}
6666

6767
trait MyTrait {
68-
public static function myFunc() {
69-
$this->doSomething();
70-
}
68+
public static function myFunc() {
69+
$this->doSomething();
70+
}
7171
}
7272

7373
$b = new class()
7474
{
75-
public static function myFunc() {
76-
$this->doSomething();
77-
}
75+
public static function myFunc() {
76+
$this->doSomething();
77+
}
78+
79+
public static function other() {
80+
return fn () => $this->name;
81+
}
82+
83+
public static function anonClassUseThis() {
84+
return new class($this) {
85+
public function __construct($class) {
86+
}
87+
};
88+
}
89+
90+
public static function anonClassAnotherThis() {
91+
return new class() {
92+
public function __construct() {
93+
$this->id = 1;
94+
}
95+
};
96+
}
97+
98+
public static function anonClassNestedUseThis() {
99+
return new class(new class($this) {}) {
100+
};
101+
}
102+
103+
public static function anonClassNestedAnotherThis() {
104+
return new class(new class() {
105+
public function __construct() {
106+
$this->id = 1;
107+
}
108+
}) {
109+
};
110+
}
111+
112+
public static function thisMustBeLowercase() {
113+
$This = 'hey';
114+
115+
return $This;
116+
}
78117
}

src/Standards/Squiz/Tests/Scope/StaticThisUsageUnitTest.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,13 @@ public function getErrorList()
3131
9 => 1,
3232
14 => 1,
3333
20 => 1,
34+
41 => 1,
3435
61 => 1,
3536
69 => 1,
3637
76 => 1,
38+
80 => 1,
39+
84 => 1,
40+
99 => 1,
3741
];
3842

3943
}//end getErrorList()

0 commit comments

Comments
 (0)