Skip to content

Commit

Permalink
fix: Unify the usage of () after class instances (including anonymous…
Browse files Browse the repository at this point in the history
… classes)

According to chapter 4:

> ## 4. Classes, Properties, and Methods
>
> The term "class" refers to all classes, interfaces, and traits.
>
> Any closing brace MUST NOT be followed by any comment or statement on the same line.
>
> When instantiating a new class, parentheses MUST always be present even when there are no arguments passed to the constructor.

This MR updates the spec to adhere to that rule on anonymous classes too.
(taken from moved from php-fig/fig-standards#1283 but also builds upon php-fig#17)
  • Loading branch information
fruitl00p committed Jun 23, 2022
1 parent b5cac84 commit 7d6282e
Showing 1 changed file with 7 additions and 4 deletions.
11 changes: 7 additions & 4 deletions spec.md
Original file line number Diff line number Diff line change
Expand Up @@ -1055,24 +1055,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.

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

0 comments on commit 7d6282e

Please sign in to comment.