Skip to content

Commit 6a01c7d

Browse files
Handle nested classes
1 parent 230789a commit 6a01c7d

File tree

3 files changed

+39
-8
lines changed

3 files changed

+39
-8
lines changed

SymfonyCustom/Sniffs/Functions/ScopeOrderSniff.php

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ class ScopeOrderSniff implements Sniff
2525
*/
2626
public function register(): array
2727
{
28-
return [T_CLASS, T_INTERFACE];
28+
return [T_CLASS, T_INTERFACE, T_ANON_CLASS];
2929
}
3030

3131
/**
@@ -45,19 +45,24 @@ public function process(File $phpcsFile, $stackPtr): void
4545
2 => T_PRIVATE,
4646
];
4747

48-
while ($function) {
49-
$end = null;
50-
51-
if (isset($tokens[$stackPtr]['scope_closer'])) {
52-
$end = $tokens[$stackPtr]['scope_closer'];
53-
}
48+
$end = null;
49+
if (isset($tokens[$stackPtr]['scope_closer'])) {
50+
$end = $tokens[$stackPtr]['scope_closer'];
51+
}
5452

53+
while ($function) {
5554
$function = $phpcsFile->findNext(
56-
T_FUNCTION,
55+
[T_FUNCTION, T_ANON_CLASS],
5756
$function + 1,
5857
$end
5958
);
6059

60+
if (T_ANON_CLASS === $tokens[$function]['code']) {
61+
$function = $tokens[$function]['scope_closer'];
62+
63+
continue;
64+
}
65+
6166
if (isset($tokens[$function]['parenthesis_opener'])) {
6267
$scope = $phpcsFile->findPrevious($scopes, $function - 1, $stackPtr);
6368
$name = $phpcsFile->findNext(

SymfonyCustom/Tests/Functions/ScopeOrderUnitTest.inc

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,28 @@ class myClass2
2020
protected function functionProtected2() {}
2121
}
2222

23+
class Embedded
24+
{
25+
public function test()
26+
{
27+
$class = new class
28+
{
29+
private function foo() {}
30+
31+
protected function bar() {}
32+
};
33+
34+
return new class
35+
{
36+
protected function foo() {}
37+
};
38+
}
39+
40+
public function test2()
41+
{
42+
return new class
43+
{
44+
protected function foo() {}
45+
};
46+
}
47+
}

SymfonyCustom/Tests/Functions/ScopeOrderUnitTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ protected function getErrorList(): array
1919
return [
2020
9 => 1,
2121
20 => 1,
22+
31 => 1,
2223
];
2324
}
2425

0 commit comments

Comments
 (0)