Skip to content

Commit fcc65fd

Browse files
authored
Merge pull request #26 from Crell/attributes
Add Attributes style guide
2 parents b49fd56 + a774903 commit fcc65fd

File tree

1 file changed

+98
-0
lines changed

1 file changed

+98
-0
lines changed

spec.md

+98
Original file line numberDiff line numberDiff line change
@@ -1319,6 +1319,104 @@ function allowed()
13191319
}
13201320
```
13211321

1322+
## 11. Attributes
1323+
1324+
### 11.1 Basics
1325+
1326+
Attribute names MUST immediately follow the opening attribute block indicator `#[` with no space.
1327+
1328+
If an attribute has no arguments, the `()` MUST be omitted.
1329+
1330+
The closing attribute block indicator `]` MUST follow the last character of the attribute name or the closing `)` of
1331+
its argument list, with no preceding space.
1332+
1333+
The construct `#[...]` is referred to as an "attribute block" in this document.
1334+
1335+
### 11.2 Placement
1336+
1337+
Attributes on classes, methods, functions, constants and properties MUST
1338+
be placed on their own line, immediately prior to the structure being described.
1339+
1340+
For attributes on parameters, if the parameter list is presented on a single line,
1341+
the attribute MUST be placed inline with the parameter it describes, separated by a single space.
1342+
If the parameter list is split into multiple lines for any reason, the attribute MUST be placed on
1343+
its own line prior to the parameter, indented the same as the parameter. If the parameter list
1344+
is split into multiple lines, a blank line MAY be included between one parameter and the attributes
1345+
of the following parameter in order to aid readability.
1346+
1347+
If a comment docblock is present on a structure that also includes an attribute, the comment block MUST
1348+
come first, followed by any attributes, followed by the structure itself. There MUST NOT be any blank lines
1349+
between the docblock and attributes, or the attributes and the structure.
1350+
1351+
If two separate attribute blocks are used in a multi-line context, they MUST be on separate lines with no blank
1352+
lines between them.
1353+
1354+
### 11.3 Compound attributes
1355+
1356+
If multiple attributes are placed in the same attribute block, they MUST be separated by a comma with a space
1357+
following but no space preceding. If the attribute list is split into multiple lines for any reason, then the
1358+
attributes MUST be placed in separate attribute blocks. Those blocks may themselves contain multiple
1359+
attributes provided this rule is respected.
1360+
1361+
If an attribute's argument list is split into multiple lines for any reason, then:
1362+
1363+
* The attribute MUST be the only one in its attribute block.
1364+
* The attribute arguments MUST follow the same rules as defined for multiline function calls.
1365+
1366+
### 11.4 Example
1367+
1368+
The following is an example of valid attribute usage.
1369+
1370+
```php
1371+
#[Foo]
1372+
#[Bar('baz')]
1373+
class Demo
1374+
{
1375+
#[Beep]
1376+
private Foo $foo;
1377+
1378+
public function __construct(
1379+
#[Load(context: 'foo', bar: true)]
1380+
private readonly FooService $fooService,
1381+
1382+
#[LoadProxy(context: 'bar')]
1383+
private readonly BarService $barService,
1384+
) {}
1385+
1386+
/**
1387+
* Sets the foo.
1388+
*/
1389+
#[Poink('narf'), Narf('poink')]
1390+
public function setFoo(#[Beep] Foo $new): void
1391+
{
1392+
// ...
1393+
}
1394+
1395+
#[Complex(
1396+
prop: 'val',
1397+
other: 5,
1398+
)]
1399+
#[Other, Stuff]
1400+
#[Here]
1401+
public function complicated(
1402+
string $a,
1403+
1404+
#[Decl]
1405+
string $b,
1406+
1407+
#[Complex(
1408+
prop: 'val',
1409+
other: 5,
1410+
)]
1411+
string $c,
1412+
1413+
int $d,
1414+
): string {
1415+
// ...
1416+
}
1417+
}
1418+
```
1419+
13221420
[PSR-1]: https://www.php-fig.org/psr/psr-1/
13231421
[PSR-12]: https://www.php-fig.org/psr/psr-12/
13241422
[keywords]: http://php.net/manual/en/reserved.keywords.php

0 commit comments

Comments
 (0)