Skip to content

Commit d26de94

Browse files
committed
Added RequireArrowFunctionSniff
1 parent 1543263 commit d26de94

9 files changed

+238
-0
lines changed

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -431,6 +431,14 @@ However, if you prefer Yoda conditions, you can use `RequireYodaComparisonSniff`
431431

432432
Disallows arrow functions.
433433

434+
#### SlevomatCodingStandard.Functions.RequireArrowFunction 🔧
435+
436+
Requires arrow functions.
437+
438+
Sniff provides the following settings:
439+
440+
* `allowNested` (defaults to `true`)
441+
434442
#### SlevomatCodingStandard.Functions.TrailingCommaInCall 🔧
435443

436444
Commas after the last parameter in function or method call make adding a new parameter easier and result in a cleaner versioning diff.
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace SlevomatCodingStandard\Sniffs\Functions;
4+
5+
use PHP_CodeSniffer\Files\File;
6+
use PHP_CodeSniffer\Sniffs\Sniff;
7+
use SlevomatCodingStandard\Helpers\ScopeHelper;
8+
use SlevomatCodingStandard\Helpers\TokenHelper;
9+
use function count;
10+
use const T_BITWISE_AND;
11+
use const T_CLOSURE;
12+
use const T_FN;
13+
use const T_RETURN;
14+
use const T_SEMICOLON;
15+
use const T_USE;
16+
use const T_WHITESPACE;
17+
18+
class RequireArrowFunctionSniff implements Sniff
19+
{
20+
21+
public const CODE_REQUIRED_ARROW_FUNCTION = 'RequiredArrowFunction';
22+
23+
/** @var bool */
24+
public $allowNested = true;
25+
26+
/**
27+
* @return (int|string)[]
28+
*/
29+
public function register(): array
30+
{
31+
return [
32+
T_CLOSURE,
33+
];
34+
}
35+
36+
/**
37+
* @phpcsSuppress SlevomatCodingStandard.TypeHints.ParameterTypeHint.MissingNativeTypeHint
38+
* @param \PHP_CodeSniffer\Files\File $phpcsFile
39+
* @param int $closurePointer
40+
*/
41+
public function process(File $phpcsFile, $closurePointer): void
42+
{
43+
$tokens = $phpcsFile->getTokens();
44+
45+
$returnPointer = TokenHelper::findNextEffective($phpcsFile, $tokens[$closurePointer]['scope_opener'] + 1);
46+
if ($tokens[$returnPointer]['code'] !== T_RETURN) {
47+
return;
48+
}
49+
50+
$usePointer = TokenHelper::findNextEffective($phpcsFile, $tokens[$closurePointer]['parenthesis_closer'] + 1);
51+
if ($tokens[$usePointer]['code'] === T_USE) {
52+
$useOpenParenthesisPointer = TokenHelper::findNextEffective($phpcsFile, $usePointer + 1);
53+
if (TokenHelper::findNext($phpcsFile, T_BITWISE_AND, $useOpenParenthesisPointer + 1, $tokens[$useOpenParenthesisPointer]['parenthesis_closer']) !== null) {
54+
return;
55+
}
56+
}
57+
58+
if (!$this->allowNested) {
59+
$closureOrArrowFunctionPointer = TokenHelper::findNext($phpcsFile, [T_CLOSURE, T_FN], $tokens[$closurePointer]['scope_opener'] + 1, $tokens[$closurePointer]['scope_closer']);
60+
if ($closureOrArrowFunctionPointer !== null) {
61+
return;
62+
}
63+
}
64+
65+
$fix = $phpcsFile->addFixableError(
66+
'Use arrow function.',
67+
$closurePointer,
68+
self::CODE_REQUIRED_ARROW_FUNCTION
69+
);
70+
if (!$fix) {
71+
return;
72+
}
73+
74+
$pointerAfterReturn = TokenHelper::findNextExcluding($phpcsFile, T_WHITESPACE, $returnPointer + 1);
75+
$semicolonAfterReturn = $this->findSemicolon($phpcsFile, $returnPointer);
76+
77+
$phpcsFile->fixer->beginChangeset();
78+
$phpcsFile->fixer->replaceToken($closurePointer, 'fn');
79+
80+
for ($i = $tokens[$closurePointer]['parenthesis_closer'] + 1; $i < $pointerAfterReturn; $i++) {
81+
$phpcsFile->fixer->replaceToken($i, '');
82+
}
83+
84+
$phpcsFile->fixer->addContent($tokens[$closurePointer]['parenthesis_closer'], ' => ');
85+
86+
for ($i = $semicolonAfterReturn; $i <= $tokens[$closurePointer]['scope_closer']; $i++) {
87+
$phpcsFile->fixer->replaceToken($i, '');
88+
}
89+
90+
$phpcsFile->fixer->endChangeset();
91+
}
92+
93+
private function findSemicolon(File $phpcsFile, int $pointer): int
94+
{
95+
$tokens = $phpcsFile->getTokens();
96+
97+
$semicolonPointer = null;
98+
for ($i = $pointer + 1; $i < count($tokens) - 1; $i++) {
99+
if ($tokens[$i]['code'] !== T_SEMICOLON) {
100+
continue;
101+
}
102+
103+
if (!ScopeHelper::isInSameScope($phpcsFile, $pointer, $i)) {
104+
continue;
105+
}
106+
107+
$semicolonPointer = $i;
108+
break;
109+
}
110+
111+
/** @var int $semicolonPointer */
112+
$semicolonPointer = $semicolonPointer;
113+
return $semicolonPointer;
114+
}
115+
116+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace SlevomatCodingStandard\Sniffs\Functions;
4+
5+
use SlevomatCodingStandard\Sniffs\TestCase;
6+
7+
class RequireArrowFunctionSniffTest extends TestCase
8+
{
9+
10+
public function testDisallowNestedNoErrors(): void
11+
{
12+
$report = self::checkFile(__DIR__ . '/data/requireArrowFunctionDisallowNestedNoErrors.php', [
13+
'allowNested' => false,
14+
]);
15+
self::assertNoSniffErrorInFile($report);
16+
}
17+
18+
public function testDisallowNestedErrors(): void
19+
{
20+
$report = self::checkFile(__DIR__ . '/data/requireArrowFunctionDisallowNestedErrors.php');
21+
22+
self::assertSame(3, $report->getErrorCount());
23+
24+
self::assertSniffError($report, 3, RequireArrowFunctionSniff::CODE_REQUIRED_ARROW_FUNCTION);
25+
self::assertSniffError($report, 7, RequireArrowFunctionSniff::CODE_REQUIRED_ARROW_FUNCTION);
26+
self::assertSniffError($report, 12, RequireArrowFunctionSniff::CODE_REQUIRED_ARROW_FUNCTION);
27+
28+
self::assertAllFixedInFile($report);
29+
}
30+
31+
public function testAllowNestedNoErrors(): void
32+
{
33+
$report = self::checkFile(__DIR__ . '/data/requireArrowFunctionAllowNestedNoErrors.php', [
34+
'allowNested' => true,
35+
]);
36+
self::assertNoSniffErrorInFile($report);
37+
}
38+
39+
public function testAllowNestedErrors(): void
40+
{
41+
$report = self::checkFile(__DIR__ . '/data/requireArrowFunctionAllowNestedErrors.php', [
42+
'allowNested' => true,
43+
]);
44+
45+
self::assertSame(3, $report->getErrorCount());
46+
47+
self::assertSniffError($report, 3, RequireArrowFunctionSniff::CODE_REQUIRED_ARROW_FUNCTION);
48+
self::assertSniffError($report, 4, RequireArrowFunctionSniff::CODE_REQUIRED_ARROW_FUNCTION);
49+
self::assertSniffError($report, 4, RequireArrowFunctionSniff::CODE_REQUIRED_ARROW_FUNCTION);
50+
51+
self::assertAllFixedInFile($report);
52+
}
53+
54+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<?php // lint >= 7.4
2+
3+
$a = fn ($aa) => fn ($bb) => fn ($cc) => $aa + $bb + $cc;
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php // lint >= 7.4
2+
3+
$a = function ($aa) {
4+
return function ($bb) use ($aa) {
5+
return function ($cc) use ($bb, $aa) {
6+
return $aa + $bb + $cc;
7+
};
8+
};
9+
};
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<?php // lint >= 7.4
2+
3+
$a = fn () => fn () => true;
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php // lint >= 7.4
2+
3+
$a = fn ($aa) => $aa + 1;
4+
5+
$b = fn ($bb) => $bb + $a;
6+
7+
$c = function ($cc) {
8+
return fn () => $cc + 1;
9+
};
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php // lint >= 7.4
2+
3+
$a = function ($aa) {
4+
return $aa + 1;
5+
};
6+
7+
$b = function ($bb) use ($a) {
8+
return $bb + $a;
9+
};
10+
11+
$c = function ($cc) {
12+
return function () use ($cc) {
13+
return $cc + 1;
14+
};
15+
};
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php // lint >= 7.4
2+
3+
$a = function ($aa) {
4+
if ($aa) {
5+
return true;
6+
} else {
7+
return false;
8+
}
9+
};
10+
11+
$b = function () use (&$a) {
12+
return $a + 1;
13+
};
14+
15+
$c = function () {
16+
echo 'something';
17+
};
18+
19+
$d = function () {
20+
return fn () => true;
21+
};

0 commit comments

Comments
 (0)