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 Dec 5, 2023
1 parent 68e7d9e commit 884d702
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 1 deletion.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,8 @@ The file documents changes to the PHP_CodeSniffer project.
- Thanks to Juliette Reinders Folmer (@jrfnl) for the patch
- Fixed bug #3789 : Incorrect tokenization for ternary operator with match inside of it
- Thanks to Juliette Reinders Folmer (@jrfnl) for the patch
- Fixed bug #3790 : PSR12/AnonClassDeclaration: prevent fixer creating parse error when there was no space before the open brace
- Thanks to Juliette Reinders Folmer (@jrfnl) for the patch
- Fixed bug #3797 : Tokenizer/PHP: more context sensitive keyword fixes
- Thanks to Juliette Reinders Folmer (@jrfnl) for the patch
- Fixed bug #3801 : File::getMethodParameters(): allow for readonly promoted properties without visibility
Expand Down
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 884d702

Please sign in to comment.