From 1f3ed7605d044a77757e205fac15f4eb55013003 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 --- ...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()