Skip to content

Commit 5f27b94

Browse files
committed
Generic/OpeningFunctionBraceKernighanRitchie: 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 "SpaceBeforeBrace" check. Fixed now. Includes tests.
1 parent 9fecccc commit 5f27b94

File tree

5 files changed

+20
-10
lines changed

5 files changed

+20
-10
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.OpeningFunctionBraceKernighanRitchie will now check the spacing before the opening brace for empty functions
77+
- Thanks to Juliette Reinders Folmer (@jrfnl) for the patch
7678
- PSR2.Classes.PropertyDeclaration now enforces that the readonly modifier comes after the visibility modifier
7779
- PSR2 and PSR12 do not have documented rules for this as they pre-date the readonly modifier
7880
- PSR-PER has been used to confirm the order of this keyword so it can be applied to PSR2 and PSR12 correctly

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

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -130,17 +130,15 @@ public function process(File $phpcsFile, $stackPtr)
130130
$ignore[] = T_WHITESPACE;
131131
$next = $phpcsFile->findNext($ignore, ($openingBrace + 1), null, true);
132132
if ($tokens[$next]['line'] === $tokens[$openingBrace]['line']) {
133-
if ($next === $tokens[$stackPtr]['scope_closer']
134-
|| $tokens[$next]['code'] === T_CLOSE_TAG
133+
// Only throw this error when this is not an empty function.
134+
if ($next !== $tokens[$stackPtr]['scope_closer']
135+
&& $tokens[$next]['code'] !== T_CLOSE_TAG
135136
) {
136-
// Ignore empty functions.
137-
return;
138-
}
139-
140-
$error = 'Opening brace must be the last content on the line';
141-
$fix = $phpcsFile->addFixableError($error, $openingBrace, 'ContentAfterBrace');
142-
if ($fix === true) {
143-
$phpcsFile->fixer->addNewline($openingBrace);
137+
$error = 'Opening brace must be the last content on the line';
138+
$fix = $phpcsFile->addFixableError($error, $openingBrace, 'ContentAfterBrace');
139+
if ($fix === true) {
140+
$phpcsFile->fixer->addNewline($openingBrace);
141+
}
144142
}
145143
}
146144

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,3 +208,7 @@ function myFunction($a, $lot, $of, $params)
208208
: array { // phpcs:ignore Standard.Category.Sniff -- for reasons.
209209
return null;
210210
}
211+
212+
function myFunction() {}
213+
function myFunction() {} // Too many spaces with an empty function.
214+
function myFunction() {} // Too many spaces (tab) with an empty function.

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,3 +196,7 @@ function myFunction($a, $lot, $of, $params)
196196
: array { // phpcs:ignore Standard.Category.Sniff -- for reasons.
197197
return null;
198198
}
199+
200+
function myFunction() {}
201+
function myFunction() {} // Too many spaces with an empty function.
202+
function myFunction() {} // Too many spaces (tab) with an empty function.

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ public function getErrorList()
5252
191 => 1,
5353
197 => 1,
5454
203 => 1,
55+
213 => 1,
56+
214 => 1,
5557
];
5658

5759
}//end getErrorList()

0 commit comments

Comments
 (0)