Skip to content

Commit 884d702

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 68e7d9e commit 884d702

File tree

5 files changed

+22
-1
lines changed

5 files changed

+22
-1
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,8 @@ The file documents changes to the PHP_CodeSniffer project.
129129
- Thanks to Juliette Reinders Folmer (@jrfnl) for the patch
130130
- Fixed bug #3789 : Incorrect tokenization for ternary operator with match inside of it
131131
- Thanks to Juliette Reinders Folmer (@jrfnl) for the patch
132+
- Fixed bug #3790 : PSR12/AnonClassDeclaration: prevent fixer creating parse error when there was no space before the open brace
133+
- Thanks to Juliette Reinders Folmer (@jrfnl) for the patch
132134
- Fixed bug #3797 : Tokenizer/PHP: more context sensitive keyword fixes
133135
- Thanks to Juliette Reinders Folmer (@jrfnl) for the patch
134136
- Fixed bug #3801 : File::getMethodParameters(): allow for readonly promoted properties without visibility

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

Lines changed: 5 additions & 1 deletion
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

Lines changed: 6 additions & 0 deletions
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

Lines changed: 7 additions & 0 deletions
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

Lines changed: 2 additions & 0 deletions
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)