PHPCS: PSR12
Personal documentation for the sniffs available in the PHPCS PSR12 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.PHP.LowerCaseType
| [ref]
All PHP types used for parameter type and return type declarations should be lowercase.
Valid: Lowercase type declarations used. | Invalid: Non-lowercase type declarations used. |
---|---|
|
|
Valid: Lowercase type used. | Invalid: Non-lowercase type used. |
---|---|
|
|
Generic.WhiteSpace.DisallowTabIndent
| [ref]
Spaces should be used for indentation instead of tabs.
Generic.WhiteSpace.IncrementDecrementSpacing
| [ref]
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.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. |
---|---|
|
|
PSR12.Classes.AnonClassDeclaration
| [ref]
PSR12.Classes.ClassInstantiation
| [ref]
When instantiating a new class, parenthesis MUST always be present even when there are no arguments passed to the constructor.
Valid: Parenthesis used. | Invalid: Parenthesis not used. |
---|---|
|
|
PSR12.Classes.ClosingBrace
| [ref]
The closing brace of object-oriented constructs and functions must not be followed by any comment or statement on the same line.
Valid: Closing brace is the last content on the line. | Invalid: Comment or statement following the closing brace on the same line. |
---|---|
|
|
PSR12.Classes.OpeningBraceSpace
| [ref]
The opening brace of an object-oriented construct must not be followed by a blank line.
Valid: No blank lines after opening brace. | Invalid: Blank line after opening brace. |
---|---|
|
|
PSR12.ControlStructures.BooleanOperatorPlacement
| [ref]
Boolean operators between conditions in control structures must always be at the beginning or at the end of the line, not a mix of both.
This rule applies to if/else conditions, while loops and switch/match statements.
Valid: Boolean operator between conditions consistently at the beginning of the line. | Invalid: Mix of boolean operators at the beginning and the end of the line. |
---|---|
|
|
Valid: Boolean operator between conditions consistently at the end of the line. | Invalid: Mix of boolean operators at the beginning and the end of the line. |
---|---|
|
|
PSR12.ControlStructures.ControlStructureSpacing
| [ref]
PSR12.Files.DeclareStatement
| [ref]
PSR12.Files.FileHeader
| [ref]
PSR12.Files.ImportStatement
| [ref]
PSR12.Files.OpenTag
| [ref]
When the opening <?php tag is on the first line of the file, it must be on its own line with no other statements unless it is a file containing markup outside of PHP opening and closing tags.
Valid: Opening PHP tag on a line by itself. | Invalid: Opening PHP tag not on a line by itself. |
---|---|
|
|
Valid: Opening PHP tag not on a line by itself, but has markup outside the closing PHP tag. | Invalid: Opening PHP tag not on a line by itself without any markup in the file. |
---|---|
|
|
PSR12.Functions.NullableTypeDeclaration
| [ref]
In nullable type declarations there MUST NOT be a space between the question mark and the type.
Valid: no whitespace used. | Invalid: superfluous whitespace used. |
---|---|
|
|
Valid: no unexpected characters. | Invalid: unexpected characters used. |
---|---|
|
|
PSR12.Functions.ReturnTypeDeclaration
| [ref]
PSR12.Keywords.ShortFormTypeKeywords
| [ref]
Short form of type keywords MUST be used i.e. bool instead of boolean, int instead of integer etc.
Valid: Short form type used. | Invalid: Long form type type used. |
---|---|
|
|
PSR12.Namespaces.CompoundNamespaceDepth
| [ref]
Compound namespaces with a depth of more than two MUST NOT be used.
Valid: Max depth of 2. | Invalid: Max depth of 3. |
---|---|
|
|
PSR12.Operators.OperatorSpacing
| [ref]
All binary and ternary (but not unary) operators MUST be preceded and followed by at least one space. This includes all arithmetic, comparison, assignment, bitwise, logical (excluding ! which is unary), string concatenation, type operators, trait operators (insteadof and as), and the single pipe operator (e.g. ExceptionType1 | ExceptionType2 $e).
Valid: At least 1 space used. | Invalid: No spacing used. |
---|---|
|
|
PSR12.Properties.ConstantVisibility
| [ref]
PSR12.Traits.UseDeclaration
| [ref]
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.CastSpacing
| [ref]
Casts should not have whitespace inside the parentheses.
Valid: No spaces. | Invalid: Whitespace used inside parentheses. |
---|---|
|
|
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?