View
231
Download
0
Category
Preview:
Citation preview
7/25/2019 PSR-2_Coding Style Guide - PHP-FIG
1/24
7/25/2019 PSR-2_Coding Style Guide - PHP-FIG
2/24
MUST be one blank line afterthe block of use declarations.
Opening bracesforclassesMUST go on the next line, andclosing braces
MUST go on the next line afterthe body.
Opening bracesformethodsMUST go on the next line, andclosing braces
MUST go on the next line afterthe body.
Visibility MUST be declaredon all propertiesandmethods; abstract and
final MUST be declaredbefore the visibility; static MUST be declared
afterthe visibility.
Control structure keywordsMUST have one space afterthem; methodand
function callsMUST NOT.
Opening bracesforcontrol structuresMUST go on the same line, andclosing bracesMUST go on the next line afterthe body.
Opening parenthesesforcontrol structuresMUST NOT have a space after
them, andclosing parenthesesforcontrol structuresMUST NOT have a
space before.
1.1. Example
Thisexample encompassessome of the rulesbelowasa quick overview:
7/25/2019 PSR-2_Coding Style Guide - PHP-FIG
3/24
if($a===$b) {
bar();
} elseif($a>$b) {
$foo->bar($arg1);
} else{
BazClass::bar($arg2, $arg3); }
}
fi lpublicst ticfu ctio b r()
{
// method body
}
}
2. General
2.1Basic Coding Standard
Code MUST followall rulesoutlinedin PSR-1.
2.2Files
All PHP filesMUST use the Unix LF (linefeed) line ending.
All PHP filesMUST endwith a single blank line.
The closing ?> tag MUST be omittedfrom filescontaining only PHP.
2.3. Lines
There MUST NOT be a hardlimit on line length.
2: Coding Style Guide - PHP-FIG http://www.php-fig.org/p
24 19/06/2016 0
7/25/2019 PSR-2_Coding Style Guide - PHP-FIG
4/24
The soft limit on line length MUST be 120characters; automatedstyle
checkersMUST warn but MUST NOT errorat the soft limit.
LinesSHOULD NOT be longerthan 80characters; lineslongerthan that
SHOULD be split into multiple subsequent linesof no more than 80
characterseach.
There MUST NOT be trailing whitespace at the endof non-blank lines.
Blank linesMAY be addedto improve readability andto indicate related
blocksof code.
There MUST NOT be more than one statement perline.
2.4. Indenting
Code MUST use an indent of 4 spaces, andMUST NOT use tabsforindenting.
N.b.: Using only spaces, andnot mixing spaceswith tabs, helpsto avoid
problemswith diffs, patches, history, andannotations. The use of spacesalso
makesit easy to insert fine-grainedsub-indentation forinter-line alignment.
2.5. KeywordsandTrue/False/Null
PHP keywordsMUST be in lowercase.
The PHP constants true , false , and null MUST be in lowercase.
3. Namespace andUse Declarations
When present, there MUST be one blank line afterthe namespace declaration.
2: Coding Style Guide - PHP-FIG http://www.php-fig.org/p
24 19/06/2016 0
7/25/2019 PSR-2_Coding Style Guide - PHP-FIG
5/24
When present, all use declarationsMUST go afterthe namespace declaration.
There MUST be one use keywordperdeclaration.
There MUST be one blank line afterthe use block.
Forexample:
7/25/2019 PSR-2_Coding Style Guide - PHP-FIG
6/24
useFooClass;
useBarClass sBar;
useOtherVendor\OtherPackage\BazClass;
cl ssCl ssN meexte dsParentClass impleme ts\ArrayAccess, \Countable{
// constants, properties, methods
}
Listsof implements MAY be split acrossmultiple lines, where each
subsequent line isindentedonce. When doing so, the first item in the list
MUST be on the next line, andthere MUST be only one interface perline.
7/25/2019 PSR-2_Coding Style Guide - PHP-FIG
7/24
The var keywordMUST NOT be usedto declare a property.
There MUST NOT be more than one property declaredperstatement.
Property namesSHOULD NOT be prefixedwith a single underscore to indicate
protectedorprivate visibility.
A property declaration lookslike the following.
7/25/2019 PSR-2_Coding Style Guide - PHP-FIG
8/24
7/25/2019 PSR-2_Coding Style Guide - PHP-FIG
9/24
next line, andthere MUST be only one argument perline.
When the argument list issplit acrossmultiple lines, the closing parenthesis
andopening brace MUST be placedtogetheron theirown line with one space
between them.
7/25/2019 PSR-2_Coding Style Guide - PHP-FIG
10/24
protectedst tic$foo;
bstr ctprotectedfu ctio zim();
fi lpublicst ticfu ctio b r()
{ // method body
}
}
4.6. MethodandFunction Calls
When making a methodorfunction call, there MUST NOT be a space between
the methodorfunction name andthe opening parenthesis, there MUST NOT
be a space afterthe opening parenthesis, andthere MUST NOT be a space
before the closing parenthesis. In the argument list, there MUST NOT be a
space before each comma, andthere MUST be one space aftereach comma.
7/25/2019 PSR-2_Coding Style Guide - PHP-FIG
11/24
);
5. Control Structures
The general style rulesforcontrol structuresare asfollows:
There MUST be one space afterthe control structure keyword
There MUST NOT be a space afterthe opening parenthesis
There MUST NOT be a space before the closing parenthesis
There MUST be one space between the closing parenthesisandthe
opening brace
The structure body MUST be indentedonce
The closing brace MUST be on the next line afterthe body
The body of each structure MUST be enclosedby braces. Thisstandardizes
howthe structureslook, andreducesthe likelihoodof introducing errorsas
newlinesget addedto the body.
5.1. if, elseif, else
An if structure lookslike the following. Note the placement of parentheses,
spaces, andbraces; andthat else and elseif are on the same line asthe
closing brace from the earlierbody.
7/25/2019 PSR-2_Coding Style Guide - PHP-FIG
12/24
// else body;
}
The keyword elseif SHOULD be usedinsteadof else if so that all control
keywordslook like single words.
5.2. switch, case
A switch structure lookslike the following. Note the placement of
parentheses, spaces, andbraces. The case statement MUST be indented
once from switch , andthe break keyword(orotherterminating keyword)
MUST be indentedat the same level asthe case body. There MUST be a
comment such as // no break when fall-through isintentional in a
non-empty case body.
7/25/2019 PSR-2_Coding Style Guide - PHP-FIG
13/24
5.3. while, do while
A while statement lookslike the following. Note the placement of
parentheses, spaces, andbraces.
7/25/2019 PSR-2_Coding Style Guide - PHP-FIG
14/24
parentheses, spaces, andbraces.
7/25/2019 PSR-2_Coding Style Guide - PHP-FIG
15/24
parenthesisof the argument list orvariable list.
In the argument list andvariable list, there MUST NOT be a space before each
comma, andthere MUST be one space aftereach comma.
Closure argumentswith default valuesMUST go at the endof the argument
list.
A closure declaration lookslike the following. Note the placement of
parentheses, commas, spaces, andbraces:
7/25/2019 PSR-2_Coding Style Guide - PHP-FIG
16/24
7/25/2019 PSR-2_Coding Style Guide - PHP-FIG
17/24
};
$shortArgs_longVars=fu ctio ($arg) use(
$longVar1,
$longerVar2,
$muchLongerVar3) {
// body
};
Note that the formatting rulesalso apply when the closure isuseddirectly in
a function ormethodcall asan argument.
7/25/2019 PSR-2_Coding Style Guide - PHP-FIG
18/24
Commentsanddocumentation blocks
Classname prefixesandsuffixes
Best practices
Future recommendationsMAY revise andextendthisguide to addressthose
orotherelementsof style andpractice.
Appendix A. Survey
In writing thisstyle guide, the group took a survey of memberprojectsto
determine common practices. The survey isretainedherein forposterity.
A.1. Survey Data
url,http://www.horde.org/apps/horde/docs/CODING_STANDARDS,http://pear.php.
voting,yes,yes,yes,yes,yes,yes,yes,yes,yes,yes,yes,yes,yes,yes,yes,no,no,n
indent_type,4,4,4,4,4,tab,4,tab,tab,2,4,tab,4,4,4,4,4,4,tab,tab,4,tab
line_length_limit_soft,75,75,75,75,no,85,120,120,80,80,80,no,100,80,80,?,?,
line_length_limit_hard,85,85,85,85,no,no,no,no,100,?,no,no,no,100,100,?,12
class_names,studly,studly,studly,studly,studly,studly,studly,studly,studly,
class_brace_line,next,next,next,next,next,same,next,same,same,same,same,ne
constant_names,upper,upper,upper,upper,upper,upper,upper,upper,upper,upper,
true_false_null,lower,lower,lower,lower,lower,lower,lower,lower,lower,uppe
method_names,camel,camel,camel,camel,camel,camel,camel,camel,camel,camel,c
method_brace_line,next,next,next,next,next,same,next,same,same,same,same,n
control_brace_line,same,same,same,same,same,same,next,same,same,same,same,
control_space_after,yes,yes,yes,yes,yes,no,yes,yes,yes,yes,no,yes,yes,yes,
always_use_control_braces,yes,yes,yes,yes,yes,yes,no,yes,yes,yes,no,yes,ye
else_elseif_line,same,same,same,same,same,same,next,same,same,next,same,ne
case_break_indent_from_switch,0/1,0/1,0/1,1/2,1/2,1/2,1/2,1/1,1/1,1/2,1/2,
function_space_after,no,no,no,no,no,no,no,no,no,no,no,no,no,no,no,no,no,no,
closing_php_tag_required,no,no,no,no,no,no,no,no,yes,no,no,no,no,yes,no,no,
2: Coding Style Guide - PHP-FIG http://www.php-fig.org/p
24 19/06/2016 0
7/25/2019 PSR-2_Coding Style Guide - PHP-FIG
19/24
line_endings,LF,LF,LF,LF,LF,LF,LF,LF,?,LF,?,LF,LF,LF,LF,?,,LF,?,LF,LF,LF
static_or_visibility_first,static,?,static,either,either,either,visibility,
control_space_parens,no,no,no,no,no,no,yes,no,no,no,no,no,no,yes,?,no,no,n
blank_line_after_php,no,no,no,no,yes,no,no,no,no,yes,yes,no,no,yes,?,yes,y
class_method_control_brace,next/next/same,next/next/same,next/next/same,ne
A.2. Survey Legend
indent_type : The type of indenting. tab = "Use a tab", 2 or 4 = "numberof
spaces"
line_length_limit_soft : The "soft" line length limit, in characters. ? = not
discernible orno response, no meansno limit.
line_length_limit_hard : The "hard" line length limit, in characters. ? = not
discernible orno response, no meansno limit.
class_names : Howclassesare named. lower = lowercase only, lower_under =
lowercase with underscore separators, studly = StudlyCase.
class_brace_line : Doesthe opening brace fora classgo on the same line as
the classkeyword, oron the next line afterit?
constant_names : Howare classconstantsnamed? upper = Uppercase with
underscore separators.
true_false_null : Are the true , false , and null keywordsspelledasall
lower case, orall upper case?
method_names : Howare methodsnamed? camel = camelCase , lower_under =
lowercase with underscore separators.
2: Coding Style Guide - PHP-FIG http://www.php-fig.org/p
24 19/06/2016 0
7/25/2019 PSR-2_Coding Style Guide - PHP-FIG
20/24
method_brace_line : Doesthe opening brace fora methodgo on the same line
asthe methodname, oron the next line?
control_brace_line : Doesthe opening brace fora control structure go on the
same line, oron the next line?
control_space_after : Isthere a space afterthe control structure keyword?
always_use_control_braces : Do control structuresalwaysuse braces?
else_elseif_line : When using else or elseif , doesit go on the same line as
the previousclosing brace, ordoesit go on the next line?
case_break_indent_from_switch : Howmany timesare case and break
indentedfrom an opening switch statement?
function_space_after : Do function callshave a space afterthe function name
andbefore the opening parenthesis?
closing_php_tag_required : In filescontaining only PHP, isthe closing ?> tag
required?
line_endings : What type of line ending isused?
static_or_visibility_first : When declaring a method, does static come
first, ordoesthe visibility come first?
control_space_parens : In a control structure expression, isthere a space after
the opening parenthesisanda space before the closing parenthesis? yes =
if ( $expr ) , no = if ($expr) .
blank_line_after_php : Isthere a blank line afterthe opening PHP tag?
2: Coding Style Guide - PHP-FIG http://www.php-fig.org/p
24 19/06/2016 0
7/25/2019 PSR-2_Coding Style Guide - PHP-FIG
21/24
class_method_control_brace : A summary of what line the opening bracesgo
on forclasses, methods, andcontrol structures.
A.3. Survey Results
indent_type:
tab: 7
2: 1
4: 14
line_length_limit_soft:
?: 2
no: 3
75: 4
80: 6
85: 1
100: 1
120: 4
150: 1
line_length_limit_hard:
?: 2
no: 11
85: 4
100: 3
120: 2
class_names:
?: 1
lower: 1
lower_under: 1
studly: 19
class_brace_line:
next: 16
same: 6
constant_names:
upper: 22
2: Coding Style Guide - PHP-FIG http://www.php-fig.org/p
24 19/06/2016 0
7/25/2019 PSR-2_Coding Style Guide - PHP-FIG
22/24
true_false_null:
lower: 19
upper: 3
method_names:
camel: 21
lower_under: 1method_brace_line:
next: 15
same: 7
control_brace_line:
next: 4
same: 18
control_space_after:
no: 2
yes: 20
always_use_control_braces:
no: 3
yes: 19
else_elseif_line:
next: 6
same: 16case_break_indent_from_switch:
0/1: 4
1/1: 4
1/2: 14
function_space_after:
no: 22
closing_php_tag_required:
no: 19
yes: 3
line_endings:
?: 5
LF: 17
static_or_visibility_first:
?: 5
2: Coding Style Guide - PHP-FIG http://www.php-fig.org/p
24 19/06/2016 0
7/25/2019 PSR-2_Coding Style Guide - PHP-FIG
23/24
either: 7
static: 4
visibility: 6
control_space_parens:
?: 1
no: 19 yes: 2
blank_line_after_php:
?: 1
no: 13
yes: 8
class_method_control_brace:
next/next/next: 4
next/next/same: 11
next/same/same: 1
same/same/same: 6
Additional Info:
Available translations:
PSR-2: Coding Style Guide
PSR-2Meta Document
English (official)
2: Coding Style Guide - PHP-FIG http://www.php-fig.org/p
24 19/06/2016 0
7/25/2019 PSR-2_Coding Style Guide - PHP-FIG
24/24
Followuson Twitter Chat on IRC Channel Contribute via Github Join ourmailing list
2016PHP Framework Interop Group. Site design by Jonathan Reinink.
2: Coding Style Guide - PHP-FIG http://www.php-fig.org/p
Recommended