Skip to content

Commit 8f06255

Browse files
committed
Generic/OpeningFunctionBraceBsdAllman: check spacing before brace for empty functions
As things were, when an empty function was detected, the sniff would bow out and not execute the "BraceIndent" check. Fixed now. Includes tests.
1 parent ce46913 commit 8f06255

File tree

4 files changed

+23
-9
lines changed

4 files changed

+23
-9
lines changed

src/Standards/Generic/Sniffs/Functions/OpeningFunctionBraceBsdAllmanSniff.php

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -170,15 +170,13 @@ public function process(File $phpcsFile, $stackPtr)
170170
$ignore[] = T_WHITESPACE;
171171
$next = $phpcsFile->findNext($ignore, ($openingBrace + 1), null, true);
172172
if ($tokens[$next]['line'] === $tokens[$openingBrace]['line']) {
173-
if ($next === $tokens[$stackPtr]['scope_closer']) {
174-
// Ignore empty functions.
175-
return;
176-
}
177-
178-
$error = 'Opening brace must be the last content on the line';
179-
$fix = $phpcsFile->addFixableError($error, $openingBrace, 'ContentAfterBrace');
180-
if ($fix === true) {
181-
$phpcsFile->fixer->addNewline($openingBrace);
173+
// Only throw this error when this is not an empty function.
174+
if ($next !== $tokens[$stackPtr]['scope_closer']) {
175+
$error = 'Opening brace must be the last content on the line';
176+
$fix = $phpcsFile->addFixableError($error, $openingBrace, 'ContentAfterBrace');
177+
if ($fix === true) {
178+
$phpcsFile->fixer->addNewline($openingBrace);
179+
}
182180
}
183181
}
184182

src/Standards/Generic/Tests/Functions/OpeningFunctionBraceBsdAllmanUnitTest.inc

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,3 +261,10 @@ class Issue3357WithComment
261261
// code here.
262262
}
263263
}
264+
265+
function myFunction()
266+
{}
267+
function myFunction()
268+
{} // Too many spaces indent with an empty function.
269+
function myFunction()
270+
{} // Too little spaces indent with an empty function.

src/Standards/Generic/Tests/Functions/OpeningFunctionBraceBsdAllmanUnitTest.inc.fixed

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,3 +278,10 @@ class Issue3357WithComment
278278
// code here.
279279
}
280280
}
281+
282+
function myFunction()
283+
{}
284+
function myFunction()
285+
{} // Too many spaces indent with an empty function.
286+
function myFunction()
287+
{} // Too little spaces indent with an empty function.

src/Standards/Generic/Tests/Functions/OpeningFunctionBraceBsdAllmanUnitTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,8 @@ public function getErrorList()
6161
244 => 1,
6262
252 => 1,
6363
260 => 1,
64+
268 => 1,
65+
270 => 1,
6466
];
6567

6668
}//end getErrorList()

0 commit comments

Comments
 (0)