|
| 1 | +# Migrating from PER-CS v2.0 to PER-CS v3.0 ### |
| 2 | + |
| 3 | +## Summary |
| 4 | + |
| 5 | +PER-CS is the next evolution of the PSR set of Coding Standards from the |
| 6 | +PHP-FIG (Framework Interoperability Group). It extends the Coding Standards |
| 7 | +laid out in PSR-12 to the newest functionality added to PHP such as the match |
| 8 | +keyword, enums, attributes, and more. |
| 9 | + |
| 10 | +This document describes the changes and additions on a section by section |
| 11 | +basis between PER-CS v3.0 and PER-CS v2.0. |
| 12 | + |
| 13 | +It is derived in part from [a GitHub-generated diff](https://github.com/php-fig/per-coding-style/compare/2.0.0...3.0.0#files_bucket) |
| 14 | +and focuses on the changes on a section-by-section basis as its focus is to be more readable. |
| 15 | + |
| 16 | +This document intends to provide a summary of these changes that can |
| 17 | +then be used to drive action lists for toolset producers to support PER-CS v3.0. |
| 18 | + |
| 19 | +This document is non-normative. The published [3.0 PER-CS](https://www.php-fig.org/per/coding-style/) specification |
| 20 | +is the canonical source for the PER-CS formatting expectations. |
| 21 | + |
| 22 | +## [Section 2.5 - Keywords and Types](https://www.php-fig.org/per/coding-style/#25-keywords-and-types) |
| 23 | + |
| 24 | +Formatting conventions are now provided for compound types (those that include union or intersection type declarations). |
| 25 | + |
| 26 | +* The `|` and `&` symbols and parentheses MUST NOT have leading or trailing spaces. |
| 27 | +* If a type declaration is long enough to split to multiple lines, each ANDed block must be on one line, and each ORed block on its own line. |
| 28 | +* If one of the types listed is `null`, it must come last. |
| 29 | +* Favor `?` over `|null` in cases where both work. |
| 30 | +* A multi-catch statement must follow the same rules. (See section 5.6 as well.) |
| 31 | + |
| 32 | +```php |
| 33 | +function foo(int|string $a): User|Product |
| 34 | +{ |
| 35 | + // ... |
| 36 | +} |
| 37 | + |
| 38 | +function complex(array|(ArrayAccess&Traversable) $input): ArrayAccess&Traversable |
| 39 | +{ |
| 40 | + // ... |
| 41 | +} |
| 42 | + |
| 43 | +function veryComplex( |
| 44 | + array |
| 45 | + |(ArrayAccess&Traversable) |
| 46 | + |(Traversable&Countable) $input): ArrayAccess&Traversable |
| 47 | +{ |
| 48 | + // ... |
| 49 | +} |
| 50 | +``` |
| 51 | + |
| 52 | +## [Section 2.7 - Naming](https://www.php-fig.org/per/coding-style/#27-naming) |
| 53 | + |
| 54 | +PER-CS now recommends following the same naming conventions as PHP Internals for abbreviations and acronyms. Specifically, only uppercase the first character of the acronym: `XmlFormatter`, not `XMLFormatter`. |
| 55 | + |
| 56 | +## [Section 3 - Declare Statements, Namespace, and Import Statements](https://www.php-fig.org/per/coding-style/#3-declare-statements-namespace-and-import-statements) |
| 57 | + |
| 58 | +The `<?php` tag must always be all-lowercase. |
| 59 | + |
| 60 | +## [Section 4 - Classes, Properties, and Methods](https://www.php-fig.org/per/coding-style/#4-classes-properties-and-methods) |
| 61 | + |
| 62 | +If using PHP 8.4 or later, parentheses should be omitted around a `new` declaration. |
| 63 | + |
| 64 | +## [Section 4.3 - Properties and Constants](https://www.php-fig.org/per/coding-style/#43-properties-and-constants) |
| 65 | + |
| 66 | +Class constants must have a visibility and type declared. |
| 67 | + |
| 68 | +If a property has a `set`-visibility defined, the `get` visibility may be omitted. |
| 69 | + |
| 70 | +```php |
| 71 | +class Foo |
| 72 | +{ |
| 73 | + // This is allowed. |
| 74 | + private(set) string $name; |
| 75 | +} |
| 76 | +``` |
| 77 | + |
| 78 | +## [Section 4.6 - Modifier Keywords](https://www.php-fig.org/per/coding-style/#46-modifier-keywords) |
| 79 | + |
| 80 | +At least one of `readonly`, `get`-visibility, and `set`-visibility must be specified. If at least one is specified, the others may be omitted. |
| 81 | + |
| 82 | +```php |
| 83 | +class Foo |
| 84 | +{ |
| 85 | + // These are all acceptable. |
| 86 | + public private(set) string $one; |
| 87 | + private(set) string $two; |
| 88 | + readonly string $three; |
| 89 | + |
| 90 | + // These are not. |
| 91 | + private(set) public string $four; |
| 92 | +} |
| 93 | +``` |
| 94 | + |
| 95 | +## [Section 4.7 - Methods and Function Calls](https://www.php-fig.org/per/coding-style/#47-method-and-function-calls) |
| 96 | + |
| 97 | +The `exit()` and `die()` functions should always be called with parentheses, even though PHP technically permits them without. |
| 98 | + |
| 99 | +## [Section 4.9 - Property hooks](https://www.php-fig.org/per/coding-style/#49-property-hooks) |
| 100 | + |
| 101 | +Formatting guidelines for property hooks are now included. |
| 102 | + |
| 103 | +When using property hooks on a constructor promoted property, only single-line set hooks are allowed. Anything more complicated MUST be defined separately from the constructor. |
| 104 | + |
| 105 | +The following are examples of properly formatted hooks: |
| 106 | + |
| 107 | +```php |
| 108 | +class Example |
| 109 | +{ |
| 110 | + public string $one = 'Me' { |
| 111 | + set(string $value) { |
| 112 | + // ... |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + public string $two { |
| 117 | + get { |
| 118 | + // ... |
| 119 | + } |
| 120 | + set { |
| 121 | + // ... |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + public string $three { |
| 126 | + get => __CLASS__; |
| 127 | + set => ucfirst($value); |
| 128 | + } |
| 129 | + |
| 130 | + public string $four { get => __CLASS__; } |
| 131 | +} |
| 132 | +``` |
| 133 | + |
| 134 | +## [Section 5.2 - Switch, Case, and Match](https://www.php-fig.org/per/coding-style/#52-switch-case-match) |
| 135 | + |
| 136 | +When breaking a series of boolean operators across multiple lines, the operator MUST be at the beginning of each line, not the end of each line. |
| 137 | + |
| 138 | +(This applies to `switch`, `match`, `while`, `do while`, and any other expression.) |
| 139 | + |
| 140 | +## [Section 6.4 - Operator placement](https://www.php-fig.org/per/coding-style/#64-operator-placement) |
| 141 | + |
| 142 | +When breaking a series of chained operations across multiple lines, the operator MUST be at the beginning of each line, not the end of each line, and all lines but the first MUST be indented. Examples include `??` and ternary conditionals. |
| 143 | + |
| 144 | +```php |
| 145 | +<?php |
| 146 | + |
| 147 | +$variable1 = $ternaryOperatorExpr |
| 148 | + ? 'fizz' |
| 149 | + : 'buzz'; |
| 150 | + |
| 151 | +$variable2 = $possibleNullableExpr |
| 152 | + ?? 'fallback'; |
| 153 | + |
| 154 | +$variable3 = $elvisExpr |
| 155 | + ?: 'qix'; |
| 156 | +``` |
0 commit comments