Skip to content

Commit f15e94e

Browse files
committed
Add Attributes style guide.
1 parent 0774582 commit f15e94e

File tree

1 file changed

+81
-0
lines changed

1 file changed

+81
-0
lines changed

spec.md

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1276,6 +1276,87 @@ function allowed()
12761276
compliant
12771277
nowdoc
12781278
COMPLIANT;
1279+
```
1280+
1281+
## 11. Attributes
1282+
1283+
### 12.1 Basics
1284+
1285+
Attribute names must immediately follow the opening attribute block indicator `#[` with no space.
1286+
1287+
If an attribute has no arguments, the `()` MUST be omitted.
1288+
1289+
The closing attribute block indicator `]` MUST follow the last character of the attribute name or the closing `)` of
1290+
its argument list, with no preceding space.
1291+
1292+
### 12.2 Placement
1293+
1294+
Attributes on classes, methods, functions, constants and properties MUST
1295+
be placed on their own line, immediately prior to the structure being described.
1296+
1297+
For attributes on parameters, if the parameter list is presented on a single line,
1298+
the attribute MUST be placed inline with the parameter it describes, separated by a single space.
1299+
If the parameter list is split into multiple lines for any reason, the attribute MUST be placed on
1300+
its own line prior to the parameter, indented the same as the parameter.
1301+
1302+
If a comment docblock is present on a structure that also includes an attribute, the comment block MUST
1303+
come first, followed by any attributes, followed by the structure itself. There MUST NOT be any blank lines
1304+
between the docblock and attributes, or the attributes and the structure.
1305+
1306+
If two separate attribute blocks (denoted by separate `#[]` markers) are used in a multi-line context,
1307+
they MUST be on separate lines with no blank lines between them.
1308+
1309+
### 12.3 Compound attributes
1310+
1311+
Multiple attributes MAY be placed in the same attribute block (`#[]`) if and only if the entire block is listed on a
1312+
single line. They must be separated by a comma with a space following but no space preceding. If the attribute list
1313+
is split into multiple lines for any reason, then the attributes MUST be placed in separate attribute blocks.
1314+
Those blocks may themselves contain multiple attributes provided this rule is respected.
1315+
1316+
If an attribute's argument list is split into multiple lines for any reason, then:
1317+
1318+
* The attribute MUST be the only one in its attribute block.
1319+
* The attribute arguments MUST follow the same rules as defined for multiline function calls.
1320+
1321+
### 12.4 Example
1322+
1323+
The following is an example of valid attribute usage.
1324+
1325+
```php
1326+
#[Foo]
1327+
#[Bar('baz')]
1328+
class Demo
1329+
{
1330+
#[Beep]
1331+
private Foo $foo;
1332+
1333+
/**
1334+
* Sets the foo.
1335+
*/
1336+
#[Poink('narf'), Narf('poink')]
1337+
public function setFoo(#[Beep] Foo $new): void
1338+
{
1339+
// ...
1340+
}
1341+
1342+
#[Complex(
1343+
prop: 'val',
1344+
other: 5,
1345+
)]
1346+
#[Other, Stuff, Here]
1347+
public function complicated(
1348+
string $a,
1349+
#[Decl]
1350+
string $b,
1351+
#[Complex(
1352+
prop: 'val',
1353+
other: 5,
1354+
)]
1355+
string $c,
1356+
int $d,
1357+
): string {
1358+
// ...
1359+
}
12791360
}
12801361
```
12811362

0 commit comments

Comments
 (0)