Skip to content

Commit 17e17ac

Browse files
committed
Squiz/FunctionSpacing: bug fix - prevent fixer conflict with itself
The `Squiz.WhiteSpace.FunctionSpacing` sniff demands # lines above and below a function declaration, but when determining the number of lines above or below, it does not care for additional code on the same line as the function declaration. I.e.: it does not demand that a function declaration starts on its own line or that the close brace is on its own line (there are other sniffs for that). The sniff also tries to prevent "double" errors for the same issue, i.e. when two functions are each on their own line without blank lines between them, the sniff will only throw one error for "spacing after" the first function and will not throw an error for "spacing before" for the second function. To determine whether the "spacing before" error needs to be hidden, the sniff tries to check whether the tokens on the previous line indicate the line contains a function declaration. As things were, however, this check could _bow out_ too early as it stopped at a scope opener for a wrapping construct (class), however, that wrapping construct _could_ be declared on the same line as the function, which means that in that case, the sniff would not determine the `$foundLines` correctly, as it stops on the current line at the scope opener instead of on a non-blank line above the function line. This commit fixes this bug by changing the `$stopAt` value to contain the wrapping scope opener applicable to the code _before_ the function line. This allows the `$foundLines` to be determined correctly and prevents the fixer conflict. Includes additional tests. Fixes 3904
1 parent b50cf54 commit 17e17ac

File tree

5 files changed

+34
-8
lines changed

5 files changed

+34
-8
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,8 @@ The file documents changes to the PHP_CodeSniffer project.
157157
- Thanks to @simonsan for the patch
158158
- Fixed bug #3893 : Generic/DocComment : the SpacingAfterTagGroup fixer could accidentally remove ignore annotations
159159
- Thanks to Juliette Reinders Folmer (@jrfnl) for the patch
160+
- Fixed bug #3904 : Squiz/FunctionSpacing : prevent potential fixer conflict
161+
- Thanks to Juliette Reinders Folmer (@jrfnl) for the patch
160162
- Fixed bug #3906 : Tokenizer/CSS: fixed a bug related to the unsupported slash comment syntax
161163
- Thanks to Dan Wallis (@fredden) for the patch
162164

src/Standards/Squiz/Sniffs/WhiteSpace/FunctionSpacingSniff.php

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -262,22 +262,21 @@ public function process(File $phpcsFile, $stackPtr)
262262
$prevContent = $phpcsFile->findPrevious(T_WHITESPACE, ($tokens[$prevContent]['comment_opener'] - 1), null, true);
263263
}
264264

265-
$prevLineToken = $prevContent;
266-
267265
// Before we throw an error, check that we are not throwing an error
268266
// for another function. We don't want to error for no blank lines after
269267
// the previous function and no blank lines before this one as well.
270-
$prevLine = ($tokens[$prevContent]['line'] - 1);
271-
$i = ($stackPtr - 1);
272-
$foundLines = 0;
273-
274268
$stopAt = 0;
275-
if (isset($tokens[$stackPtr]['conditions']) === true) {
276-
$conditions = $tokens[$stackPtr]['conditions'];
269+
if (isset($tokens[$prevLineToken]['conditions']) === true) {
270+
$conditions = $tokens[$prevLineToken]['conditions'];
277271
$conditions = array_keys($conditions);
278272
$stopAt = array_pop($conditions);
279273
}
280274

275+
$prevLineToken = $prevContent;
276+
$prevLine = ($tokens[$prevContent]['line'] - 1);
277+
$i = ($stackPtr - 1);
278+
$foundLines = 0;
279+
281280
while ($currentLine !== $prevLine && $currentLine > 1 && $i > $stopAt) {
282281
if ($tokens[$i]['code'] === T_FUNCTION) {
283282
// Found another interface or abstract function.

src/Standards/Squiz/Tests/WhiteSpace/FunctionSpacingUnitTest.1.inc

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -574,3 +574,11 @@ class ClassWithAttributes {
574574
// phpcs:set Squiz.WhiteSpace.FunctionSpacing spacing 2
575575
// phpcs:set Squiz.WhiteSpace.FunctionSpacing spacingBeforeFirst 2
576576
// phpcs:set Squiz.WhiteSpace.FunctionSpacing spacingAfterLast 2
577+
578+
// Issue #3904.
579+
echo 'this line belongs with the #3904 test';
580+
class Person {public function __construct($name){}}
581+
echo 'this line belongs with the #3904 test';
582+
583+
function Foo() {} function bar($name){}
584+
echo 'this line belongs with the #3904 test';

src/Standards/Squiz/Tests/WhiteSpace/FunctionSpacingUnitTest.1.inc.fixed

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -656,3 +656,18 @@ class ClassWithAttributes {
656656
// phpcs:set Squiz.WhiteSpace.FunctionSpacing spacing 2
657657
// phpcs:set Squiz.WhiteSpace.FunctionSpacing spacingBeforeFirst 2
658658
// phpcs:set Squiz.WhiteSpace.FunctionSpacing spacingAfterLast 2
659+
660+
// Issue #3904.
661+
echo 'this line belongs with the #3904 test';
662+
663+
664+
class Person {public function __construct($name){}}
665+
666+
667+
echo 'this line belongs with the #3904 test';
668+
669+
670+
function Foo() {} function bar($name){}
671+
672+
673+
echo 'this line belongs with the #3904 test';

src/Standards/Squiz/Tests/WhiteSpace/FunctionSpacingUnitTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,8 @@ public function getErrorList($testFile='')
9595
553 => 1,
9696
560 => 1,
9797
566 => 1,
98+
580 => 2,
99+
583 => 3,
98100
];
99101

100102
case 'FunctionSpacingUnitTest.2.inc':

0 commit comments

Comments
 (0)