Skip to content
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

Add Attributes style guide #26

Merged
merged 7 commits into from
Nov 10, 2022
Merged
Changes from 6 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
95 changes: 95 additions & 0 deletions spec.md
Original file line number Diff line number Diff line change
Expand Up @@ -1279,6 +1279,101 @@ function allowed()
}
```

## 11. Attributes

### 11.1 Basics

Attribute names must immediately follow the opening attribute block indicator `#[` with no space.

If an attribute has no arguments, the `()` MUST be omitted.

The closing attribute block indicator `]` MUST follow the last character of the attribute name or the closing `)` of
its argument list, with no preceding space.

### 11.2 Placement

Attributes on classes, methods, functions, constants and properties MUST
be placed on their own line, immediately prior to the structure being described.

For attributes on parameters, if the parameter list is presented on a single line,
the attribute MUST be placed inline with the parameter it describes, separated by a single space.
If the parameter list is split into multiple lines for any reason, the attribute MUST be placed on
its own line prior to the parameter, indented the same as the parameter. If the parameter list
is split into multiple lines, a blank line MAY be included between one parameter and the attributes
of the following parameter in order to aid readability.

If a comment docblock is present on a structure that also includes an attribute, the comment block MUST
come first, followed by any attributes, followed by the structure itself. There MUST NOT be any blank lines
between the docblock and attributes, or the attributes and the structure.

If two separate attribute blocks (denoted by separate `#[]` markers) are used in a multi-line context,
they MUST be on separate lines with no blank lines between them.

### 11.3 Compound attributes

Multiple attributes MAY be placed in the same attribute block (`#[]`) if and only if the entire block is listed on a
single line and each attribute is reasonably short. They MUST be separated by a comma with a space following but no space preceding. If the attribute list
is split into multiple lines for any reason, then the attributes MUST be placed in separate attribute blocks.
Those blocks may themselves contain multiple attributes provided this rule is respected.

If an attribute's argument list is split into multiple lines for any reason, then:

* The attribute MUST be the only one in its attribute block.
* The attribute arguments MUST follow the same rules as defined for multiline function calls.

### 11.4 Example

The following is an example of valid attribute usage.

```php
#[Foo]
#[Bar('baz')]
class Demo
{
#[Beep]
private Foo $foo;

public function __construct(
#[Load(context: 'foo', bar: true)]
private readonly FooService $fooService,

#[LoadProxy(context: 'bar')]
private readonly BarService $barService,
) {}

/**
* Sets the foo.
*/
#[Poink('narf'), Narf('poink')]
public function setFoo(#[Beep] Foo $new): void
{
// ...
}

#[Complex(
prop: 'val',
other: 5,
)]
#[Other, Stuff, Here]
public function complicated(
string $a,

#[Decl]
string $b,

#[Complex(
prop: 'val',
other: 5,
)]
string $c,

int $d,
): string {
// ...
}
}
```

[PSR-1]: https://www.php-fig.org/psr/psr-1/
[PSR-12]: https://www.php-fig.org/psr/psr-12/
[keywords]: http://php.net/manual/en/reserved.keywords.php
Expand Down