Skip to content

Commit f163ec4

Browse files
authored
Merge pull request #175 from doctrine/new-rule/arrow-functions-declaration
Add rule to ensure Arrow Functions declaration format
2 parents e4b9733 + 7e99a44 commit f163ec4

File tree

5 files changed

+101
-6
lines changed

5 files changed

+101
-6
lines changed

lib/Doctrine/ruleset.xml

+8
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 -->
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` -->

tests/expected_report.txt

+3-2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ PHP CODE SNIFFER REPORT SUMMARY
44
FILE ERRORS WARNINGS
55
----------------------------------------------------------------------
66
tests/input/array_indentation.php 10 0
7+
tests/input/arrow-functions-format.php 10 0
78
tests/input/assignment-operators.php 4 0
89
tests/input/class-references.php 10 0
910
tests/input/concatenation_spacing.php 24 0
@@ -40,9 +41,9 @@ tests/input/use-ordering.php 1 0
4041
tests/input/useless-semicolon.php 2 0
4142
tests/input/UselessConditions.php 20 0
4243
----------------------------------------------------------------------
43-
A TOTAL OF 299 ERRORS AND 0 WARNINGS WERE FOUND IN 36 FILES
44+
A TOTAL OF 309 ERRORS AND 0 WARNINGS WERE FOUND IN 37 FILES
4445
----------------------------------------------------------------------
45-
PHPCBF CAN FIX 238 OF THESE SNIFF VIOLATIONS AUTOMATICALLY
46+
PHPCBF CAN FIX 248 OF THESE SNIFF VIOLATIONS AUTOMATICALLY
4647
----------------------------------------------------------------------
4748

4849

+43
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+
);
+42
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+
);

tests/php-compatibility.patch

+5-4
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ index 1e809f9..490fcbd 100644
55
@@ -5,44 +5,48 @@ FILE ERRORS WARNINGS
66
----------------------------------------------------------------------
77
tests/input/array_indentation.php 10 0
8+
tests/input/arrow-functions-format.php 10 0
89
tests/input/assignment-operators.php 4 0
910
+tests/input/binary_operators.php 9 0
1011
tests/input/class-references.php 10 0
@@ -52,11 +53,11 @@ index 1e809f9..490fcbd 100644
5253
tests/input/useless-semicolon.php 2 0
5354
tests/input/UselessConditions.php 20 0
5455
----------------------------------------------------------------------
55-
-A TOTAL OF 299 ERRORS AND 0 WARNINGS WERE FOUND IN 36 FILES
56-
+A TOTAL OF 373 ERRORS AND 0 WARNINGS WERE FOUND IN 40 FILES
56+
-A TOTAL OF 309 ERRORS AND 0 WARNINGS WERE FOUND IN 37 FILES
57+
+A TOTAL OF 383 ERRORS AND 0 WARNINGS WERE FOUND IN 41 FILES
5758
----------------------------------------------------------------------
58-
-PHPCBF CAN FIX 238 OF THESE SNIFF VIOLATIONS AUTOMATICALLY
59-
+PHPCBF CAN FIX 308 OF THESE SNIFF VIOLATIONS AUTOMATICALLY
59+
-PHPCBF CAN FIX 248 OF THESE SNIFF VIOLATIONS AUTOMATICALLY
60+
+PHPCBF CAN FIX 318 OF THESE SNIFF VIOLATIONS AUTOMATICALLY
6061
----------------------------------------------------------------------
6162

6263

0 commit comments

Comments
 (0)