From 1f3ed7605d044a77757e205fac15f4eb55013003 Mon Sep 17 00:00:00 2001 From: Dan Wallis Date: Sun, 22 Sep 2024 21:08:19 +0100 Subject: [PATCH 1/5] Fix fixer conflict: PSR12/Squiz.Functions.FunctionDeclarationArgumentSpacing --- ...unctionDeclarationArgumentSpacingSniff.php | 26 +++++++++++++------ ...tionDeclarationArgumentSpacingUnitTest.inc | 4 +++ ...clarationArgumentSpacingUnitTest.inc.fixed | 3 +++ ...tionDeclarationArgumentSpacingUnitTest.php | 1 + 4 files changed, 26 insertions(+), 8 deletions(-) diff --git a/src/Standards/Squiz/Sniffs/Functions/FunctionDeclarationArgumentSpacingSniff.php b/src/Standards/Squiz/Sniffs/Functions/FunctionDeclarationArgumentSpacingSniff.php index 7357a507e0..5b382e1fc2 100644 --- a/src/Standards/Squiz/Sniffs/Functions/FunctionDeclarationArgumentSpacingSniff.php +++ b/src/Standards/Squiz/Sniffs/Functions/FunctionDeclarationArgumentSpacingSniff.php @@ -235,12 +235,13 @@ 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'], @@ -248,13 +249,22 @@ public function processBracket($phpcsFile, $openBracket) ]; $fix = $phpcsFile->addFixableError($error, $typeHintToken, 'SpacingAfterHint', $data); if ($fix === true) { - if ($gap === 0) { - $phpcsFile->fixer->addContent($typeHintToken, ' '); + $phpcsFile->fixer->beginChangeset(); + $i = $typeHintToken; + + if ($tokens[($i + 1)]['code'] === T_WHITESPACE) { + $phpcsFile->fixer->replaceToken(++$i, ' '); } else { - $phpcsFile->fixer->replaceToken(($typeHintToken + 1), ' '); + $phpcsFile->fixer->addContent($typeHintToken, ' '); } + + while ($tokens[++$i]['code'] === T_WHITESPACE) { + $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..075b137675 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); + +public function newlineAfterType(int +$number) +{} diff --git a/src/Standards/Squiz/Tests/Functions/FunctionDeclarationArgumentSpacingUnitTest.inc.fixed b/src/Standards/Squiz/Tests/Functions/FunctionDeclarationArgumentSpacingUnitTest.inc.fixed index 68fb1c1c37..f212e63019 100644 --- a/src/Standards/Squiz/Tests/Functions/FunctionDeclarationArgumentSpacingUnitTest.inc.fixed +++ b/src/Standards/Squiz/Tests/Functions/FunctionDeclarationArgumentSpacingUnitTest.inc.fixed @@ -109,3 +109,6 @@ $a = function ($var1, $var2=false) use ( ) {}; fn ($a, $b=null) => $a($b); + +public function newlineAfterType(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() From 4e97c930ccc3ba5c5d3d1aae467ecfca76e2a9e5 Mon Sep 17 00:00:00 2001 From: Dan Wallis Date: Tue, 7 Jan 2025 20:19:26 +0000 Subject: [PATCH 2/5] Improve error message text --- .../FunctionDeclarationArgumentSpacingSniff.php | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/Standards/Squiz/Sniffs/Functions/FunctionDeclarationArgumentSpacingSniff.php b/src/Standards/Squiz/Sniffs/Functions/FunctionDeclarationArgumentSpacingSniff.php index 5b382e1fc2..0813a45e87 100644 --- a/src/Standards/Squiz/Sniffs/Functions/FunctionDeclarationArgumentSpacingSniff.php +++ b/src/Standards/Squiz/Sniffs/Functions/FunctionDeclarationArgumentSpacingSniff.php @@ -245,9 +245,15 @@ public function processBracket($phpcsFile, $openBracket) $error = 'Expected 1 space between type hint and argument "%s"; %s found'; $data = [ $param['name'], - $gap, ]; - $fix = $phpcsFile->addFixableError($error, $typeHintToken, 'SpacingAfterHint', $data); + $found = str_replace(["\r", "\n", "\t"], ['\r', '\n', '\t'], $gap); + if ($found === $gap) { + $data[] = strlen($gap); + } else { + $data[] = '"'.$found.'"'; + } + + $fix = $phpcsFile->addFixableError($error, $typeHintToken, 'SpacingAfterHint', $data); if ($fix === true) { $phpcsFile->fixer->beginChangeset(); $i = $typeHintToken; From a32b78802f79e186141d2b0aca299a88bd61848c Mon Sep 17 00:00:00 2001 From: Dan Wallis Date: Tue, 7 Jan 2025 20:26:56 +0000 Subject: [PATCH 3/5] Simplify fixer logic --- .../FunctionDeclarationArgumentSpacingSniff.php | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/src/Standards/Squiz/Sniffs/Functions/FunctionDeclarationArgumentSpacingSniff.php b/src/Standards/Squiz/Sniffs/Functions/FunctionDeclarationArgumentSpacingSniff.php index 0813a45e87..eff995939c 100644 --- a/src/Standards/Squiz/Sniffs/Functions/FunctionDeclarationArgumentSpacingSniff.php +++ b/src/Standards/Squiz/Sniffs/Functions/FunctionDeclarationArgumentSpacingSniff.php @@ -256,15 +256,9 @@ public function processBracket($phpcsFile, $openBracket) $fix = $phpcsFile->addFixableError($error, $typeHintToken, 'SpacingAfterHint', $data); if ($fix === true) { $phpcsFile->fixer->beginChangeset(); - $i = $typeHintToken; + $phpcsFile->fixer->addContent($typeHintToken, ' '); - if ($tokens[($i + 1)]['code'] === T_WHITESPACE) { - $phpcsFile->fixer->replaceToken(++$i, ' '); - } else { - $phpcsFile->fixer->addContent($typeHintToken, ' '); - } - - while ($tokens[++$i]['code'] === T_WHITESPACE) { + for ($i = ($typeHintToken + 1); $tokens[$i]['code'] === T_WHITESPACE; $i++) { $phpcsFile->fixer->replaceToken($i, ''); } From 7a2a574d1246c3d318aa65df7619414df1ec7e58 Mon Sep 17 00:00:00 2001 From: Dan Wallis Date: Tue, 7 Jan 2025 20:31:16 +0000 Subject: [PATCH 4/5] Improve test per review suggestion --- .../FunctionDeclarationArgumentSpacingUnitTest.inc | 6 +++--- .../FunctionDeclarationArgumentSpacingUnitTest.inc.fixed | 3 +-- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/src/Standards/Squiz/Tests/Functions/FunctionDeclarationArgumentSpacingUnitTest.inc b/src/Standards/Squiz/Tests/Functions/FunctionDeclarationArgumentSpacingUnitTest.inc index 075b137675..07a6d452f5 100644 --- a/src/Standards/Squiz/Tests/Functions/FunctionDeclarationArgumentSpacingUnitTest.inc +++ b/src/Standards/Squiz/Tests/Functions/FunctionDeclarationArgumentSpacingUnitTest.inc @@ -110,6 +110,6 @@ $a = function ($var1, $var2=false) use ( fn ($a,$b = null) => $a($b); -public function newlineAfterType(int -$number) -{} +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 f212e63019..cb4265057c 100644 --- a/src/Standards/Squiz/Tests/Functions/FunctionDeclarationArgumentSpacingUnitTest.inc.fixed +++ b/src/Standards/Squiz/Tests/Functions/FunctionDeclarationArgumentSpacingUnitTest.inc.fixed @@ -110,5 +110,4 @@ $a = function ($var1, $var2=false) use ( fn ($a, $b=null) => $a($b); -public function newlineAfterType(int $number) -{} +function multipleWhitespaceTokensAfterType(int $number) {} From 1a3915497827bcf02f17ad5fde2efe1f474bd451 Mon Sep 17 00:00:00 2001 From: Dan Wallis Date: Thu, 9 Jan 2025 13:48:28 +0000 Subject: [PATCH 5/5] Make use of Common::prepareForOutput() method --- .../Functions/FunctionDeclarationArgumentSpacingSniff.php | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/Standards/Squiz/Sniffs/Functions/FunctionDeclarationArgumentSpacingSniff.php b/src/Standards/Squiz/Sniffs/Functions/FunctionDeclarationArgumentSpacingSniff.php index eff995939c..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 @@ -246,11 +247,12 @@ public function processBracket($phpcsFile, $openBracket) $data = [ $param['name'], ]; - $found = str_replace(["\r", "\n", "\t"], ['\r', '\n', '\t'], $gap); - if ($found === $gap) { + if (trim($gap, ' ') === '') { + // Gap contains only space characters: report the number of spaces. $data[] = strlen($gap); } else { - $data[] = '"'.$found.'"'; + // Gap contains more than just spaces: render these for better clarity. + $data[] = '"'.Common::prepareForOutput($gap).'"'; } $fix = $phpcsFile->addFixableError($error, $typeHintToken, 'SpacingAfterHint', $data);