Skip to content

Commit c0e4cfb

Browse files
authored
Merge pull request #80 from PHPCSStandards/feature/generic-openingfunctionbrace-check-space-before-brace
Generic/OpeningFunctionBrace*: check spacing before brace for empty functions
2 parents 9fecccc + 1c10cba commit c0e4cfb

9 files changed

+45
-19
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,10 @@ 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
78+
- Generic.Functions.OpeningFunctionBraceKernighanRitchie will now check the spacing before the opening brace for empty functions
79+
- Thanks to Juliette Reinders Folmer (@jrfnl) for the patch
7680
- PSR2.Classes.PropertyDeclaration now enforces that the readonly modifier comes after the visibility modifier
7781
- PSR2 and PSR12 do not have documented rules for this as they pre-date the readonly modifier
7882
- 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/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/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/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()

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)