From 3ffacff83bd53364a74cfaf9e5cbee63d26a86cd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Denis=20=C5=BDoljom?= Date: Fri, 12 Aug 2022 16:13:34 +0200 Subject: [PATCH] Add the traits use statement section Co-authored-by: Juliette <663378+jrfnl@users.noreply.github.com> Co-authored-by: Gary Jones --- wordpress-coding-standards/php.md | 47 ++++++++++++++++++++++++++++++- 1 file changed, 46 insertions(+), 1 deletion(-) diff --git a/wordpress-coding-standards/php.md b/wordpress-coding-standards/php.md index e65fed6..8b38f1a 100644 --- a/wordpress-coding-standards/php.md +++ b/wordpress-coding-standards/php.md @@ -88,11 +88,15 @@ Use lowercase letters in variable, action/filter, and function names (never `cam function some_name( $some_variable ) { [...] } ``` -Class names should use capitalized words separated by underscores. Any acronyms should be all upper case. +Class, trait, interface and enum names should use capitalized words separated by underscores. Any acronyms should be all upper case. ```php class Walker_Category extends Walker { [...] } class WP_HTTP { [...] } + +interface Mailer_Interface { [...] } +trait Forbid_Dynamic_Properties { [...] } +enum Post_Status { [...] } ``` Constants should be in all upper-case with underscores separating words: @@ -507,6 +511,47 @@ class Example_Class { [...] } class Example_Class_Extended { [...] } ``` +### Trait Use Statements + +Trait `use` statements should be at the top of a class and should have exactly one blank line before the first `use` statement, and at least one blank line after the last statement. The only exception is when the class only contains trait `use` statements, in which case the blank line after may be omitted. + +The following code examples show the formatting requirements for trait `use` statements regarding things like spacing, grouping and indentation. + +```php +// Correct. +class Foo { + + use Bar_Trait; + use Foo_Trait, + Bazinga_Trait { + Bar_Trait::method_name insteadof Bar_Trait; + Bazinga_Trait::method_name as bazinga_method; + } + use Loopy_Trait { eat as protected; } + + public $baz = true; + + ... +} + +// Incorrect. +class Foo { + // No blank line before trait use statement, multiple spaces after the use keyword. + use Bar_Trait; + + /* + * Multiple spaces when importing traits, no new line after opening brace. + * Aliasing should be done on the same line as the method it's replacing. + */ + use Foo_Trait, Bazinga_Trait{Bar_Trait::method_name insteadof Foo_Trait; Bazinga_Trait::method_name + as bazinga_method; + }; // Wrongly indented brace. + public $baz = true; // Missing blank line after trait import. + + ... +} +``` + ## Control Structures ### Use `elseif`, not `else if`