Skip to content

Commit 0a6c51e

Browse files
committed
Do not ignore closures
1 parent 1504f91 commit 0a6c51e

File tree

3 files changed

+45
-15
lines changed

3 files changed

+45
-15
lines changed

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

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -69,13 +69,10 @@ public function processTokenWithinScope(File $phpcsFile, $stackPtr, $currScope)
6969
$end = $tokens[$stackPtr]['scope_closer'];
7070

7171
do {
72-
$next = $phpcsFile->findNext([T_VARIABLE, T_CLOSURE, T_FN, T_ANON_CLASS], ($next + 1), $end);
72+
$next = $phpcsFile->findNext([T_VARIABLE, T_ANON_CLASS], ($next + 1), $end);
7373
if ($next === false) {
7474
continue;
75-
} else if ($tokens[$next]['code'] === T_CLOSURE
76-
|| $tokens[$next]['code'] === T_FN
77-
|| $tokens[$next]['code'] === T_ANON_CLASS
78-
) {
75+
} else if ($tokens[$next]['code'] === T_ANON_CLASS) {
7976
$next = $tokens[$next]['scope_closer'];
8077
continue;
8178
} else if (strtolower($tokens[$next]['content']) !== '$this') {

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

Lines changed: 39 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -57,26 +57,55 @@ 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+
}
7897

79-
public static function other() {
80-
return fn () => $this->name;
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+
};
81110
}
82111
}

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)