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..
if ($test) {
    $var = 1;
}
if ($test)
    $var = 1;

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.
$foo = 1;
$bar = 2;
$foo = 1; $bar = 2;

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.
function foo($bar, $baz)
{
}
function foo($bar,$baz)
{
}
Valid: Single spaces around an equals sign in function declaration. Invalid: No spaces around an equals sign in function declaration.
function foo($bar, $baz = true)
{
}
function foo($bar, $baz=true)
{
}

Generic.NamingConventions.UpperCaseConstantName | [ref]

Constants should always be all-uppercase, with underscores to separate words.

Valid: all uppercase Invalid: mixed case
define('FOO_CONSTANT', 'foo');

class FooClass
{
    const FOO_CONSTANT = 'foo';
}
define('Foo_Constant', 'foo');

class FooClass
{
    const foo_constant = 'foo';
}

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
if ($var === false || $var === null) {
    $var = true;
}
if ($var === FALSE || $var === NULL) {
    $var = TRUE;
}

Generic.PHP.LowerCaseKeyword | [ref]

All PHP keywords should be lowercase.

Valid: Lowercase array keyword used. Invalid: Non-lowercase array keyword used.
$foo = array();
$foo = Array();

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.
if ($test) {
    $var = 1;
}
if ($test) {
        $var = 1;
}

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
function connect($dsn, $persistent = false)
{
    ...
}
function connect($persistent = false, $dsn)
{
    ...
}

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.
<?php
namespace Foo;

class Bar {
}
<?php
namespace Foo;

class Bar {
}

class Baz {
}
Valid: A vendor-level namespace is used. Invalid: No namespace used in file.
<?php
namespace Foo;

class Bar {
}
<?php
class Bar {
}

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.
<?php
class Foo
{
}
<?php
class Foo
{
}

echo "Class Foo loaded."

PSR1.Methods.CamelCapsMethodName | [ref]

Method names MUST be declared in camelCase.

Valid: method name in camelCase. Invalid: method name not in camelCase.
class Foo
{
    private function doBar()
    {
    }
}
class Foo
{
    private function do_bar()
    {
    }
}

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.
abstract class Foo
{
}
abstract  class  Foo
{
}

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.
class Foo
{
    private $bar;
}
class Foo
{
    private $_bar;
}
Valid: Visibility of property declared. Invalid: Var keyword used to declare property.
class Foo
{
    private $bar;
}
class Foo
{
    var $bar;
}
Valid: One property declared per statement. Invalid: Multiple properties declared in one statement.
class Foo
{
    private $bar;
    private $baz;
}
class Foo
{
    private $bar, $baz;
}
Valid: If declared as static, the static declaration must come after the visibility declaration. Invalid: Static declaration before the visibility declaration.
class Foo
{
    public static $bar;
    private $baz;
}
class Foo
{
    static protected $bar;
}

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.
if ($foo) {
    $var = 1;
}
if ( $foo ) {
    $var = 1;
}

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.
if ($foo) {
    $var = 1;
} elseif ($bar) {
    $var = 2;
}
if ($foo) {
    $var = 1;
} else if ($bar) {
    $var = 2;
}

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.
switch ($foo) {
    case 'bar':
        break;
}
switch ($foo) {
case 'bar':
    break;
}
Valid: Case statement followed by 1 space. Invalid: Case statement not followed by 1 space.
switch ($foo) {
    case 'bar':
        break;
}
switch ($foo) {
    case'bar':
        break;
}
Valid: Colons not prefixed by whitespace. Invalid: Colons prefixed by whitespace.
switch ($foo) {
    case 'bar':
        break;
    default:
        break;
}
switch ($foo) {
    case 'bar' :
        break;
    default :
        break;
}
Valid: Break statement indented correctly. Invalid: Break statement not indented 4 spaces.
switch ($foo) {
    case 'bar':
        break;
}
switch ($foo) {
    case 'bar':
    break;
}
Valid: Comment marking intentional fall-through. Invalid: No comment marking intentional fall-through.
switch ($foo) {
    case 'bar':
    // no break
    default:
        break;
}
switch ($foo) {
    case 'bar':
    default:
        break;
}

PSR2.Files.ClosingTag | [ref]

Checks that the file does not end with a closing tag.

Valid: Closing tag not used. Invalid: Closing tag used.
<?php
echo 'Foo';
<?php
echo 'Foo';
?>

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.
foo($bar, $baz);
foo ( $bar, $baz );
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.
foo(
    $bar,
    $baz
);
foo(
  $bar,
      $baz
);
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.
foo(
    $bar,
    $baz
);
foo(
    $bar,
    $baz);
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.
foo(
    $bar,
    $baz
);
foo($bar,
    $baz
);
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.
foo(
    $bar,
    $baz
);
foo(
    $bar, $baz
);
Valid: No blank lines in a multi-line function call. Invalid: Blank line in multi-line function call.
foo(
    $bar,
    $baz
);
foo(
    $bar,

    $baz
);

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.
function foo()
{
    echo 'foo';
}
function foo()
{
    echo 'foo';

}

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.
class Foo
{
    private function bar()
    {
    }
}
class Foo
{
    private function _bar()
    {
    }
}
Valid: Correct ordering of method prefixes. Invalid: static keyword used before visibility and final used after.
class Foo
{
    final public static function bar()
    {
    }
}
class Foo
{
    static public final function bar()
    {
    }
}

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.
namespace \Foo\Bar;

use \Baz;
namespace \Foo\Bar;
use \Baz;

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.
use \Foo;
use \Bar;
use \Foo, \Bar;
Valid: Use statements come after first namespace. Invalid: Namespace declared after use.
namespace \Foo;

use \Bar;
use \Bar;

namespace \Foo;
Valid: A single blank line after the final use statement. Invalid: No blank line after the final use statement.
use \Foo;
use \Bar;

class Baz
{
}
use \Foo;
use \Bar;
class Baz
{
}

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.
foreach ($foo as $bar => $baz) {
    echo $baz;
}
foreach ( $foo  as  $bar=>$baz ) {
    echo $baz;
}
Valid: Lowercase as keyword. Invalid: Uppercase as keyword.
foreach ($foo as $bar => $baz) {
    echo $baz;
}
foreach ($foo AS $bar => $baz) {
    echo $baz;
}

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.
for ($i = 0; $i < 10; $i++) {
    echo $i;
}
for ( $i = 0; $i < 10; $i++ ) {
    echo $i;
}
Valid: Correct spacing used. Invalid: Invalid spacing used before semicolons.
for ($i = 0; $i < 10; $i++) {
    echo $i;
}
for ($i = 0 ; $i < 10 ; $i++) {
    echo $i;
}
Valid: Correct spacing used. Invalid: Invalid spacing used after semicolons.
for ($i = 0; $i < 10; $i++) {
    echo $i;
}
for ($i = 0;$i < 10;$i++) {
    echo $i;
}

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.
if ($foo) {
    $bar = true;
}
IF ($foo) {
    $bar = true;
}

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.
function foo()
{
    return true;
}
FUNCTION foo()
{
    return true;
}

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.
public static function foo()
{
}
public  static  function foo()
{
}

Squiz.WhiteSpace.SuperfluousWhitespace | [ref]

avatar

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?