Skip to content

Commit 7a46d7b

Browse files
committed
Improved fixer for when there are multiple blank lines after the open bracket
1 parent c436252 commit 7a46d7b

File tree

5 files changed

+36
-3
lines changed

5 files changed

+36
-3
lines changed

package.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@ http://pear.php.net/dtd/package-2.0.xsd">
126126
-- If you don't want this new behaviour, exclude the SpacingAfterReference error message in a ruleset.xml file
127127
- Squiz.Functions.FunctionDeclarationArgumentSpacing now checks for no space after a variadic operator
128128
-- If you don't want this new behaviour, exclude the SpacingAfterVariadic error message in a ruleset.xml file
129+
- Squiz.Functions.MultiLineFunctionDeclaration now has improved fixing for the FirstParamSpacing and UseFirstParamSpacing errors
129130
- Squiz.Operators.IncrementDecrementUsage now suggests pre-increment of variables instead of post-increment
130131
-- This change does not enforce pre-increment over post-increment; only the suggestion has changed
131132
-- Thanks to Juliette Reinders Folmer for the patch

src/Standards/Squiz/Sniffs/Functions/MultiLineFunctionDeclarationSniff.php

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -204,10 +204,25 @@ public function processBracket($phpcsFile, $openBracket, $tokens, $type='functio
204204
$error = 'The first parameter of a multi-line '.$type.' declaration must be on the line after the opening bracket';
205205
$fix = $phpcsFile->addFixableError($error, $next, $errorPrefix.'FirstParamSpacing');
206206
if ($fix === true) {
207-
$phpcsFile->fixer->addNewline($openBracket);
207+
if ($tokens[$next]['line'] === $tokens[$openBracket]['line']) {
208+
$phpcsFile->fixer->addNewline($openBracket);
209+
} else {
210+
$phpcsFile->fixer->beginChangeset();
211+
for ($x = $openBracket; $x < $next; $x++) {
212+
if ($tokens[$x]['line'] === $tokens[$openBracket]['line']) {
213+
continue;
214+
}
215+
216+
if ($tokens[$x]['line'] === $tokens[$next]['line']) {
217+
break;
218+
}
219+
}
220+
221+
$phpcsFile->fixer->endChangeset();
222+
}
208223
}
209-
}
210-
}
224+
}//end if
225+
}//end if
211226

212227
// Each line between the brackets should contain a single parameter.
213228
$lastComma = null;

src/Standards/Squiz/Tests/Functions/MultiLineFunctionDeclarationUnitTest.inc

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,3 +189,11 @@ public function hello(string $greeting // cant fix this
189189
public function hello(string $greeting // phpcs:ignore Standard.Category.Sniff -- for reasons.
190190
) {
191191
}
192+
193+
function foo(
194+
195+
196+
$foo,
197+
$bar
198+
) {
199+
}

src/Standards/Squiz/Tests/Functions/MultiLineFunctionDeclarationUnitTest.inc.fixed

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,3 +203,9 @@ public function hello(string $greeting // phpcs:ignore Standard.Category.Sniff -
203203
)
204204
{
205205
}
206+
207+
function foo(
208+
$foo,
209+
$bar
210+
) {
211+
}

src/Standards/Squiz/Tests/Functions/MultiLineFunctionDeclarationUnitTest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,9 @@ public function getErrorList($testFile='MultiLineFunctionDeclarationUnitTest.inc
5353
182 => 2,
5454
186 => 2,
5555
190 => 2,
56+
194 => 1,
57+
195 => 1,
58+
196 => 1,
5659
];
5760
} else {
5861
$errors = [

0 commit comments

Comments
 (0)