Skip to content

Commit

Permalink
Update Short Closures spec to remove space after fn keyword
Browse files Browse the repository at this point in the history
Per the accepted [Arrow Functions RFC](https://wiki.php.net/rfc/arrow_functions_v2) and [per its author](https://www.reddit.com/r/PHP/comments/fctb18/comment/fjd9u6a/), short closures were intended to be written without a space after the `fn` keyword.

This is how most open source projects I'm familiar with currently format them, and is also the default formatting used in PhpStorm.

PHP-CS-Fixer was an outlier which incorrectly enforced spacing after the `fn` keyword using the same setting as for the `function` keyword. This was a bug and has been corrected with the addition of a separate `closure_fn_spacing` setting (though this setting still defaults to `one` for backwards compatibility - it will most likely be changed in the next major release to follow this coding style guide).

The primary intention of arrow functions is to avoid much of the verbosity of anonymous functions, so requiring a space after the `fn` keyword is a step backwards from this, and also doesn't align with general usage by the community.

The section on Short Closures was not added until July 17 by php-fig#17, which was after the 1.0.0 spec was approved/released on June 9, so I'm hoping it's not too late to correct it.
  • Loading branch information
theodorejb authored Nov 10, 2022
1 parent b49fd56 commit 1b27dce
Showing 1 changed file with 4 additions and 4 deletions.
8 changes: 4 additions & 4 deletions spec.md
Original file line number Diff line number Diff line change
Expand Up @@ -1148,7 +1148,7 @@ $foo->bar(
Short closures, also known as arrow functions, MUST follow the same guidelines
and principles as long closures above, with the following additions.

The `fn` keyword MUST be preceded and succeeded by a space.
The `fn` keyword MUST be preceded by a space, but MUST NOT be succeeded by a space.

The `=>` symbol MUST be preceded and succeeded by a space.

Expand All @@ -1161,12 +1161,12 @@ The following examples show proper common usage of short closures.

```php

$func = fn (int $x, int $y): int => $x + $y;
$func = fn(int $x, int $y): int => $x + $y;

$func = fn (int $x, int $y): int
$func = fn(int $x, int $y): int
=> $x + $y;

$func = fn (
$func = fn(
int $x,
int $y,
): int
Expand Down

0 comments on commit 1b27dce

Please sign in to comment.