Skip to content

Add rule to ensure Arrow Functions declaration format #175

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 17, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions lib/Doctrine/ruleset.xml
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,14 @@
<rule ref="SlevomatCodingStandard.Exceptions.DeadCatch"/>
<!-- Require using Throwable instead of Exception -->
<rule ref="SlevomatCodingStandard.Exceptions.ReferenceThrowableOnly"/>
<!-- Ensure Arrow Functions declaration format -->
<rule ref="SlevomatCodingStandard.Functions.ArrowFunctionDeclaration">
<properties>
<property name="spacesCountAfterKeyword" value="1"/>
<property name="spacesCountBeforeArrow" value="1"/>
<property name="spacesCountAfterArrow" value="1"/>
</properties>
</rule>
<!-- Require closures not referencing $this be static -->
<rule ref="SlevomatCodingStandard.Functions.StaticClosure"/>
<!-- Forbid unused variables passed to closures via `use` -->
Expand Down
5 changes: 3 additions & 2 deletions tests/expected_report.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ PHP CODE SNIFFER REPORT SUMMARY
FILE ERRORS WARNINGS
----------------------------------------------------------------------
tests/input/array_indentation.php 10 0
tests/input/arrow-functions-format.php 10 0
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Errors:

FILE: tests/input/arrow-functions-format.php
-------------------------------------------------------------------------------------------------
FOUND 10 ERRORS AFFECTING 7 LINES
-------------------------------------------------------------------------------------------------
  5 | ERROR | [x] Closure not using "$this" should be declared static.
  7 | ERROR | [x] Useless parentheses.
  9 | ERROR | [x] There must be no whitespace between closing parenthesis and return type colon.
 11 | ERROR | [x] Expected 0 spaces between argument "$a" and comma; 1 found
 13 | ERROR | [x] There must be exactly 1 whitespace after "fn" keyword.
 13 | ERROR | [x] There must be exactly 1 whitespace before =>.
 13 | ERROR | [x] There must be exactly 1 whitespace after =>.
 15 | ERROR | [x] There must be exactly 1 whitespace before =>.
 15 | ERROR | [x] There must be exactly 1 whitespace after =>.
 40 | ERROR | [x] Multi-line arrays must have a trailing comma after the last element.
-------------------------------------------------------------------------------------------------
PHPCBF CAN FIX THE 10 MARKED SNIFF VIOLATIONS AUTOMATICALLY
-------------------------------------------------------------------------------------------------

Time: 139ms; Memory: 10MB

tests/input/assignment-operators.php 4 0
tests/input/class-references.php 10 0
tests/input/concatenation_spacing.php 24 0
Expand Down Expand Up @@ -40,9 +41,9 @@ tests/input/use-ordering.php 1 0
tests/input/useless-semicolon.php 2 0
tests/input/UselessConditions.php 20 0
----------------------------------------------------------------------
A TOTAL OF 299 ERRORS AND 0 WARNINGS WERE FOUND IN 36 FILES
A TOTAL OF 309 ERRORS AND 0 WARNINGS WERE FOUND IN 37 FILES
----------------------------------------------------------------------
PHPCBF CAN FIX 238 OF THESE SNIFF VIOLATIONS AUTOMATICALLY
PHPCBF CAN FIX 248 OF THESE SNIFF VIOLATIONS AUTOMATICALLY
----------------------------------------------------------------------


43 changes: 43 additions & 0 deletions tests/fixed/arrow-functions-format.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

declare(strict_types=1);

$missingStatic = static fn ($a, $b) => $a + $b;

$uselessParentheses = static fn ($x) => $x + $y;

$withReturnType = static fn (): int => 1 + 2;

$withTypesInArguments = static fn (int $a, int $b): int => $a + $b;

$spacing = static fn (int $x) => $x * 2;

$nested = static fn ($x) => static fn ($y) => $x * $y + $z;

$returningObject = static fn () => new stdClass();

$multiLineArrowFunctions = Collection::from([1, 2])
->map(
static fn (int $v): int => $v * 2
)
->reduce(
static fn (int $tmp, int $v): int => $tmp + $v
);

$thisIsNotAnArrowFunction = [$this->fn => 'value'];

$arrayWithArrowFunctions = [
'true' => static fn () => true,
'false' => static fn () => false,
];

$singleLineArrayReturn = Collection::map(
static fn () => [1, 2]
);

$wrongMultiLineArrayReturn = Collection::map(
static fn () => [
1,
2,
]
);
42 changes: 42 additions & 0 deletions tests/input/arrow-functions-format.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

declare(strict_types=1);

$missingStatic = fn ($a, $b) => $a + $b;

$uselessParentheses = static fn ($x) => ($x + $y);

$withReturnType = static fn () : int => 1 + 2;

$withTypesInArguments = static fn (int $a , int $b): int => $a + $b;

$spacing = static fn(int $x) => $x * 2;

$nested = static fn ($x)=>static fn ($y) => $x * $y + $z;

$returningObject = static fn () => new stdClass();

$multiLineArrowFunctions = Collection::from([1, 2])
->map(
static fn (int $v): int => $v * 2
)
->reduce(
static fn (int $tmp, int $v): int => $tmp + $v
);

$thisIsNotAnArrowFunction = [$this->fn => 'value'];

$arrayWithArrowFunctions = [
'true' => static fn () => true,
'false' => static fn () => false,
];

$singleLineArrayReturn = Collection::map(
static fn () => [1, 2]
);

$wrongMultiLineArrayReturn = Collection::map(
static fn () => [
1, 2
]
);
9 changes: 5 additions & 4 deletions tests/php-compatibility.patch
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ index 1e809f9..490fcbd 100644
@@ -5,44 +5,48 @@ FILE ERRORS WARNINGS
----------------------------------------------------------------------
tests/input/array_indentation.php 10 0
tests/input/arrow-functions-format.php 10 0
tests/input/assignment-operators.php 4 0
+tests/input/binary_operators.php 9 0
tests/input/class-references.php 10 0
Expand Down Expand Up @@ -52,11 +53,11 @@ index 1e809f9..490fcbd 100644
tests/input/useless-semicolon.php 2 0
tests/input/UselessConditions.php 20 0
----------------------------------------------------------------------
-A TOTAL OF 299 ERRORS AND 0 WARNINGS WERE FOUND IN 36 FILES
+A TOTAL OF 373 ERRORS AND 0 WARNINGS WERE FOUND IN 40 FILES
-A TOTAL OF 309 ERRORS AND 0 WARNINGS WERE FOUND IN 37 FILES
+A TOTAL OF 383 ERRORS AND 0 WARNINGS WERE FOUND IN 41 FILES
----------------------------------------------------------------------
-PHPCBF CAN FIX 238 OF THESE SNIFF VIOLATIONS AUTOMATICALLY
+PHPCBF CAN FIX 308 OF THESE SNIFF VIOLATIONS AUTOMATICALLY
-PHPCBF CAN FIX 248 OF THESE SNIFF VIOLATIONS AUTOMATICALLY
+PHPCBF CAN FIX 318 OF THESE SNIFF VIOLATIONS AUTOMATICALLY
----------------------------------------------------------------------


Expand Down