Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ public function process(File $phpcsFile, $stackPtr)

$openingBrace = $tokens[$stackPtr]['scope_opener'];
$closeBracket = $tokens[$stackPtr]['parenthesis_closer'];

if ($tokens[$stackPtr]['code'] === T_CLOSURE) {
$use = $phpcsFile->findNext(T_USE, ($closeBracket + 1), $tokens[$stackPtr]['scope_opener']);
if ($use !== false) {
Expand All @@ -95,6 +96,18 @@ public function process(File $phpcsFile, $stackPtr)
}

if ($lineDifference === 0) {
// Ignore constructors with empty function body
if ($tokens[$stackPtr]['code'] === T_FUNCTION
&& $phpcsFile->getDeclarationName($stackPtr) === '__construct'
) {
$closingBrace = $tokens[$stackPtr]['scope_closer'];
$lastContent = $phpcsFile->findPrevious([T_WHITESPACE], ($closingBrace - 1), $openingBrace, true);

if ($lastContent === $openingBrace) {
return;
}
}

$error = 'Opening brace should be on a new line';
$fix = $phpcsFile->addFixableError($error, $openingBrace, 'BraceOnSameLine');
if ($fix === true) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ function myFunction()

class myClass
{
// Good.
function __construct() {}

// Brace should be on new line.
function myFunction() {
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ function myFunction()

class myClass
{
// Good.
function __construct() {}

// Brace should be on new line.
function myFunction()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,41 +26,41 @@ class OpeningFunctionBraceBsdAllmanUnitTest extends AbstractSniffUnitTest
public function getErrorList()
{
return [
4 => 1,
13 => 1,
19 => 1,
24 => 1,
30 => 1,
40 => 1,
44 => 1,
50 => 1,
55 => 1,
67 => 1,
78 => 1,
85 => 1,
91 => 1,
98 => 1,
110 => 1,
115 => 1,
122 => 1,
128 => 1,
155 => 1,
7 => 1,
16 => 1,
22 => 1,
27 => 1,
33 => 1,
43 => 1,
47 => 1,
53 => 1,
58 => 1,
70 => 1,
81 => 1,
88 => 1,
94 => 1,
101 => 1,
113 => 1,
118 => 1,
125 => 1,
131 => 1,
158 => 1,
164 => 1,
168 => 1,
172 => 1,
176 => 1,
196 => 1,
201 => 1,
205 => 2,
210 => 2,
215 => 1,
220 => 1,
231 => 1,
236 => 1,
244 => 1,
252 => 1,
260 => 1,
161 => 1,
167 => 1,
171 => 1,
175 => 1,
179 => 1,
199 => 1,
204 => 1,
208 => 2,
213 => 2,
218 => 1,
223 => 1,
234 => 1,
239 => 1,
247 => 1,
255 => 1,
263 => 1,
];

}//end getErrorList()
Expand Down
18 changes: 18 additions & 0 deletions src/Standards/Squiz/Sniffs/WhiteSpace/ScopeClosingBraceSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,24 @@ public function process(File $phpcsFile, $stackPtr)
$lastContent = $phpcsFile->findPrevious([T_INLINE_HTML, T_WHITESPACE, T_OPEN_TAG], ($scopeEnd - 1), $scopeStart, true);
for ($lineStart = $scopeEnd; $tokens[$lineStart]['column'] > 1; $lineStart--);

// Allow opening and closing brackets with empty body on the same line for constructors
if ($tokens[$stackPtr]['code'] === T_FUNCTION
&& $phpcsFile->getDeclarationName($stackPtr) === '__construct'
&& $lastContent === $scopeStart
) {
$whitespaceBetween = $phpcsFile->findPrevious([T_WHITESPACE], ($scopeEnd - 1), $scopeStart);

if ($whitespaceBetween) {
$error = 'Expected no space between opening and closing brace';
$fix = $phpcsFile->addFixableError($error, $scopeEnd, 'ContentBefore');
if ($fix === true) {
$phpcsFile->fixer->replaceToken($whitespaceBetween, '');
}
}

return;
}

if ($tokens[$lastContent]['line'] === $tokens[$scopeEnd]['line']
|| ($tokens[$lineStart]['code'] === T_INLINE_HTML
&& trim($tokens[$lineStart]['content']) !== '')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ use some\foo\{ClassA, ClassB, ClassC as C};
class Test
{
public function __construct()
{
}
{}

function test1()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ class ScopeClosingBraceUnitTest extends AbstractSniffUnitTest
public function getErrorList()
{
return [
7 => 1,
11 => 1,
13 => 1,
24 => 1,
Expand Down