Skip to content

Commit ab09909

Browse files
authored
Merge pull request #620 from fredden/feature/fixer-conflict/PSR12/Squiz.Functions.FunctionDeclarationArgumentSpacing
Fix fixer conflict: `PSR12` / `Squiz.Functions.FunctionDeclarationArgumentSpacing`
2 parents b43e1d8 + d74714d commit ab09909

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)