Skip to content

Commit

Permalink
Add the blank line rule for function bodies
Browse files Browse the repository at this point in the history
  • Loading branch information
dingo-d committed Aug 26, 2022
1 parent 1d40ea1 commit b650342
Showing 1 changed file with 136 additions and 0 deletions.
136 changes: 136 additions & 0 deletions wordpress-coding-standards/php.md
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,8 @@ switch ( $type ) {

Remove trailing whitespace at the end of each line. Omitting the closing PHP tag at the end of a file is preferred. If you use the tag, make sure you remove trailing whitespace.

There should be no trailing blank lines at the end of a function body as well.

## Formatting

### Brace Style
Expand Down Expand Up @@ -370,6 +372,140 @@ $a = foo(
);
```

### Type declarations

[alert]
Type declaration usage has the following restrictions based on the minimum required PHP version:

- The scalar `bool`, `int`, `float`, and `string` type declarations cannot be used until the minimum PHP version is PHP 7.0 or higher.
- Return type declarations cannot be used until the minimum PHP version is PHP 7.0 or higher.
- Nullable type declarations cannot be used until the minimum PHP version is PHP 7.1 or higher.
- The `iterable` and `void` type declarations cannot be used until the minimum PHP version is PHP 7.1 or higher. The `void` can only be used as a return type.
- The `object` type declaration cannot be used until the minimum PHP version is PHP 7.2 or higher.
- Property type declarations cannot be used until the minimum PHP version is PHP 7.4 or higher.
- `static` return type cannot be used until the minimum PHP version is PHP 8 or higher.
- `mixed` type cannot be used until the minimum PHP version is PHP 8 or higher. Because `mixed` type includes `null`. it's not allowed to make it nullable.
- Union types cannot be used until the minimum PHP version is PHP 8 or higher.
- Intersection types cannot be used until the minimum PHP version is PHP 8.1 or higher.
- `never` return type cannot be used until the minimum PHP version is PHP 8.1 or higher.
[/alert]

Type declarations must have exactly one space before and after the type. The nullability operator (`?`) is regarded as part of the type declaration and there should be no space between this operator and the actual type. Class/interface name based type declarations should use the case of the class/interface name as declared, while the keyword-based type declarations should be lowercased.

Return type declarations should have no space between the closing parenthesis of the function declaration and the colon starting a return type.

These rules apply to all structures allowing for type declarations: functions, closures, catch conditions as well as the PHP 7.4 arrow functions and typed properties.

The following examples showcase what type hints are currently available in the WordPress Core due to the minimum supported PHP version

```php
// Correct.
function foo( Class_Name $parameter, callable $callable, array $number_of_things = [] ) {
// Do something.
}

function bar(
Interface_Name $param_a,
array $param_b,
callable $param_c = 'default_callable'
) {
// Do something.
}

// Incorrect.
function baz(Class_Name $param_a, String$param_b, CALLABLE $param_c ) {
// Do something.
}
```

The following examples showcase what you can use in your themes and plugins if you require specific minimum PHP version (7.4 for instance)

```php
// Correct.
function foo( Class_Name $param_a, ?string $param_b, callable $param_c ): ?\Class_Name {
// Do something.
}

function bar(
Interface_Name $param_a,
?int $param_b,
bool $param_c
): iterable {
// Do something.
}

// Incorrect.
function baz(Class_Name $param_a, ? Float$param_b, CALLABLE $param_c ) : ? \Class_Name{
// Do something.
}
```

_Usage in WordPress Core_

[warning]
Adding type declarations to existing WordPress Core functions should be done with extreme care.
[/warning]

While rare, `array` and class/interface name based type declarations already exist in WordPress Core.

The function signature of any function (method) which can be overloaded by plugins or themes should not be touched.
This includes all existing `public` and `protected` class methods and any conditionally declared (pluggable) functions in the global namespace.

This leaves, for now, only unconditionally declared functions in the global namespace, `private` class methods, and code newly being introduced as candidates for adding type declarations.

For those, adding `callable`, class and interface name based parameter type declarations where the parameter type only expects one type and it would allow for simplification of existing/new code, is allowed and tentatively encouraged.

Using the `array` keyword in type declarations is **strongly discouraged** for now, as most often, it would be better to use `iterable` to allow for more flexibility in the implementation and that keyword is not yet available for use in WordPress Core until the minimum requirements are raised to PHP 7.1.

### The ::class constant

When using the `::class` constant for class name resolution you should add no space between the `::class` and the preceding class name it applies to. There should be no space between the double colon and the `class` keyword, and the `class` keyword should be in lowercase.

```php
// Correct.
add_action( 'action_name', array( My_Class::class, 'method_name' ) );

// Incorrect.
add_action( 'action_name', array( My_Class :: CLASS, 'method_name' ) );
```

_Usage in WordPress Core_

The `::class` constant can be used in WordPress Core.
Replacing existing uses of `__CLASS__` with `self::class` and `get_called_class()` with `static::class`, where appropriate, is encouraged.

### Spread operator `...`

Spread or splat operator (as it's known in other languages), is used for packing arguments in function declarations (variadic functions), and unpacking them in function calls in PHP 5.6. In PHP 7.4 the spread operator is also used for unpacking arrays (string-keyed array unpacking is available since PHP 8.1).

When using the spread operator, there should be one space, or a new line with the appropriate indentation before the spread operator. There should be no spaces between the spread operator and the variable/function call it applies to. When combining the spread operator with the reference operator (`&`), there should be no spaces between them.

```php
// Correct.
function foo( &...$spread ) {
bar( ...$spread );

bar(
array( ...$foo ),
...array_values( $keyed_array )
);
}

// Incorrect.
function fool( & ... $spread ) {
bar(...
$spread );

bar(
[... $foo ],.../*comment*/array_values( $keyed_array )
);
}
```

_Usage in WordPress Core_

The spread operator can be used for packing arguments in function declarations, and unpacking them in function calls, but cannot be used for array unpacking, until the minimum PHP version has been raised to PHP 7.4.

## Declare Statements, Namespace, and Import Statements

### Namespace declarations
Expand Down

0 comments on commit b650342

Please sign in to comment.