Skip to content

Commit

Permalink
Add the traits use statement section
Browse files Browse the repository at this point in the history
Co-authored-by: Juliette <[email protected]>
Co-authored-by: Gary Jones <[email protected]>
  • Loading branch information
3 people committed Aug 26, 2022
1 parent 80b5fc8 commit 3ffacff
Showing 1 changed file with 46 additions and 1 deletion.
47 changes: 46 additions & 1 deletion wordpress-coding-standards/php.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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`
Expand Down

0 comments on commit 3ffacff

Please sign in to comment.