Skip to content

Handle nested classes in scopeOrderSniff #147

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 20, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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