Skip to content

Commit 103a84d

Browse files
authored
Merge pull request #93 from PHPCSStandards/feature/3904-squiz-functionspacing-fixer-conflict-with-itself
Squiz/FunctionSpacing: bug fix - prevent fixer conflict with itself
2 parents b50cf54 + 17e17ac commit 103a84d

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)