Skip to content

General informations

Konafets edited this page Dec 23, 2014 · 3 revisions

TYPO3SniffPool Coding Standard

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!';
## Assignment, arithmetic and comparison spacing Assignment, arithmetic and comparison operators should be surrounded with spaces Assignment surrounded with spaces
Valid: Space before and after assignment operator. Invalid: No space before and after the assignment operator.
$foo = $bar; $foo=$bar;
$foo= $bar;
$foo =$bar;
Arithmethic-Assignment operators surround with spaces
Valid: Space before and after arithmetic-assignment. Invalid: No space before and after the arithmetic-assignment.
$foo += $bar;
$foo -= $bar;
$foo *= $bar;
$foo /= $bar;
$foo %= $bar;
$foo &= $bar;
$foo .= $bar;
$foo ^= $bar;
$foo+=$bar;
$foo-=$bar;
$foo*=$bar;
$foo/=$bar;
$foo%=$bar;
$foo&=$bar;
$foo.=$bar;
$foo^=$bar;
Comparison operators surrounded by space
Valid: Space before and after comparison. Invalid: No space before and after the comparison.
$foo = ($bar == $baz);
$foo = ($bar === $baz);

$foo = ($bar != $baz);
$foo = ($bar !== $baz);

$foo = ($bar < $baz);
$foo = ($bar > $baz);

$foo = ($bar <= $baz);
$foo = ($bar >= $baz);
$foo = ($bar==$baz);
$foo = ($bar===$baz);

$foo = ($bar!=$baz);
$foo = ($bar!==$baz);

$foo = ($bar<$baz);
$foo = ($bar>$baz);

$foo = ($bar<=$baz);
$foo = ($bar>=$baz);
Arithmetic operators surrounded by space
Valid: Space before and after arithmetic operator. Invalid: No space before and after arithmetic operator.
$foo = 1 + 2;
$foo = 1 - 2;
$foo = 1 * 2;
$foo = 1 / 2;
$foo = 1 % 2;
$foo = (1 + 2) * (5 % 3);
$foo = 1+2;
$foo = 1-2;
$foo = 1*2;
$foo = 1/2;
$foo = 1%2;
$foo = (1+2)*(5%3);
Double arrow operator surrounded by space
Valid: Space before and after double arrow operator. Invalid: No space before and after double arrow operator.
$foo = array('foo' => 'bar'); $foo = array('foo'=>'bar');
No space after minus operator when its negate a number
Valid: No space after minus. Invalid: Space after minus.
foo::bar($baz, -10);
foo::bar(-10, $baz);

$foo = $bar * -1;


function foo($bar){
return -5;
}
foo::bar($baz, - 10);


$foo = $bar * - 1;
$foo = $bar *-1;

function foo($bar){
return - 5;
}

function foo($bar){
return- 5;
}
Allow multiple whitespaces (helps for aligning)
Valid: No space after minus.
$foo   = $bar;
$foo  += $bar;
$foo -=  $bar;
$foo .=   $bar;
$foo   &= $bar;
$foo %=  $bar;
$foo   *=   $bar;
$foo    /= $bar;
$foo =  ($bar    == $baz);
$foo = ($bar ===    $baz);
$foo = ($bar  !=   $baz);
$foo = ($bar   !== $baz);
$foo = ($bar <  $baz);
$foo = ($bar   > $baz);
$foo = ($bar  <=  $baz);
$foo = ($bar    >=       $baz);
$foo = 1   + 2;
$foo = 1 -   2;
$foo = 1   *   2;
$foo = 1  /  2;
$foo    =    1 %   2;
$foo = (1 + 2) * (5  %   3);
$foo    ^=   $bar;
$foo = array('foo'    =>   'bar');
No space after prefix operator
Valid: No space after prefix operator. Invalid: Space after prefix operator.
$foo = ++$i;
$foo = --$i;
$foo = ++ $i;
$foo = -- $i;
No space before postfix operator
Valid: No space before postfix operator. Invalid: Space before postfix operator.
$foo = $i++;
$foo = $i--;
$foo = $i ++;
$foo = $i --;
## 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 Tue, 23 Dec 2014 12:47:54 +0100 by [PHP_CodeSniffer 2.1.1](http://pear.php.net/package/PHP_CodeSniffer/ \"PHP_CodeSniffer 2.1.1\")

Clone this wiki locally