PHPCS: Zend

Personal documentation for the sniffs available in the PHPCS Zend Coding Standards to help build an own rulesets and search for error explanations.

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

Looking for a developer who
truly cares about your business?

My team and I provide expert consultations, top-notch coding, and comprehensive audits to elevate your success.

Feedback

How satisfied you are after reading this article?