Skip to content

Commit d74714d

Browse files
committed
Fix fixer conflict: PSR12/Squiz.Functions.FunctionDeclarationArgumentSpacing
This commit resolves the a fixer conflict between `Squiz.Functions.FunctionDeclarationArgumentSpacing` and `Squiz.WhiteSpace.SuperfluousWhitespace`. This is done by changing the logic within the `FunctionDeclarationArgumentSpacing` sniff to no longer look at (the length of) only one `T_WHITESPACE` token, but instead consider all `T_WHITESPACE` tokens between the type and parameter tokens, and validate on their content (not just length). As part of this, the error message has been changed slightly in some circumstances. The error code has not been changed. * When there are only space characters between the two tokens, the error message remains unchanged. * When there are tabs or newlines included between the two tokens, these are rendered as part of the error message.
1 parent b43e1d8 commit d74714d

File tree

4 files changed

+30
-11
lines changed

4 files changed

+30
-11
lines changed

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

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
use PHP_CodeSniffer\Files\File;
1313
use PHP_CodeSniffer\Sniffs\Sniff;
14+
use PHP_CodeSniffer\Util\Common;
1415
use PHP_CodeSniffer\Util\Tokens;
1516

1617
class FunctionDeclarationArgumentSpacingSniff implements Sniff
@@ -235,26 +236,37 @@ public function processBracket($phpcsFile, $openBracket)
235236
if ($param['type_hint_token'] !== false) {
236237
$typeHintToken = $param['type_hint_end_token'];
237238

238-
$gap = 0;
239-
if ($tokens[($typeHintToken + 1)]['code'] === T_WHITESPACE) {
240-
$gap = $tokens[($typeHintToken + 1)]['length'];
239+
$gap = '';
240+
$i = $typeHintToken;
241+
while ($tokens[++$i]['code'] === T_WHITESPACE) {
242+
$gap .= $tokens[$i]['content'];
241243
}
242244

243-
if ($gap !== 1) {
245+
if ($gap !== ' ') {
244246
$error = 'Expected 1 space between type hint and argument "%s"; %s found';
245247
$data = [
246248
$param['name'],
247-
$gap,
248249
];
249-
$fix = $phpcsFile->addFixableError($error, $typeHintToken, 'SpacingAfterHint', $data);
250+
if (trim($gap, ' ') === '') {
251+
// Gap contains only space characters: report the number of spaces.
252+
$data[] = strlen($gap);
253+
} else {
254+
// Gap contains more than just spaces: render these for better clarity.
255+
$data[] = '"'.Common::prepareForOutput($gap).'"';
256+
}
257+
258+
$fix = $phpcsFile->addFixableError($error, $typeHintToken, 'SpacingAfterHint', $data);
250259
if ($fix === true) {
251-
if ($gap === 0) {
252-
$phpcsFile->fixer->addContent($typeHintToken, ' ');
253-
} else {
254-
$phpcsFile->fixer->replaceToken(($typeHintToken + 1), ' ');
260+
$phpcsFile->fixer->beginChangeset();
261+
$phpcsFile->fixer->addContent($typeHintToken, ' ');
262+
263+
for ($i = ($typeHintToken + 1); $tokens[$i]['code'] === T_WHITESPACE; $i++) {
264+
$phpcsFile->fixer->replaceToken($i, '');
255265
}
266+
267+
$phpcsFile->fixer->endChangeset();
256268
}
257-
}
269+
}//end if
258270
}//end if
259271

260272
$commaToken = false;

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,3 +109,7 @@ $a = function ($var1, $var2=false) use (
109109
) {};
110110

111111
fn ($a,$b = null) => $a($b);
112+
113+
function multipleWhitespaceTokensAfterType(int
114+
115+
$number) {}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,3 +109,5 @@ $a = function ($var1, $var2=false) use (
109109
) {};
110110

111111
fn ($a, $b=null) => $a($b);
112+
113+
function multipleWhitespaceTokensAfterType(int $number) {}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ public function getErrorList()
6868
106 => 1,
6969
107 => 2,
7070
111 => 3,
71+
113 => 1,
7172
];
7273

7374
}//end getErrorList()

0 commit comments

Comments
 (0)