Skip to content

Commit 4c89af7

Browse files
committed
Avoid multiple loops when fixing whitespace error
1 parent e2b1a9c commit 4c89af7

File tree

4 files changed

+24
-11
lines changed

4 files changed

+24
-11
lines changed

src/Standards/PSR12/Sniffs/Functions/ReturnTypeDeclarationSniff.php

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -69,23 +69,24 @@ public function process(File $phpcsFile, int $stackPtr)
6969

7070
if ($tokens[($returnType - 1)]['code'] !== T_WHITESPACE
7171
|| $tokens[($returnType - 1)]['content'] !== ' '
72-
|| $tokens[($returnType - 2)]['code'] !== T_COLON
72+
|| ($returnType - 2) !== $colon
7373
) {
7474
$error = 'There must be a single space between the colon and type in a return type declaration';
75-
if ($tokens[($returnType - 1)]['code'] === T_WHITESPACE
76-
&& $tokens[($returnType - 2)]['code'] === T_COLON
77-
) {
78-
$fix = $phpcsFile->addFixableError($error, $returnType, 'SpaceBeforeReturnType');
79-
if ($fix === true) {
80-
$phpcsFile->fixer->replaceToken(($returnType - 1), ' ');
81-
}
82-
} elseif ($tokens[($returnType - 1)]['code'] === T_COLON) {
75+
76+
$nonWhitespaceToken = $phpcsFile->findNext(T_WHITESPACE, ($colon + 1), $returnType, true);
77+
if ($nonWhitespaceToken !== false) {
78+
$phpcsFile->addError($error, $returnType, 'SpaceBeforeReturnType');
79+
} else {
8380
$fix = $phpcsFile->addFixableError($error, $returnType, 'SpaceBeforeReturnType');
8481
if ($fix === true) {
82+
$phpcsFile->fixer->beginChangeset();
83+
for ($i = ($returnType - 1); $i > $colon; $i--) {
84+
$phpcsFile->fixer->replaceToken($i, '');
85+
}
86+
8587
$phpcsFile->fixer->addContentBefore($returnType, ' ');
88+
$phpcsFile->fixer->endChangeset();
8689
}
87-
} else {
88-
$phpcsFile->addError($error, $returnType, 'SpaceBeforeReturnType');
8990
}
9091
}
9192

src/Standards/PSR12/Tests/Functions/ReturnTypeDeclarationUnitTest.1.inc

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,3 +64,11 @@ function functionName(?string $arg1, ?int &$arg2):
6464
fn (?\DateTime $arg) : ?\DateTime => $arg;
6565

6666
return (!$a ? [ new class { public function b(): c {} } ] : []);
67+
68+
// This should be fixed in one fixer loop iteration.
69+
function multipleWhitespaceTokens():
70+
71+
72+
73+
74+
?int {}

src/Standards/PSR12/Tests/Functions/ReturnTypeDeclarationUnitTest.1.inc.fixed

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,3 +60,6 @@ function functionName(?string $arg1, ?int &$arg2): ?string {}
6060
fn (?\DateTime $arg): ?\DateTime => $arg;
6161

6262
return (!$a ? [ new class { public function b(): c {} } ] : []);
63+
64+
// This should be fixed in one fixer loop iteration.
65+
function multipleWhitespaceTokens(): ?int {}

src/Standards/PSR12/Tests/Functions/ReturnTypeDeclarationUnitTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ protected function getErrorList($testFile = '')
4848
60 => 1,
4949
62 => 1,
5050
64 => 1,
51+
74 => 1,
5152
];
5253
default:
5354
return [];

0 commit comments

Comments
 (0)