Skip to content

Commit

Permalink
Merge pull request #147 from VincentLanglet/fixScopeOrder
Browse files Browse the repository at this point in the history
Handle nested classes in scopeOrderSniff
  • Loading branch information
VincentLanglet authored Dec 20, 2021
2 parents 230789a + 6a01c7d commit 142155f
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 8 deletions.
21 changes: 13 additions & 8 deletions SymfonyCustom/Sniffs/Functions/ScopeOrderSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -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];
}

/**
Expand All @@ -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(
Expand Down
25 changes: 25 additions & 0 deletions SymfonyCustom/Tests/Functions/ScopeOrderUnitTest.inc
Original file line number Diff line number Diff line change
Expand Up @@ -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() {}
};
}
}
1 change: 1 addition & 0 deletions SymfonyCustom/Tests/Functions/ScopeOrderUnitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ protected function getErrorList(): array
return [
9 => 1,
20 => 1,
31 => 1,
];
}

Expand Down

0 comments on commit 142155f

Please sign in to comment.