Skip to content

Commit 4b42157

Browse files
Handle nested classes
1 parent 230789a commit 4b42157

File tree

3 files changed

+40
-8
lines changed

3 files changed

+40
-8
lines changed

SymfonyCustom/Sniffs/Functions/ScopeOrderSniff.php

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
use function array_keys;
1111
use function in_array;
12+
use function var_dump;
1213

1314
/**
1415
* Throws warnings if properties are declared after methods
@@ -25,7 +26,7 @@ class ScopeOrderSniff implements Sniff
2526
*/
2627
public function register(): array
2728
{
28-
return [T_CLASS, T_INTERFACE];
29+
return [T_CLASS, T_INTERFACE, T_ANON_CLASS];
2930
}
3031

3132
/**
@@ -45,19 +46,24 @@ public function process(File $phpcsFile, $stackPtr): void
4546
2 => T_PRIVATE,
4647
];
4748

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

54+
while ($function) {
5555
$function = $phpcsFile->findNext(
56-
T_FUNCTION,
56+
[T_FUNCTION, T_ANON_CLASS],
5757
$function + 1,
5858
$end
5959
);
6060

61+
if (T_ANON_CLASS === $tokens[$function]['code']) {
62+
$function = $tokens[$function]['scope_closer'];
63+
64+
continue;
65+
}
66+
6167
if (isset($tokens[$function]['parenthesis_opener'])) {
6268
$scope = $phpcsFile->findPrevious($scopes, $function - 1, $stackPtr);
6369
$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)