TYPO3SniffPool Coding Standards

Table of Contents

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 ? '.' : ';');

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

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 Mon, 04 Nov 2013 18:48:15 +0100 by PHP_CodeSniffer 1.4.6