Skip to content

Commit 0324f09

Browse files
committed
Add rule to ensure Arrow Functions declaration format
1 parent c665bae commit 0324f09

File tree

3 files changed

+93
-0
lines changed

3 files changed

+93
-0
lines changed

lib/Doctrine/ruleset.xml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,14 @@
267267
<rule ref="SlevomatCodingStandard.Exceptions.DeadCatch"/>
268268
<!-- Require using Throwable instead of Exception -->
269269
<rule ref="SlevomatCodingStandard.Exceptions.ReferenceThrowableOnly"/>
270+
<!-- Ensure Arrow Functions declaration format 10 -->
271+
<rule ref="SlevomatCodingStandard.Functions.ArrowFunctionDeclaration">
272+
<properties>
273+
<property name="spacesCountAfterKeyword" value="1"/>
274+
<property name="spacesCountBeforeArrow" value="1"/>
275+
<property name="spacesCountAfterArrow" value="1"/>
276+
</properties>
277+
</rule>
270278
<!-- Require closures not referencing $this be static -->
271279
<rule ref="SlevomatCodingStandard.Functions.StaticClosure"/>
272280
<!-- Forbid unused variables passed to closures via `use` -->
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
$missingStatic = static fn ($a, $b) => $a + $b;
6+
7+
$uselessParentheses = static fn ($x) => $x + $y;
8+
9+
$withReturnType = static fn (): int => 1 + 2;
10+
11+
$withTypesInArguments = static fn (int $a, int $b): int => $a + $b;
12+
13+
$spacing = static fn (int $x) => $x * 2;
14+
15+
$nested = static fn ($x) => static fn ($y) => $x * $y + $z;
16+
17+
$returningObject = static fn () => new stdClass();
18+
19+
$multiLineArrowFunctions = Collection::from([1, 2])
20+
->map(
21+
static fn (int $v): int => $v * 2
22+
)
23+
->reduce(
24+
static fn (int $tmp, int $v): int => $tmp + $v
25+
);
26+
27+
$thisIsNotAnArrowFunction = [$this->fn => 'value'];
28+
29+
$arrayWithArrowFunctions = [
30+
'true' => static fn () => true,
31+
'false' => static fn () => false,
32+
];
33+
34+
$singleLineArrayReturn = Collection::map(
35+
static fn () => [1, 2]
36+
);
37+
38+
$wrongMultiLineArrayReturn = Collection::map(
39+
static fn () => [
40+
1,
41+
2,
42+
]
43+
);
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
$missingStatic = fn ($a, $b) => $a + $b;
6+
7+
$uselessParentheses = static fn ($x) => ($x + $y);
8+
9+
$withReturnType = static fn () : int => 1 + 2;
10+
11+
$withTypesInArguments = static fn (int $a , int $b): int => $a + $b;
12+
13+
$spacing = static fn(int $x) => $x * 2;
14+
15+
$nested = static fn ($x)=>static fn ($y) => $x * $y + $z;
16+
17+
$returningObject = static fn () => new stdClass();
18+
19+
$multiLineArrowFunctions = Collection::from([1, 2])
20+
->map(
21+
static fn (int $v): int => $v * 2
22+
)
23+
->reduce(
24+
static fn (int $tmp, int $v): int => $tmp + $v
25+
);
26+
27+
$thisIsNotAnArrowFunction = [$this->fn => 'value'];
28+
29+
$arrayWithArrowFunctions = [
30+
'true' => static fn () => true,
31+
'false' => static fn () => false,
32+
];
33+
34+
$singleLineArrayReturn = Collection::map(
35+
static fn () => [1, 2]
36+
);
37+
38+
$wrongMultiLineArrayReturn = Collection::map(
39+
static fn () => [
40+
1, 2
41+
]
42+
);

0 commit comments

Comments
 (0)