Skip to content

Commit 1c10cba

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 5f27b94 commit 1c10cba

File tree

5 files changed

+25
-9
lines changed

5 files changed

+25
-9
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,8 @@ The file documents changes to the PHP_CodeSniffer project.
7373
- Thanks to Juliette Reinders Folmer (@jrfnl) for the patch
7474
- Sniff error messages are now more informative to help bugs get reported to the correct project
7575
- Thanks to Juliette Reinders Folmer (@jrfnl) for the patch
76+
- Generic.Functions.OpeningFunctionBraceBsdAllman will now check the brace indent before the opening brace for empty functions
77+
- Thanks to Juliette Reinders Folmer (@jrfnl) for the patch
7678
- Generic.Functions.OpeningFunctionBraceKernighanRitchie will now check the spacing before the opening brace for empty functions
7779
- Thanks to Juliette Reinders Folmer (@jrfnl) for the patch
7880
- PSR2.Classes.PropertyDeclaration now enforces that the readonly modifier comes after the visibility modifier

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)