From d74714d330321fca93c5fc5493f8d7bb4cdcca14 Mon Sep 17 00:00:00 2001 From: Dan Wallis Date: Sun, 22 Sep 2024 21:08:19 +0100 Subject: [PATCH] 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. --- ...unctionDeclarationArgumentSpacingSniff.php | 34 +++++++++++++------ ...tionDeclarationArgumentSpacingUnitTest.inc | 4 +++ ...clarationArgumentSpacingUnitTest.inc.fixed | 2 ++ ...tionDeclarationArgumentSpacingUnitTest.php | 1 + 4 files changed, 30 insertions(+), 11 deletions(-) diff --git a/src/Standards/Squiz/Sniffs/Functions/FunctionDeclarationArgumentSpacingSniff.php b/src/Standards/Squiz/Sniffs/Functions/FunctionDeclarationArgumentSpacingSniff.php index 7357a507e0..11cdba8d2e 100644 --- a/src/Standards/Squiz/Sniffs/Functions/FunctionDeclarationArgumentSpacingSniff.php +++ b/src/Standards/Squiz/Sniffs/Functions/FunctionDeclarationArgumentSpacingSniff.php @@ -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 @@ -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; diff --git a/src/Standards/Squiz/Tests/Functions/FunctionDeclarationArgumentSpacingUnitTest.inc b/src/Standards/Squiz/Tests/Functions/FunctionDeclarationArgumentSpacingUnitTest.inc index 33564e2e0b..07a6d452f5 100644 --- a/src/Standards/Squiz/Tests/Functions/FunctionDeclarationArgumentSpacingUnitTest.inc +++ b/src/Standards/Squiz/Tests/Functions/FunctionDeclarationArgumentSpacingUnitTest.inc @@ -109,3 +109,7 @@ $a = function ($var1, $var2=false) use ( ) {}; fn ($a,$b = null) => $a($b); + +function multipleWhitespaceTokensAfterType(int + + $number) {} diff --git a/src/Standards/Squiz/Tests/Functions/FunctionDeclarationArgumentSpacingUnitTest.inc.fixed b/src/Standards/Squiz/Tests/Functions/FunctionDeclarationArgumentSpacingUnitTest.inc.fixed index 68fb1c1c37..cb4265057c 100644 --- a/src/Standards/Squiz/Tests/Functions/FunctionDeclarationArgumentSpacingUnitTest.inc.fixed +++ b/src/Standards/Squiz/Tests/Functions/FunctionDeclarationArgumentSpacingUnitTest.inc.fixed @@ -109,3 +109,5 @@ $a = function ($var1, $var2=false) use ( ) {}; fn ($a, $b=null) => $a($b); + +function multipleWhitespaceTokensAfterType(int $number) {} diff --git a/src/Standards/Squiz/Tests/Functions/FunctionDeclarationArgumentSpacingUnitTest.php b/src/Standards/Squiz/Tests/Functions/FunctionDeclarationArgumentSpacingUnitTest.php index 62ccfef2ec..391463bbea 100644 --- a/src/Standards/Squiz/Tests/Functions/FunctionDeclarationArgumentSpacingUnitTest.php +++ b/src/Standards/Squiz/Tests/Functions/FunctionDeclarationArgumentSpacingUnitTest.php @@ -68,6 +68,7 @@ public function getErrorList() 106 => 1, 107 => 2, 111 => 3, + 113 => 1, ]; }//end getErrorList()