Skip to content

Commit 8320af5

Browse files
committed
Restructure hook descriptions.
1 parent 5a7b88e commit 8320af5

File tree

1 file changed

+55
-36
lines changed

1 file changed

+55
-36
lines changed

spec.md

Lines changed: 55 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -777,22 +777,59 @@ If so, the `...` MUST NOT include any whitespace before or after. That is, the c
777777

778778
Object properties may also include hooks, which have a number of syntactic options.
779779

780-
If there is only one hook implementation, and that hook implementation itself uses
781-
the short-hook syntax, then the hook declaration MAY be listed entirely inline. The hook
782-
name MUST be separated from the opening brace and the arrow operator by a single
783-
space, and the semicolon ending of the hook MUST be separated from the closing brace
784-
by a single space. For example:
780+
When using the long form of hooks:
781+
782+
* The opening brace MUST be on the same line as the property.
783+
* The opening brace MUST be separated from the property name or its default value by a single space.
784+
* The closing brace MUST be on its own line, and have no comment following it.
785+
* The entire body of the hook definition MUST be indented one level.
786+
* The body of each hook MUST be indented one level.
787+
* If multiple hooks are declared, they MUST be separated by at least a single line break. They
788+
MAY be separated by an additional blank line to aid readability.
789+
790+
For example:
785791

786792
```php
787793
class Example
788794
{
789-
public string $myName { get => __CLASS__; }
795+
public string $newName = 'Me' {
796+
set {
797+
if (strlen($value) < 3) {
798+
throw new \Exception('Too short');
799+
}
800+
$this->newName = ucfirst($value);
801+
}
802+
}
790803

791-
public string $newName { set => ucfirst($value); }
804+
public string $department {
805+
get {
806+
return $this->values[__PROPERTY__];
807+
}
808+
set {
809+
$this->values[__PROPERTY__] = $value;
810+
}
811+
}
812+
// or
813+
public string $department {
814+
get {
815+
return $this->values[__PROPERTY__];
816+
}
817+
818+
set {
819+
$this->values[__PROPERTY__] = $value;
820+
}
821+
}
792822
}
793823
```
794824

795-
If those two criteria are not met, then the hook block MUST be spread across multiple lines:
825+
Property hooks also support multiple short-hook variations.
826+
827+
For a `set` hook, if the argument name and type do not need to be redefined, then they MAY be omitted.
828+
829+
If a hook consists of a single expression, then PHP allows it to be shortened using `=>`. In that case:
830+
831+
* There MUST be a single space on either side of the `=>` symbol.
832+
* The body MUST be on the same line as the hook declaration, unless it gets prohibitively long. If it gets prohibitively long, the developer SHOULD consider not using the short-hook syntax.
796833

797834
```php
798835
class Example
@@ -807,45 +844,27 @@ class Example
807844
}
808845
```
809846

810-
When using the multi-line form, the opening brace MUST be on the same line as the property and
811-
the closing brace MUST be on its own line, with the body indented one level.
812-
813-
The `=>` operator MUST have a single space on either side.
847+
Additionally, if the following criteria are met:
814848

815-
If multiple hooks are declared, they MUST be separated by at least a single line break. They
816-
MAY be separated by an additional blank line to aid readability.
849+
* There is only one hook implementation.
850+
* That hook uses the short-hook syntax.
817851

818-
If the property includes a default value, there MUST be a single space between the default value
819-
and the opening brace.
852+
Then the hook MAY be listed entirely inline. In that case,
820853

821-
```php
822-
class Example
823-
{
824-
public string $newName = 'Me' {
825-
set => ucfirst($value);
826-
}
827-
}
828-
```
854+
* The hook name MUST be separated from the opening brace and the arrow operator by a single space
855+
* The semicolon ending of the hook MUST be separated from the closing brace by a single space.
829856

830-
If a hook is using its multi-line form, the opening brace MUST be on the same line as the hook
831-
name, and the closing brace MUST be on its own line. The body MUST be indented one level.
857+
For example:
832858

833859
```php
834860
class Example
835861
{
836-
public string $newName = 'Me' {
837-
set {
838-
if (strlen($value) < 3) {
839-
throw new \Exception('Too short');
840-
}
841-
$this->newName = ucfirst($value);
842-
}
843-
}
862+
public string $myName { get => __CLASS__; }
863+
864+
public string $newName { set => ucfirst($value); }
844865
}
845866
```
846867

847-
For a `set` hook, if the argument name and type do not need to be redefined, then they MAY be omitted.
848-
849868
Property hooks MAY also be defined in constructor-promoted properties. However, they
850869
MUST be only a single hook, with a short-syntax body, defined on a single line as above.
851870
If those criteria are not met, then the promoted property MUST NOT have any hooks defined

0 commit comments

Comments
 (0)