Skip to content

Commit 5229526

Browse files
authored
Merge pull request #108 from Crell/hooks
Specify hooks formatting
2 parents a027285 + 0a13031 commit 5229526

File tree

1 file changed

+167
-0
lines changed

1 file changed

+167
-0
lines changed

spec.md

+167
Original file line numberDiff line numberDiff line change
@@ -859,6 +859,173 @@ A function or method may be referenced in a way that creates a closure out of it
859859

860860
If so, the `...` MUST NOT include any whitespace before or after. That is, the correct format is `foo(...)`.
861861

862+
### 4.9 Property Hooks
863+
864+
Object properties may also include hooks, which have a number of syntactic options.
865+
866+
When using the long form of hooks:
867+
868+
* The opening brace MUST be on the same line as the property.
869+
* The opening brace MUST be separated from the property name or its default value by a single space.
870+
* The closing brace MUST be on its own line, and have no comment following it.
871+
* The entire body of the hook definition MUST be indented one level.
872+
* The body of each hook MUST be indented one level.
873+
* If multiple hooks are declared, they MUST be separated by at least a single line break. They
874+
MAY be separated by an additional blank line to aid readability.
875+
876+
For example:
877+
878+
```php
879+
class Example
880+
{
881+
public string $newName = 'Me' {
882+
set(string $value) {
883+
if (strlen($value) < 3) {
884+
throw new \Exception('Too short');
885+
}
886+
$this->newName = ucfirst($value);
887+
}
888+
}
889+
890+
public string $department {
891+
get {
892+
return $this->values[__PROPERTY__];
893+
}
894+
set {
895+
$this->values[__PROPERTY__] = $value;
896+
}
897+
}
898+
// or
899+
public string $department {
900+
get {
901+
return $this->values[__PROPERTY__];
902+
}
903+
904+
set {
905+
$this->values[__PROPERTY__] = $value;
906+
}
907+
}
908+
}
909+
```
910+
911+
Property hooks also support multiple short-hook variations.
912+
913+
For a `set` hook, if the argument name and type do not need to be redefined, then they MAY be omitted.
914+
915+
If a hook consists of a single expression, then PHP allows it to be shortened using `=>`. In that case:
916+
917+
* There MUST be a single space on either side of the `=>` symbol.
918+
* The body MUST begin on the same line as the hook name and `=>`.
919+
* Wrapping is allowed if the expression used allows for wrapping, using the rules defined elsewhere in this document.
920+
921+
```php
922+
class Example
923+
{
924+
public string $myName {
925+
get => __CLASS__;
926+
}
927+
928+
public string $newName {
929+
set => ucfirst($value);
930+
}
931+
}
932+
```
933+
934+
Additionally, if the following criteria are met:
935+
936+
* There is only one hook implementation.
937+
* That hook uses the short-hook syntax.
938+
* That hook expression does not contain any wrapping.
939+
940+
Then the hook MAY be listed entirely inline. In that case,
941+
942+
* The hook name MUST be separated from the opening brace and the arrow operator by a single space
943+
* The semicolon ending of the hook MUST be separated from the closing brace by a single space.
944+
945+
For example:
946+
947+
```php
948+
class Example
949+
{
950+
public string $myName { get => __CLASS__; }
951+
952+
public string $newName { set => ucfirst($value); }
953+
}
954+
```
955+
956+
Property hooks MAY also be defined in constructor-promoted properties. However, they
957+
MUST be only a single hook, with a short-syntax body, defined on a single line as above.
958+
If those criteria are not met, then the promoted property MUST NOT have any hooks defined
959+
inline.
960+
961+
```php
962+
class Example
963+
{
964+
public function __construct(
965+
public string $name { set => ucfirst($value); }
966+
) {}
967+
}
968+
```
969+
970+
The following is ***not allowed*** due to the hook being too complex:
971+
972+
```php
973+
class Example
974+
{
975+
public function __construct(
976+
public string $name {
977+
set {
978+
if (strlen($value) < 3) {
979+
throw new \Exception('Too short');
980+
}
981+
$this->newName = ucfirst($value);
982+
}
983+
}
984+
) {}
985+
}
986+
```
987+
988+
## 4.10 Interface and abstract properties
989+
990+
Abstract properties may be defined in interfaces or abstract classes, but are required to
991+
specify if they must support `get` operations, `set` operations, or both. In the case
992+
of abstract classes, they MAY include a body for one or another hook.
993+
994+
If there is a body for any hook, then the entire hook block MUST follow
995+
the same rules as for defined hooks above. The only difference is that
996+
a hook that has no body specified have a single semicolon after the hook
997+
keyword, with no space before it.
998+
999+
```php
1000+
abstract class Example {
1001+
abstract public string $name {
1002+
get => ucfirst($this->name);
1003+
set;
1004+
}
1005+
}
1006+
```
1007+
1008+
If there is no body for either hook, then the following rules apply:
1009+
1010+
* The operation block MUST be on the same line as the property.
1011+
* There MUST be a single space between the property name and the operation block `{}`.
1012+
* There MUST be a single space after the opening `{`.
1013+
* There MUST be a single space before the closing `}`;
1014+
* There MUST NOT be a space between the operation and its required semicolon.
1015+
* If multiple operations are specified, they MUST be separated by a single space.
1016+
* The `get` operation MUST be listed before the `set` operation.
1017+
1018+
```php
1019+
interface Example
1020+
{
1021+
public string $readable { get; }
1022+
1023+
public string $writeable { set; }
1024+
1025+
public string $both { get; set; }
1026+
}
1027+
```
1028+
8621029
## 5. Control Structures
8631030

8641031
The general style rules for control structures are as follows:

0 commit comments

Comments
 (0)