Skip to content
Merged
Changes from 33 commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
a6ff325
Some initial notes re differences between PSR12 and PERCSv2.0
kenguest Oct 12, 2023
3ccb609
Rename
kenguest Oct 12, 2023
9aed798
Rename
kenguest Oct 12, 2023
aaf2b42
Improve markup
kenguest Oct 12, 2023
91819f2
Fix markup for sections on short closures and enums
kenguest Oct 12, 2023
3b29740
Tidy up
kenguest Oct 12, 2023
5cd50f2
Add link to autogenerated changelog
kenguest Oct 13, 2023
209c586
Call this a migration document
kenguest Oct 13, 2023
2734f41
Additional changes as suggested by Crell
kenguest Oct 15, 2023
3635c2e
Remove unnecessary empty line
kenguest Oct 15, 2023
feb4e9d
Remove text from previous PSRs that are not new to PER-CS v2
kenguest Oct 17, 2023
05a0748
Method chaining
kenguest Oct 17, 2023
7aa1183
file name - drop the version being migrated from
kenguest Oct 17, 2023
6fbc900
Oxford comma...
kenguest Oct 19, 2023
e116298
Update migration-2.0.md
kenguest Oct 19, 2023
e354506
Update migration-2.0.md
kenguest Oct 19, 2023
40f0c37
Update migration-2.0.md
kenguest Oct 19, 2023
2960dec
Update migration-2.0.md
kenguest Oct 19, 2023
e0baa66
Update migration-2.0.md
kenguest Oct 19, 2023
303fcb7
Update migration-2.0.md
kenguest Oct 19, 2023
ea0002e
Update migration-2.0.md
kenguest Oct 19, 2023
838bcad
Update migration-2.0.md
kenguest Oct 19, 2023
d1490b4
Update migration-2.0.md
kenguest Oct 19, 2023
50e058a
Consistency
kenguest Oct 19, 2023
6e92d9b
Update migration-2.0.md
kenguest Oct 23, 2023
62f4377
Update migration-2.0.md
kenguest Oct 23, 2023
a90e814
Update migration-2.0.md
kenguest Oct 23, 2023
4c160bd
Apply suggestions from review
kenguest Oct 23, 2023
fae5c1e
Update migration-2.0.md
kenguest Oct 23, 2023
62e3123
More concise, with examples.
kenguest Oct 26, 2023
9a4d7e2
Syntax highlighting for example php code
kenguest Oct 27, 2023
a217170
Fix syntax re syntax highlighting
kenguest Oct 27, 2023
c789b06
Syntax highlighting for example php code
kenguest Oct 27, 2023
3dff722
Add some more examples.
kenguest Oct 28, 2023
f7b112c
Forgot closing syntax markers
kenguest Oct 28, 2023
4ec7bb5
Add some more examples.
kenguest Oct 28, 2023
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
233 changes: 233 additions & 0 deletions migration-2.0.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,233 @@
# Migrating from PER-CS v1.0 (PSR-12) to PER-CS v2.0 ###

## Summary

PER-CS is the next evolution of the PSR set of Coding Standards from the
PHP-FIG (Framework Interoperability Group). It extends the Coding Standards
laid out in PSR-12 to the newest functionality added to PHP such as the match
keyword, enums, attributes, and more.

This document describes the changes and additions on a section by section
basis between PER-CS v2.0 and PER-CS v1.0 (which is a direct equivalent of
PSR12 with very minor changes).

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)
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 v2.0.

This document is non-normative. The published [2.0 PER-CS](https://www.php-fig.org/per/coding-style/) specification
is the canonical source for the PER-CS formatting expectations.

## [Section 2.6 - Trailing Commas](https://www.php-fig.org/per/coding-style/#26-trailing-commas)

Numerous constructs now allow a sequence of values to have an optional trailing
comma:
* If the final item is on the same line then there MUST NOT be a trailing comma
* If the final item is not on the same line then there MUST be a trailing comma

## [Section 4.4 - Methods and Functions](https://www.php-fig.org/per/coding-style/#44-methods-and-functions)

If a function or method contains no statements or comments (such as an empty
no-op implementation or when using constructor property promotion), then the
body SHOULD be abbreviated as {} and placed on the same line as the previous
symbol, separated by a space.
So, for a method of a subclass that does nothing:

```php
<?php
class SubClass extends BaseClass
{
protected function init() {}
}
```

## [Section 4.6 - Modifier Keywords](https://www.php-fig.org/per/coding-style/#46-modifier-keywords)

Modifier keywords are keywords that alter how PHP handles classes,
properties and methods.

These keywords MUST BE ordered as follows:

[abstract|final] [public|protected|private] [static] [readonly] [type] name

Furthermore, all keywords must be on a single line and MUST be separated
by a single space.

## [Section 4.7 - Method and Function Calls](https://www.php-fig.org/per/coding-style/#47-method-and-function-calls)

If using named arguments, there MUST NOT be a space between the argument name and the colon,
and there MUST be a single space between the colon and the argument value.

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.

## [Section 4.8 - Function Callable References](https://www.php-fig.org/per/coding-style/#48-function-callable-references)

Function callable references - there must not be whitespace surrounding the '...' operator ()

## [Section 5.2 - Switch, Case, Match](https://www.php-fig.org/per/coding-style/#52-switch-case-match)

The match keyword is now covered.

## [Section 7.1 - Short Closures](https://www.php-fig.org/per/coding-style/#71-short-closures)

A new subsection about Short Closures, as per the link above. Example as follows:

```php
<?php
$func = fn(int $x, int $y): int => $x + $y;

$func = fn(int $x, int $y): int
=> $x + $y;

$func = fn(
int $x,
int $y,
): int
=> $x + $y;

$result = $collection->reduce(fn(int $x, int $y): int => $x + $y, 0);
```

## [Section 9 - Enumerations](https://www.php-fig.org/per/coding-style/#9-enumerations)

Enums are now covered, as per the link above. Please see below for examples.

```php
<?php
enum Suit: string
{
case Hearts = 'H';
case Diamonds = 'D';
case Clubs = 'C';
case Spades = 'S';
}
```

```php
<?php
enum Size
{
case Small;
case Medium;
case Large;

public const Huge = self::Large;
}
```

## [Section 10 - Heredoc and Nowdoc](https://www.php-fig.org/per/coding-style/#10-heredoc-and-nowdoc)

This is a new section about Heredoc and Nowdoc notation as per the link above.
Example follows:

```php
<?php
function allowed()
{
$allowedHeredoc = <<<COMPLIANT
This
is
a
compliant
heredoc
COMPLIANT;

$allowedNowdoc = <<<'COMPLIANT'
This
is
a
compliant
nowdoc
COMPLIANT;

var_dump(
'foo',
<<<'COMPLIANT'
This
is
a
compliant
parameter
COMPLIANT,
'bar',
);
}
```

## [Section 11 - Arrays](https://www.php-fig.org/per/coding-style/#11-arrays)

This is a new section about arrays, as per the link above.
Example as follows:

```php
<?php
$arr1 = ['single', 'line', 'declaration'];
$arr2 = [
'multi',
'line',
'declaration',
['values' => 1, 5, 7],
[
'nested',
'array',
],
];
```

## [Section 12 - Attributes](https://www.php-fig.org/per/coding-style/#12-attributes)

This is a new section, as per the above link.
The following is an example of valid usage.

```php
<?php
#[Foo]
#[Bar('baz')]
class Demo
{
#[Beep]
private Foo $foo;

public function __construct(
#[Load(context: 'foo', bar: true)]
private readonly FooService $fooService,

#[LoadProxy(context: 'bar')]
private readonly BarService $barService,
) {}

/**
* Sets the foo.
*/
#[Poink('narf'), Narf('poink')]
public function setFoo(#[Beep] Foo $new): void
{
// ...
}

#[Complex(
prop: 'val',
other: 5,
)]
#[Other, Stuff]
#[Here]
public function complicated(
string $a,

#[Decl]
string $b,

#[Complex(
prop: 'val',
other: 5,
)]
string $c,

int $d,
): string {
// ...
}
}
```