PHPCS: PSR2
Personal documentation for the sniffs available in the PHPCS PSR2 Coding Standards to help build an own rulesets and search for error explanations.
Generic.ControlStructures.InlineControlStructure
| [ref]
Control Structures should use braces.
Valid: Braces are used around the control structure. | Invalid: No braces are used for the control structure.. |
---|---|
|
|
Generic.Files.ByteOrderMark
| [ref]
Byte Order Marks that may corrupt your application should not be used. These include 0xefbbbf (UTF-8), 0xfeff (UTF-16 BE) and 0xfffe (UTF-16 LE).
Generic.Files.LineEndings
| [ref]
Unix-style line endings are preferred ("\n" instead of "\r\n").
Generic.Files.LineLength
| [ref]
It is recommended to keep lines at approximately 80 characters long for better code readability.
Generic.Formatting.DisallowMultipleStatements
| [ref]
Multiple statements are not allowed on a single line.
Valid: Two statements are spread out on two separate lines. | Invalid: Two statements are combined onto one line. |
---|---|
|
|
Generic.Functions.FunctionCallArgumentSpacing
| [ref]
Function arguments should have one space after a comma, and single spaces surrounding the equals sign for default values.
Valid: Single spaces after a comma. | Invalid: No spaces after a comma. |
---|---|
|
|
Valid: Single spaces around an equals sign in function declaration. | Invalid: No spaces around an equals sign in function declaration. |
---|---|
|
|
Generic.NamingConventions.UpperCaseConstantName
| [ref]
Constants should always be all-uppercase, with underscores to separate words.
Valid: all uppercase | Invalid: mixed case |
---|---|
|
|
Generic.PHP.DisallowAlternativePHPTags
| [ref]
Always use <?php ?> to delimit PHP code, do not use the ASP <% %> style tags nor the <script language="php"></script> tags. This is the most portable way to include PHP code on differing operating systems and setups.
Generic.PHP.DisallowShortOpenTag
| [ref]
Always use <?php ?> to delimit PHP code, not the <? ?> shorthand. This is the most portable way to include PHP code on differing operating systems and setups.
Generic.PHP.LowerCaseConstant
| [ref]
The true, false and null constants must always be lowercase.
Valid: lowercase constants | Invalid: uppercase constants |
---|---|
|
|
Generic.PHP.LowerCaseKeyword
| [ref]
All PHP keywords should be lowercase.
Valid: Lowercase array keyword used. | Invalid: Non-lowercase array keyword used. |
---|---|
|
|
Generic.WhiteSpace.DisallowTabIndent
| [ref]
Spaces should be used for indentation instead of tabs.
Generic.WhiteSpace.ScopeIndent
| [ref]
Indentation for control structures, classes, and functions should be 4 spaces per level.
Valid: 4 spaces are used to indent a control structure. | Invalid: 8 spaces are used to indent a control structure. |
---|---|
|
|
PEAR.Functions.ValidDefaultValue
| [ref]
Arguments with default values go at the end of the argument list.
Valid: argument with default value at end of declaration | Invalid: argument with default value at start of declaration |
---|---|
|
|
PSR1.Classes.ClassDeclaration
| [ref]
Each class must be in a file by itself and must be under a namespace (a top-level vendor name).
Valid: One class in a file. | Invalid: Multiple classes in a single file. |
---|---|
|
|
Valid: A vendor-level namespace is used. | Invalid: No namespace used in file. |
---|---|
|
|
PSR1.Files.SideEffects
| [ref]
A php file should either contain declarations with no side effects, or should just have logic (including side effects) with no declarations.
Valid: A class defined in a file by itself. | Invalid: A class defined in a file with other code. |
---|---|
|
|
PSR1.Methods.CamelCapsMethodName
| [ref]
Method names MUST be declared in camelCase.
Valid: method name in camelCase. | Invalid: method name not in camelCase. |
---|---|
|
|
PSR2.Classes.ClassDeclaration
| [ref]
There should be exactly 1 space between the abstract or final keyword and the class keyword and between the class keyword and the class name. The extends and implements keywords, if present, must be on the same line as the class name. When interfaces implemented are spread over multiple lines, there should be exactly 1 interface mentioned per line indented by 1 level. The closing brace of the class must go on the first line after the body of the class and must be on a line by itself.
Valid: Correct spacing around class keyword. | Invalid: 2 spaces used around class keyword. |
---|---|
|
|
PSR2.Classes.PropertyDeclaration
| [ref]
Property names should not be prefixed with an underscore to indicate visibility. Visibility should be used to declare properties rather than the var keyword. Only one property should be declared within a statement. The static declaration must come after the visibility declaration.
Valid: Correct property naming. | Invalid: An underscore prefix used to indicate visibility. |
---|---|
|
|
Valid: Visibility of property declared. | Invalid: Var keyword used to declare property. |
---|---|
|
|
Valid: One property declared per statement. | Invalid: Multiple properties declared in one statement. |
---|---|
|
|
Valid: If declared as static, the static declaration must come after the visibility declaration. | Invalid: Static declaration before the visibility declaration. |
---|---|
|
|
PSR2.ControlStructures.ControlStructureSpacing
| [ref]
Control Structures should have 0 spaces after opening parentheses and 0 spaces before closing parentheses.
Valid: Correct spacing. | Invalid: Whitespace used inside the parentheses. |
---|---|
|
|
PSR2.ControlStructures.ElseIfDeclaration
| [ref]
PHP's elseif keyword should be used instead of else if.
Valid: Single word elseif keyword used. | Invalid: Separate else and if keywords used. |
---|---|
|
|
PSR2.ControlStructures.SwitchDeclaration
| [ref]
Case statements should be indented 4 spaces from the switch keyword. It should also be followed by a space. Colons in switch declarations should not be preceded by whitespace. Break statements should be indented 4 more spaces from the case statement. There must be a comment when falling through from one case into the next.
Valid: Case statement indented correctly. | Invalid: Case statement not indented 4 spaces. |
---|---|
|
|
Valid: Case statement followed by 1 space. | Invalid: Case statement not followed by 1 space. |
---|---|
|
|
Valid: Colons not prefixed by whitespace. | Invalid: Colons prefixed by whitespace. |
---|---|
|
|
Valid: Break statement indented correctly. | Invalid: Break statement not indented 4 spaces. |
---|---|
|
|
Valid: Comment marking intentional fall-through. | Invalid: No comment marking intentional fall-through. |
---|---|
|
|
PSR2.Files.ClosingTag
| [ref]
Checks that the file does not end with a closing tag.
Valid: Closing tag not used. | Invalid: Closing tag used. |
---|---|
|
|
PSR2.Files.EndFileNewline
| [ref]
PHP Files should end with exactly one newline.
PSR2.Methods.FunctionCallSignature
| [ref]
Checks that the function call format is correct.
Valid: Correct spacing is used around parentheses. | Invalid: Incorrect spacing used, too much space around the parentheses. |
---|---|
|
|
Valid: Correct number of spaces used for indent in a multi-line function call. | Invalid: Incorrect number of spaces used for indent in a multi-line function call. |
---|---|
|
|
Valid: Closing parenthesis for a multi-line function call is on a new line after the last parameter. | Invalid: Closing parenthesis for a multi-line function call is not on a new line after the last parameter. |
---|---|
|
|
Valid: The first argument of a multi-line function call is on a new line. | Invalid: The first argument of a multi-line function call is not on a new line. |
---|---|
|
|
Valid: Only one argument per line in a multi-line function call. | Invalid: Two or more arguments per line in a multi-line function call. |
---|---|
|
|
Valid: No blank lines in a multi-line function call. | Invalid: Blank line in multi-line function call. |
---|---|
|
|
PSR2.Methods.FunctionClosingBrace
| [ref]
Checks that the closing brace of a function goes directly after the body.
Valid: Closing brace directly follows the function body. | Invalid: Blank line between the function body and the closing brace. |
---|---|
|
|
PSR2.Methods.MethodDeclaration
| [ref]
Method names should not be prefixed with an underscore to indicate visibility. The static keyword, when present, should come after the visibility declaration, and the final and abstract keywords should come before.
Valid: Correct method naming. | Invalid: An underscore prefix used to indicate visibility. |
---|---|
|
|
Valid: Correct ordering of method prefixes. | Invalid: static keyword used before visibility and final used after. |
---|---|
|
|
PSR2.Namespaces.NamespaceDeclaration
| [ref]
There must be one blank line after the namespace declaration.
Valid: One blank line after the namespace declaration. | Invalid: No blank line after the namespace declaration. |
---|---|
|
|
PSR2.Namespaces.UseDeclaration
| [ref]
Each use declaration must contain only one namespace and must come after the first namespace declaration. There should be one blank line after the final use statement.
Valid: One use declaration per namespace. | Invalid: Multiple namespaces in a use declaration. |
---|---|
|
|
Valid: Use statements come after first namespace. | Invalid: Namespace declared after use. |
---|---|
|
|
Valid: A single blank line after the final use statement. | Invalid: No blank line after the final use statement. |
---|---|
|
|
Squiz.Classes.ValidClassName
| [ref]
Squiz.ControlStructures.ControlSignature
| [ref]
Squiz.ControlStructures.ForEachLoopDeclaration
| [ref]
There should be a space between each element of a foreach loop and the as keyword should be lowercase.
Valid: Correct spacing used. | Invalid: Invalid spacing used. |
---|---|
|
|
Valid: Lowercase as keyword. | Invalid: Uppercase as keyword. |
---|---|
|
|
Squiz.ControlStructures.ForLoopDeclaration
| [ref]
In a for loop declaration, there should be no space inside the brackets and there should be 0 spaces before and 1 space after semicolons.
Valid: Correct spacing used. | Invalid: Invalid spacing used inside brackets. |
---|---|
|
|
Valid: Correct spacing used. | Invalid: Invalid spacing used before semicolons. |
---|---|
|
|
Valid: Correct spacing used. | Invalid: Invalid spacing used after semicolons. |
---|---|
|
|
Squiz.ControlStructures.LowercaseDeclaration
| [ref]
The php keywords if, else, elseif, foreach, for, do, switch, while, try, and catch should be lowercase.
Valid: Lowercase if keyword. | Invalid: Uppercase if keyword. |
---|---|
|
|
Squiz.Functions.FunctionDeclaration
| [ref]
Squiz.Functions.FunctionDeclarationArgumentSpacing
| [ref]
Squiz.Functions.LowercaseFunctionKeywords
| [ref]
The php keywords function, public, private, protected, and static should be lowercase.
Valid: Lowercase function keyword. | Invalid: Uppercase function keyword. |
---|---|
|
|
Squiz.Functions.MultiLineFunctionDeclaration
| [ref]
Squiz.Scope.MethodScope
| [ref]
Squiz.WhiteSpace.ControlStructureSpacing
| [ref]
Squiz.WhiteSpace.ScopeClosingBrace
| [ref]
Squiz.WhiteSpace.ScopeKeywordSpacing
| [ref]
The php keywords static, public, private, and protected should have one space after them.
Valid: A single space following the keywords. | Invalid: Multiple spaces following the keywords. |
---|---|
|
|
Squiz.WhiteSpace.SuperfluousWhitespace
| [ref]
Looking for a developer who
truly cares about your business?
My team and I provide expert consultations, top-notch coding, and comprehensive audits to elevate your success.
Feedback
How satisfied you are after reading this article?