Skip to content

Commit d12619a

Browse files
authored
Merge branch 'master' into enums
2 parents 9ed23b0 + 6c0a417 commit d12619a

File tree

2 files changed

+158
-9
lines changed

2 files changed

+158
-9
lines changed

meta.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,8 @@ This PER shares the same goals as PSR-12.
4444
> of guidelines to be used among all those projects. Thus, the benefit of this guide is
4545
> not in the rules themselves, but in the sharing of those rules.
4646
47-
This PER is an extension of PSR-12, and therefore also an extension of PSR-1. The basis of PER is
48-
PSR-12 and therefore a list of differences is provided below to assist with migration,
47+
This PER is an extension of PSR-12, and therefore also an extension of PSR-1.
48+
With PSR-12 being the base of this PER, a list of differences is provided below to assist with migration,
4949
but it should be considered as an independent specification.
5050

5151
This PER will include coding style guidelines related to new functionality added to PHP
@@ -192,6 +192,7 @@ specification for a full understanding of its contents.
192192
* Kathryn Reeve
193193
* Ken Guest
194194
* Larry Garfield
195+
* Luke Diggins
195196
* Woody Gilk
196197

197198
### 6.4. Special Thanks

spec.md

Lines changed: 155 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,47 @@ Any new types and keywords added to future PHP versions MUST be in lower case.
128128
Short form of type keywords MUST be used i.e. `bool` instead of `boolean`,
129129
`int` instead of `integer` etc.
130130

131+
### 2.6 Trailing commas
132+
133+
Numerous PHP constructs allow a sequence of values to be separated by a comma,
134+
and the final item may have an optional comma. Examples include array key/value pairs,
135+
function arguments, closure `use` statements, `match()` statement branches, etc.
136+
137+
If that list is contained on a single line, then the last item MUST NOT have a trailing comma.
138+
139+
If the list is split across multiple lines, then the last item MUST have a trailing comma.
140+
141+
The following are examples of correct comma placement:
142+
143+
```php
144+
function beep(string $a, string $b, string $c)
145+
{
146+
// ...
147+
}
148+
149+
function beep(
150+
string $a,
151+
string $b,
152+
string $c,
153+
) {
154+
// ...
155+
}
156+
157+
$arr = ['a' => 'A', 'b' => 'B', 'c' => 'C'];
158+
159+
$arr = [
160+
'a' => 'A',
161+
'b' => 'B',
162+
'c' => 'C',
163+
];
164+
165+
$result = match ($a) {
166+
'foo' => 'Foo',
167+
'bar' => 'Bar',
168+
default => 'Baz',
169+
};
170+
```
171+
131172
## 3. Declare Statements, Namespace, and Import Statements
132173

133174
The header of a PHP file may consist of a number of different blocks. If present,
@@ -231,7 +272,7 @@ For example:
231272
</html>
232273
```
233274

234-
Declare statements MUST contain no spaces and MUST be exactly `declare(strict_types=1)`
275+
Declare statements MUST NOT contain any spaces and MUST be exactly `declare(strict_types=1)`
235276
(with an optional semicolon terminator).
236277

237278
Block declare statements are allowed and MUST be formatted as below. Note position of
@@ -590,13 +631,19 @@ public function process(string $algorithm, &...$parts)
590631
}
591632
```
592633

593-
### 4.6 `abstract`, `final`, and `static`
634+
### 4.6 Modifier Keywords
594635

595-
When present, the `abstract` and `final` declarations MUST precede the
596-
visibility declaration.
636+
Properties and methods of a class have numerous keyword modifiers that alter how the
637+
engine and language handles them. When present, they MUST be in the following order:
597638

598-
When present, the `static` declaration MUST come after the visibility
599-
declaration.
639+
* Inheritance modifier: `abstract` or `final`
640+
* Visibility modifier: `public`, `protected`, or `private`
641+
* Scope modifier: `static`
642+
* Mutation modifier: `readonly`
643+
* Type declaration
644+
* Name
645+
646+
All keywords MUST be on a single line, and MUST be separated by a single space.
600647

601648
```php
602649
<?php
@@ -605,7 +652,9 @@ namespace Vendor\Package;
605652

606653
abstract class ClassName
607654
{
608-
protected static $foo;
655+
protected static readonly string $foo;
656+
657+
final protected int $beep;
609658

610659
abstract protected function zim();
611660

@@ -614,6 +663,11 @@ abstract class ClassName
614663
// method body
615664
}
616665
}
666+
667+
readonly class ValueObject
668+
{
669+
// ...
670+
}
617671
```
618672

619673
### 4.7 Method and Function Calls
@@ -1058,6 +1112,36 @@ $foo->bar(
10581112
);
10591113
```
10601114

1115+
### 7.1 Short Closures
1116+
1117+
Short closures, also known as arrow functions, MUST follow the same guidelines
1118+
and principles as long closures above, with the following additions.
1119+
1120+
The `fn` keyword MUST be preceded and succeeded by a space.
1121+
1122+
The `=>` symbol MUST be preceded and succeeded by a space.
1123+
1124+
The semicolon at the end of the expression MUST NOT be preceded by a space.
1125+
1126+
The expression portion MAY be split to a subsequent line. If so, the `=>` MUST be included
1127+
on the second line, and MUST be indented once.
1128+
1129+
The following examples show proper common usage of short closures.
1130+
1131+
```php
1132+
1133+
$func = fn (int $x, int $y): int => $x + $y;
1134+
1135+
$func = fn (int $x, int $y): int
1136+
=> $x + $y;
1137+
1138+
$func = fn (
1139+
int $x,
1140+
int $y
1141+
): int
1142+
=> $x + $y;
1143+
```
1144+
10611145
## 8. Anonymous Classes
10621146

10631147
Anonymous Classes MUST follow the same guidelines and principles as closures
@@ -1118,6 +1202,70 @@ enum Suit: string
11181202
const Wild = self::Spades;
11191203
}
11201204

1205+
## 10. Heredoc and Nowdoc
1206+
1207+
A nowdoc SHOULD be used wherever possible. Heredoc MAY be used when a nowdoc
1208+
does not satisfy requirements.
1209+
1210+
Heredoc and nowdoc syntax is largely governed by PHP requirements with the only
1211+
allowed variation being indentation. Declared heredocs or nowdocs MUST
1212+
begin on the same line as the context the declaration is being used in.
1213+
Subsequent lines in the heredoc or nowdoc MUST be indented once past the scope
1214+
indentation they are declared in.
1215+
1216+
The following is ***not allowed*** due to the heredoc beginning on a
1217+
different line than the context it's being declared in:
1218+
```php
1219+
$notAllowed =
1220+
<<<'COUNTEREXAMPLE'
1221+
This
1222+
is
1223+
not
1224+
allowed.
1225+
COUNTEREXAMPLE;
1226+
```
1227+
1228+
Instead the heredoc MUST be declared on the same line as the variable
1229+
declaration it's being set against.
1230+
1231+
The follow is ***not allowed*** due to the scope indention not matching the scope the
1232+
heredoc is declared in:
1233+
```php
1234+
function notAllowed()
1235+
{
1236+
$notAllowed = <<<'COUNTEREXAMPLE'
1237+
This
1238+
is
1239+
not
1240+
allowed.
1241+
COUNTEREXAMPLE
1242+
}
1243+
```
1244+
1245+
Instead, the heredoc MUST be indented once past the indentation of the scope
1246+
it's declared in.
1247+
1248+
The following is an example of both a heredoc and a nowdoc declared in a
1249+
compliant way:
1250+
```php
1251+
function allowed()
1252+
{
1253+
$allowed = <<<COMPLIANT
1254+
This
1255+
is
1256+
a
1257+
compliant
1258+
heredoc
1259+
COMPLIANT;
1260+
1261+
$allowedNowdoc = <<<'COMPLIANT'
1262+
This
1263+
is
1264+
a
1265+
compliant
1266+
heredoc
1267+
COMPLIANT;
1268+
}
11211269
```
11221270

11231271
[PSR-1]: https://www.php-fig.org/psr/psr-1/

0 commit comments

Comments
 (0)