|
| 1 | +# Migrating from PER-CS v1.0 (PSR-12) to PER-CS v2.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 v2.0 and PER-CS v1.0 (which is a direct equivalent of |
| 12 | +PSR12 with very minor changes). |
| 13 | + |
| 14 | +It is derived in part from [a GitHub-generated diff](https://github.com/php-fig/per-coding-style/compare/1.0.0...2.0.0#files_bucket) |
| 15 | +and focuses on the changes on a section-by-section basis as its focus is to be more readable. |
| 16 | + |
| 17 | +This document intends to provide a summary of these changes that can |
| 18 | +then be used to drive action lists for toolset producers to support PER-CS v2.0. |
| 19 | + |
| 20 | +This document is non-normative. The published [2.0 PER-CS](https://www.php-fig.org/per/coding-style/) specification |
| 21 | +is the canonical source for the PER-CS formatting expectations. |
| 22 | + |
| 23 | +## [Section 2.6 - Trailing Commas](https://www.php-fig.org/per/coding-style/#26-trailing-commas) |
| 24 | + |
| 25 | +Numerous constructs now allow a sequence of values to have an optional trailing |
| 26 | +comma: |
| 27 | + * If the final item is on the same line then there MUST NOT be a trailing comma |
| 28 | + * If the final item is not on the same line then there MUST be a trailing comma |
| 29 | + |
| 30 | +```php |
| 31 | +<?php |
| 32 | +$sequence = [1, 2, 3, 5, 8, 13]; |
| 33 | + |
| 34 | +function beep( |
| 35 | + string $a, |
| 36 | + string $b, |
| 37 | + string $c, |
| 38 | +) { |
| 39 | + // ... |
| 40 | +} |
| 41 | +``` |
| 42 | + |
| 43 | +## [Section 4.4 - Methods and Functions](https://www.php-fig.org/per/coding-style/#44-methods-and-functions) |
| 44 | + |
| 45 | +If a function or method contains no statements or comments (such as an empty |
| 46 | +no-op implementation or when using constructor property promotion), then the |
| 47 | +body SHOULD be abbreviated as {} and placed on the same line as the previous |
| 48 | +symbol, separated by a space. |
| 49 | +So, for a method of a subclass that does nothing: |
| 50 | + |
| 51 | +```php |
| 52 | +<?php |
| 53 | +class SubClass extends BaseClass |
| 54 | +{ |
| 55 | + protected function init() {} |
| 56 | +} |
| 57 | +``` |
| 58 | + |
| 59 | +## [Section 4.6 - Modifier Keywords](https://www.php-fig.org/per/coding-style/#46-modifier-keywords) |
| 60 | + |
| 61 | +Modifier keywords are keywords that alter how PHP handles classes, |
| 62 | +properties and methods. |
| 63 | + |
| 64 | +These keywords MUST BE ordered as follows: |
| 65 | + |
| 66 | +[abstract|final] [public|protected|private] [static] [readonly] [type] name |
| 67 | + |
| 68 | +```php |
| 69 | +<?php |
| 70 | +namespace Vendor\Package; |
| 71 | + |
| 72 | +abstract class ClassName |
| 73 | +{ |
| 74 | + protected static readonly string $foo; |
| 75 | + |
| 76 | + final protected int $beep; |
| 77 | + |
| 78 | + abstract protected function zim(); |
| 79 | + |
| 80 | + final public static function bar() |
| 81 | + { |
| 82 | + // method body |
| 83 | + } |
| 84 | +} |
| 85 | +``` |
| 86 | + |
| 87 | +Furthermore, all keywords must be on a single line and MUST be separated |
| 88 | +by a single space. |
| 89 | + |
| 90 | +## [Section 4.7 - Method and Function Calls](https://www.php-fig.org/per/coding-style/#47-method-and-function-calls) |
| 91 | + |
| 92 | +If using named arguments, there MUST NOT be a space between the argument name and the colon, |
| 93 | +and there MUST be a single space between the colon and the argument value. |
| 94 | + |
| 95 | +```php |
| 96 | +<?php |
| 97 | +somefunction($a, b: $b, c: 'c'); |
| 98 | +``` |
| 99 | + |
| 100 | +Method chaining MAY be put on separate lines, where each subsequent line is indented once. When doing so, the first method MUST be on the next line. |
| 101 | + |
| 102 | +```php |
| 103 | +<?php |
| 104 | +$someInstance |
| 105 | + ->create() |
| 106 | + ->prepare() |
| 107 | + ->run(); |
| 108 | +``` |
| 109 | + |
| 110 | +## [Section 4.8 - Function Callable References](https://www.php-fig.org/per/coding-style/#48-function-callable-references) |
| 111 | + |
| 112 | +Function callable references - there must not be whitespace surrounding the '...' operator () |
| 113 | + |
| 114 | +```php |
| 115 | +<?php |
| 116 | +$callable = $item->doSomething(...); |
| 117 | +``` |
| 118 | + |
| 119 | +## [Section 5.2 - Switch, Case, Match](https://www.php-fig.org/per/coding-style/#52-switch-case-match) |
| 120 | + |
| 121 | +The match keyword is now covered. |
| 122 | + |
| 123 | +```php |
| 124 | +<?php |
| 125 | +$result = match ($a) { |
| 126 | + 'foo' => 'Foo', |
| 127 | + 'bar' => 'Bar', |
| 128 | + default => 'Baz', |
| 129 | +}; |
| 130 | +``` |
| 131 | + |
| 132 | +```php |
| 133 | +<?php |
| 134 | +$returnValue = match ($expr) { |
| 135 | + 0 => 'First case', |
| 136 | + 1, 2, 3 => multipleCases(), |
| 137 | + default => 'Default case', |
| 138 | +}; |
| 139 | +``` |
| 140 | + |
| 141 | +## [Section 7.1 - Short Closures](https://www.php-fig.org/per/coding-style/#71-short-closures) |
| 142 | + |
| 143 | +A new subsection about Short Closures, as per the link above. Example as follows: |
| 144 | + |
| 145 | +```php |
| 146 | +<?php |
| 147 | +$func = fn(int $x, int $y): int => $x + $y; |
| 148 | + |
| 149 | +$func = fn(int $x, int $y): int |
| 150 | + => $x + $y; |
| 151 | + |
| 152 | +$func = fn( |
| 153 | + int $x, |
| 154 | + int $y, |
| 155 | +): int |
| 156 | + => $x + $y; |
| 157 | + |
| 158 | +$result = $collection->reduce(fn(int $x, int $y): int => $x + $y, 0); |
| 159 | +``` |
| 160 | + |
| 161 | +## [Section 9 - Enumerations](https://www.php-fig.org/per/coding-style/#9-enumerations) |
| 162 | + |
| 163 | +Enums are now covered, as per the link above. Please see below for examples. |
| 164 | + |
| 165 | +```php |
| 166 | +<?php |
| 167 | +enum Suit: string |
| 168 | +{ |
| 169 | + case Hearts = 'H'; |
| 170 | + case Diamonds = 'D'; |
| 171 | + case Clubs = 'C'; |
| 172 | + case Spades = 'S'; |
| 173 | +} |
| 174 | +``` |
| 175 | + |
| 176 | +```php |
| 177 | +<?php |
| 178 | +enum Size |
| 179 | +{ |
| 180 | + case Small; |
| 181 | + case Medium; |
| 182 | + case Large; |
| 183 | + |
| 184 | + public const Huge = self::Large; |
| 185 | +} |
| 186 | +``` |
| 187 | + |
| 188 | +## [Section 10 - Heredoc and Nowdoc](https://www.php-fig.org/per/coding-style/#10-heredoc-and-nowdoc) |
| 189 | + |
| 190 | +This is a new section about Heredoc and Nowdoc notation as per the link above. |
| 191 | +Example follows: |
| 192 | + |
| 193 | +```php |
| 194 | +<?php |
| 195 | +function allowed() |
| 196 | +{ |
| 197 | + $allowedHeredoc = <<<COMPLIANT |
| 198 | + This |
| 199 | + is |
| 200 | + a |
| 201 | + compliant |
| 202 | + heredoc |
| 203 | + COMPLIANT; |
| 204 | + |
| 205 | + $allowedNowdoc = <<<'COMPLIANT' |
| 206 | + This |
| 207 | + is |
| 208 | + a |
| 209 | + compliant |
| 210 | + nowdoc |
| 211 | + COMPLIANT; |
| 212 | + |
| 213 | + var_dump( |
| 214 | + 'foo', |
| 215 | + <<<'COMPLIANT' |
| 216 | + This |
| 217 | + is |
| 218 | + a |
| 219 | + compliant |
| 220 | + parameter |
| 221 | + COMPLIANT, |
| 222 | + 'bar', |
| 223 | + ); |
| 224 | +} |
| 225 | +``` |
| 226 | + |
| 227 | +## [Section 11 - Arrays](https://www.php-fig.org/per/coding-style/#11-arrays) |
| 228 | + |
| 229 | +This is a new section about arrays, as per the link above. |
| 230 | +Example as follows: |
| 231 | + |
| 232 | +```php |
| 233 | +<?php |
| 234 | +$arr1 = ['single', 'line', 'declaration']; |
| 235 | +$arr2 = [ |
| 236 | + 'multi', |
| 237 | + 'line', |
| 238 | + 'declaration', |
| 239 | + ['values' => 1, 5, 7], |
| 240 | + [ |
| 241 | + 'nested', |
| 242 | + 'array', |
| 243 | + ], |
| 244 | +]; |
| 245 | +``` |
| 246 | + |
| 247 | +## [Section 12 - Attributes](https://www.php-fig.org/per/coding-style/#12-attributes) |
| 248 | + |
| 249 | +This is a new section, as per the above link. |
| 250 | +The following is an example of valid usage. |
| 251 | + |
| 252 | +```php |
| 253 | +<?php |
| 254 | +#[Foo] |
| 255 | +#[Bar('baz')] |
| 256 | +class Demo |
| 257 | +{ |
| 258 | + #[Beep] |
| 259 | + private Foo $foo; |
| 260 | + |
| 261 | + public function __construct( |
| 262 | + #[Load(context: 'foo', bar: true)] |
| 263 | + private readonly FooService $fooService, |
| 264 | + |
| 265 | + #[LoadProxy(context: 'bar')] |
| 266 | + private readonly BarService $barService, |
| 267 | + ) {} |
| 268 | + |
| 269 | + /** |
| 270 | + * Sets the foo. |
| 271 | + */ |
| 272 | + #[Poink('narf'), Narf('poink')] |
| 273 | + public function setFoo(#[Beep] Foo $new): void |
| 274 | + { |
| 275 | + // ... |
| 276 | + } |
| 277 | + |
| 278 | + #[Complex( |
| 279 | + prop: 'val', |
| 280 | + other: 5, |
| 281 | + )] |
| 282 | + #[Other, Stuff] |
| 283 | + #[Here] |
| 284 | + public function complicated( |
| 285 | + string $a, |
| 286 | + |
| 287 | + #[Decl] |
| 288 | + string $b, |
| 289 | + |
| 290 | + #[Complex( |
| 291 | + prop: 'val', |
| 292 | + other: 5, |
| 293 | + )] |
| 294 | + string $c, |
| 295 | + |
| 296 | + int $d, |
| 297 | + ): string { |
| 298 | + // ... |
| 299 | + } |
| 300 | +} |
| 301 | +``` |
0 commit comments