diff --git a/SymfonyCustom/Sniffs/Functions/ScopeOrderSniff.php b/SymfonyCustom/Sniffs/Functions/ScopeOrderSniff.php index 7d0b689..be34e08 100644 --- a/SymfonyCustom/Sniffs/Functions/ScopeOrderSniff.php +++ b/SymfonyCustom/Sniffs/Functions/ScopeOrderSniff.php @@ -25,7 +25,7 @@ class ScopeOrderSniff implements Sniff */ public function register(): array { - return [T_CLASS, T_INTERFACE]; + return [T_CLASS, T_INTERFACE, T_ANON_CLASS]; } /** @@ -45,19 +45,24 @@ public function process(File $phpcsFile, $stackPtr): void 2 => T_PRIVATE, ]; - while ($function) { - $end = null; - - if (isset($tokens[$stackPtr]['scope_closer'])) { - $end = $tokens[$stackPtr]['scope_closer']; - } + $end = null; + if (isset($tokens[$stackPtr]['scope_closer'])) { + $end = $tokens[$stackPtr]['scope_closer']; + } + while ($function) { $function = $phpcsFile->findNext( - T_FUNCTION, + [T_FUNCTION, T_ANON_CLASS], $function + 1, $end ); + if (T_ANON_CLASS === $tokens[$function]['code']) { + $function = $tokens[$function]['scope_closer']; + + continue; + } + if (isset($tokens[$function]['parenthesis_opener'])) { $scope = $phpcsFile->findPrevious($scopes, $function - 1, $stackPtr); $name = $phpcsFile->findNext( diff --git a/SymfonyCustom/Tests/Functions/ScopeOrderUnitTest.inc b/SymfonyCustom/Tests/Functions/ScopeOrderUnitTest.inc index 03929ca..878104a 100644 --- a/SymfonyCustom/Tests/Functions/ScopeOrderUnitTest.inc +++ b/SymfonyCustom/Tests/Functions/ScopeOrderUnitTest.inc @@ -20,3 +20,28 @@ class myClass2 protected function functionProtected2() {} } +class Embedded +{ + public function test() + { + $class = new class + { + private function foo() {} + + protected function bar() {} + }; + + return new class + { + protected function foo() {} + }; + } + + public function test2() + { + return new class + { + protected function foo() {} + }; + } +} diff --git a/SymfonyCustom/Tests/Functions/ScopeOrderUnitTest.php b/SymfonyCustom/Tests/Functions/ScopeOrderUnitTest.php index 212e793..d00ea12 100644 --- a/SymfonyCustom/Tests/Functions/ScopeOrderUnitTest.php +++ b/SymfonyCustom/Tests/Functions/ScopeOrderUnitTest.php @@ -19,6 +19,7 @@ protected function getErrorList(): array return [ 9 => 1, 20 => 1, + 31 => 1, ]; }