diff --git a/src/Standards/PSR12/Sniffs/Classes/AnonClassDeclarationSniff.php b/src/Standards/PSR12/Sniffs/Classes/AnonClassDeclarationSniff.php index db67fb9428..0e9197e354 100644 --- a/src/Standards/PSR12/Sniffs/Classes/AnonClassDeclarationSniff.php +++ b/src/Standards/PSR12/Sniffs/Classes/AnonClassDeclarationSniff.php @@ -97,7 +97,11 @@ public function process(File $phpcsFile, $stackPtr) $first = $phpcsFile->findFirstOnLine(T_WHITESPACE, $stackPtr, true); $indent = str_repeat(' ', ($tokens[$first]['column'] - 1)); $phpcsFile->fixer->beginChangeset(); - $phpcsFile->fixer->replaceToken(($prev + 1), ''); + + if ($tokens[($prev + 1)]['code'] === \T_WHITESPACE) { + $phpcsFile->fixer->replaceToken(($prev + 1), ''); + } + $phpcsFile->fixer->addNewline($prev); $phpcsFile->fixer->addContentBefore($opener, $indent); $phpcsFile->fixer->endChangeset(); diff --git a/src/Standards/PSR12/Tests/Classes/AnonClassDeclarationUnitTest.inc b/src/Standards/PSR12/Tests/Classes/AnonClassDeclarationUnitTest.inc index e132b54df1..fe3ee02ef8 100644 --- a/src/Standards/PSR12/Tests/Classes/AnonClassDeclarationUnitTest.inc +++ b/src/Standards/PSR12/Tests/Classes/AnonClassDeclarationUnitTest.inc @@ -82,3 +82,9 @@ $foo->bar( foo(new class { }); + +// Issue #3790: OpenBraceSameLine fixer should not remove open brace. +$instance = new class() extends SomeClass implements + SomeInterface{ + public function __construct() {} +}; diff --git a/src/Standards/PSR12/Tests/Classes/AnonClassDeclarationUnitTest.inc.fixed b/src/Standards/PSR12/Tests/Classes/AnonClassDeclarationUnitTest.inc.fixed index 921dcc02de..4a6c5a1f1a 100644 --- a/src/Standards/PSR12/Tests/Classes/AnonClassDeclarationUnitTest.inc.fixed +++ b/src/Standards/PSR12/Tests/Classes/AnonClassDeclarationUnitTest.inc.fixed @@ -84,3 +84,10 @@ $foo->bar( foo(new class { }); + +// Issue #3790: OpenBraceSameLine fixer should not remove open brace. +$instance = new class () extends SomeClass implements + SomeInterface +{ + public function __construct() {} +}; diff --git a/src/Standards/PSR12/Tests/Classes/AnonClassDeclarationUnitTest.php b/src/Standards/PSR12/Tests/Classes/AnonClassDeclarationUnitTest.php index cc162b290b..860ce0b797 100644 --- a/src/Standards/PSR12/Tests/Classes/AnonClassDeclarationUnitTest.php +++ b/src/Standards/PSR12/Tests/Classes/AnonClassDeclarationUnitTest.php @@ -48,6 +48,8 @@ public function getErrorList() 56 => 2, 63 => 1, 75 => 1, + 87 => 1, + 88 => 1, ]; }//end getErrorList()