Skip to content

Commit 8f7a1de

Browse files
committed
RequireArrowFunctionSniff: Fixed fixer
1 parent db3b3b5 commit 8f7a1de

File tree

5 files changed

+20
-9
lines changed

5 files changed

+20
-9
lines changed

SlevomatCodingStandard/Sniffs/Functions/RequireArrowFunctionSniff.php

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use SlevomatCodingStandard\Helpers\TokenHelper;
99
use function count;
1010
use const T_BITWISE_AND;
11+
use const T_CLOSE_PARENTHESIS;
1112
use const T_CLOSURE;
1213
use const T_FN;
1314
use const T_RETURN;
@@ -73,15 +74,25 @@ public function process(File $phpcsFile, $closurePointer): void
7374

7475
$pointerAfterReturn = TokenHelper::findNextExcluding($phpcsFile, T_WHITESPACE, $returnPointer + 1);
7576
$semicolonAfterReturn = $this->findSemicolon($phpcsFile, $returnPointer);
77+
$usePointer = TokenHelper::findNext($phpcsFile, T_USE, $tokens[$closurePointer]['parenthesis_closer'] + 1, $tokens[$closurePointer]['scope_opener']);
78+
$nonWhitespacePointerBeforeScopeOpener = TokenHelper::findPreviousExcluding($phpcsFile, T_WHITESPACE, $tokens[$closurePointer]['scope_opener'] - 1);
7679

7780
$phpcsFile->fixer->beginChangeset();
7881
$phpcsFile->fixer->replaceToken($closurePointer, 'fn');
7982

80-
for ($i = $tokens[$closurePointer]['parenthesis_closer'] + 1; $i < $pointerAfterReturn; $i++) {
83+
if ($usePointer !== null) {
84+
$useParenthesiCloserPointer = TokenHelper::findNext($phpcsFile, T_CLOSE_PARENTHESIS, $usePointer + 1);
85+
$nonWhitespacePointerAfterUseParanthesisCloser = TokenHelper::findNextExcluding($phpcsFile, T_WHITESPACE, $useParenthesiCloserPointer + 1);
86+
for ($i = $tokens[$closurePointer]['parenthesis_closer'] + 1; $i < $nonWhitespacePointerAfterUseParanthesisCloser; $i++) {
87+
$phpcsFile->fixer->replaceToken($i, '');
88+
}
89+
}
90+
91+
for ($i = $nonWhitespacePointerBeforeScopeOpener + 1; $i < $pointerAfterReturn; $i++) {
8192
$phpcsFile->fixer->replaceToken($i, '');
8293
}
8394

84-
$phpcsFile->fixer->addContent($tokens[$closurePointer]['parenthesis_closer'], ' => ');
95+
$phpcsFile->fixer->addContent($nonWhitespacePointerBeforeScopeOpener, ' => ');
8596

8697
for ($i = $semicolonAfterReturn; $i <= $tokens[$closurePointer]['scope_closer']; $i++) {
8798
$phpcsFile->fixer->replaceToken($i, '');
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
<?php // lint >= 7.4
22

3-
$a = fn ($aa) => fn ($bb) => fn ($cc) => $aa + $bb + $cc;
3+
$a = fn ($aa) => fn ($bb): int => fn ($cc): int => $aa + $bb + $cc;

tests/Sniffs/Functions/data/requireArrowFunctionAllowNestedErrors.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
<?php // lint >= 7.4
22

33
$a = function ($aa) {
4-
return function ($bb) use ($aa) {
5-
return function ($cc) use ($bb, $aa) {
4+
return function ($bb) use ($aa): int {
5+
return function ($cc) use ($bb, $aa): int {
66
return $aa + $bb + $cc;
77
};
88
};

tests/Sniffs/Functions/data/requireArrowFunctionDisallowNestedErrors.fixed.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@
44

55
$b = fn ($bb) => $bb + $a;
66

7-
$c = function ($cc) {
8-
return fn () => $cc + 1;
7+
$c = function ($cc): int {
8+
return fn (): int => $cc + 1;
99
};

tests/Sniffs/Functions/data/requireArrowFunctionDisallowNestedErrors.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88
return $bb + $a;
99
};
1010

11-
$c = function ($cc) {
12-
return function () use ($cc) {
11+
$c = function ($cc): int {
12+
return function () use ($cc): int {
1313
return $cc + 1;
1414
};
1515
};

0 commit comments

Comments
 (0)