Skip to content

Commit

Permalink
Fixed bug #2678 : PSR12.Classes.AnonClassDeclaration incorrectly enfo…
Browse files Browse the repository at this point in the history
…rcing that closing brace be on a line by itself
  • Loading branch information
gsherwood committed Nov 3, 2019
1 parent b1c7d88 commit 6a4abf4
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 15 deletions.
1 change: 1 addition & 0 deletions package.xml
Expand Up @@ -32,6 +32,7 @@ http://pear.php.net/dtd/package-2.0.xsd">
-- Thanks to Raphael Horber for the patch
- Fixed bug #2674 : Squiz.Functions.FunctionDeclarationArgumentSpacing prints wrong argument name in error message
- Fixed bug #2676 : PSR12.Files.FileHeader locks up when file ends with multiple inline comments
- Fixed bug #2678 : PSR12.Classes.AnonClassDeclaration incorrectly enforcing that closing brace be on a line by itself
</notes>
<contents>
<dir name="/">
Expand Down
Expand Up @@ -79,3 +79,6 @@ $foo->bar(
// ...
},
);

foo(new class {
});
Expand Up @@ -81,3 +81,6 @@ $foo->bar(
// ...
},
);

foo(new class {
});
32 changes: 17 additions & 15 deletions src/Standards/PSR2/Sniffs/Classes/ClassDeclarationSniff.php
Expand Up @@ -503,21 +503,23 @@ public function processClose(File $phpcsFile, $stackPtr)
}
}//end if

// Check the closing brace is on it's own line, but allow
// for comments like "//end class".
$ignoreTokens = Tokens::$phpcsCommentTokens;
$ignoreTokens[] = T_WHITESPACE;
$ignoreTokens[] = T_COMMENT;
$ignoreTokens[] = T_SEMICOLON;
$ignoreTokens[] = T_COMMA;
$nextContent = $phpcsFile->findNext($ignoreTokens, ($closeBrace + 1), null, true);
if ($tokens[$nextContent]['content'] !== $phpcsFile->eolChar
&& $tokens[$nextContent]['line'] === $tokens[$closeBrace]['line']
) {
$type = strtolower($tokens[$stackPtr]['content']);
$error = 'Closing %s brace must be on a line by itself';
$data = [$type];
$phpcsFile->addError($error, $closeBrace, 'CloseBraceSameLine', $data);
if ($tokens[$stackPtr]['code'] !== T_ANON_CLASS) {
// Check the closing brace is on it's own line, but allow
// for comments like "//end class".
$ignoreTokens = Tokens::$phpcsCommentTokens;
$ignoreTokens[] = T_WHITESPACE;
$ignoreTokens[] = T_COMMENT;
$ignoreTokens[] = T_SEMICOLON;
$ignoreTokens[] = T_COMMA;
$nextContent = $phpcsFile->findNext($ignoreTokens, ($closeBrace + 1), null, true);
if ($tokens[$nextContent]['content'] !== $phpcsFile->eolChar
&& $tokens[$nextContent]['line'] === $tokens[$closeBrace]['line']
) {
$type = strtolower($tokens[$stackPtr]['content']);
$error = 'Closing %s brace must be on a line by itself';
$data = [$type];
$phpcsFile->addError($error, $closeBrace, 'CloseBraceSameLine', $data);
}
}

}//end processClose()
Expand Down
3 changes: 3 additions & 0 deletions src/Standards/PSR2/Tests/Classes/ClassDeclarationUnitTest.inc
Expand Up @@ -236,3 +236,6 @@ class
C8
{
}

foo(new class {
});
Expand Up @@ -229,3 +229,6 @@ class C7 extends \Foo\Bar implements \Baz\Bar // comment
class C8
{
}

foo(new class {
});

0 comments on commit 6a4abf4

Please sign in to comment.