From 10c52851cec6ae74bcef7b7b6bcaecfdd8838650 Mon Sep 17 00:00:00 2001 From: Larry Garfield Date: Mon, 25 Apr 2022 14:36:03 -0500 Subject: [PATCH 1/5] Add section on enumerations. --- spec.md | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/spec.md b/spec.md index 66dbad4..c1674d1 100644 --- a/spec.md +++ b/spec.md @@ -1081,6 +1081,36 @@ $instance = new class extends \Foo implements }; ``` +## 9. Enumerations + +Enumerations MUST follow the same guidelines as classes, except where otherwise noted here. + +Methods in Enumerations MUST follow the same guidelines as methods in classes. Non-public methods MUST use `private` +instead of `protected`. + +In the case of a backed enum, there MUST NOT be a space between the enum name and colon, and there MUST be exactly one +space between the colon and the backing type. + +Enumeration cases MUST use CamelCase capitalization. + +Constants in Enumerations MAY use either CamelCase or UPPER_CASE capitalization. CamelCase is RECOMMENDED, +so that it is consistent with cases. + +No more than one case is permitted per line. + +```php +enum Suit: string +{ + case Hearts = 'H'; + case Diamonds = 'D'; + case Spades = 'S'; + case Clubs = 'C'; + + const Wild = self::Spades; +} + +``` + [PSR-1]: https://www.php-fig.org/psr/psr-1/ [PSR-12]: https://www.php-fig.org/psr/psr-12/ [keywords]: http://php.net/manual/en/reserved.keywords.php From 6acd1d614b2eb02f67e3280fb13a2d111df3f1ed Mon Sep 17 00:00:00 2001 From: Larry Garfield Date: Mon, 2 May 2022 11:28:05 -0500 Subject: [PATCH 2/5] Assorted fixups. --- spec.md | 27 ++++++++++++++++++--------- 1 file changed, 18 insertions(+), 9 deletions(-) diff --git a/spec.md b/spec.md index c1674d1..358d934 100644 --- a/spec.md +++ b/spec.md @@ -68,6 +68,17 @@ class Foo extends Bar implements FooInterface // method body } } + +Enum Beep: int +{ + case A = 1; + case B = 2; + + public function isOdd(): bool + { + return $this->value() % 2; + } +} ``` ## 2. General @@ -233,7 +244,7 @@ declare(ticks=1) { ## 4. Classes, Properties, and Methods -The term "class" refers to all classes, interfaces, and traits. +The term "class" refers to all classes, interfaces, traits, and enums. Any closing brace MUST NOT be followed by any comment or statement on the same line. @@ -1083,21 +1094,19 @@ $instance = new class extends \Foo implements ## 9. Enumerations -Enumerations MUST follow the same guidelines as classes, except where otherwise noted here. +Enumerations (enums) MUST follow the same guidelines as classes, except where otherwise noted here. -Methods in Enumerations MUST follow the same guidelines as methods in classes. Non-public methods MUST use `private` -instead of `protected`. +Methods in enums MUST follow the same guidelines as methods in classes. Non-public methods MUST use `private` +instead of `protected`, as enums do not support inheritance. -In the case of a backed enum, there MUST NOT be a space between the enum name and colon, and there MUST be exactly one -space between the colon and the backing type. +When using a backed enum, there MUST NOT be a space between the enum name and colon, and there MUST be exactly one +space between the colon and the backing type. This is consistent with the style for return types. -Enumeration cases MUST use CamelCase capitalization. +Enum case declarations MUST use CamelCase capitalization. Enum case declarations MUST be on their own line. Constants in Enumerations MAY use either CamelCase or UPPER_CASE capitalization. CamelCase is RECOMMENDED, so that it is consistent with cases. -No more than one case is permitted per line. - ```php enum Suit: string { From 7145d7b0cdaf2eb4c348a38e27d24f4421b70381 Mon Sep 17 00:00:00 2001 From: Larry Garfield Date: Tue, 10 May 2022 12:12:05 -0500 Subject: [PATCH 3/5] Wordsmithing. --- spec.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/spec.md b/spec.md index 358d934..d4c2bb9 100644 --- a/spec.md +++ b/spec.md @@ -1094,7 +1094,7 @@ $instance = new class extends \Foo implements ## 9. Enumerations -Enumerations (enums) MUST follow the same guidelines as classes, except where otherwise noted here. +Enumerations (enums) MUST follow the same guidelines as classes, except where otherwise noted below. Methods in enums MUST follow the same guidelines as methods in classes. Non-public methods MUST use `private` instead of `protected`, as enums do not support inheritance. @@ -1105,7 +1105,7 @@ space between the colon and the backing type. This is consistent with the style Enum case declarations MUST use CamelCase capitalization. Enum case declarations MUST be on their own line. Constants in Enumerations MAY use either CamelCase or UPPER_CASE capitalization. CamelCase is RECOMMENDED, -so that it is consistent with cases. +so that it is consistent with case declarations. ```php enum Suit: string From b0f2ac7c720948975a49b2b5b9f4174ad48e61aa Mon Sep 17 00:00:00 2001 From: Larry Garfield Date: Thu, 23 Jun 2022 16:32:48 -0500 Subject: [PATCH 4/5] Use more demonstrative enum case names. Co-authored-by: Aleksei Gagarin --- spec.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/spec.md b/spec.md index d4c2bb9..57f703b 100644 --- a/spec.md +++ b/spec.md @@ -71,8 +71,8 @@ class Foo extends Bar implements FooInterface Enum Beep: int { - case A = 1; - case B = 2; + case Foo = 1; + case Bar = 2; public function isOdd(): bool { From 9ed23b0257ee1d22c19cf6ef222913f109840bfb Mon Sep 17 00:00:00 2001 From: Alexander Makarov Date: Sun, 17 Jul 2022 23:27:40 +0300 Subject: [PATCH 5/5] Update spec.md --- spec.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spec.md b/spec.md index 57f703b..cc84b03 100644 --- a/spec.md +++ b/spec.md @@ -1104,7 +1104,7 @@ space between the colon and the backing type. This is consistent with the style Enum case declarations MUST use CamelCase capitalization. Enum case declarations MUST be on their own line. -Constants in Enumerations MAY use either CamelCase or UPPER_CASE capitalization. CamelCase is RECOMMENDED, +Constants in Enumerations MAY use either PascalCase or UPPER_CASE capitalization. PascalCase is RECOMMENDED, so that it is consistent with case declarations. ```php