Skip to content

Commit

Permalink
Handle multi-line anon class in SpaceBeforeClassBrace.BraceNotOnOwnLine
Browse files Browse the repository at this point in the history
The open braces should be enforced relative to the parenthesis closer
when looking at anon classes.

Bug: T347440
Change-Id: I03f075a716a14770da09a4eff9fc6a819ac5eb59
  • Loading branch information
umherirrender committed Mar 18, 2024
1 parent 9906359 commit f70ed43
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 1 deletion.
8 changes: 7 additions & 1 deletion MediaWiki/Sniffs/WhiteSpace/SpaceBeforeClassBraceSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,13 @@ public function process( File $phpcsFile, $stackPtr ) {
// Find previous non-whitespace token from the opening brace
$pre = $phpcsFile->findPrevious( T_WHITESPACE, $openBrace - 1, null, true );

if ( $tokens[$openBrace]['line'] - $tokens[$stackPtr]['line'] >= 2 ) {
$afterClass = $stackPtr;
if ( $tokens[$stackPtr]['code'] === T_ANON_CLASS && isset( $tokens[$stackPtr]['parenthesis_closer'] ) ) {
// The anon class could be multi-line, start after the class definition
$afterClass = $tokens[$stackPtr]['parenthesis_closer'];
}

if ( $tokens[$openBrace]['line'] - $tokens[$afterClass]['line'] >= 2 ) {
// If the class ... { statement is more than two lines, then
// the { should be on a line by itself.
if ( $tokens[$pre]['line'] === $tokens[$openBrace]['line'] ) {
Expand Down
5 changes: 5 additions & 0 deletions MediaWiki/Tests/files/WhiteSpace/space_before_class_brace.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,8 @@ trait ThisTraitShouldFail{

$thisShouldFail = new class (){
};

$thisIsOK2 = new class( [
'foo' => 'bar',
] ) extends AClass {
};
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,8 @@ $thisIsOK = new class () {

$thisShouldFail = new class () {
};

$thisIsOK2 = new class( [
'foo' => 'bar',
] ) extends AClass {
};

0 comments on commit f70ed43

Please sign in to comment.