PHP_Code_Sniffer Cheatset

Personal documentation for the sniffs available in the PHP Code Sniffer coding standards to help build an own rulesets and search for error explanations.

Script

The simple bash script created for personal needs to list all the sniffs available in the coding standards provided by PHP_Code_Sniffer. It iterates through the standards, list sniffs with -e attribute, exctract sniff codes, and generates docs for each of them.

Standards: | Generic | Pear | PSR1 | PSR12 | PSR2 | Squiz | Zend |

#!/bin/bash

STANDARDS=('Generic' 'Pear' 'PSR1' 'PSR12' 'PSR2' 'Squiz' 'Zend')
output=""

for standard in "${STANDARDS[@]}"; do
  output+="# Standard: $standard"
  output+=$'\n\n'

  list=$(vendor/bin/phpcs --standard=$standard -e)
  codes=$(echo "$list" | grep -E '^[[:space:]]{2,}[A-Za-z0-9.]+' | sed 's/^[[:space:]]*//')

  IFS=$'\n'
  for code in $codes; do
    stnd=$(echo "$code" | cut -d'.' -f1)
    category=$(echo "$code" | cut -d'.' -f2)
    sniff=$(echo "$code" | cut -d'.' -f3)
    url="https://github.com/PHPCSStandards/PHP_CodeSniffer/blob/master/src/Standards/${stnd}/Sniffs/${category}/${sniff}Sniff.php"

    output+=$'\n\n'
    output+="\`$code\` | [[ref]($url)]"
    output+=$(vendor/bin/phpcs --standard=$standard --generator=markdown --sniffs="$code")
    output+=$'\n\n---\n\n'
  done
done

output=$(echo "$output" | sed -E "s/Documentation generated on [[:alpha:]]+, [0-9]+ [[:alpha:]]+ [0-9]+ [0-9]+:[0-9]+:[0-9]+ \+[0-9]+ by \[PHP_CodeSniffer [0-9]+\.[0-9]+\.[0-9]+\](.*)//")
output=$(echo "$output" | sed -E "s/# (Generic|Pear|PSR1|PSR12|PSR2|Squiz|Zend) Coding Standard//")
output=$(echo "$output" | sed '/^$/N;/^\n$/D')
output=$(echo "$output" | sed '/^##/d')

echo "$output"
./docs.sh > docs.md

https://github.com/PHPCSStandards/PHP_CodeSniffer/issues/317


Standard: Generic

Generic.Arrays.ArrayIndent | [ref]

Generic.Arrays.DisallowLongArraySyntax | [ref]

Short array syntax must be used to define arrays.

Valid: Short form of array. Invalid: Long form of array.
$arr = [
    'foo' => 'bar',
];
$arr = array(
    'foo' => 'bar',
);

Generic.Arrays.DisallowShortArraySyntax | [ref]

Long array syntax must be used to define arrays.

Valid: Long form of array. Invalid: Short form of array.
$arr = array(
    'foo' => 'bar',
);
$arr = [
    'foo' => 'bar',
];

Generic.Classes.DuplicateClassName | [ref]

Class and Interface names should be unique in a project. They should never be duplicated.

Valid: A unique class name. Invalid: A class duplicated (including across multiple files).
class Foo
{
}
class Foo
{
}

class Foo
{
}

Generic.Classes.OpeningBraceSameLine | [ref]

The opening brace of a class must be on the same line after the definition and must be the last thing on that line.

Valid: Opening brace on the same line. Invalid: Opening brace on the next line.
class Foo {
}
class Foo
{
}
Valid: Opening brace is the last thing on the line. Invalid: Opening brace not last thing on the line.
class Foo {
}
class Foo { // Start of class.
}

Generic.CodeAnalysis.AssignmentInCondition | [ref]

Variable assignments should not be made within conditions.

Valid: A variable comparison being executed within a condition. Invalid: A variable assignment being made within a condition.
if ($test === 'abc') {
    // Code.
}
if ($test = 'abc') {
    // Code.
}

Generic.CodeAnalysis.EmptyPHPStatement | [ref]

Empty PHP tags are not allowed.

Valid: There is at least one statement inside the PHP tag pair. Invalid: There is no statement inside the PHP tag pair.
<?php echo 'Hello World'; ?>
<?= 'Hello World'; ?>
<?php ; ?>
<?=  ?>
Superfluous semi-colons are not allowed.
Valid: There is no superfluous semi-colon after a PHP statement. Invalid: There are one or more superfluous semi-colons after a PHP statement.
function_call();
if (true) {
    echo 'Hello World';
}
function_call();;;
if (true) {
    echo 'Hello World';
};

Generic.CodeAnalysis.EmptyStatement | [ref]

Control Structures must have at least one statement inside of the body.

Valid: There is a statement inside the control structure. Invalid: The control structure has no statements.
if ($test) {
    $var = 1;
}
if ($test) {
    // do nothing
}

Generic.CodeAnalysis.ForLoopShouldBeWhileLoop | [ref]

For loops that have only a second expression (the condition) should be converted to while loops.

Valid: A for loop is used with all three expressions. Invalid: A for loop is used without a first or third expression.
for ($i = 0; $i < 10; $i++) {
    echo "{$i}\n";
}
for (;$test;) {
    $test = doSomething();
}

Generic.CodeAnalysis.ForLoopWithTestFunctionCall | [ref]

For loops should not call functions inside the test for the loop when they can be computed beforehand.

Valid: A for loop that determines its end condition before the loop starts. Invalid: A for loop that unnecessarily computes the same value on every iteration.
$end = count($foo);
for ($i = 0; $i < $end; $i++) {
    echo $foo[$i]."\n";
}
for ($i = 0; $i < count($foo); $i++) {
    echo $foo[$i]."\n";
}

Generic.CodeAnalysis.JumbledIncrementer | [ref]

Incrementers in nested loops should use different variable names.

Valid: Two different variables being used to increment. Invalid: Inner incrementer is the same variable name as the outer one.
for ($i = 0; $i < 10; $i++) {
    for ($j = 0; $j < 10; $j++) {
    }
}
for ($i = 0; $i < 10; $i++) {
    for ($j = 0; $j < 10; $i++) {
    }
}

Generic.CodeAnalysis.UnconditionalIfStatement | [ref]

If statements that are always evaluated should not be used.

Valid: An if statement that only executes conditionally. Invalid: An if statement that is always performed.
if ($test) {
    $var = 1;
}
if (true) {
    $var = 1;
}
Valid: An if statement that only executes conditionally. Invalid: An if statement that is never performed.
if ($test) {
    $var = 1;
}
if (false) {
    $var = 1;
}

Generic.CodeAnalysis.UnnecessaryFinalModifier | [ref]

Methods should not be declared final inside of classes that are declared final.

Valid: A method in a final class is not marked final. Invalid: A method in a final class is also marked final.
final class Foo
{
    public function bar()
    {
    }
}
final class Foo
{
    public final function bar()
    {
    }
}

Generic.CodeAnalysis.UnusedFunctionParameter | [ref]

All parameters in a functions signature should be used within the function.

Valid: All the parameters are used. Invalid: One of the parameters is not being used.
function addThree($a, $b, $c)
{
    return $a + $b + $c;
}
function addThree($a, $b, $c)
{
    return $a + $b;
}

Generic.CodeAnalysis.UselessOverridingMethod | [ref]

Methods should not be defined that only call the parent method.

Valid: A method that extends functionality on a parent method. Invalid: An overriding method that only calls the parent.
final class Foo
{
    public function bar()
    {
        parent::bar();
        $this->doSomethingElse();
    }
}
final class Foo
{
    public function bar()
    {
        parent::bar();
    }
}

Generic.Commenting.DocComment | [ref]

Generic.Commenting.Fixme | [ref]

FIXME Statements should be taken care of.

Valid: A comment without a fixme. Invalid: A fixme comment.
// Handle strange case
if ($test) {
    $var = 1;
}
// FIXME: This needs to be fixed!
if ($test) {
    $var = 1;
}

Generic.Commenting.Todo | [ref]

TODO Statements should be taken care of.

Valid: A comment without a todo. Invalid: A todo comment.
// Handle strange case
if ($test) {
    $var = 1;
}
// TODO: This needs to be fixed!
if ($test) {
    $var = 1;
}

Generic.ControlStructures.DisallowYodaConditions | [ref]

Yoda conditions are disallowed.

Valid: value to be asserted must go on the right side of the comparison. Invalid: value to be asserted must not be on the left.
if ($test === null) {
    $var = 1;
}
if (null === $test) {
    $var = 1;
}

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.Debug.ClosureLinter | [ref]

All javascript files should pass basic Closure Linter tests.

Valid: Valid JS Syntax is used. Invalid: Trailing comma in a javascript array.
var foo = [1, 2];
var foo = [1, 2,];

Generic.Debug.CSSLint | [ref]

All css files should pass the basic csslint tests.

Valid: Valid CSS Syntax is used. Invalid: The CSS has a typo in it.
.foo: { width: 100%; }
.foo: { width: 100 %; }

Generic.Debug.ESLint | [ref]

Generic.Debug.JSHint | [ref]

All javascript files should pass basic JSHint tests.

Valid: Valid JS Syntax is used. Invalid: The Javascript is using an undefined variable.
var foo = 5;
foo = 5;

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.EndFileNewline | [ref]

Files should end with a newline character.

Generic.Files.EndFileNoNewline | [ref]

Files should not end with a newline character.

Generic.Files.ExecutableFile | [ref]

Files should not be executable.

Generic.Files.InlineHTML | [ref]

Files that contain php code should only have php code and should not have any "inline html".

Valid: A php file with only php code in it. Invalid: A php file with html in it outside of the php tags.
<?php
$foo = 'bar';
echo $foo . 'baz';
some string here
<?php
$foo = 'bar';
echo $foo . 'baz';

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.Files.LowercasedFilename | [ref]

Lowercase filenames are required.

Generic.Files.OneClassPerFile | [ref]

There should only be one class defined in a file.

Valid: Only one class in the file. Invalid: Multiple classes defined in one file.
<?php
class Foo
{
}
<?php
class Foo
{
}

class Bar
{
}

Generic.Files.OneInterfacePerFile | [ref]

There should only be one interface defined in a file.

Valid: Only one interface in the file. Invalid: Multiple interfaces defined in one file.
<?php
interface Foo
{
}
<?php
interface Foo
{
}

interface Bar
{
}

Generic.Files.OneObjectStructurePerFile | [ref]

There should only be one class or interface or trait defined in a file.

Valid: Only one object structure in the file. Invalid: Multiple object structures defined in one file.
<?php
trait Foo
{
}
<?php
trait Foo
{
}

class Bar
{
}

Generic.Files.OneTraitPerFile | [ref]

There should only be one trait defined in a file.

Valid: Only one trait in the file. Invalid: Multiple traits defined in one file.
<?php
trait Foo
{
}
<?php
trait Foo
{
}

trait Bar
{
}

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.Formatting.MultipleStatementAlignment | [ref]

There should be one space on either side of an equals sign used to assign a value to a variable. In the case of a block of related assignments, more space may be inserted to promote readability.

Equals signs aligned Not aligned; harder to read
$shortVar        = (1 + 2);
$veryLongVarName = 'string';
$var             = foo($bar, $baz);
$shortVar = (1 + 2);
$veryLongVarName = 'string';
$var = foo($bar, $baz);
When using plus-equals, minus-equals etc. still ensure the equals signs are aligned to one space after the longest variable name.
Equals signs aligned; only one space after longest var name Two spaces after longest var name
$shortVar       += 1;
$veryLongVarName = 1;
$shortVar        += 1;
$veryLongVarName  = 1;
Equals signs aligned Equals signs not aligned
$shortVar         = 1;
$veryLongVarName -= 1;
$shortVar        = 1;
$veryLongVarName -= 1;

Generic.Formatting.NoSpaceAfterCast | [ref]

Spaces are not allowed after casting operators.

Valid: A cast operator is immediately before its value. Invalid: A cast operator is followed by whitespace.
$foo = (string)1;
$foo = (string) 1;

Generic.Formatting.SpaceAfterCast | [ref]

Exactly one space is allowed after a cast.

Valid: A cast operator is followed by one space. Invalid: A cast operator is not followed by whitespace.
$foo = (string) 1;
$foo = (string)1;

Generic.Formatting.SpaceAfterNot | [ref]

Exactly one space is allowed after the NOT operator.

Valid: A NOT operator followed by one space. Invalid: A NOT operator not followed by whitespace or followed by too much whitespace.
if (! $someVar || ! $x instanceOf stdClass) {};
if (!$someVar || !$x instanceOf stdClass) {};

if (!     $someVar || !
    $x instanceOf stdClass) {};

Generic.Formatting.SpaceBeforeCast | [ref]

There should be exactly one space before a cast operator.

Valid: Single space before a cast operator. Invalid: No space or multiple spaces before a cast operator.
$integer = (int) $string;
$c = $a . (string) $b;
$integer =(int) $string;
$c = $a .   (string) $b;

Generic.Functions.CallTimePassByReference | [ref]

Call-time pass-by-reference is not allowed. It should be declared in the function definition.

Valid: Pass-by-reference is specified in the function definition. Invalid: Pass-by-reference is done in the call to a function.
function foo(&$bar)
{
    $bar++;
}

$baz = 1;
foo($baz);
function foo($bar)
{
    $bar++;
}

$baz = 1;
foo(&$baz);

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.Functions.OpeningFunctionBraceBsdAllman | [ref]

Function declarations follow the "BSD/Allman style". The function brace is on the line following the function declaration and is indented to the same column as the start of the function declaration.

Valid: brace on next line Invalid: brace on same line
function fooFunction($arg1, $arg2 = '')
{
    ...
}
function fooFunction($arg1, $arg2 = '') {
    ...
}

Generic.Functions.OpeningFunctionBraceKernighanRitchie | [ref]

Function declarations follow the "Kernighan/Ritchie style". The function brace is on the same line as the function declaration. One space is required between the closing parenthesis and the brace.

Valid: brace on same line Invalid: brace on next line
function fooFunction($arg1, $arg2 = '') {
    ...
}
function fooFunction($arg1, $arg2 = '')
{
    ...
}

Generic.Metrics.CyclomaticComplexity | [ref]

Functions should not have a cyclomatic complexity greater than 20, and should try to stay below a complexity of 10.

Generic.Metrics.NestingLevel | [ref]

Functions should not have a nesting level greater than 10, and should try to stay below 5.

Generic.NamingConventions.AbstractClassNamePrefix | [ref]

Abstract class names must be prefixed with "Abstract", e.g. AbstractBar.

Valid: Invalid:
abstract class AbstractBar
{
}
abstract class Bar
{
}

Generic.NamingConventions.CamelCapsFunctionName | [ref]

Functions should use camelCaps format for their names. Only PHP's magic methods should use a double underscore prefix.

Valid: A function in camelCaps format. Invalid: A function in snake_case format.
function doSomething()
{
}
function do_something()
{
}

Generic.NamingConventions.ConstructorName | [ref]

Constructors should be named __construct, not after the class.

Valid: The constructor is named __construct. Invalid: The old style class name constructor is used.
class Foo
{
    function __construct()
    {
    }
}
class Foo
{
    function Foo()
    {
    }
}

Generic.NamingConventions.InterfaceNameSuffix | [ref]

Interface names must be suffixed with "Interface", e.g. BarInterface.

Valid: Invalid:
interface BarInterface
{
}
interface Bar
{
}

Generic.NamingConventions.TraitNameSuffix | [ref]

Trait names must be suffixed with "Trait", e.g. BarTrait.

Valid: Invalid:
trait BarTrait
{
}
trait Bar
{
}

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.BacktickOperator | [ref]

Disallow the use of the backtick operator for execution of shell commands.

Generic.PHP.CharacterBeforePHPOpeningTag | [ref]

The opening php tag should be the first item in the file.

Valid: A file starting with an opening php tag. Invalid: A file with content before the opening php tag.
<?php
echo 'Foo';
Beginning content
<?php
echo 'Foo';

Generic.PHP.ClosingPHPTag | [ref]

All opening php tags should have a corresponding closing tag.

Valid: A closing tag paired with it's opening tag. Invalid: No closing tag paired with the opening tag.
<?php
echo 'Foo';
?>
<?php
echo 'Foo';

Generic.PHP.DeprecatedFunctions | [ref]

Deprecated functions should not be used.

Valid: A non-deprecated function is used. Invalid: A deprecated function is used.
$foo = explode('a', $bar);
$foo = split('a', $bar);

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.DisallowRequestSuperglobal | [ref]

$_REQUEST should never be used due to the ambiguity created as to where the data is coming from. Use $_POST, $_GET, or $_COOKIE instead.

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.DiscourageGoto | [ref]

Discourage the use of the PHP goto language construct.

Generic.PHP.ForbiddenFunctions | [ref]

The forbidden functions sizeof() and delete() should not be used.

Valid: count() is used in place of sizeof(). Invalid: sizeof() is used.
$foo = count($bar);
$foo = sizeof($bar);

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.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.
function myFunction(int $foo) : string {
}
function myFunction(Int $foo) : STRING {
}
All PHP types used for type casting should be lowercase.
Valid: Lowercase type used. Invalid: Non-lowercase type used.
$foo = (bool) $isValid;
$foo = (BOOL) $isValid;

Generic.PHP.NoSilencedErrors | [ref]

Suppressing Errors is not allowed.

Valid: isset() is used to verify that a variable exists before trying to use it. Invalid: Errors are suppressed.
if (isset($foo) && $foo) {
    echo "Hello\n";
}
if (@$foo) {
    echo "Hello\n";
}

Generic.PHP.RequireStrictTypes | [ref]

Generic.PHP.SAPIUsage | [ref]

The PHP_SAPI constant should be used instead of php_sapi_name().

Valid: PHP_SAPI is used. Invalid: php_sapi_name() is used.
if (PHP_SAPI === 'cli') {
    echo "Hello, CLI user.";
}
if (php_sapi_name() === 'cli') {
    echo "Hello, CLI user.";
}

Generic.PHP.Syntax | [ref]

The code should use valid PHP syntax.

Valid: No PHP syntax errors. Invalid: Code contains PHP syntax errors.
echo "Hello!";
$array = [1, 2, 3];
echo "Hello!" // Missing semicolon.
$array = [1, 2, 3; // Missing closing bracket.

Generic.PHP.UpperCaseConstant | [ref]

The true, false and null constants must always be uppercase.

Valid: uppercase constants Invalid: lowercase constants
if ($var === FALSE || $var === NULL) {
    $var = TRUE;
}
if ($var === false || $var === null) {
    $var = true;
}

Generic.Strings.UnnecessaryStringConcat | [ref]

Strings should not be concatenated together.

Valid: A string can be concatenated with an expression. Invalid: Strings should not be concatenated together.
echo '5 + 2 = ' . (5 + 2);
echo 'Hello' . ' ' . 'World';

Generic.VersionControl.GitMergeConflict | [ref]

Generic.VersionControl.SubversionProperties | [ref]

All php files in a subversion repository should have the svn:keywords property set to 'Author Id Revision' and the svn:eol-style property set to 'native'.

Generic.WhiteSpace.ArbitraryParenthesesSpacing | [ref]

Arbitrary sets of parentheses should have no spaces inside.

Valid: no spaces on the inside of a set of arbitrary parentheses. Invalid: spaces or new lines on the inside of a set of arbitrary parentheses.
$a = (null !== $extra);
$a = ( null !== $extra );

$a = (
    null !== $extra
);

Generic.WhiteSpace.DisallowSpaceIndent | [ref]

Tabs should be used for indentation instead of spaces.

Generic.WhiteSpace.DisallowTabIndent | [ref]

Spaces should be used for indentation instead of tabs.

Generic.WhiteSpace.IncrementDecrementSpacing | [ref]

Generic.WhiteSpace.LanguageConstructSpacing | [ref]

Language constructs that can be used without parentheses, must have a single space between the language construct keyword and its content.

Valid: Single space after language construct. Invalid: No space, more than one space or newline after language construct.
echo 'Hello, World!';
throw new Exception();
return $newLine;
echo'Hello, World!';
throw   new   Exception();
return
$newLine;
A single space must be used between the "yield" and "from" keywords for a "yield from" expression.
Valid: Single space between yield and from. Invalid: No space, more than one space or newline between yield and from.
yield from [1, 2, 3];
yieldfrom [1, 2, 3];
yield  from [1, 2, 3];
yield
from [1, 2, 3];

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;
}

Generic.WhiteSpace.SpreadOperatorSpacingAfter | [ref]

There should be no space between the spread operator and the variable/function call it applies to.

Valid: No space between the spread operator and the variable/function call it applies to. Invalid: space found between the spread operator and the variable/function call it applies to.
function foo(&...$spread) {
    bar(...$spread);

    bar(
        [...$foo],
        ...array_values($keyedArray)
    );
}
function bar(... $spread) {
    bar(...
        $spread
    );

    bar(
        [...  $foo ],.../*comment*/array_values($keyedArray)
    );
}

Standard: Pear

Generic.Commenting.DocComment | [ref]# PEAR Coding Standard

Generic.ControlStructures.InlineControlStructure | [ref]# PEAR Coding Standard

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.LineEndings | [ref]# PEAR Coding Standard

Unix-style line endings are preferred ("\n" instead of "\r\n").

Generic.Files.LineLength | [ref]# PEAR Coding Standard

It is recommended to keep lines at approximately 80 characters long for better code readability.

Generic.Functions.FunctionCallArgumentSpacing | [ref]# PEAR Coding Standard

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]# PEAR Coding Standard

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.DisallowShortOpenTag | [ref]# PEAR Coding Standard

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]# PEAR Coding Standard

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.WhiteSpace.DisallowTabIndent | [ref]# PEAR Coding Standard

Spaces should be used for indentation instead of tabs.

PEAR.Classes.ClassDeclaration | [ref]# PEAR Coding Standard

The opening brace of a class must be on the line after the definition by itself.

Valid: Opening brace on the correct line. Invalid: Opening brace on same line as declaration.
class Foo
{
}
class Foo {
}

PEAR.Commenting.ClassComment | [ref]# PEAR Coding Standard

Classes and interfaces must have a non-empty doc comment. The short description must be on the second line of the comment. Each description must have one blank comment line before and after. There must be one blank line before the tags in the comments. A @version tag must be in Release: package_version format.

Valid: A doc comment for the class. Invalid: No doc comment for the class.
/**
 * The Foo class.
 */
class Foo
{
}
class Foo
{
}
Valid: A doc comment for the class. Invalid: Invalid comment type for the class.
/**
 * The Foo class.
 */
class Foo
{
}
// The Foo class.
class Foo
{
}
Valid: A doc comment for the class. Invalid: The blank line after the comment makes it appear as a file comment, not a class comment.
/**
 * The Foo class.
 */
class Foo
{
}
/**
 * The Foo class.
 */

class Foo
{
}
Valid: Short description is the second line of the comment. Invalid: An extra blank line before the short description.
/**
 * The Foo class.
 */
class Foo
{
}
/**
 *
 * The Foo class.
 */
class Foo
{
}
Valid: Exactly one blank line around descriptions. Invalid: Extra blank lines around the descriptions.
/**
 * The Foo class.
 *
 * A helper for the Bar class.
 *
 * @see Bar
 */
class Foo
{
}
/**
 * The Foo class.
 *
 *
 * A helper for the Bar class.
 *
 *
 * @see Bar
 */
class Foo
{
}
Valid: Exactly one blank line before the tags. Invalid: Extra blank lines before the tags.
/**
 * The Foo class.
 *
 * @see Bar
 */
class Foo
{
}
/**
 * The Foo class.
 *
 *
 * @see Bar
 */
class Foo
{
}
Valid: Version tag is in the correct format. Invalid: No Release: text.
/**
 * The Foo class.
 *
 * @version Release: 1.0
 */
class Foo
{
}
/**
 * The Foo class.
 *
 * @version 1.0
 */
class Foo
{
}

PEAR.Commenting.FileComment | [ref]# PEAR Coding Standard

Files must have a non-empty doc comment. The short description must be on the second line of the comment. Each description must have one blank comment line before and after. There must be one blank line before the tags in the comments. There must be a category, package, author, license, and link tag. There may only be one category, package, subpackage, license, version, since and deprecated tag. The tags must be in the order category, package, subpackage, author, copyright, license, version, link, see, since, and deprecated. The php version must be specified.

Valid: A file comment is used. Invalid: No doc comment for the class.
<?php
/**
 * Short description here.
 *
 * PHP version 5
 *
 * @category Foo
 * @package Foo_Helpers
 * @author Marty McFly <mmcfly@example.com>
 * @copyright 2013-2014 Foo Inc.
 * @license MIT License
 * @link http://example.com
 */
<?php
Valid: Short description is the second line of the comment. Invalid: An extra blank line before the short description.
<?php
/**
 * Short description here.
 *
 * PHP version 5
 *
 * @category Foo
 * @package Foo_Helpers
 * @author Marty McFly <mmcfly@example.com>
 * @copyright 2013-2014 Foo Inc.
 * @license MIT License
 * @link http://example.com
 */
<?php
/**
 *
 * Short description here.
 *
 * PHP version 5
 *
 * @category Foo
 * @package Foo_Helpers
 * @author Marty McFly <mmcfly@example.com>
 * @copyright 2013-2014 Foo Inc.
 * @license MIT License
 * @link http://example.com
 */
Valid: Exactly one blank line around descriptions. Invalid: Extra blank lines around the descriptions.
<?php
/**
 * Short description here.
 *
 * PHP version 5
 *
 * @category Foo
 * @package Foo_Helpers
 * @author Marty McFly <mmcfly@example.com>
 * @copyright 2013-2014 Foo Inc.
 * @license MIT License
 * @link http://example.com
 */
<?php
/**
 * Short description here.
 *
 *
 * PHP version 5
 *
 *
 * @category Foo
 * @package Foo_Helpers
 * @author Marty McFly <mmcfly@example.com>
 * @copyright 2013-2014 Foo Inc.
 * @license MIT License
 * @link http://example.com
 */
Valid: Exactly one blank line before the tags. Invalid: Extra blank lines before the tags.
<?php
/**
 * Short description here.
 *
 * PHP version 5
 *
 * @category Foo
 * @package Foo_Helpers
 * @author Marty McFly <mmcfly@example.com>
 * @copyright 2013-2014 Foo Inc.
 * @license MIT License
 * @link http://example.com
 */
<?php
/**
 * Short description here.
 *
 * PHP version 5
 *
 *
 * @category Foo
 * @package Foo_Helpers
 * @author Marty McFly <mmcfly@example.com>
 * @copyright 2013-2014 Foo Inc.
 * @license MIT License
 * @link http://example.com
 */
Valid: All required tags are used. Invalid: Missing an author tag.
<?php
/**
 * Short description here.
 *
 * PHP version 5
 *
 * @category Foo
 * @package Foo_Helpers
 * @author Marty McFly <mmcfly@example.com>
 * @copyright 2013-2014 Foo Inc.
 * @license MIT License
 * @link http://example.com
 */
<?php
/**
 * Short description here.
 *
 * PHP version 5
 *
 * @category Foo
 * @package Foo_Helpers
 * @copyright 2013-2014 Foo Inc.
 * @license MIT License
 * @link http://example.com
 */
Valid: Tags that should only be used once are only used once. Invalid: Multiple category tags.
<?php
/**
 * Short description here.
 *
 * PHP version 5
 *
 * @category Foo
 * @package Foo_Helpers
 * @author Marty McFly <mmcfly@example.com>
 * @copyright 2013-2014 Foo Inc.
 * @license MIT License
 * @link http://example.com
 */
<?php
/**
 * Short description here.
 *
 * PHP version 5
 *
 * @category Foo
 * @category Bar
 * @package Foo_Helpers
 * @author Marty McFly <mmcfly@example.com>
 * @copyright 2013-2014 Foo Inc.
 * @license MIT License
 * @link http://example.com
 */
Valid: PHP version specified. Invalid: Category and package tags are swapped in order.
<?php
/**
 * Short description here.
 *
 * PHP version 5
 *
 * @category Foo
 * @package Foo_Helpers
 * @author Marty McFly <mmcfly@example.com>
 * @copyright 2013-2014 Foo Inc.
 * @license MIT License
 * @link http://example.com
 */
<?php
/**
 * Short description here.
 *
 * PHP version 5
 *
 * @package Foo_Helpers
 * @category Foo
 * @author Marty McFly <mmcfly@example.com>
 * @copyright 2013-2014 Foo Inc.
 * @license MIT License
 * @link http://example.com
 */
Valid: Tags are in the correct order. Invalid: No php version specified.
<?php
/**
 * Short description here.
 *
 * PHP version 5
 *
 * @category Foo
 * @package Foo_Helpers
 * @author Marty McFly <mmcfly@example.com>
 * @copyright 2013-2014 Foo Inc.
 * @license MIT License
 * @link http://example.com
 */
<?php
/**
 * Short description here.
 *
 * @category Foo
 * @package Foo_Helpers
 * @author Marty McFly <mmcfly@example.com>
 * @copyright 2013-2014 Foo Inc.
 * @license MIT License
 * @link http://example.com
 */

PEAR.Commenting.FunctionComment | [ref]# PEAR Coding Standard

Functions must have a non-empty doc comment. The short description must be on the second line of the comment. Each description must have one blank comment line before and after. There must be one blank line before the tags in the comments. There must be a tag for each of the parameters in the right order with the right variable names with a comment. There must be a return tag. Any throw tag must have an exception class.

Valid: A function doc comment is used. Invalid: No doc comment for the function.
/**
 * Short description here.
 *
 * @return void
 */
 function foo()
 {
 }
function foo()
 {
 }
Valid: Short description is the second line of the comment. Invalid: An extra blank line before the short description.
/**
 * Short description here.
 *
 * @return void
 */
 function foo()
 {
 }
/**
 *
 * Short description here.
 *
 * @return void
 */
 function foo()
 {
 }
Valid: Exactly one blank line around descriptions. Invalid: Extra blank lines around the descriptions.
/**
 * Short description here.
 *
 * Long description here.
 *
 * @return void
 */
 function foo()
 {
 }
/**
 * Short description here.
 *
 *
 * Long description here.
 *
 *
 * @return void
 */
 function foo()
 {
 }
Valid: Exactly one blank line before the tags. Invalid: Extra blank lines before the tags.
/**
 * Short description here.
 *
 * Long description here.
 *
 * @return void
 */
 function foo()
 {
 }
/**
 * Short description here.
 *
 * Long description here.
 *
 *
 * @return void
 */
 function foo()
 {
 }
Valid: Throws tag has an exception class. Invalid: No exception class given for throws tag.
/**
 * Short description here.
 *
 * @return void
 * @throws FooException
 */
 function foo()
 {
 }
/**
 * Short description here.
 *
 * @return void
 * @throws
 */
 function foo()
 {
 }
Valid: Return tag present. Invalid: No return tag.
/**
 * Short description here.
 *
 * @return void
 */
 function foo()
 {
 }
/**
 * Short description here.
 */
 function foo()
 {
 }
Valid: Param names are correct. Invalid: Wrong parameter name doesn't match function signature.
/**
 * Short description here.
 *
 * @param string $foo Foo parameter
 * @param string $bar Bar parameter
 * @return void
 */
 function foo($foo, $bar)
 {
 }
/**
 * Short description here.
 *
 * @param string $foo Foo parameter
 * @param string $qux Bar parameter
 * @return void
 */
 function foo($foo, $bar)
 {
 }
Valid: Param names are ordered correctly. Invalid: Wrong parameter order.
/**
 * Short description here.
 *
 * @param string $foo Foo parameter
 * @param string $bar Bar parameter
 * @return void
 */
 function foo($foo, $bar)
 {
 }
/**
 * Short description here.
 *
 * @param string $bar Bar parameter
 * @param string $foo Foo parameter
 * @return void
 */
 function foo($foo, $bar)
 {
 }

PEAR.Commenting.InlineComment | [ref]# PEAR Coding Standard

Perl-style # comments are not allowed.

Valid: A // style comment. Invalid: A # style comment.
// A comment.
# A comment.

PEAR.ControlStructures.ControlSignature | [ref]# PEAR Coding Standard

Control structures should use one space around the parentheses in conditions. The opening brace should be preceded by one space and should be at the end of the line.

Valid: Correct spacing around the condition. Invalid: Incorrect spacing around the condition.
if ($foo) {
}
if($foo){
}
Valid: Correct placement of the opening brace. Invalid: Incorrect placement of the opening brace on a new line.
if ($foo) {
}
if ($foo)
{
}

PEAR.ControlStructures.MultiLineCondition | [ref]# PEAR Coding Standard

Multi-line if conditions should be indented one level and each line should begin with a boolean operator. The end parenthesis should be on a new line.

Valid: Correct indentation. Invalid: No indentation used on the condition lines.
if ($foo
    && $bar
) {
}
if ($foo
&& $bar
) {
}
Valid: Boolean operator at the start of the line. Invalid: Boolean operator at the end of the line.
if ($foo
    && $bar
) {
}
if ($foo &&
    $bar
) {
}
Valid: End parenthesis on a new line. Invalid: End parenthesis not moved to a new line.
if ($foo
    && $bar
) {
}
if ($foo
    && $bar) {
}

PEAR.Files.IncludingFile | [ref]# PEAR Coding Standard

Anywhere you are unconditionally including a class file, use require_once. Anywhere you are conditionally including a class file (for example, factory methods), use include_once. Either of these will ensure that class files are included only once. They share the same file list, so you don't need to worry about mixing them - a file included with require_once will not be included again by include_once. Note that include_once and require_once are statements, not functions. Parentheses should not surround the subject filename.

Valid: used as statement Invalid: used as function
require_once 'PHP/CodeSniffer.php';
require_once('PHP/CodeSniffer.php');

PEAR.Formatting.MultiLineAssignment | [ref]# PEAR Coding Standard

Multi-line assignment should have the equals sign be the first item on the second line indented correctly.

Valid: Assignment operator at the start of the second line. Invalid: Assignment operator at end of first line.
$foo
    = $bar;
$foo =
    $bar;
Valid: Assignment operator indented one level. Invalid: Assignment operator not indented.
$foo
    = $bar;
$foo
= $bar;

PEAR.Functions.FunctionCallSignature | [ref]# PEAR Coding Standard

Functions should be called with no spaces between the function name, the opening parenthesis, and the first parameter; and no space between the last parameter, the closing parenthesis, and the semicolon.

Valid: spaces between parameters Invalid: additional spaces used
$var = foo($bar, $baz, $quux);
$var = foo ( $bar, $baz, $quux ) ;

PEAR.Functions.FunctionDeclaration | [ref]# PEAR Coding Standard

There should be exactly 1 space after the function keyword and 1 space on each side of the use keyword. Closures should use the Kernighan/Ritchie Brace style and other single-line functions should use the BSD/Allman style. Multi-line function declarations should have the parameter lists indented one level with the closing parenthesis on a newline followed by a single space and the opening brace of the function.

Valid: Correct spacing around function and use keywords. Invalid: No spacing around function and use keywords.
$foo = function () use ($bar) {
};
$foo = function()use($bar){
};
Valid: Multi-line function declaration formatted properly. Invalid: Invalid indentation and formatting of closing parenthesis.
function foo(
    $bar,
    $baz
) {
};
function foo(
$bar,
$baz)
{
};

PEAR.Functions.ValidDefaultValue | [ref]# PEAR Coding Standard

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)
{
    ...
}

PEAR.NamingConventions.ValidClassName | [ref]# PEAR Coding Standard

Classes should be given descriptive names. Avoid using abbreviations where possible. Class names should always begin with an uppercase letter. The PEAR class hierarchy is also reflected in the class name, each level of the hierarchy separated with a single underscore.

Examples of valid class names Examples of invalid class names
Log
Net_Finger
HTML_Upload_Error
log
NetFinger
HTML-Upload-Error

PEAR.NamingConventions.ValidFunctionName | [ref]# PEAR Coding Standard

Functions and methods should be named using the "studly caps" style (also referred to as "bumpy case" or "camel caps"). Functions should in addition have the package name as a prefix, to avoid name collisions between packages. The initial letter of the name (after the prefix) is lowercase, and each letter that starts a new "word" is capitalized.

Examples of valid function names Examples of invalid function names
connect()
getData()
buildSomeWidget()
XML_RPC_serializeData()
Connect()
get_data()

PEAR.NamingConventions.ValidVariableName | [ref]# PEAR Coding Standard

Private member variable names should be prefixed with an underscore and public/protected variable names should not.

Valid: Proper member variable names. Invalid: underscores used on public/protected variables and not used on private variables.
class Foo
{
    public $publicVar;
    protected $protectedVar;
    private $_privateVar;
}
class Foo
{
    public $_publicVar;
    protected $_protectedVar;
    private $privateVar;
}

PEAR.WhiteSpace.ObjectOperatorIndent | [ref]# PEAR Coding Standard

Chained object operators when spread out over multiple lines should be the first thing on the line and be indented by 1 level.

Valid: Object operator at the start of a new line. Invalid: Object operator at the end of the line.
$foo
    ->bar()
    ->baz();
$foo->
    bar()->
    baz();
Valid: Object operator indented correctly. Invalid: Object operator not indented correctly.
$foo
    ->bar()
    ->baz();
$foo
->bar()
->baz();

PEAR.WhiteSpace.ScopeClosingBrace | [ref]# PEAR Coding Standard

Closing braces should be indented at the same level as the beginning of the scope.

Valid: Consistent indentation level for scope. Invalid: The ending brace is indented further than the if statement.
if ($test) {
    $var = 1;
}
if ($test) {
    $var = 1;
    }

PEAR.WhiteSpace.ScopeIndent | [ref]# PEAR Coding Standard

Any scope openers except for switch statements should be indented 1 level. This includes classes, functions, and control structures.

Valid: Consistent indentation level for scope. Invalid: Indentation is not used for scope.
function foo()
{
    if ($test) {
        $var = 1;
    }
}
function foo()
{
if ($test) {
$var = 1;
}
}

Squiz.Commenting.DocCommentAlignment | [ref]# PEAR Coding Standard

The asterisks in a doc comment should align, and there should be one space between the asterisk and tags.

Valid: Asterisks are aligned. Invalid: Asterisks are not aligned.
/**
 * @see foo()
 */
/**
  * @see foo()
*/
Valid: One space between asterisk and tag. Invalid: Incorrect spacing used.
/**
 * @see foo()
 */
/**
 *  @see foo()
 */

Standard: PSR1

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.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.

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()
    {
    }
}

Squiz.Classes.ValidClassName | [ref]

Standard: PSR12

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.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.
function myFunction(int $foo) : string {
}
function myFunction(Int $foo) : STRING {
}
All PHP types used for type casting should be lowercase.
Valid: Lowercase type used. Invalid: Non-lowercase type used.
$foo = (bool) $isValid;
$foo = (BOOL) $isValid;

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.
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.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()
    {
    }
}

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.
new Foo();
new Foo;

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.
class Foo
{
    // Class content.
}

function bar()
{
    // Function content.
}
interface Foo2
{
    // Interface content.
} echo 'Hello!';

function bar()
{
    // Function content.
} //end bar()

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.
class Foo
{
    public function bar()
    {
        // Method content.
    }
}
class Foo
{

    public function bar()
    {
        // Method content.
    }
}

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.
if (
    $expr1
    && $expr2
    && ($expr3
    || $expr4)
    && $expr5
) {
    // if body.
}
if (
    $expr1 &&
    ($expr2 || $expr3)
    && $expr4
) {
    // if body.
}
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.
if (
    $expr1 ||
    ($expr2 || $expr3) &&
    $expr4
) {
    // if body.
}
match (
    $expr1
    && $expr2 ||
    $expr3
) {
    // structure body.
};

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.
<?php

echo 'hi';
<?php echo 'hi';
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.
<?php declare(strict_types=1); ?>
<html>
<body>
    <?php
        // ... additional PHP code ...
    ?>
</body>
</html>
<?php declare(strict_types=1); ?>

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.
public function functionName(
    ?string $arg1,
    ?int $arg2
): ?string {
}
public function functionName(
    ? string $arg1,
    ? int $arg2
): ? string {
}
Valid: no unexpected characters. Invalid: unexpected characters used.
public function foo(?int $arg): ?string
{
}
public function bar(? /* comment */ int $arg): ?
    // nullable for a reason
    string
{
}

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.
$foo = (bool) $isValid;
$foo = (boolean) $isValid;

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.
use Vendor\Package\SomeNamespace\{
    SubnamespaceOne\ClassA,
    SubnamespaceOne\ClassB,
    SubnamespaceTwo\ClassY,
    ClassZ,
};
use Vendor\Package\SomeNamespace\{
    SubnamespaceOne\AnotherNamespace\ClassA,
    SubnamespaceOne\ClassB,
    ClassZ,
};

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.
if ($a === $b) {
    $foo = $bar ?? $a ?? $b;
} elseif ($a > $b) {
    $variable = $foo ? 'foo' : 'bar';
}
if ($a===$b) {
    $foo=$bar??$a??$b;
} elseif ($a>$b) {
    $variable=$foo?'foo':'bar';
}

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.
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.CastSpacing | [ref]

Casts should not have whitespace inside the parentheses.

Valid: No spaces. Invalid: Whitespace used inside parentheses.
$foo = (int)'42';
$foo = ( int )'42';

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]

Standard: PSR2

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]

Standard: Squiz

Generic.Arrays.ArrayIndent | [ref]

Generic.Arrays.DisallowLongArraySyntax | [ref]

Short array syntax must be used to define arrays.

Valid: Short form of array. Invalid: Long form of array.
$arr = [
    'foo' => 'bar',
];
$arr = array(
    'foo' => 'bar',
);

Generic.CodeAnalysis.EmptyStatement | [ref]

Control Structures must have at least one statement inside of the body.

Valid: There is a statement inside the control structure. Invalid: The control structure has no statements.
if ($test) {
    $var = 1;
}
if ($test) {
    // do nothing
}

Generic.Commenting.DocComment | [ref]

Generic.Commenting.Todo | [ref]

TODO Statements should be taken care of.

Valid: A comment without a todo. Invalid: A todo comment.
// Handle strange case
if ($test) {
    $var = 1;
}
// TODO: This needs to be fixed!
if ($test) {
    $var = 1;
}

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.Debug.ClosureLinter | [ref]

All javascript files should pass basic Closure Linter tests.

Valid: Valid JS Syntax is used. Invalid: Trailing comma in a javascript array.
var foo = [1, 2];
var foo = [1, 2,];

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.Formatting.MultipleStatementAlignment | [ref]

There should be one space on either side of an equals sign used to assign a value to a variable. In the case of a block of related assignments, more space may be inserted to promote readability.

Equals signs aligned Not aligned; harder to read
$shortVar        = (1 + 2);
$veryLongVarName = 'string';
$var             = foo($bar, $baz);
$shortVar = (1 + 2);
$veryLongVarName = 'string';
$var = foo($bar, $baz);
When using plus-equals, minus-equals etc. still ensure the equals signs are aligned to one space after the longest variable name.
Equals signs aligned; only one space after longest var name Two spaces after longest var name
$shortVar       += 1;
$veryLongVarName = 1;
$shortVar        += 1;
$veryLongVarName  = 1;
Equals signs aligned Equals signs not aligned
$shortVar         = 1;
$veryLongVarName -= 1;
$shortVar        = 1;
$veryLongVarName -= 1;

Generic.Formatting.SpaceAfterCast | [ref]

Exactly one space is allowed after a cast.

Valid: A cast operator is followed by one space. Invalid: A cast operator is not followed by whitespace.
$foo = (string) 1;
$foo = (string)1;

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.ConstructorName | [ref]

Constructors should be named __construct, not after the class.

Valid: The constructor is named __construct. Invalid: The old style class name constructor is used.
class Foo
{
    function __construct()
    {
    }
}
class Foo
{
    function Foo()
    {
    }
}

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.DeprecatedFunctions | [ref]

Deprecated functions should not be used.

Valid: A non-deprecated function is used. Invalid: A deprecated function is used.
$foo = explode('a', $bar);
$foo = split('a', $bar);

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.ForbiddenFunctions | [ref]

The forbidden functions sizeof() and delete() should not be used.

Valid: count() is used in place of sizeof(). Invalid: sizeof() is used.
$foo = count($bar);
$foo = sizeof($bar);

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.Strings.UnnecessaryStringConcat | [ref]

Strings should not be concatenated together.

Valid: A string can be concatenated with an expression. Invalid: Strings should not be concatenated together.
echo '5 + 2 = ' . (5 + 2);
echo 'Hello' . ' ' . 'World';

Generic.WhiteSpace.DisallowTabIndent | [ref]

Spaces should be used for indentation instead of tabs.

Generic.WhiteSpace.IncrementDecrementSpacing | [ref]

Generic.WhiteSpace.LanguageConstructSpacing | [ref]

Language constructs that can be used without parentheses, must have a single space between the language construct keyword and its content.

Valid: Single space after language construct. Invalid: No space, more than one space or newline after language construct.
echo 'Hello, World!';
throw new Exception();
return $newLine;
echo'Hello, World!';
throw   new   Exception();
return
$newLine;
A single space must be used between the "yield" and "from" keywords for a "yield from" expression.
Valid: Single space between yield and from. Invalid: No space, more than one space or newline between yield and from.
yield from [1, 2, 3];
yieldfrom [1, 2, 3];
yield  from [1, 2, 3];
yield
from [1, 2, 3];

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.ControlStructures.MultiLineCondition | [ref]

Multi-line if conditions should be indented one level and each line should begin with a boolean operator. The end parenthesis should be on a new line.

Valid: Correct indentation. Invalid: No indentation used on the condition lines.
if ($foo
    && $bar
) {
}
if ($foo
&& $bar
) {
}
Valid: Boolean operator at the start of the line. Invalid: Boolean operator at the end of the line.
if ($foo
    && $bar
) {
}
if ($foo &&
    $bar
) {
}
Valid: End parenthesis on a new line. Invalid: End parenthesis not moved to a new line.
if ($foo
    && $bar
) {
}
if ($foo
    && $bar) {
}

PEAR.Files.IncludingFile | [ref]

Anywhere you are unconditionally including a class file, use require_once. Anywhere you are conditionally including a class file (for example, factory methods), use include_once. Either of these will ensure that class files are included only once. They share the same file list, so you don't need to worry about mixing them - a file included with require_once will not be included again by include_once. Note that include_once and require_once are statements, not functions. Parentheses should not surround the subject filename.

Valid: used as statement Invalid: used as function
require_once 'PHP/CodeSniffer.php';
require_once('PHP/CodeSniffer.php');

PEAR.Formatting.MultiLineAssignment | [ref]

Multi-line assignment should have the equals sign be the first item on the second line indented correctly.

Valid: Assignment operator at the start of the second line. Invalid: Assignment operator at end of first line.
$foo
    = $bar;
$foo =
    $bar;
Valid: Assignment operator indented one level. Invalid: Assignment operator not indented.
$foo
    = $bar;
$foo
= $bar;

PEAR.Functions.FunctionCallSignature | [ref]

Functions should be called with no spaces between the function name, the opening parenthesis, and the first parameter; and no space between the last parameter, the closing parenthesis, and the semicolon.

Valid: spaces between parameters Invalid: additional spaces used
$var = foo($bar, $baz, $quux);
$var = foo ( $bar, $baz, $quux ) ;

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)
{
    ...
}

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.Files.EndFileNewline | [ref]

PHP Files should end with exactly one newline.

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()
    {
    }
}

Squiz.Arrays.ArrayBracketSpacing | [ref]

When referencing arrays you should not put whitespace around the opening bracket or before the closing bracket.

Valid: No spaces around the brackets. Invalid: Spaces around the brackets.
$foo['bar'];
$foo [ 'bar' ];

Squiz.Arrays.ArrayDeclaration | [ref]

This standard covers all array declarations, regardless of the number and type of values contained within the array. The array keyword must be lowercase.

Valid: array keyword lowercase Invalid: first letter capitalised
$array = array('val1', 'val2');
$array = Array('val1', 'val2');
The first array key must begin on the line after the *array* keyword.
Valid: first key on second line Invalid: first key on same line
$array = array(
          'key1' => 'value1',
          'key2' => 'value2',
         );
$array = array('key1' => 'value1',
          'key2' => 'value2',
         );
All array keys must be indented to one space after the start of the *array* keyword. The closing parenthesis must be aligned with the start of the *array* keyword.
Valid: aligned correctly Invalid: keys and parenthesis aligned incorrectly
$array = array(
          'key1' => 'value1',
          'key2' => 'value2',
         );
$array = array(
         'key1' => 'value1',
         'key2' => 'value2',
);
All double arrow symbols must be aligned to one space after the longest array key. Alignment must be achieved using spaces.
Valid: keys and values aligned Invalid: alignment incorrect
$array = array(
          'keyTen'    => 'ValueTen',
          'keyTwenty' => 'ValueTwenty',
         );
$array = array(
          'keyTen' => 'ValueTen',
          'keyTwenty' => 'ValueTwenty',
         );
All array values must be followed by a comma, including the final value.
Valid: comma after each value Invalid: no comma after last value
$array = array(
          'key1' => 'value1',
          'key2' => 'value2',
          'key3' => 'value3',
         );
$array = array(
          'key1' => 'value1',
          'key2' => 'value2',
          'key3' => 'value3'
         );

Squiz.Classes.ClassDeclaration | [ref]

Squiz.Classes.ClassFileName | [ref]

Squiz.Classes.DuplicateProperty | [ref]

Squiz.Classes.LowercaseClassKeywords | [ref]

The php keywords class, interface, trait, extends, implements, abstract, final, var, and const should be lowercase.

Valid: Lowercase class keywords. Invalid: Initial capitalization of class keywords.
final class Foo extends Bar
{
}
Final Class Foo Extends Bar
{
}

Squiz.Classes.SelfMemberReference | [ref]

The self keyword should be used instead of the current class name, should be lowercase, and should not have spaces before or after it.

Valid: Lowercase self used. Invalid: Uppercase self used.
self::foo();
SELF::foo();
Valid: Correct spacing used. Invalid: Incorrect spacing used.
self::foo();
self :: foo();
Valid: Self used as reference. Invalid: Local class name used as reference.
class Foo
{
    public static function bar()
    {
    }

    public static function baz()
    {
        self::bar();
    }
}
class Foo
{
    public static function bar()
    {
    }

    public static function baz()
    {
        Foo::bar();
    }
}

Squiz.Classes.ValidClassName | [ref]

Squiz.Commenting.BlockComment | [ref]

Squiz.Commenting.ClassComment | [ref]

Squiz.Commenting.ClosingDeclarationComment | [ref]

Squiz.Commenting.DocCommentAlignment | [ref]

The asterisks in a doc comment should align, and there should be one space between the asterisk and tags.

Valid: Asterisks are aligned. Invalid: Asterisks are not aligned.
/**
 * @see foo()
 */
/**
  * @see foo()
*/
Valid: One space between asterisk and tag. Invalid: Incorrect spacing used.
/**
 * @see foo()
 */
/**
 *  @see foo()
 */

Squiz.Commenting.EmptyCatchComment | [ref]

Squiz.Commenting.FileComment | [ref]

Squiz.Commenting.FunctionComment | [ref]

Squiz.Commenting.FunctionCommentThrowTag | [ref]

If a function throws any exceptions, they should be documented in a @throws tag.

Valid: @throws tag used. Invalid: No @throws tag used for throwing function.
/**
 * @throws Exception all the time
 * @return void
 */
function foo()
{
    throw new Exception('Danger!');
}
/**
 * @return void
 */
function foo()
{
    throw new Exception('Danger!');
}

Squiz.Commenting.InlineComment | [ref]

Squiz.Commenting.LongConditionClosingComment | [ref]

Squiz.Commenting.PostStatementComment | [ref]

Squiz.Commenting.VariableComment | [ref]

Squiz.ControlStructures.ControlSignature | [ref]

Squiz.ControlStructures.ElseIfDeclaration | [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.InlineIfDeclaration | [ref]

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.ControlStructures.SwitchDeclaration | [ref]

Squiz.CSS.ClassDefinitionClosingBraceSpace | [ref]

Squiz.CSS.ClassDefinitionNameSpacing | [ref]

Squiz.CSS.ClassDefinitionOpeningBraceSpace | [ref]

Squiz.CSS.ColonSpacing | [ref]

Squiz.CSS.ColourDefinition | [ref]

Squiz.CSS.DisallowMultipleStyleDefinitions | [ref]

Squiz.CSS.DuplicateClassDefinition | [ref]

Squiz.CSS.DuplicateStyleDefinition | [ref]

Squiz.CSS.EmptyClassDefinition | [ref]

Squiz.CSS.EmptyStyleDefinition | [ref]

Squiz.CSS.ForbiddenStyles | [ref]

Squiz.CSS.Indentation | [ref]

Squiz.CSS.LowercaseStyleDefinition | [ref]

Squiz.CSS.MissingColon | [ref]

Squiz.CSS.NamedColours | [ref]

Squiz.CSS.Opacity | [ref]

Squiz.CSS.SemicolonSpacing | [ref]

Squiz.CSS.ShorthandSize | [ref]

Squiz.Debug.JavaScriptLint | [ref]

Squiz.Debug.JSLint | [ref]

Squiz.Files.FileExtension | [ref]

Squiz.Formatting.OperatorBracket | [ref]

Squiz.Functions.FunctionDeclaration | [ref]

Squiz.Functions.FunctionDeclarationArgumentSpacing | [ref]

Squiz.Functions.FunctionDuplicateArgument | [ref]

All PHP built-in functions should be lowercased when called.

Valid: Lowercase function call. Invalid: isset not called as lowercase.
if (isset($foo)) {
    echo $foo;
}
if (isSet($foo)) {
    echo $foo;
}

Squiz.Functions.GlobalFunction | [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.NamingConventions.ValidFunctionName | [ref]

Squiz.NamingConventions.ValidVariableName | [ref]

Squiz.Objects.DisallowObjectStringIndex | [ref]

Squiz.Objects.ObjectInstantiation | [ref]

Squiz.Objects.ObjectMemberComma | [ref]

Squiz.Operators.ComparisonOperatorUsage | [ref]

Squiz.Operators.IncrementDecrementUsage | [ref]

Squiz.Operators.ValidLogicalOperators | [ref]

Squiz.PHP.CommentedOutCode | [ref]

Squiz.PHP.DisallowBooleanStatement | [ref]

Squiz.PHP.DisallowComparisonAssignment | [ref]

Squiz.PHP.DisallowInlineIf | [ref]

Squiz.PHP.DisallowMultipleAssignments | [ref]

Squiz.PHP.DisallowSizeFunctionsInLoops | [ref]

Squiz.PHP.DiscouragedFunctions | [ref]

Squiz.PHP.EmbeddedPhp | [ref]

Squiz.PHP.Eval | [ref]

Squiz.PHP.GlobalKeyword | [ref]

Squiz.PHP.Heredoc | [ref]

Squiz.PHP.InnerFunctions | [ref]

Squiz.PHP.LowercasePHPFunctions | [ref]

Squiz.PHP.NonExecutableCode | [ref]

Squiz.Scope.MemberVarScope | [ref]

Squiz.Scope.MethodScope | [ref]

Squiz.Scope.StaticThisUsage | [ref]

Static methods should not use $this.

Valid: Using self:: to access static variables. Invalid: Using $this-> to access static variables.
class Foo
{
    static function bar()
    {
        return self::$staticMember;
    }
}
class Foo
{
    static function bar()
    {
    return $this->$staticMember;
    }
}

Squiz.Strings.ConcatenationSpacing | [ref]

Squiz.Strings.DoubleQuoteUsage | [ref]

Squiz.Strings.EchoedStrings | [ref]

Simple strings should not be enclosed in parentheses when being echoed.

Valid: Using echo without parentheses. Invalid: Using echo with parentheses.
echo "Hello";
echo("Hello");

Squiz.WhiteSpace.CastSpacing | [ref]

Casts should not have whitespace inside the parentheses.

Valid: No spaces. Invalid: Whitespace used inside parentheses.
$foo = (int)'42';
$foo = ( int )'42';

Squiz.WhiteSpace.ControlStructureSpacing | [ref]

Squiz.WhiteSpace.FunctionClosingBraceSpace | [ref]

Squiz.WhiteSpace.FunctionOpeningBraceSpace | [ref]

Squiz.WhiteSpace.FunctionSpacing | [ref]

Squiz.WhiteSpace.LanguageConstructSpacing | [ref]

The php constructs echo, print, return, include, include_once, require, require_once, and new should have one space after them.

Valid: echo statement with a single space after it. Invalid: echo statement with no space after it.
echo "hi";
echo"hi";

Squiz.WhiteSpace.LogicalOperatorSpacing | [ref]

Squiz.WhiteSpace.MemberVarSpacing | [ref]

Squiz.WhiteSpace.ObjectOperatorSpacing | [ref]

The object operator (->) should not have any space around it.

Valid: No spaces around the object operator. Invalid: Whitespace surrounding the object operator.
$foo->bar();
$foo -> bar();

Squiz.WhiteSpace.OperatorSpacing | [ref]

Squiz.WhiteSpace.PropertyLabelSpacing | [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.SemicolonSpacing | [ref]

Semicolons should not have spaces before them.

Valid: No space before the semicolon. Invalid: Space before the semicolon.
echo "hi";
echo "hi" ;

Squiz.WhiteSpace.SuperfluousWhitespace | [ref]

Zend.Debug.CodeAnalyzer | [ref]

PHP Code should pass the zend code analyzer.

Valid: Valid PHP Code. Invalid: There is an unused function parameter.
function foo($bar, $baz)
{
    return $bar + $baz;
}
function foo($bar, $baz)
{
    return $bar + 2;
}

Zend.Files.ClosingTag | [ref]

Files should not have closing php tags.

Valid: No closing tag at the end of the file. Invalid: A closing php tag is included at the end of the file.
<?php
$var = 1;
<?php
$var = 1;
?>

Standard: Zend

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.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.Functions.OpeningFunctionBraceBsdAllman | [ref]

Function declarations follow the "BSD/Allman style". The function brace is on the line following the function declaration and is indented to the same column as the start of the function declaration.

Valid: brace on next line Invalid: brace on same line
function fooFunction($arg1, $arg2 = '')
{
    ...
}
function fooFunction($arg1, $arg2 = '') {
    ...
}

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.WhiteSpace.DisallowTabIndent | [ref]

Spaces should be used for indentation instead of tabs.

PEAR.Classes.ClassDeclaration | [ref]

The opening brace of a class must be on the line after the definition by itself.

Valid: Opening brace on the correct line. Invalid: Opening brace on same line as declaration.
class Foo
{
}
class Foo {
}

PEAR.ControlStructures.ControlSignature | [ref]

Control structures should use one space around the parentheses in conditions. The opening brace should be preceded by one space and should be at the end of the line.

Valid: Correct spacing around the condition. Invalid: Incorrect spacing around the condition.
if ($foo) {
}
if($foo){
}
Valid: Correct placement of the opening brace. Invalid: Incorrect placement of the opening brace on a new line.
if ($foo) {
}
if ($foo)
{
}

PEAR.Functions.FunctionCallSignature | [ref]

Functions should be called with no spaces between the function name, the opening parenthesis, and the first parameter; and no space between the last parameter, the closing parenthesis, and the semicolon.

Valid: spaces between parameters Invalid: additional spaces used
$var = foo($bar, $baz, $quux);
$var = foo ( $bar, $baz, $quux ) ;

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)
{
    ...
}

PEAR.WhiteSpace.ScopeClosingBrace | [ref]

Closing braces should be indented at the same level as the beginning of the scope.

Valid: Consistent indentation level for scope. Invalid: The ending brace is indented further than the if statement.
if ($test) {
    $var = 1;
}
if ($test) {
    $var = 1;
    }

Squiz.Functions.GlobalFunction | [ref]

Zend.Debug.CodeAnalyzer | [ref]

PHP Code should pass the zend code analyzer.

Valid: Valid PHP Code. Invalid: There is an unused function parameter.
function foo($bar, $baz)
{
    return $bar + $baz;
}
function foo($bar, $baz)
{
    return $bar + 2;
}

Zend.Files.ClosingTag | [ref]

Files should not have closing php tags.

Valid: No closing tag at the end of the file. Invalid: A closing php tag is included at the end of the file.
<?php
$var = 1;
<?php
$var = 1;
?>

Zend.NamingConventions.ValidVariableName | [ref]

Variable names should be camelCased with the first letter lowercase. Private and protected member variables should begin with an underscore

Valid: A multi-word variable uses camel casing. Invalid: A multi-word variable uses underscores and initial capitalization.
$testNumber = 1;
$Test_Number = 1;
Valid: A private member variable begins with an underscore. Invalid: A private member variable does not begin with an underscore.
class Foo
{
    private $_bar;
}
class Foo
{
    private $bar;
}

Feedback

How satisfied you are after reading this article?