« Previous - Version 60/66 (diff) - Next » - Current version
Andy Grunwald, 2012-07-13 21:59


FLOW3 / TYPO3 Phoenix Sniffs with code examples

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

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

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

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

UpperCase constant

Sniff Description Notice Implemented PHPUnit Test
Generic.PHP.UpperCaseConstant False:
false / true / null
Correct:
FALSE / TRUE / NULL
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

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 UNIX Line endings only (\n) Must have X X

Encoding UTF8

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

Whitespace

Trailing 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

Disallow space indent

Sniff Code Example Notice Implemented PHPUnit Test
TYPO3.WhiteSpace.DisallowSpaceIndent Indention with tabs (\t) ONLY Must have X X

Semicolon spacing

Sniff Code Example Notice Implemented PHPUnit Test
Squiz.WhiteSpace.SemicolonSpacing False;
functionCall() ;
Correct:
functionCall();
Should 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) {}
Correct:
if ($foo || $bar) {}
if ($foo || $bar && $baz) {}
Must have X X

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 X X

Asteriks whitespaces

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

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 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...
Correct:
// The answer of life, the universe...
$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

Space after double slash

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

Control structures

Inline control structure

Sniff Description Notice Implemented PHPUnit Test
Generic.ControlStructures.InlineControlStructure False:
if($foo == $bar)
    $foo = $baz;
$this->doFunction();
Correct:
if($foo == $bar) {
    $foo = $baz;
}
$this->doFunction();
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

Strings

Unnecessary string concat

Sniff Description Notice Implemented PHPUnit Test
Generic.Strings.UnnecessaryStringConcat 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";
$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

Scope

Member var scope

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

Method scope

Sniff Description Notice Implemented PHPUnit Test
Squiz.Scope.MethodScope False:
class foo {
    function foo() {}

    function bar() {}

    function baz() {}
}
Correct:
class foo {
    public function foo() {}

    private function bar() {}

    protected function baz() {}
}
Must have X X

Naming conventions

Constructor name

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

UpperCase constant name

Sniff Description Notice Implemented PHPUnit Test
Generic.NamingConventions.UpperCaseConstantName False:
class foo {
    const foo = 0;
    const foo_bar = 1;
    const fooBar = 2;
}

Correct:
class foo {
    const FOO = 0;
    const FOO_BAR = 1;
    const FOOBAR = 2;
}
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

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

Functions

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

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

Code Analysis

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

@todo section

File names

File name must represent class name

Sniff Description Notice FLOW3 Implemented PHPUnit Test Hudson Tests
@todo False:
File name:
SomeThing.php

Class name:
class ClassName

Correct:
File name:
ClassName.php

Class name:
class ClassName
Must have F3 only

Namespace

Namespace has to represent directory structure in a certain way

Sniff Description Notice FLOW3 Implemented PHPUnit Test Hudson Tests
@todo False:
Directory stucture:
FLOW3/Classes/Cache/CacheManagerController.php

Namespace:
namespace F3\FLOW3\Cache\Controller

Correct:
Directory stucture:
FLOW3/Classes/Cache/Controller/CacheManagerController.php

Namespace:
namespace F3\FLOW3\Cache\Controller
Must have, but we should only check below package name ("FLOW3" in the example), because the package name can't bee seen in all cases. Tests represent the namespace and folder structure of the tested class, but have a "Tests/Unit" instead of "Classes" in the directory stucture F3 only

Tests

Class and filename of (unit-)tests in the "Tests" folder have to end with "Test"

Sniff Description Notice FLOW3 Implemented PHPUnit Test Hudson Tests
@todo False:
File name:
QueryResultTest.php

Class name:
QueryResult

Correct:
File name:
QueryResultTest.php

Class name:
QueryResultTest
F3 only

Namespace may not contain "Test" in Test classes

Sniff Description Notice FLOW3 Implemented PHPUnit Test Hudson Tests
@todo False:
Directory stucture:
FLOW3/Tests/Unit/Persistence/QueryResultTest.php

Namespace:
namespace F3\FLOW3\Tests\Unit\Persistence

Correct:
Directory stucture:
FLOW3/Tests/Unit/Persistence/QueryResultTest.php

Namespace:
namespace F3\FLOW3\Persistence;
Must have F3 only

FLOW3: Inline control structures are not allowed, except Exception Throwing

Sniff Description Notice FLOW Implemented PHPUnit Test Hudson Tests
@todo False:
foreach($SOBE->include_once as $INC_FILE)    include_once($INC_FILE);
Correct:
foreach($SOBE->include_once as $INC_FILE) {
    include_once($INC_FILE);
}

if (allGoesWrong() === TRUE) throw new \Exception('Hey, all went wrong!', 123);
Must have F3 only