Skip to content

Commit 172e8b3

Browse files
committed
PSR12/AnonClassDeclaration: prevent fixer creating parse error
This fix prevents the fixer from removing the opening brace when there is no whitespace between the last character of the name of an interface and the open brace. With this fix in place, all other symptoms reported are also gone as they were a side-effect of the parse error being created. Includes unit test. Fixes 3790
1 parent b0d171b commit 172e8b3

File tree

4 files changed

+20
-1
lines changed

4 files changed

+20
-1
lines changed

src/Standards/PSR12/Sniffs/Classes/AnonClassDeclarationSniff.php

+5-1
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,11 @@ public function process(File $phpcsFile, $stackPtr)
9797
$first = $phpcsFile->findFirstOnLine(T_WHITESPACE, $stackPtr, true);
9898
$indent = str_repeat(' ', ($tokens[$first]['column'] - 1));
9999
$phpcsFile->fixer->beginChangeset();
100-
$phpcsFile->fixer->replaceToken(($prev + 1), '');
100+
101+
if ($tokens[($prev + 1)]['code'] === \T_WHITESPACE) {
102+
$phpcsFile->fixer->replaceToken(($prev + 1), '');
103+
}
104+
101105
$phpcsFile->fixer->addNewline($prev);
102106
$phpcsFile->fixer->addContentBefore($opener, $indent);
103107
$phpcsFile->fixer->endChangeset();

src/Standards/PSR12/Tests/Classes/AnonClassDeclarationUnitTest.inc

+6
Original file line numberDiff line numberDiff line change
@@ -82,3 +82,9 @@ $foo->bar(
8282

8383
foo(new class {
8484
});
85+
86+
// Issue #3790: OpenBraceSameLine fixer should not remove open brace.
87+
$instance = new class() extends SomeClass implements
88+
SomeInterface{
89+
public function __construct() {}
90+
};

src/Standards/PSR12/Tests/Classes/AnonClassDeclarationUnitTest.inc.fixed

+7
Original file line numberDiff line numberDiff line change
@@ -84,3 +84,10 @@ $foo->bar(
8484

8585
foo(new class {
8686
});
87+
88+
// Issue #3790: OpenBraceSameLine fixer should not remove open brace.
89+
$instance = new class () extends SomeClass implements
90+
SomeInterface
91+
{
92+
public function __construct() {}
93+
};

src/Standards/PSR12/Tests/Classes/AnonClassDeclarationUnitTest.php

+2
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ public function getErrorList()
4848
56 => 2,
4949
63 => 1,
5050
75 => 1,
51+
87 => 1,
52+
88 => 1,
5153
];
5254

5355
}//end getErrorList()

0 commit comments

Comments
 (0)