@@ -131,7 +131,7 @@ Short form of type keywords MUST be used i.e. `bool` instead of `boolean`,
131
131
### 2.6 Trailing commas
132
132
133
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,
134
+ and the final item may have an optional comma. Examples include array key/value pairs,
135
135
function arguments, closure ` use ` statements, ` match() ` statement branches, etc.
136
136
137
137
If that list is contained on a single line, then the last item MUST NOT have a trailing comma.
@@ -279,6 +279,7 @@ Declare statements MUST NOT contain any spaces and MUST be exactly `declare(stri
279
279
280
280
Block declare statements are allowed and MUST be formatted as below. Note position of
281
281
braces and spacing:
282
+
282
283
``` php
283
284
declare(ticks=1) {
284
285
// some code
@@ -293,15 +294,15 @@ Any closing brace MUST NOT be followed by any comment or statement on the
293
294
same line.
294
295
295
296
When instantiating a new class, parentheses MUST always be present even when
296
- there are no arguments passed to the constructor.
297
+ there are no arguments passed to the constructor. For example:
297
298
298
299
``` php
299
300
new Foo();
300
301
```
301
302
302
303
If class contains no additional declarations (such as an exception that exists only extend another exception with a new type),
303
304
then the body of the class SHOULD be abbreviated as ` {} ` and placed on the same line as the previous symbol,
304
- separated by a space. For example:
305
+ separated by a space. For example:
305
306
306
307
``` php
307
308
class MyException extends \RuntimeException {}
@@ -312,15 +313,13 @@ class MyException extends \RuntimeException {}
312
313
The ` extends ` and ` implements ` keywords MUST be declared on the same line as
313
314
the class name.
314
315
315
- The opening brace for the class MUST go on its own line; the closing brace
316
- for the class MUST go on the next line after the body.
317
-
318
- Opening braces MUST be on their own line and MUST NOT be preceded or followed
319
- by a blank line.
316
+ The opening brace for the class MUST go on its own line, and MUST NOT be
317
+ preceded or followed by a blank line.
320
318
321
- Closing braces MUST be on their own line and MUST NOT be preceded by a blank
322
- line.
319
+ The closing brace for the class MUST go on its own line, immediately following
320
+ the last line of the class body, and MUST NOT be preceded by a blank line.
323
321
322
+ The following is a validly formatted class:
324
323
325
324
``` php
326
325
<?php
@@ -340,7 +339,7 @@ class ClassName extends ParentClass implements \ArrayAccess, \Countable
340
339
Lists of ` implements ` and, in the case of interfaces, ` extends ` MAY be split
341
340
across multiple lines, where each subsequent line is indented once. When doing
342
341
so, the first item in the list MUST be on the next line, and there MUST be only
343
- one interface per line.
342
+ one interface per line. For example:
344
343
345
344
``` php
346
345
<?php
@@ -365,22 +364,11 @@ class ClassName extends ParentClass implements
365
364
The ` use ` keyword used inside the classes to implement traits MUST be
366
365
declared on the next line after the opening brace.
367
366
368
- ``` php
369
- <?php
370
-
371
- namespace Vendor\Package;
372
-
373
- use Vendor\Package\FirstTrait;
374
-
375
- class ClassName
376
- {
377
- use FirstTrait;
378
- }
379
- ```
380
-
381
367
Each individual trait that is imported into a class MUST be included
382
368
one-per-line and each inclusion MUST have its own ` use ` import statement.
383
369
370
+ The following is a correct example of trait usage.
371
+
384
372
``` php
385
373
<?php
386
374
@@ -400,6 +388,7 @@ class ClassName
400
388
401
389
When the class has nothing after the ` use ` import statement, the class
402
390
closing brace MUST be on the next line after the ` use ` import statement.
391
+ For example:
403
392
404
393
``` php
405
394
<?php
@@ -414,7 +403,7 @@ class ClassName
414
403
}
415
404
```
416
405
417
- Otherwise, it MUST have a blank line after the ` use ` import statement.
406
+ Otherwise, it MUST have a blank line after the ` use ` import statement, as in:
418
407
419
408
``` php
420
409
<?php
@@ -431,7 +420,7 @@ class ClassName
431
420
}
432
421
```
433
422
434
- When using the ` insteadof ` and ` as ` operators they must be used as follows taking
423
+ When using the ` insteadof ` and ` as ` operators they MUST be used as follows taking
435
424
note of indentation, spacing, and new lines.
436
425
437
426
``` php
@@ -525,7 +514,7 @@ function fooBarBaz($arg1, &$arg2, $arg3 = [])
525
514
526
515
If a function or method contains no statements (such as a no-op implementation or when using
527
516
constructor property promotion), then the body SHOULD be abbreviated as ` {} ` and placed on the same
528
- line as the previous symbol, separated by a space. For example:
517
+ line as the previous symbol, separated by a space. For example:
529
518
530
519
``` php
531
520
class Point
@@ -552,7 +541,7 @@ In the argument list, there MUST NOT be a space before each comma, and there
552
541
MUST be one space after each comma.
553
542
554
543
Method and function parameters with default values MUST go at the end of the argument
555
- list.
544
+ list. For example:
556
545
557
546
``` php
558
547
<?php
@@ -574,7 +563,7 @@ next line, and there MUST be only one argument per line.
574
563
575
564
When the argument list is split across multiple lines, the closing parenthesis
576
565
and opening brace MUST be placed together on their own line with one space
577
- between them.
566
+ between them. For example:
578
567
579
568
``` php
580
569
<?php
@@ -596,7 +585,7 @@ class ClassName
596
585
When you have a return type declaration present, there MUST be one space after
597
586
the colon followed by the type declaration. The colon and declaration MUST be
598
587
on the same line as the argument list closing parenthesis with no spaces between
599
- the two characters.
588
+ the two characters. For example:
600
589
601
590
``` php
602
591
<?php
@@ -623,7 +612,7 @@ class ReturnTypeVariations
623
612
```
624
613
625
614
In nullable type declarations, there MUST NOT be a space between the question mark
626
- and the type.
615
+ and the type. For example:
627
616
628
617
``` php
629
618
<?php
@@ -667,7 +656,7 @@ public function process(string $algorithm, &...$parts)
667
656
### 4.6 Modifier Keywords
668
657
669
658
Classes, properties, and methods have numerous keyword modifiers that alter how the
670
- engine and language handles them. When present, they MUST be in the following order:
659
+ engine and language handles them. When present, they MUST be in the following order:
671
660
672
661
* Inheritance modifier: ` abstract ` or ` final `
673
662
* Visibility modifier: ` public ` , ` protected ` , or ` private `
@@ -678,6 +667,8 @@ engine and language handles them. When present, they MUST be in the following o
678
667
679
668
All keywords MUST be on a single line, and MUST be separated by a single space.
680
669
670
+ The following is a correct example of modifier keyword usage:
671
+
681
672
``` php
682
673
<?php
683
674
@@ -711,6 +702,8 @@ after the opening parenthesis, and there MUST NOT be a space before the
711
702
closing parenthesis. In the argument list, there MUST NOT be a space before
712
703
each comma, and there MUST be one space after each comma.
713
704
705
+ The following lines show correct calls:
706
+
714
707
``` php
715
708
<?php
716
709
@@ -725,6 +718,8 @@ next line, and there MUST be only one argument per line. A single argument being
725
718
split across multiple lines (as might be the case with an anonymous function or
726
719
array) does not constitute splitting the argument list itself.
727
720
721
+ The following examples show correct argument usage.
722
+
728
723
``` php
729
724
<?php
730
725
@@ -748,14 +743,14 @@ $app->get('/hello/{name}', function ($name) use ($app) {
748
743
```
749
744
750
745
If using named arguments, there MUST NOT be a space between the argument name
751
- and colon, and there MUST be a single space between the colon and the argument value.
746
+ and colon, and there MUST be a single space between the colon and the argument value. For example:
752
747
753
748
``` php
754
749
somefunction($a, b: $b, c: 'c');
755
750
```
756
751
757
752
Method chaining MAY be put on separate lines, where each subsequent line is indented once. When doing so, the first
758
- method MUST be on the next line.
753
+ method MUST be on the next line. For example:
759
754
760
755
``` php
761
756
$someInstance
@@ -768,7 +763,7 @@ $someInstance
768
763
769
764
A function or method may be referenced in a way that creates a closure out of it, by providing ` ... ` in place of arguments.
770
765
771
- If so, the ` ... ` MUST NOT include any whitespace before or after. That is, the correct format is ` foo(...) ` .
766
+ If so, the ` ... ` MUST NOT include any whitespace before or after. That is, the correct format is ` foo(...) ` .
772
767
773
768
## 5. Control Structures
774
769
@@ -815,6 +810,8 @@ placed together on their own line with one space between them. Boolean
815
810
operators between conditions MUST always be at the beginning or at the end of
816
811
the line, not a mix of both.
817
812
813
+ For example:
814
+
818
815
``` php
819
816
<?php
820
817
@@ -867,6 +864,8 @@ placed together on their own line with one space between them. Boolean
867
864
operators between conditions MUST always be at the beginning or at the end of
868
865
the line, not a mix of both.
869
866
867
+ For example:
868
+
870
869
``` php
871
870
<?php
872
871
936
935
Expressions in parentheses MAY be split across multiple lines, where each
937
936
subsequent line is indented at least once. When doing so, the first condition
938
937
MUST be on the next line. Boolean operators between conditions MUST
939
- always be at the beginning or at the end of the line, not a mix of both.
938
+ always be at the beginning or at the end of the line, not a mix of both. For example:
940
939
941
940
``` php
942
941
<?php
@@ -965,7 +964,7 @@ for ($i = 0; $i < 10; $i++) {
965
964
Expressions in parentheses MAY be split across multiple lines, where each
966
965
subsequent line is indented at least once. When doing so, the first expression
967
966
MUST be on the next line. The closing parenthesis and opening brace MUST be
968
- placed together on their own line with one space between them.
967
+ placed together on their own line with one space between them. For example:
969
968
970
969
``` php
971
970
<?php
@@ -1023,7 +1022,7 @@ All operators not described here are left undefined.
1023
1022
### 6.1. Unary operators
1024
1023
1025
1024
The increment/decrement operators MUST NOT have any space between
1026
- the operator and operand.
1025
+ the operator and operand:
1027
1026
1028
1027
``` php
1029
1028
$i++;
@@ -1063,6 +1062,7 @@ $variable = $foo ? 'foo' : 'bar';
1063
1062
1064
1063
When the middle operand of the conditional operator is omitted, the operator
1065
1064
MUST follow the same style rules as other binary [ comparison] [ ] operators:
1065
+
1066
1066
``` php
1067
1067
$variable = $foo ?: 'bar';
1068
1068
```
@@ -1194,7 +1194,7 @@ The `=>` symbol MUST be preceded and succeeded by a space.
1194
1194
1195
1195
The semicolon at the end of the expression MUST NOT be preceded by a space.
1196
1196
1197
- The expression portion MAY be split to a subsequent line. If so, the ` => ` MUST be included
1197
+ The expression portion MAY be split to a subsequent line. If so, the ` => ` MUST be included
1198
1198
on the second line, and MUST be indented once.
1199
1199
1200
1200
The following examples show proper common usage of short closures.
@@ -1231,7 +1231,7 @@ the list of `implements` interfaces does not wrap. If the list of interfaces
1231
1231
wraps, the brace MUST be placed on the line immediately following the last
1232
1232
interface.
1233
1233
1234
- If the anonymous class has no arguments, the ` () ` after ` class ` MUST be omitted.
1234
+ If the anonymous class has no arguments, the ` () ` after ` class ` MUST be omitted. For example:
1235
1235
1236
1236
``` php
1237
1237
<?php
@@ -1260,17 +1260,19 @@ $instance = new class($a) extends \Foo implements
1260
1260
1261
1261
Enumerations (enums) MUST follow the same guidelines as classes, except where otherwise noted below.
1262
1262
1263
- Methods in enums MUST follow the same guidelines as methods in classes. Non-public methods MUST use ` private `
1263
+ Methods in enums MUST follow the same guidelines as methods in classes. Non-public methods MUST use ` private `
1264
1264
instead of ` protected ` , as enums do not support inheritance.
1265
1265
1266
1266
When using a backed enum, there MUST NOT be a space between the enum name and colon, and there MUST be exactly one
1267
- space between the colon and the backing type. This is consistent with the style for return types.
1267
+ space between the colon and the backing type. This is consistent with the style for return types.
1268
1268
1269
- Enum case declarations MUST use PascalCase capitalization. Enum case declarations MUST be on their own line.
1269
+ Enum case declarations MUST use PascalCase capitalization. Enum case declarations MUST be on their own line.
1270
1270
1271
1271
Constants in Enumerations MAY use either PascalCase or UPPER_CASE capitalization. PascalCase is RECOMMENDED,
1272
1272
so that it is consistent with case declarations.
1273
1273
1274
+ The following example shows a typical valid Enum:
1275
+
1274
1276
``` php
1275
1277
enum Suit: string
1276
1278
{
@@ -1296,6 +1298,7 @@ indentation they are declared in.
1296
1298
1297
1299
The following is *** not allowed*** due to the heredoc beginning on a
1298
1300
different line than the context it's being declared in:
1301
+
1299
1302
``` php
1300
1303
$notAllowed =
1301
1304
<<<'COUNTEREXAMPLE'
@@ -1306,11 +1309,12 @@ $notAllowed =
1306
1309
COUNTEREXAMPLE;
1307
1310
```
1308
1311
1309
- Instead the heredoc MUST be declared on the same line as the variable
1312
+ Instead, the heredoc MUST be declared on the same line as the variable
1310
1313
declaration it's being set against.
1311
1314
1312
1315
The follow is *** not allowed*** due to the scope indention not matching the scope the
1313
1316
heredoc is declared in:
1317
+
1314
1318
``` php
1315
1319
function notAllowed()
1316
1320
{
@@ -1328,6 +1332,7 @@ it's declared in.
1328
1332
1329
1333
The following is an example of both a heredoc and a nowdoc declared in a
1330
1334
compliant way:
1335
+
1331
1336
``` php
1332
1337
function allowed()
1333
1338
{
@@ -1383,6 +1388,8 @@ MUST be placed on the next line after the last value. There MUST NOT be more
1383
1388
than one value assignment per line. Value assignments MAY use a single line
1384
1389
or multiple lines.
1385
1390
1391
+ The following example shows correct array usage:
1392
+
1386
1393
```php
1387
1394
<?php
1388
1395
@@ -1421,12 +1428,12 @@ be placed on their own line, immediately prior to the structure being described.
1421
1428
For attributes on parameters, if the parameter list is presented on a single line,
1422
1429
the attribute MUST be placed inline with the parameter it describes, separated by a single space.
1423
1430
If the parameter list is split into multiple lines for any reason, the attribute MUST be placed on
1424
- its own line prior to the parameter, indented the same as the parameter. If the parameter list
1431
+ its own line prior to the parameter, indented the same as the parameter. If the parameter list
1425
1432
is split into multiple lines, a blank line MAY be included between one parameter and the attributes
1426
1433
of the following parameter in order to aid readability.
1427
1434
1428
1435
If a comment docblock is present on a structure that also includes an attribute, the comment block MUST
1429
- come first, followed by any attributes, followed by the structure itself. There MUST NOT be any blank lines
1436
+ come first, followed by any attributes, followed by the structure itself. There MUST NOT be any blank lines
1430
1437
between the docblock and attributes, or the attributes and the structure.
1431
1438
1432
1439
If two separate attribute blocks are used in a multi-line context, they MUST be on separate lines with no blank
@@ -1435,7 +1442,7 @@ lines between them.
1435
1442
### 12.3 Compound attributes
1436
1443
1437
1444
If multiple attributes are placed in the same attribute block, they MUST be separated by a comma with a space
1438
- following but no space preceding. If the attribute list is split into multiple lines for any reason, then the
1445
+ following but no space preceding. If the attribute list is split into multiple lines for any reason, then the
1439
1446
attributes MUST be placed in separate attribute blocks. Those blocks may themselves contain multiple
1440
1447
attributes provided this rule is respected.
1441
1448
0 commit comments