Skip to content

Commit

Permalink
Fix fixer conflict: PSR12/Squiz.Functions.FunctionDeclarationArgument…
Browse files Browse the repository at this point in the history
…Spacing

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.
  • Loading branch information
fredden authored and jrfnl committed Jan 11, 2025
1 parent 3766969 commit bcddabb
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

use PHP_CodeSniffer\Files\File;
use PHP_CodeSniffer\Sniffs\Sniff;
use PHP_CodeSniffer\Util\Common;
use PHP_CodeSniffer\Util\Tokens;

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

$gap = 0;
if ($tokens[($typeHintToken + 1)]['code'] === T_WHITESPACE) {
$gap = $tokens[($typeHintToken + 1)]['length'];
$gap = '';
$i = $typeHintToken;
while ($tokens[++$i]['code'] === T_WHITESPACE) {
$gap .= $tokens[$i]['content'];
}

if ($gap !== 1) {
if ($gap !== ' ') {
$error = 'Expected 1 space between type hint and argument "%s"; %s found';
$data = [
$param['name'],
$gap,
];
$fix = $phpcsFile->addFixableError($error, $typeHintToken, 'SpacingAfterHint', $data);
if (trim($gap, ' ') === '') {
// Gap contains only space characters: report the number of spaces.
$data[] = strlen($gap);
} else {
// Gap contains more than just spaces: render these for better clarity.
$data[] = '"'.Common::prepareForOutput($gap).'"';
}

$fix = $phpcsFile->addFixableError($error, $typeHintToken, 'SpacingAfterHint', $data);
if ($fix === true) {
if ($gap === 0) {
$phpcsFile->fixer->addContent($typeHintToken, ' ');
} else {
$phpcsFile->fixer->replaceToken(($typeHintToken + 1), ' ');
$phpcsFile->fixer->beginChangeset();
$phpcsFile->fixer->addContent($typeHintToken, ' ');

for ($i = ($typeHintToken + 1); $tokens[$i]['code'] === T_WHITESPACE; $i++) {
$phpcsFile->fixer->replaceToken($i, '');
}

$phpcsFile->fixer->endChangeset();
}
}
}//end if
}//end if

$commaToken = false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,3 +109,7 @@ $a = function ($var1, $var2=false) use (
) {};

fn ($a,$b = null) => $a($b);

function multipleWhitespaceTokensAfterType(int

$number) {}
Original file line number Diff line number Diff line change
Expand Up @@ -109,3 +109,5 @@ $a = function ($var1, $var2=false) use (
) {};

fn ($a, $b=null) => $a($b);

function multipleWhitespaceTokensAfterType(int $number) {}
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ public function getErrorList()
106 => 1,
107 => 2,
111 => 3,
113 => 1,
];

}//end getErrorList()
Expand Down

0 comments on commit bcddabb

Please sign in to comment.