@@ -297,6 +297,14 @@ there are no arguments passed to the constructor.
297297new Foo();
298298```
299299
300+ If class contains no additional declarations (such as an exception that exists only extend another exception with a new type),
301+ then the body of the class SHOULD be abbreviated as ` {} ` and placed on the same line as the previous symbol,
302+ separated by a space. For example:
303+
304+ ``` php
305+ class MyException extends \RuntimeException {}
306+ ```
307+
300308### 4.1 Extends and Implements
301309
302310The ` extends ` and ` implements ` keywords MUST be declared on the same line as
@@ -513,12 +521,35 @@ function fooBarBaz($arg1, &$arg2, $arg3 = [])
513521}
514522```
515523
516- ### 4.5 Method and Function Arguments
524+ If a function or method contains no statements (such as a no-op implementation or when using
525+ constructor property promotion), then the body SHOULD be abbreviated as ` {} ` and placed on the same
526+ line as the previous symbol, separated by a space. For example:
527+
528+ ``` php
529+ class Point
530+ {
531+ public function __construct(private int $x, private int $y) {}
532+
533+ // ...
534+ }
535+ ```
536+
537+ ``` php
538+ class Point
539+ {
540+ public function __construct(
541+ public readonly int $x,
542+ public readonly int $y,
543+ ) {}
544+ }
545+ ```
546+
547+ ### 4.5 Method and Function Parameters
517548
518549In the argument list, there MUST NOT be a space before each comma, and there
519550MUST be one space after each comma.
520551
521- Method and function arguments with default values MUST go at the end of the argument
552+ Method and function parameters with default values MUST go at the end of the argument
522553list.
523554
524555``` php
@@ -633,7 +664,7 @@ public function process(string $algorithm, &...$parts)
633664
634665### 4.6 Modifier Keywords
635666
636- Properties and methods of a class have numerous keyword modifiers that alter how the
667+ Classes, properties, and methods have numerous keyword modifiers that alter how the
637668engine and language handles them. When present, they MUST be in the following order:
638669
639670* Inheritance modifier: ` abstract ` or ` final `
@@ -714,6 +745,13 @@ $app->get('/hello/{name}', function ($name) use ($app) {
714745});
715746```
716747
748+ If using named arguments, there MUST NOT be a space between the argument name
749+ and colon, and there MUST be a single space between the colon and the argument value.
750+
751+ ``` php
752+ somefunction($a, b: $b, c: 'c');
753+ ```
754+
717755Method chaining MAY be put on separate lines, where each subsequent line is indented once. When doing so, the first
718756method MUST be on the next line.
719757
@@ -990,7 +1028,9 @@ $i++;
9901028++$j;
9911029```
9921030
993- Type casting operators MUST NOT have any space within the parentheses:
1031+ Type casting operators MUST NOT have any space within the parentheses and MUST be separated from the variable they are
1032+ operating on by exactly one space:
1033+
9941034``` php
9951035$intValue = (int) $input;
9961036```
@@ -1354,6 +1394,103 @@ $arr2 = [
13541394 'array',
13551395 ],
13561396];
1397+
1398+ ## 12. Attributes
1399+
1400+ ### 12.1 Basics
1401+
1402+ Attribute names MUST immediately follow the opening attribute block indicator `#[` with no space.
1403+
1404+ If an attribute has no arguments, the `()` MUST be omitted.
1405+
1406+ The closing attribute block indicator `]` MUST follow the last character of the attribute name or the closing `)` of
1407+ its argument list, with no preceding space.
1408+
1409+ The construct `#[...]` is referred to as an "attribute block" in this document.
1410+
1411+ ### 12.2 Placement
1412+
1413+ Attributes on classes, methods, functions, constants and properties MUST
1414+ be placed on their own line, immediately prior to the structure being described.
1415+
1416+ For attributes on parameters, if the parameter list is presented on a single line,
1417+ the attribute MUST be placed inline with the parameter it describes, separated by a single space.
1418+ If the parameter list is split into multiple lines for any reason, the attribute MUST be placed on
1419+ its own line prior to the parameter, indented the same as the parameter. If the parameter list
1420+ is split into multiple lines, a blank line MAY be included between one parameter and the attributes
1421+ of the following parameter in order to aid readability.
1422+
1423+ If a comment docblock is present on a structure that also includes an attribute, the comment block MUST
1424+ come first, followed by any attributes, followed by the structure itself. There MUST NOT be any blank lines
1425+ between the docblock and attributes, or the attributes and the structure.
1426+
1427+ If two separate attribute blocks are used in a multi-line context, they MUST be on separate lines with no blank
1428+ lines between them.
1429+
1430+ ### 12.3 Compound attributes
1431+
1432+ If multiple attributes are placed in the same attribute block, they MUST be separated by a comma with a space
1433+ following but no space preceding. If the attribute list is split into multiple lines for any reason, then the
1434+ attributes MUST be placed in separate attribute blocks. Those blocks may themselves contain multiple
1435+ attributes provided this rule is respected.
1436+
1437+ If an attribute's argument list is split into multiple lines for any reason, then:
1438+
1439+ * The attribute MUST be the only one in its attribute block.
1440+ * The attribute arguments MUST follow the same rules as defined for multiline function calls.
1441+
1442+ ### 12.4 Example
1443+
1444+ The following is an example of valid attribute usage.
1445+
1446+ ```php
1447+ #[Foo]
1448+ #[Bar('baz')]
1449+ class Demo
1450+ {
1451+ #[Beep]
1452+ private Foo $foo;
1453+
1454+ public function __construct(
1455+ #[Load(context: 'foo', bar: true)]
1456+ private readonly FooService $fooService,
1457+
1458+ #[LoadProxy(context: 'bar')]
1459+ private readonly BarService $barService,
1460+ ) {}
1461+
1462+ /**
1463+ * Sets the foo.
1464+ */
1465+ #[Poink('narf'), Narf('poink')]
1466+ public function setFoo(#[Beep] Foo $new): void
1467+ {
1468+ // ...
1469+ }
1470+
1471+ #[Complex(
1472+ prop: 'val',
1473+ other: 5,
1474+ )]
1475+ #[Other, Stuff]
1476+ #[Here]
1477+ public function complicated(
1478+ string $a,
1479+
1480+ #[Decl]
1481+ string $b,
1482+
1483+ #[Complex(
1484+ prop: 'val',
1485+ other: 5,
1486+ )]
1487+ string $c,
1488+
1489+ int $d,
1490+ ): string {
1491+ // ...
1492+ }
1493+ }
13571494```
13581495
13591496[ PSR-1 ] : https://www.php-fig.org/psr/psr-1/
0 commit comments