The sniffs with code examples

Variables

Code rule Sniff Description Notice Implemented
Variables must be lowerCamelCased TYPO3v4/Sniffs/NamingConventions/ValidVariableNameSniff.php False:
$BAD_name
Correct:
$goodName
Must have X
Underscore characters in variables are not allowed. TYPO3v4/Sniffs/NamingConventions/ValidVariableNameSniff.php False:
$BAD_name
Correct:
$goodName
Must have X

Files

Code rule Sniff Description Notice Implemented
filenames are all lowercase TYPO3v4/Sniffs/Files/LowercasedFilenameSniff.php False:
CLASS.tx_MyExtIs_veryCool.php
Correct:
class.tx_myextis_verycool.php
Must have, but this sniff must be reworked, because it compares the file name with the including class name X

PHP

Code rule Sniff Description Notice Implemented
always long opening tag Generic/Sniffs/PHP/DisallowShortOpenTagSniff.php False:
<?
Correct:
<?php
Must have X
exactly one pair of opening and closing tags (no closing and opening tags in the middle of the file) @todo False:
<?php
doSomething();
?>
<span>some html content</span>
<?php 
doSomethingMore(); 
?>
Correct:
<?php
doSomething();
echo '<span>some html content</span>';
doSomethingMore(); 
?>
Must have X
always closing tag TYPO3v4/Sniffs/PHP/ClosingPHPTagSniff.php False:
// Some code
// End of File
Correct:
// Some code
// End of File
?>
Must have X
Uppercase Booleans Generic/Sniffs/PHP/UpperCaseConstantSniff.php False:
false / true / null
Correct:
FALSE / TRUE / NULL
Must have X
Find deprecated PHP functions Generic/Sniffs/PHP/DeprecatedFunctionsSniff.php False:
ereg() / call_user_method()
Correct:
preg_match() / call_user_func()
Nice to have; Have a look at http://php.net/manual/de/migration53.deprecated.php X
Constructor name Generic/Sniffs/NamingConventions/ConstructorNameSniff.php False:
function ClassName()
Correct:
function __construct()
Must have X
Files are included using require_once TYPO3v4/Sniffs/Files/IncludingFileSniff.php False:
include() / include_once() / require()
Correct:
require_once()
Must have X
The use of eval() is discouraged Squiz/Sniffs/PHP/EvalSniff.php False:
$color = 'green';
$str = 'This is a $color tree.';
eval ("\$str = \"$str\";");
Correct:
$color = 'green';
$str = 'This is a ' . $color . ' tree.';
Should have if eval() is forbidden X
do not use the global keyword, use $GLOBALS[] instead Squiz/Sniffs/PHP/GlobalKeywordSniff.php False:
global $BE_USER;
$BE_USER->doSomething();
Correct:
$GLOBALS['BE_USER']->doSomething();
Should have X
debug() or t3lib_div::debug() function calls must be removed (removed, not commented!) TYPO3v4/Sniffs/Debug/DebugCodeSniff.php False:
$array = $this->doSomething();
# debug($array);
Correct:
$array = $this->doSomething();
Must have X

Whitespace

Code rule Sniff Description Notice Implemented
Space must be added on both sides of string, arithmetic, assignment and other similar operators (for example, ., =, +, -, ?, :, *, etc) @todo False:
$foo=$bar;
$foo=($bar+$baz)*5;
$foo=($bar==$baz?1:2);
Correct:
$foo = $bar;
$foo = ($bar + $baz) * 5;
$foo = ($bar == $baz ? 1 : 2);
Must have Julian Kleinhans
Space must be added after commas in functions Generic_Sniffs_Functions_FunctionCallArgumentSpacingSniff False:
$example = $this->doSomething($param1,$param2);
Correct:
$example = $this->doSomething($param1, $param2);
Must have X
Space must be added after commas in array @todo False:
$array = array(1,2,3,4);
Correct:
$array = array(1, 2, 3, 4);
Must have Julian Kleinhans
Space must be added after after asterisks in multiline comments TYPO3v4/Sniffs/Whitespaces/AsteriksWhitespacesDiff.php False:
/**
 *This class provides XYZ plugin implementation.
 *
 *@author John Doe <john.doe@example.com>
 *@author Jane Doe <jane.doe@example.com>
 */
Correct:
/**
 * This class provides XYZ plugin implementation.
 *
 * @author John Doe <john.doe@example.com>
 * @author Jane Doe <jane.doe@example.com>
 */
Must have X

Interfaces

Code rule Sniff Description Notice Implemented
only one interface per file TYPO3v4/Sniffs/Files/OneInterfacePerFileSniff.php False:
<?php
interface foo {
    // Code
}

interface bar {
    // Code
}
?>
Correct:
<?php
interface foo {
    // Code
}
?>
Must have X

Classes

Code rule Sniff Description Notice Implemented
only one class per file TYPO3v4/Sniffs/Files/OneClassPerFileSniff.php False:
<?php
class foo {
    // Code
}

class bar {
    // Code
}
?>
Correct:
<?php
class foo {
    // Code
}
?>
Must have X
lowerCamelCase, no underscores Generic/Sniffs/NamingConventions/ValidClassNameSniff.php @todo Must have; We have to reimplement this sniff to fits our needs
Ensures all class keywords are lowercase Squiz/Sniffs/Classes/LowercaseClassKeywordsSniff.php @todo Should have
Tests self member references Squiz/Sniffs/Classes/SelfMemberReferenceSniff.php
Verifies that:
- self:: is used instead of Self::
- self:: is used for local static member reference
- self:: is used instead of self ::
Should have X

Methods

Code rule Sniff Description Notice Implemented
the open curly brace must be on same line Generic/Sniffs/Functions/OpeningFunctionBraceKernighanRitchieSniff.php False:
if ($foo === 42) 
{
  do_something();
}
Correct:
if ($foo === 42) {
  do_something();
}
Must have X
have proper function names Generic/Sniffs/Functions/ValidFunctionNameSniff.php @todo Could have; This sniff needs to be reimplemented to fits our needs
Visibility of functions. Use private, protected or public @todo Correct:
private function foo()
protected function bar()
public function baz() 
Must have
Names of methods must be lowerCamelCased @todo False:
function Extended_use() {
    // Some code
}
Correct:
function extendedUse() {
    // Some code
}
Must have Laura Thewalt
If a function returns a value, it must always return it @todo False:
function extendedUse($enabled) {
    if ($enabled) {
        return 'Extended use';
    }
}
Correct:
function extendedUse($enabled) {
    $content = '';
    if ($enabled) {
        $content = 'Extended use';
    }
    return $content;
}
Must have

Scope

Code rule Sniff Description Notice Implemented
Visibility of member variables. Use private, protected or public Squiz/Sniffs/Scope/MemberVarScopeSniff.php Correct:
private $foo;
protected $bar;
public $baz; 
Must have X
Do not use 'var' keyword anymore Squiz/Sniffs/Scope/MemberVarScopeSniff.php False:
var $foo;
Correct:
private $foo;
protected $bar;
public $baz; 
Must have X

Arrays

Code rule Sniff Description Notice Implemented
Ensure that there are no spaces around square brackets. Squiz/Sniffs/Arrays/ArrayBracketSpacingSniff.php False:
$array[ 'Hello'];
$array ['Hello'];
Correct:
$array['Hello'];
Must have X
A test to ensure that arrays conform to the array coding standard. Squiz/Sniffs/Arrays/ArrayDeclarationSniff.php Correct:
array();
Could have

Strings

Code rule Sniff Description Notice Implemented
don't concate multiple strings Generic/Sniffs/Strings/UnnecessaryStringConcatSniff.php False:
'A string concate' . 'with another string'
Correct:
'Just a singe string'
Must have X
All strings must use single quotes. Double quotes are allowed only to create the new line character (“\n”). Squiz.Strings.DoubleQuoteUsage False:
echo "This is a string";
echo "This is a string with a Tab \t and a Newline\n";
Correct:
echo 'This is a string';
echo "This is a string with a Tab \t and a Newline\n";
echo 'This is a string with a Tab ' . "\t" . ' and a Newline' . "\n";
Must have X
Variables must not be embedded into strings Squiz.Strings.DoubleQuoteUsage False:
$content = "Hello $userName";
Correct:
$content = 'Hello ' . $userName;
Must have X
Multiline string concatenations are allowed. Line concatenation operator must be at the end of the line. @todo False:
$content = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. '
. 'Donec varius libero non nisi. Proin eros.';
Correct:
$content = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. ' .
    'Donec varius libero non nisi. Proin eros.';
Must have

Comments

Code rule Sniff Description Notice Implemented
Perl-style comments PEAR/Sniffs/Commenting/InlineCommentSniff.php False:
# This is a perl-style comment.
Correct:
// Better use this kind of comments
Not sure yet if perl-style comments allowed X
Keep your comments on a seperate line TYPO3v4/Sniffs/Commenting/DoubleSlashCommentsInNewLineSniff.php False:
$foo = 42 // The answer of life, the universe and everything
Correct:
// The answer of life, the universe and everything
$foo = 42
Not sure yet if perl-style comments allowed X
Intend your comments one tab @todo False:
// The answer of life, the universe and everything
$foo = 42
Correct:
    // The answer of life, the universe and everything
$foo = 42
Must have
Space must be added in single line comments after the comment sign (double slash) TYPO3v4/Sniffs/Commenting/SpaceAfterDoubleSlashSniff.php False:
//This is a example
Correct:
// This is a example
Must have X
Comment line length @todo Comment lines should be kept within a limit of about 80 characters (excluding tabs) Must have
Class constants and variable comments should follow PHP doc style and precede the variable. Variable type must be specified for non–trivial types and optional for trivial types. PEAR/Sniffs/Commenting/ClassCommentSniff.php Correct:
/** Number of images submitted by user */
protected $numberOfImages;

/**
 * Local instance of tslib_cObj class
 *
 * @var tslib_cObj
 */
protected $localCobj;
Must have; Not sure if the sniff fits our needs exactly. Maybe we have to reimplement it.
Check for function comments PEAR/Sniffs/Commenting/FunctionCommentSniff.php
Verifies that :
* A comment exists
* There is a blank newline after the short description.
* There is a blank newline between the long and short description.
* There is a blank newline between the long description and tags.
* Parameter names represent those in the method.
* Parameter comments are in the correct order
* Parameter comments are complete
* A space is present before the first and after the last parameter
* A return type exists
* There must be one blank line between body and headline comments.
* Any throw tag must have an exception class.
Must have
Doc block comments for classes, methods and functions @todo
/**
 * This class provides XYZ plugin implementation.
 *
 * @author John Doe <john.doe@example.com>
 * @author Jane Doe <jane.doe@example.com>
 */
Must have
Verifies that block comments are used appropriately Squiz/Sniffs/Commenting/BlockCommentSniff.php @todo Could have
@author tag should not be used in function or method phpDoc comment blocks – only at class level @todo False:
/**
 * This class provides XYZ plugin implementation.
 *
 * @author John Doe <john.doe@example.com>
 * @author Jane Doe <jane.doe@example.com>
 */
function fooBar(){
    // Some code
}
Correct:
/**
 * This class provides XYZ plugin implementation.
 *
 * @author John Doe <john.doe@example.com>
 * @author Jane Doe <jane.doe@example.com>
 */
class xyzImplementation {
   // Some code
}
Must have
PHPDoc: Notice the use of void when function does not return a value. @todo False:
/**
 * This function say "Hello World" 
 */
function fooBar(){
    echo 'Hello World";
}
Correct:
/**
 * This function say "Hello World" 
 *
 * @return void
 */
function fooBar(){
    echo 'Hello World";
}
Must have

Lines

Code rule Sniff Description Notice Implemented
Line endings Generic/Sniffs/Files/LineEndingsSniff.php Line endings in UNIX style (\n). No Windows (\r\n) or old MAC style (\r) Must have X
PHP line length Generic/Sniffs/Files/LineLengthSniff.php Line length of about 130 characters (including tabs) is fine Must have X
no whitespace at the end of lines @todo False:
// In the next line there is whitespace after the word "code". Mark it to see it.
// Some code     
Correct:
// In the next line there is _NO_ whitespace after the word "code". Mark it to see it.
// Some code
Must have Andy
no empty lines / newline before php tags TYPO3v4/Sniffs/PHP/CharacterBeforePHPOpeningTagSniff.php False:

<?php
// Some code
?>
Correct:
<?php
// Some code
?>
Must have X
no empty lines / newline after php tags TYPO3v4/Sniffs/PHP/CharacterAfterPHPClosingTagSniff.php False:
<?php
// Some code
?>

Correct:
<?php
// Some code
?>
Must have X
no multi-line statements, except in for-loops Generic/Sniffs/Formatting/DisallowMultipleStatementsSniff.php False:
$foo = 42; $bar = 23;
Correct:
$foo = 42; 
$bar = 23;
Could have
Use tabs for indentation TYPO3v4/Sniffs/Whitespaces/DisallowSpaceIntendSniff.php The code must intend by tabs. Spaces are not allowed. Must have X

Control structures / Scopes

Code rule Sniff Description Notice Implemented
Using braces is mandatory Generic.ControlStructures.InlineControlStructure False:
if($foo == $bar)
    $foo = $baz;
$this->doFunction();
Correct:
if($foo == $bar) {
    $foo = $baz;
}
$this->doFunction();
Must have X
Inline control structures are not allowed Generic.ControlStructures.InlineControlStructure False:
foreach($SOBE->include_once as $INC_FILE)    include_once($INC_FILE);
Correct:
foreach($SOBE->include_once as $INC_FILE) {
    include_once($INC_FILE);
}
Must have X
Opening braces must be on the same line Squiz.ControlStructures.ControlSignature False:
if($foo == $bar) 
{
    $foo = $baz;
}
Correct:
if($foo == $bar) {
    $foo = $baz;
}
Must have X
There must be a space (not a tab!) before the opening brace. Squiz.ControlStructures.ControlSignature False:
if($foo == $bar){
    $foo = $baz;
}
Correct:
if($foo == $bar) {
    $foo = $baz;
}
Must have X
The opening brace is always followed by a new line. Squiz.ControlStructures.ControlSignature False:
if($foo == $bar) { $foo = $baz;
}
Correct:
if($foo == $bar) {
    $foo = $baz;
}
Must have X
Closing braces of a scope have to be on his own line and aligned correctly PEAR/Sniffs/WhiteSpace/ScopeClosingBraceSniff.php False:
if($foo == $bar) {
    $foo = $baz;}
Correct:
if($foo == $bar) {
    $foo = $baz;
}
Must have; we have to change the indent method from spaces into tabs from line 112
TYPO3 code must not use the "else if" construct. @todo False:
if ($this->processSubmission) {
    // Process submission here
} else if ($this->internalError) {
    // Handle internal error
}
Correct:
if ($this->processSubmission) {
    // Process submission here
} elseif ($this->internalError) {
    // Handle internal error
} else {
    // Something else here
}
Must have
Dont use nested "ternary conditional" operators @todo False:
$result = ($useComma ? ',' : $useDot ? '.' : ';');
Correct:
$result = ($useComma ? ',' : '.');
Must have
Assignment in conditions / loops should be surrounded by the extra pair of brackets @todo False:
while($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)){
    // Some code
}
Correct:
while(($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res))){
    // Some code
}
Must have
Case statements are indented with a single indent (tab) inside the switch statement @todo False:
switch($foo){
case 1:
    doSomething();
break;
default:
}
Correct:
switch($foo){
    case 1:
        doSomething();
        break;
    default:
}
Must have
The code inside the case statements is further indented with a single indent. @todo False:
switch($foo){
    case 1:
    doSomething();
        break;
    default:
}
Correct:
switch($foo){
    case 1:
        doSomething();
        break;
    default:
}
Must have
The break statement is aligned with the code. @todo False:
switch($foo){
    case 1:
        doSomething();
    break;
    default:
}
Correct:
switch($foo){
    case 1:
        doSomething();
        break;
    default:
}
Must have
Only one break statement is allowed per case. @todo False:
switch($foo){
    case 1:
        if($bar == $baz) {
            // Some code
            break;
        }
        break;
    default:
}
Correct:
switch($foo){
    case 1:
        if($bar == $baz) {
            // Some code
        }
        break;
    default:
}
Must have
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. @todo False:
switch ($useType) {
    case 'extended':
        $content .= $this->extendedUse();
    case 'basic':
        $content .= $this->basicUse();
        break;
    default:
        $content .= $this->errorUse();
}
Correct:
switch ($useType) {
    case 'extended':
        $content .= $this->extendedUse();

    // Fall through
    case 'basic':
        $content .= $this->basicUse();
        break;

    default:
        $content .= $this->errorUse();
}
Must have
for loops must contain only variables inside (no function calls) @todo False:
for ($element = 0; $element < count($dataArray); $element++) {
    // Process element here
}
Correct:
$size = count($dataArray);
for ($element = 0; $element < $size; $element++) {
    // Process element here
}
Must have