Skip to content

fix: Include the example of using the shortform of a closure #1283

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

Closed
wants to merge 2 commits into from
Closed
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
17 changes: 12 additions & 5 deletions accepted/PSR-12-extended-coding-style-guide.md
Original file line number Diff line number Diff line change
Expand Up @@ -929,7 +929,7 @@ $variable = $foo ?: 'bar';
## 7. Closures

Closures MUST be declared with a space after the `function` keyword, and a
space before and after the `use` keyword.
space before and after the `use` keyword. This includes the use of the `shortform notation`.

The opening brace MUST go on the same line, and the closing brace MUST go on
the next line following the body.
Expand Down Expand Up @@ -958,6 +958,10 @@ $closureWithArgs = function ($arg1, $arg2) {
// body
};

$shortClosureWithoutArgs = fn () => /* body */;

$shortClosureWithArgs = fn ($arg1, $arg2) => /* body */;

$closureWithArgsAndVars = function ($arg1, $arg2) use ($var1, $var2) {
// body
};
Expand Down Expand Up @@ -1050,24 +1054,27 @@ in the above section.
~~~php
<?php

$instance = new class {};
$instance = new class () {};
~~~

The opening brace MAY be on the same line as the `class` keyword so long as
the list of `implements` interfaces does not wrap. If the list of interfaces
wraps, the brace MUST be placed on the line immediately following the last
interface.
interface.

This also includes the use of parentheses due to directly instantiating the
declared class.

~~~php
<?php

// Brace on the same line
$instance = new class extends \Foo implements \HandleableInterface {
$instance = new class () extends \Foo implements \HandleableInterface {
// Class content
};

// Brace on the next line
$instance = new class extends \Foo implements
$instance = new class () extends \Foo implements
\ArrayAccess,
\Countable,
\Serializable
Expand Down