Skip to content
Merged
Changes from 24 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
196 changes: 196 additions & 0 deletions migration-2.0.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,196 @@
# 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 changelog](https://github.com/php-fig/per-coding-style/compare/1.0.0...2.0.0)
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
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 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 with standard closures, 'fn' must not be succeeded by a space.
The '=>' symbol MUST be preceeded and succeeded by a space. (No leading or trailing spaces)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
The '=>' symbol MUST be preceeded and succeeded by a space. (No leading or trailing spaces)
The '=>' symbol MUST be preceded and succeeded by a space. (No leading or trailing spaces)
The semicolon at the end MUST NOT be preceeded by a leading space.
The expression portion MAY be split to a subsequent line. If so, the => MUST be
included on the second line, and MUST be indented once.

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

Enums are now covered.

When using backed enums, there MUST NOT be a space between the enum name and
colon, and there must be exactly one space between the colon and the backing
type, e.g. "enum Suit: string" or "enum BanExpiry: int".

Enum case declarations MUST use PascalCase capitalization. Enum case
declarations MUST be on their own line.

An example of this can be seen in the online php documentation at https://www.php.net/manual/en/language.enumerations.backed.php

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

Constants in Enums MAY be either PascalCase or UPPER_CASE, PascalCase
is RECOMMENDED, so that it is consistent with case declarations.

<?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.

NowDoc SHOULD be used whereever possible. Heredoc MAY be used where a
nowdoc is not sufficient.

Declared heredocs or nowdocs MUST begin on the same line as the context
the declaration is being used. Subsequent lines in the heredoc
or nowdoc MUST be indented once past the scope indentation they are
declared in.

The heredoc MUST be declared on the same line as the variable declaration it's being set against.
The heredoc MUST be indented once past the indentation of the scope it's declared in.

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

This is a new section.

Arrays MUST be declared using the short array syntax.
Arrays MUST follow the trailing comma guidelines.

Array declarations MAY be split across multiple lines, where each
subsequent line is indented once. When doing so, the first value in the
array MUST be on the next line, and there MUST be only one value per line.

When the array declaration is split across multiple lines, the opening
bracket MUST be placed on the same line as the equals sign.

The closing bracket MUST be placed on the next line after the last value.

There MUST NOT be more than one value assignment per line.

Value assignments MAY use a single line or multiple lines.

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

This is a new section.

Attribute names MUST immediately follow the opening attribute block indicator #[ with no space.

If an attribute has no arguments, the () MUST be omitted.

The closing attribute block indicator ] MUST follow the last character
of the attribute name or the closing ) of its argument list, with no
preceding space.

The construct #[...] is referred to as an "attribute block" in this document.

Attributes on classes, methods, functions, constants, and properties
MUST be placed on their own line, immediately before the structure
being described.

For attributes on parameters, if the parameter list is presented on a
single line, the attribute MUST be placed inline with the parameter it
describes, separated by a single space. If the parameter list is split
into multiple lines for any reason, the attribute MUST be placed on its
own line before the parameter, indented the same as the parameter. If
the parameter list is split into multiple lines, a blank line MAY be
included between one parameter and the attributes of the following
parameter to aid readability.

If a comment docblock is present on a structure that also includes an
attribute, the comment block MUST come first, followed by any attributes,
followed by the structure itself. There MUST NOT be any blank lines
between the docblock and attributes, or the attributes and the structure.

If two separate attribute blocks are used in a multi-line context,
they MUST be on separate lines with no blank lines between them.

* If multiple attributes are placed in the same attribute block, they MUST be separated by a comma with a space following but no space preceding.
* If the attribute list is split into multiple lines for any reason, then the attributes MUST be placed in separate attribute blocks. Those blocks may themselves contain multiple attributes provided this rule is respected.

If an attribute's argument list is split into multiple lines for any reason, then:

* The attribute MUST be the only one in its attribute block.
* The attribute arguments MUST follow the same rules as defined for multiline function calls.