Skip to content

Commit 3ce2c1a

Browse files
committed
minor #16537 Document code-style for enums and class constants (derrabus)
This PR was merged into the 4.4 branch. Discussion ---------- Document code-style for enums and class constants This PR adds some naming conventions. First of all, class constants. We seem to have an unwritten rule on how class constants should be named. I've made that rule explicit. Secondly, it adds enumerations to the mix. Those are a new language element introduced in PHP 8.1. Our codebase does not have any enums yet (apart from test fixtures), so I think we should discuss the options before the first PR adding an enum to Symfony's public API arrives. I took inspiration from the example code from the [RFC](https://wiki.php.net/rfc/enumerations). The most complete example is probably this one: ```php enum UserStatus: string { case Pending = 'P'; case Active = 'A'; case Suspended = 'S'; case CanceledByUser = 'C'; } ``` I propose to adopt the naming conventions used by the RFC because they seem reasonable to me. Fixes #16538, symfony/symfony#45516. Commits ------- 4cc6393 Document code-style for enums and class constants
2 parents bad586e + 4cc6393 commit 3ce2c1a

File tree

1 file changed

+9
-2
lines changed

1 file changed

+9
-2
lines changed

contributing/code/standards.rst

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -210,8 +210,12 @@ Naming Conventions
210210
* Use `snake_case`_ for configuration parameters and Twig template variables
211211
(e.g. ``framework.csrf_protection``, ``http_status_code``);
212212

213-
* Use namespaces for all PHP classes and `UpperCamelCase`_ for their names (e.g.
214-
``ConsoleLogger``);
213+
* Use `SCREAMING_SNAKE_CASE`_ for constants (e.g. ``InputArgument::IS_ARRAY``);
214+
215+
* Use `UpperCamelCase`_ for enumeration cases (e.g. ``InputArgumentMode::IsArray``);
216+
217+
* Use namespaces for all PHP classes, interfaces, traits and enums and
218+
`UpperCamelCase`_ for their names (e.g. ``ConsoleLogger``);
215219

216220
* Prefix all abstract classes with ``Abstract`` except PHPUnit ``*TestCase``.
217221
Please note some early Symfony classes do not follow this convention and
@@ -222,6 +226,9 @@ Naming Conventions
222226

223227
* Suffix traits with ``Trait``;
224228

229+
* Don't use a dedicated suffix for classes or enumerations (e.g. like ``Class``
230+
or ``Enum``), except for the cases listed below.
231+
225232
* Suffix exceptions with ``Exception``;
226233

227234
* Prefix PHP attributes with ``As`` where applicable (e.g. ``#[AsCommand]``

0 commit comments

Comments
 (0)