« Previous - Version 84/98 (diff) - Next » - Current version
Stefano Kowalke, 2012-07-17 11:19


TYPO3v4 Sniffs with code examples

Arrays

Array bracket spacing

Sniff Description Notice Implemented PHPUnit Test
Squiz.Arrays.ArrayBracketSpacing False:
$array[ 'Hello'];
$array ['Hello'];
Correct:
$array['Hello'];
Must have X X

Files

One Class per file

Sniff Description Notice Implemented PHPUnit Test
TYPO3.Files.OneClassPerFile False:
<?php
class foo {
    // Code
}

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

Lowercased filename

Sniff Description Notice Implemented PHPUnit Test
TYPO3.Files.LowercasedFilename False:
CLASS.tx_MyExtIs_veryCool.php
Correct:
class.tx_myextis_verycool.php
This is impossible because filenames of unit tests need an uppercase Test suffix (t3lib_tcemainTest.php) See #9884 X X

Encoding UTF8

Sniff Description Notice Implemented PHPUnit Test
TYPO3.Files.EncodingUtf8 Files must be encoded in UTF-8 Must have X

One Interface per file

Sniff Description Notice Implemented PHPUnit Test
TYPO3.Files.OneInterfacePerFile False:
<?php
interface foo {
    // Code
}

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

Line endings

Sniff Description Notice Implemented PHPUnit Test
Generic.Files.LineEndings Line endings in UNIX style (\n). No Windows (\r\n) or old MAC style (\r) Must have X X

Line length

Sniff Description Notice Implemented PHPUnit Test
Generic.Files.LineLength Line length of about 130 characters (including tabs) is fine Must have X X

Including file

Sniff Description Notice Implemented PHPUnit Test
TYPO3.Files.IncludingFile False:
include() / include_once() / require()
Correct:
require_once()
Must have X X

Classes

Lowercase class keywords

Sniff Description Notice Implemented PHPUnit Test
TYPO3.Classes.LowercaseClassKeywords False:
CLASS test {
    PRIVATE $foo;
    PUBLIC $bar;
    PROTECTED $baz;
}

Correct:
class test {
    private $foo;
    public $bar;
    protected $baz;
}
Should have X X

Self member reference

Sniff Description Notice Implemented PHPUnit Test
Squiz.Classes.SelfMemberReference
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 X

No duplicate class names

Sniff Description Notice Implemented PHPUnit Test
Generic_Sniffs_Classes_DuplicateClassNameSniff Could have X X

Debug

Debug code

Sniff Description Notice Implemented PHPUnit Test
TYPO3.Debug.DebugCode False:
$array = $this->doSomething();
# debug($array);
Correct:
$array = $this->doSomething();
Must have X X

NamingConventions

Valid variable name

Sniff Description Notice Implemented PHPUnit Test
TYPO3.NamingConventions.ValidVariableName False:
$BAD_name
$_GET['foo']
$_POST['bar']
Correct:
$goodName
t3lib_div::_GET('foo')
t3lib_div::_POST('bar')
Must have X X

Constructor name

Sniff Description Notice Implemented PHPUnit Test
Generic.NamingConventions.ConstructorName False:
function ClassName()
Correct:
function __construct()
Must have X X

Valid function name

Sniff Description Notice Implemented PHPUnit Test
TYPO3.NamingConventions.ValidFunctionName False:
function Extended_use() {
    // Some code
}
Correct:
function extendedUse() {
    // Some code
}
Must have X X

Formatting

Disallow multiple statements

Sniff Description Notice Implemented PHPUnit Test
Generic.Formatting.DisallowMultipleStatements False:
$foo = 42; $bar = 23;
Correct:
$foo = 42; 
$bar = 23;
Could have X X

PHP

Character before PHP opening tag

Sniff Description Notice Implemented PHPUnit Test
TYPO3.PHP.CharacterBeforePHPOpeningTag False:

<?php
// Some code
?>
Correct:
<?php
// Some code
?>
Must have X X

Character after PHP closing tag

Sniff Description Notice Implemented PHPUnit Test
TYPO3.PHP.CharacterAfterPHPClosingTag False:
<?php
// Some code
?>

Correct:
<?php
// Some code
?>
Must have X X

Disallow short open tag

Sniff Description Notice Implemented PHPUnit Test
Generic.PHP.DisallowShortOpenTag False:
<?
Correct:
<?php
Must have X X

Non executable code

Sniff Description Notice Implemented PHPUnit Test
Squiz.PHP.NonExecutableCode False:
<?php
function foo() {
    return TRUE;
    echo 'bar';
}
?>
Correct:
<?php
function foo() {
    return TRUE;
}
?>
Could have X X

Disallow multiple PHP tags

Sniff Description Notice Implemented PHPUnit Test
TYPO3.PHP.DisallowMultiplePHPTags False:
<?php
doSomething();
?>
<span>some html content</span>
<?php 
doSomethingMore(); 
?>
Correct:
<?php
doSomething();
echo '<span>some html content</span>';
doSomethingMore(); 
?>
Must have X X

Closing PHP tag

Sniff Description Notice Implemented PHPUnit Test
TYPO3.PHP.ClosingPHPTag False:
// Some code
// End of File
Correct:
// Some code
// End of File
?>
Must have X X

Uppercase constant

Sniff Description Notice Implemented PHPUnit Test
Generic.PHP.UpperCaseConstant False:
false / true / null
Correct:
FALSE / TRUE / NULL
Must have X X

Deprecated functions

Sniff Description Notice Implemented PHPUnit Test
Generic.PHP.DeprecatedFunctions 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 X

Eval

Sniff Description Notice Implemented PHPUnit Test
Squiz.PHP.Eval 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 X

Global keyword

Sniff Description Notice Implemented PHPUnit Test
Squiz.PHP.GlobalKeyword False:
global $BE_USER;
$BE_USER->doSomething();
Correct:
$GLOBALS['BE_USER']->doSomething();
Should have X X

XCLASS

Sniff Description Notice Implemented PHPUnit Test
TYPO3.PHP.XCLASS Correct:
if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['ext/myext/pi1/class.tx_myext_pi1.php']) {
    include_once($TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['ext/myext/pi1/class.tx_myext_pi1.php']);
}
Should have @todo @todo

Whitespace

Disallow space indent

Sniff Code Example Notice Implemented PHPUnit Test
TYPO3.WhiteSpace.DisallowSpaceIndent False:
[space]$code
[tab][space]$code
Right:
[tab]$code
Must have X X

Superfluous whitespace

Sniff Code Example Notice Implemented PHPUnit Test
Squiz.WhiteSpace.SuperfluousWhitespace 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 X X

Scope closing brace

Sniff Description Notice Implemented PHPUnit Test
PEAR.WhiteSpace.ScopeClosingBrace 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 @todo @todo @todo

Assignment arithmetic and comparison space

Sniff Code Example Notice Implemented PHPUnit Test
TYPO3.WhiteSpace.AssignmentArithmeticAndComparisonSpace 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, single if conditions are missing X X

Commas

after commas @todo

Sniff Code Example Notice FLOW3 Implemented PHPUnit Test Hudson Tests
@todo False:
$array = array(1,2,3,4);
Correct:
$array = array(1, 2, 3, 4);
Must have ?

Semicolon spacing

Sniff Code Example Notice Implemented PHPUnit Test
Squiz.WhiteSpace.SemicolonSpacing False;
functionCall() ;
Correct:
functionCall();
Should have X @todo

Whitespace after comment signs / Space after double slash

@todo check this!

Notice: Currently we have two implementations of this sniff. The first is very strict and allow excactly one whitespace, but allows comments like:

/////// 
// Comment
///////

The second sniff checks for the one necessary whitespace but allows multiple whitespaces.

Sniff Code Example Notice Implemented PHPUnit Test
TYPO3.Whitespace.WhitespaceAfterCommentSigns False:
//This is a comment
Correct:
// This is a comment
Must have @todo X
TYPO3.Commenting.SpaceAfterDoubleSlash False:
//This is a example
Correct:
// This is a example
Must have @todo X

Asteriks whitespaces diff

Sniff Code Example Notice Implemented PHPUnit Test
TYPO3.Whitespace.AsteriksWhitespaces 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 X

Logical operator spacing

Sniff Code Example Notice Implemented PHPUnit Test
Squiz.WhiteSpace.LogicalOperatorSpacing False:
if ($foo||$bar && $baz) {}
if ($foo|| $bar&&$baz) {}
if ($foo  ||   $bar   && $baz) {}
Correct:
if ($foo || $bar && $baz) {}
Must have X X

Scope

Member var scope

Sniff Description Notice Implemented PHPUnit Test
Squiz.Scope.MemberVarScope Correct:
class foo {
    private $foo;
    protected $bar;
    public $baz;
} 
Must have X X

Strings

Unnecessary string concat

Sniff Description Notice Implemented PHPUnit Test
Generic.Strings.UnnecessaryStringConcat / TYPO3.Strings.UnnecessaryStringConcat @todo False:
'A string concate' . 'with another string'
Correct:
'Just a singe string'
Must have X X

Double quote usage

Sniff Description Notice Implemented PHPUnit Test
Squiz.Strings.DoubleQuoteUsage False:
echo "This is a string";
echo "This is a string with a Tab \t and a Newline\n";
$content = "Hello $userName";
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";
$content = 'Hello ' . $userName;
Must have X X

Multiline string concatenations are allowed. Line concatenation operator must be at the end of the line.

Sniff Description Notice Implemented PHPUnit Test
@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 X (@todo)
Sniff Description Notice Implemented PHPUnit Test
TYPO3.Strings.ConcatenationSpacingSniff False:
$string = 'Hello'.$there.'. How are'.$you.$going."today $okay";
Correct:
$string = 'Hello' . $there . '. How are' . $you . $going . "today $okay";
Must have X X

Code analysis

The use of each is not allowed in loops. @todo

do

do loops must use extra brackets if assignment happens in the loop:
Sniff Description Notice Implemented PHPUnit Test
@todo False:
do ($fields = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) {
    // Do something
} while (...)
Correct:
do (($fields = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res))) {
    // Do something
} while (...)
Must have

while

while loops must use extra brackets if assignment happens in the loop:
Sniff Description Notice Implemented PHPUnit Test
@todo False:
while ($fields = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) {
    // Do something
}
Correct:
while (($fields = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res))) {
    // Do something
}
Must have

For loop with test function call

for loops must contain only variables inside (no function calls)
Sniff Description Notice Implemented PHPUnit Test
Generic.CodeAnalysis.ForLoopWithTestFunctionCall 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 X X

Unused function parameter

Sniff Description Notice Implemented PHPUnit Test
Generic.CodeAnalysis.UnusedFunctionParameter All function parameter must be used in function body Must have X X

Unconditional If Statement

Sniff Description Notice Implemented PHPUnit Test
Generic.CodeAnalysis.UnconditionalIfStatement False:
class Foo {
    public function close() {
        if (true) {
            echo 'Bar';
        }
    }
}
Correct:
class Foo {
    public function close() {
        echo 'Bar';
    }
}
Must have X X

Unnecessary Final Modifier

Sniff Description Notice Implemented PHPUnit Test
Generic.CodeAnalysis.UnnecessaryFinalModifier False:
final class Foo_Bar {
    public $foobar;
    public final $FOOBAR = 23;
}
Correct:
final class Foo_Bar {
    public $foobar;
    public $FOOBAR = 23;
}
Must have X X

Check if empty statement in the code

Sniff Description Notice Implemented PHPUnit Test
Generic.CodeAnalysis.EmptyStatement False:
 stmt {
     // foo
 }

 stmt (conditions) {
     // foo
 } 
Correct:
 stmt {
     echo '42';
 }

 stmt (conditions) {
     makeFancyStuff();
 }
Could have X X

Functions

Function call argument spacing

Sniff Description Notice Implemented PHPUnit Test
Generic.Functions.FunctionCallArgumentSpacing False:
$example = $this->doSomething($param1,$param2);
Correct:
$example = $this->doSomething($param1, $param2);
Must have X X

Opening function brace Kernighan Ritchie

Sniff Description Notice Implemented PHPUnit Test
Generic.Functions.OpeningFunctionBraceKernighanRitchie False:
if ($foo === 42) 
{
  do_something();
}
Correct:
if ($foo === 42) {
  do_something();
}
Must have X X

Method scope

Sniff Description Notice Implemented PHPUnit Test
Squiz.Scope.MethodScope False:
class foo {
    function bar(){
        // Do something
    }
}
Correct:
class foo {
    private function bar() {
        // Do something
    }
    protected function baz() {
        // Do something
    }
    public function fooBar() {
        // Do something
    }
} 
Must have X X

Always return

Sniff Description Notice Implemented PHPUnit Test
TYPO3.Scope.AlwaysReturn False:
function extendedUse($enabled) {
    if ($enabled) {
        return 'Extended use';
    }
}
Correct:
function extendedUse($enabled) {
    $content = '';
    if ($enabled) {
        $content = 'Extended use';
    }
    return $content;
}
Must have X X

Control structures

Assignments in control signatures are not allowed (@todo)

Sniff Description Notice Implemented PHPUnit Test
@todo False:
if ($debug = FALSE) {
    // Process element here
}
Correct:
$debug = TRUE
if ($debug == FALSE) {
    // Process element here
}
Must have; #9882 @todo @todo

Inline control structure

Sniff Description Notice Implemented PHPUnit Test
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 X

Control signature

Sniff Description Notice Implemented PHPUnit Test
Squiz.ControlStructures.ControlSignature False:
if($foo == $bar) 
{
    $foo = $baz;
}

if($foo == $bar){
    $foo = $baz;
}

if($foo == $bar) { $foo = $baz;
}
Correct:
if ($foo == $bar) {
    $foo = $baz;
}
Must have X X

Disallow elseif construct

Sniff Description Notice Implemented PHPUnit Test
TYPO3.ControlStructures.DisallowElseIfConstruct 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 X X

Tenery operator

Dont use nested "ternary conditional" operators

Sniff Description Notice Implemented PHPUnit Test
@todo False:
$result = ($useComma ? ',' : $useDot ? '.' : ';');
Correct:
$result = ($useComma ? ',' : '.');
Must have @todo @todo

Valid break statements in switches

Sniff Description Notice Implemented PHPUnit Test
TYPO3.ControlStructures.ValidBreakStatementsInSwitches 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 X X

Aligned break statement

Sniff Description Notice Implemented PHPUnit Test
TYPO3.ControlStructures.AlignedBreakStatement False:
switch($foo){
    case 1:
        if($bar == $baz) {
            // Some code
        }
    break;
    default:
}
Correct:
switch($foo){
    case 1:
        if($bar == $baz) {
            // Some code
        }
        break;
    default:
}
Must have X X

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.

Sniff Description Notice Implemented PHPUnit Test
@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 @todo @todo

Valid default statements in switches

@todo check if this sniffs checks if the default statement is the last?

Sniff Description Notice Implemented PHPUnit Test
TYPO3.ControlStructures.ValidDefaultStatementsInSwitches False:
switch ($useType) {
    default:
        $content .= $this->errorUse();
        break;

    case 'basic':
        $content .= $this->basicUse();
        break;
}
Correct:
switch ($useType) {
    case 'extended':
        $content .= $this->extendedUse();
        break;
    default:
        $content .= $this->errorUse();
}
Must have X X

Commenting

Inline comment

Sniff Description Notice Implemented PHPUnit Test
PEAR.Commenting.InlineComment False:
# This is a perl-style comment.
Correct:
// Better use this kind of comments
Must have. See #8634. X X

Double slash comments in new line

Sniff Description Notice Implemented PHPUnit Test
TYPO3.Commenting.DoubleSlashCommentsInNewLine False:
$foo = 42 // The answer of life, the universe and everything
Correct:
// The answer of life, the universe and everything
$foo = 42
Must have X X

Valid comment indent

Sniff Description Notice Implemented PHPUnit Test
TYPO3.Commenting.ValidCommentIndent False:
// The answer of life, the universe and everything
$foo = 42
Correct:
    // The answer of life, the universe and everything
$foo = 42
Must have X X

Valid comment line length

Sniff Description Notice Implemented PHPUnit Test
TYPO3.Commenting.ValidCommentLineLength Comment lines should be kept within a limit of about 80 characters (excluding tabs) Must have. See at #9574. X X

Class comment

Sniff Description Notice Implemented PHPUnit Test
PEAR.Commenting.ClassComment Correct:
/**
 * @param[tab]integer[tab]$foo[tab]Description
 */
protected function foo($foo){

}
Must have; More thoughtsNot sure if the sniff fits our needs exactly. Maybe we have to reimplement it. At a first glance I am not sure if any of the tags is mandatory. Sure for now: Use tabs for separations @todo was ist das? X

Function comment

Sniff Description Notice Implemented PHPUnit Test
PEAR.Commenting.FunctionComment
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 @todo @todo

Class doc comment

Sniff Description Notice Implemented PHPUnit Test
TYPO3.Commenting.ClassDocComment
/**
 * This class provides XYZ plugin implementation.
 *
 * @author John Doe <john.doe@example.com>
 * @author Jane Doe <jane.doe@example.com>
 */
Must have X X

Function doc comment

Sniff Description Notice Implemented PHPUnit Test
TYPO3.Commenting.FunctionDocComment
/**
 * This function provides XYZ plugin implementation.
 *
 * @param $a letter a
 */
Must have @todo @todo

No author annotation in function doc comment

Sniff Description Notice Implemented PHPUnit Test
TYPO3.Commenting.NoAuthorAnnotationInFunctionDocComment 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 X X

@todo / Idea section

  • Activate PEAR.Functions.ValidDefaultValue ?
  • Activate TYPO3.Commenting.FunctionDocComment ?