Skip to content

Latest commit

 

History

History
134 lines (93 loc) · 4.4 KB

File metadata and controls

134 lines (93 loc) · 4.4 KB

Migrating from PER-CS v3.0 to PER-CS v3.1

Summary

This document describes the changes and additions on a section by section basis between PER-CS v3.1 and PER-CS v3.0.

It is derived in part from a GitHub-generated diff and focuses on the changes on a section-by-section basis as its focus is to be more readable.

This document intends to provide a summary of these changes that can then be used to drive action lists for toolset producers to support PER-CS v3.1.

This document is non-normative. The published 3.1 PER-CS specification is the canonical source for the PER-CS formatting expectations.

The clone() function SHOULD always be called with parenthesis, even when the optional $withProperties argument is not provided. For example:

<?php

$b = clone($a);
$b = clone($a, [
    'foo' => 'bar',
]);

The formatting rules for switch and case have been expanded and clarified:

  • A case body MUST NOT be wrapped in {}.
  • Every non-empty case MUST end with a terminating statement (break, return, or similar), even if it is the last one in the switch block.
  • If a case condition is complex enough to warrant being multi-line, it MUST be wrapped in parentheses, with the opening parenthesis on the same line as the case keyword and a final line containing only the closing parenthesis and colon.
<?php

switch (true) {
    case (
        $a === 10
        && $b === 20
    ):
        doSomething();
        break;
}

The pipe operator |> is now listed among the binary operators and, like the others, MUST be preceded and followed by at least one space.

<?php

$result = $input |> trim(...) |> strtoupper(...);

When a chain of pipe operators is split across multiple lines, the |> operator MUST be at the beginning of each line, and all lines but the first MUST be indented, following the same rule as other chained operators.

<?php

$result = '<foo>'
    |> strtoupper(...)
    |> htmlspecialchars(...);

If a closure contains no statements, then the body MUST be abbreviated as {} and placed on the same line as the previous symbol, separated by a space. If possible, empty-body closures SHOULD be replaced with arrow functions.

<?php

$noOpFunction = function () {};

// SHOULD be preferred where possible:
$noOpFunction = fn() => null;

Formatting guidelines for attributes on anonymous classes are now included. If an anonymous class has attributes, they MUST be placed starting on the next line after the new keyword, indented once, and otherwise follow the same rules as other attributes specified in section 12. The remaining anonymous class declaration after the final attribute MUST start on a new line and keep the same indentation as the attributes:

<?php

$example = new
    #[Attribute]
    class {
        // ...
    };

Non-public enum methods were already required to use private instead of protected, as enums do not support inheritance. This rule was extended to also apply to constants.

<?php

enum Size
{
    case Small;
    case Medium;
    case Large;

    private const Huge = self::Large;
}

The opening bracket of a multiline array MUST NOT be placed on its own line. This rule applies to arrays in all contexts, not only assignments. For example:

<?php

return [
    'foo',
    'bar',
];

someFunction([
    'foo',
    'bar',
]);