Skip to content

Commit

Permalink
PSR12/AnonClassDeclaration: prevent fixer creating parse error
Browse files Browse the repository at this point in the history
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
  • Loading branch information
jrfnl committed Apr 2, 2023
1 parent ed8e00d commit f179b45
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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() {}
};
Original file line number Diff line number Diff line change
Expand Up @@ -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() {}
};
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ public function getErrorList()
56 => 2,
63 => 1,
75 => 1,
87 => 1,
88 => 1,
];

}//end getErrorList()
Expand Down

0 comments on commit f179b45

Please sign in to comment.