Skip to content

Included TYPO3CMS CodeSniffs (with code examples)

Stefano Kowalke edited this page Jul 23, 2014 · 1 revision

TYPO3CMS Coding Standards

Table of Contents

Array Bracket Spacing

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' ];

Self Member Reference

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

Duplicate Class Names

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

For Loops With Function Calls in the Test

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

Unconditional If Statements

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

Unnecessary Final Modifiers

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

Empty Statements

Control Structures must have at least one statment 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}

Inline Comments

Perl-style # comments are not allowed.

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

Inline Control Structures

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;

Switch Declaration

This standard covers all switch declarations.

The keywords Switch, Case and Default should be lowercase

Valid: Lowercase keywords Invalid: Keywords begins with a uppercase letter or are complete uppercase
switch ($something) {    case '1':        $case = '1';        break;    case '2':        $case = '3';        break;    default:        $case = null;} Switch ($something) {    CASE '1':        $case = '1';        break;    CASE '2':        $case = '3';        break;    Default:        $case = null;}

Case and Default statements are indented with a single indent (tab) inside the Switch statement.

Valid: Case statement is indent with one tab Invalid: Case statement is indent with one space. Case statement is not indent at all or with two tabs.
switch ($something) {[tab]case '1':...} switch ($something) {[space]case '1':...}switch ($something) {case '1':...}switch ($something) {[tab][tab]case '1':...}

Case keyword should be followed by a single space.

Valid: Case statement is followed by a single space Invalid: No space after Case statement
switch ($something) {    case '1':...} switch ($something) {    case'1':...}

There should be no space before the colon.

Valid: No space before the colon Invalid: Space before the colon
switch ($something) {    case '1':...} switch ($something) {    case '1' :...}

If one Case block has to pass control into another Case block without having a Break, there must be a comment about it in the code.

Valid: The fall-through is explained with a comment Invalid: No comment at the fall-through
switch ($something) {    case '1':        $case = '1';        break;    case '2':        $case = '3';        // Fall through the next case on purpose    case '3':        $case = '3';        break;    default:        $case = null;} switch ($something) {    case '1':        $case = '1';        break;    case '2':        $case = '3';            case '3':        $case = '3';        break;    default:        $case = null;}

The Default statement must be the last in the Switch and must not have a Break statement.

Valid: The default statement is the last in the switch and have no break statement. Invalid: Default statement is not the last element and it contains a break.
switch ($something) {    case '1':        $case = '1';        break;    default:        $case = null;} switch ($something) {    case '1':        $case = '1';        break;    default:        $case = null;    case '2':        $case = '2';        break;}switch ($something) {    case '1':        $case = '1';        break;    default:        $case = null;        break;}

The code inside the Case statements is further indented with a single (tab) indent.

Valid: The code is indent one more tab then case statement. Invalid: The code is indent with spaces or with only one tab.
switch ($something) {[tab]case '1':[tab][tab]$case = '1';...} switch ($something) {[tab]case '1':[space][space]$case = '1';...}switch ($something) {[tab]case '1':[tab]$case = '1';...}switch ($something) {[tab]case '1':[tab][tab][tab]$case = '1';...}

The Break statement is aligned with the code.

Valid: The break statement is aligned to the code. Invalid: The break statement is not aligned with the code.
switch ($something) {[tab]case '1':[tab][tab]$case = '1';[tab][tab]break;...} switch ($something) {[tab]case '1':[tab][tab]$case = '1';[tab]break;...}switch ($something) {[tab]case '1':[tab][tab]$case = '1';[tab][tab][tab]break;...}

There should be no blank lines before the Break statement

Valid: No blank line before the break statement Invalid: Blank line before the break statement
switch ($something) {    case '1':        $case = '1';        break;    default:        $case = null;} switch ($something) {    case '1':        $case = '1';                            break;    default:        $case = null;}

Only one Break statement is allowed per Case.

Valid: Only one break statement per case Invalid: Two case statements per case
switch ($something) {    case '1':        $case = '1';        break;...} switch ($something) {    case '1':        $case = '1';        break;        break;...}

There should be no blank lines after the Case statement

Valid: No blank line after the case statement Invalid: Blank line after the case statement
switch ($something) {    case '1':        $case = '1';        break;    default:        $case = null;} switch ($something) {    case '1':                            $case = '1';        break;    default:        $case = null;}

All Switch statements must contain a Default case

Valid: Switch with a default case Invalid: Switch without a default case
switch ($something) {    case '1':        $case = '1';        break;    default:        $case = null;} switch ($something) {    case '1':        $case = '1';        break;}

Closing brace of the Switch statement must aligned with the Switch keyword

Valid: Closing brace aligned with the switch keyword Invalid: Closing brace not aligned with the switch keyword
switch ($something) {    case '1':        $case = '1';        break;    default:        $case = null;} switch ($something) {    case '1':        $case = '1';        break;    default:        $case = null;    }

Switch statement must contain at least one Case statement

Valid: Switch contains at least one case statement Invalid: Switch contains not case statement
switch ($something) {    case '1':        $case = '1';        break;    default:        $case = null;}switch ($something) {    default:        $case = null;} switch ($something) {}

Ternary Conditional Operator

This checks the correct usage of ternary conditional operators

The ternary conditional operator ? : must be used only, if it has exactly two outcomes.

Valid: The ternary conditional operator is not nested. Invalid: The ternary conditional operator is nested.
$result = ($useComma ? ',' : '.'); $result = ($useComma ? ',' : $useDot ? '.' : ';');

One Class Per File

There should only be one class defined in a file.

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

One Interface Per File

There should only be one interface defined in a file.

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

Byte Order Marks

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

Line Endings

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

Line Length

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

Multiple Statements On a Single Line

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;

Function Argument Spacing

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

Opening Brace in Function Declarations

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

Constructor name

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

Opening Tag at Start of File

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.
<?phpecho 'Foo'; Beginning content<?phpecho 'Foo';

PHP Code Tags

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.

PHP Constants

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

Deprecated Functions

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

Closing PHP Tags

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

ConcatenationSpacing

This standard is about the spacing of concat strings

String concatenation operators must be surrounded by spaces

Valid: String concatenation operator surrounded by space Invalid: String concatenation operator is not surrounded by space
$content  = 'Hello ' . 'world!'; $content  = 'Hello '. 'world!';$content  = 'Hello ' .'world!';$content  = 'Hello '.'world!';

String concatenation operators should be surrounded by only one space

Valid: String concatenation operator surrounded by one space on every side Invalid: String concatenation operator surrounded by multiple spaces
$content  = 'Hello ' . 'world!'; $content  = 'Hello '  . 'world!';$content  = 'Hello '  .         'world!';

Semicolon Spacing

Semicolons should not have spaces before them.

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

Closing Brace Indentation

This checks the indention of the closing brace of a scope

The closing curly brace must start on a new line.

Valid: The closing curly brace starts on a new line. Invalid: The closing curly brace doesn't starts on a new line.
if ($test) {    $var = 1;}function test2() {} if ($test) {    $var = 1;}function test2() {}

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

Documentation generated on Wed, 23 Jul 2014 12:55:35 +0200 by PHP_CodeSniffer 2.0.0a1

Clone this wiki locally