diff --git a/src/Standards/PSR12/Sniffs/Functions/ReturnTypeDeclarationSniff.php b/src/Standards/PSR12/Sniffs/Functions/ReturnTypeDeclarationSniff.php index b0c610edb7..f783b691d4 100644 --- a/src/Standards/PSR12/Sniffs/Functions/ReturnTypeDeclarationSniff.php +++ b/src/Standards/PSR12/Sniffs/Functions/ReturnTypeDeclarationSniff.php @@ -61,29 +61,35 @@ public function process(File $phpcsFile, int $stackPtr) $returnType = $phpcsFile->findPrevious(T_NULLABLE, ($returnType - 1)); } + $colon = $phpcsFile->findPrevious(T_COLON, ($returnType - 1), $tokens[$stackPtr]['parenthesis_closer']); + if ($colon === false) { + // Parse error / live coding. + return; + } + if ($tokens[($returnType - 1)]['code'] !== T_WHITESPACE || $tokens[($returnType - 1)]['content'] !== ' ' - || $tokens[($returnType - 2)]['code'] !== T_COLON + || ($returnType - 2) !== $colon ) { $error = 'There must be a single space between the colon and type in a return type declaration'; - if ($tokens[($returnType - 1)]['code'] === T_WHITESPACE - && $tokens[($returnType - 2)]['code'] === T_COLON - ) { - $fix = $phpcsFile->addFixableError($error, $returnType, 'SpaceBeforeReturnType'); - if ($fix === true) { - $phpcsFile->fixer->replaceToken(($returnType - 1), ' '); - } - } elseif ($tokens[($returnType - 1)]['code'] === T_COLON) { + + $nonWhitespaceToken = $phpcsFile->findNext(T_WHITESPACE, ($colon + 1), $returnType, true); + if ($nonWhitespaceToken !== false) { + $phpcsFile->addError($error, $returnType, 'SpaceBeforeReturnType'); + } else { $fix = $phpcsFile->addFixableError($error, $returnType, 'SpaceBeforeReturnType'); if ($fix === true) { + $phpcsFile->fixer->beginChangeset(); + for ($i = ($returnType - 1); $i > $colon; $i--) { + $phpcsFile->fixer->replaceToken($i, ''); + } + $phpcsFile->fixer->addContentBefore($returnType, ' '); + $phpcsFile->fixer->endChangeset(); } - } else { - $phpcsFile->addError($error, $returnType, 'SpaceBeforeReturnType'); } } - $colon = $phpcsFile->findPrevious(T_COLON, $returnType); if ($tokens[($colon - 1)]['code'] !== T_CLOSE_PARENTHESIS) { $error = 'There must not be a space before the colon in a return type declaration'; $prev = $phpcsFile->findPrevious(T_WHITESPACE, ($colon - 1), null, true); diff --git a/src/Standards/PSR12/Tests/Functions/ReturnTypeDeclarationUnitTest.inc b/src/Standards/PSR12/Tests/Functions/ReturnTypeDeclarationUnitTest.1.inc similarity index 91% rename from src/Standards/PSR12/Tests/Functions/ReturnTypeDeclarationUnitTest.inc rename to src/Standards/PSR12/Tests/Functions/ReturnTypeDeclarationUnitTest.1.inc index 59ab1aa785..78c4b953a8 100644 --- a/src/Standards/PSR12/Tests/Functions/ReturnTypeDeclarationUnitTest.inc +++ b/src/Standards/PSR12/Tests/Functions/ReturnTypeDeclarationUnitTest.1.inc @@ -64,3 +64,11 @@ function functionName(?string $arg1, ?int &$arg2): fn (?\DateTime $arg) : ?\DateTime => $arg; return (!$a ? [ new class { public function b(): c {} } ] : []); + +// This should be fixed in one fixer loop iteration. +function multipleWhitespaceTokens(): + + + + + ?int {} diff --git a/src/Standards/PSR12/Tests/Functions/ReturnTypeDeclarationUnitTest.inc.fixed b/src/Standards/PSR12/Tests/Functions/ReturnTypeDeclarationUnitTest.1.inc.fixed similarity index 92% rename from src/Standards/PSR12/Tests/Functions/ReturnTypeDeclarationUnitTest.inc.fixed rename to src/Standards/PSR12/Tests/Functions/ReturnTypeDeclarationUnitTest.1.inc.fixed index cd79f78163..5c3c03d3ea 100644 --- a/src/Standards/PSR12/Tests/Functions/ReturnTypeDeclarationUnitTest.inc.fixed +++ b/src/Standards/PSR12/Tests/Functions/ReturnTypeDeclarationUnitTest.1.inc.fixed @@ -60,3 +60,6 @@ function functionName(?string $arg1, ?int &$arg2): ?string {} fn (?\DateTime $arg): ?\DateTime => $arg; return (!$a ? [ new class { public function b(): c {} } ] : []); + +// This should be fixed in one fixer loop iteration. +function multipleWhitespaceTokens(): ?int {} diff --git a/src/Standards/PSR12/Tests/Functions/ReturnTypeDeclarationUnitTest.2.inc b/src/Standards/PSR12/Tests/Functions/ReturnTypeDeclarationUnitTest.2.inc new file mode 100644 index 0000000000..e1029fa0be --- /dev/null +++ b/src/Standards/PSR12/Tests/Functions/ReturnTypeDeclarationUnitTest.2.inc @@ -0,0 +1,4 @@ + */ - protected function getErrorList() + protected function getErrorList($testFile = '') { - return [ - 27 => 1, - 28 => 1, - 35 => 2, - 41 => 2, - 48 => 2, - 52 => 1, - 55 => 1, - 56 => 1, - 59 => 1, - 60 => 1, - 62 => 1, - 64 => 1, - ]; + switch ($testFile) { + case 'ReturnTypeDeclarationUnitTest.1.inc': + return [ + 27 => 1, + 28 => 1, + 35 => 2, + 41 => 2, + 48 => 2, + 52 => 1, + 55 => 1, + 56 => 1, + 59 => 1, + 60 => 1, + 62 => 1, + 64 => 1, + 74 => 1, + ]; + default: + return []; + } } @@ -54,9 +62,11 @@ protected function getErrorList() * The key of the array should represent the line number and the value * should represent the number of warnings that should occur on that line. * + * @param string $testFile The name of the file being tested. + * * @return array */ - protected function getWarningList() + protected function getWarningList($testFile = '') { return []; }