FLOW3 TYPO3 Phoenix Sniffs with code examples
Version 43 (Andy Grunwald, 2012-04-14 01:05) → Version 44/66 (Andy Grunwald, 2012-04-14 01:05)
h1. FLOW3 / TYPO3 Phoenix Sniffs with code examples
{{toc}}
h2. PHP
h3. Character before PHP opening tag
|_.Sniff|_.Description|_.Notice|_.Implemented|_.PHPUnit Test|
|TYPO3.PHP.CharacterBeforePHPOpeningTag|False: <pre>
<?php
// Some code
?>
</pre>Correct: <pre>
<?php
// Some code
?>
</pre>|Must have|X|X|
h3. Character After PHP closing tag
|_.Sniff|_.Description|_.Notice|_.Implemented|_.PHPUnit Test|
|TYPO3.PHP.CharacterAfterPHPClosingTag|False: <pre>
<?php
// Some code
?>
</pre>Correct: <pre>
<?php
// Some code
?>
</pre>|Must have|X|X|
h3. Disallow Short Open Tag
|_.Sniff|_.Description|_.Notice|_.Implemented|_.PHPUnit Test|
|Generic.PHP.DisallowShortOpenTag|False: <pre><?
</pre>Correct: <pre><?php
</pre>|Must have|X|X|
h3. Disallow multiple PHP tags
|_.Sniff|_.Description|_.Notice|_.Implemented|_.PHPUnit Test|
|TYPO3.PHP.DisallowMultiplePHPTags|False: <pre><?php
doSomething();
?>
<span>some html content</span>
<?php
doSomethingMore();
?></pre>Correct:<pre><?php
doSomething();
echo '<span>some html content</span>';
doSomethingMore();
?></pre>|Must have|X|X|
h3. Closing PHP Tag
|_.Sniff|_.Description|_.Notice|_.Implemented|_.PHPUnit Test|_.Hudson Tests|
|TYPO3.PHP.ClosingPHPTag|False: <pre>// Some code
// End of File
</pre>Correct: <pre>// Some code
// End of File
?>
</pre>|Must have|X|X|
h3. Non executable code
|_.Sniff|_.Description|_.Notice|_.Implemented|_.PHPUnit Test|
|Squiz.PHP.NonExecutableCode|False: <pre><?php
function foo() {
return TRUE;
echo 'bar';
}
?></pre>Correct: <pre><?php
function foo() {
return TRUE;
}
?></pre>|Could have|X|X|
h3. Deprecated functions
|_.Sniff|_.Description|_.Notice|_.Implemented|_.PHPUnit Test|
|Generic.PHP.DeprecatedFunctions|False: <pre>ereg() / call_user_method()</pre>Correct: <pre>preg_match() / call_user_func()</pre>|Nice to have; Have a look at http://php.net/manual/de/migration53.deprecated.php|X|X|
h3. Eval
|_.Sniff|_.Description|_.Notice|_.Implemented|_.PHPUnit Test|
|Squiz.PHP.Eval|False: <pre>$color = 'green';
$str = 'This is a $color tree.';
eval ("\$str = \"$str\";");</pre>Correct: <pre>$color = 'green';
$str = 'This is a ' . $color . ' tree.';</pre>|Should have if eval() is forbidden|X|X|
h3. Global keyword
|_.Sniff|_.Description|_.Notice|_.Implemented|_.PHPUnit Test|
|Squiz.PHP.GlobalKeyword|False: <pre>global $BE_USER;
$BE_USER->doSomething();</pre>Correct: <pre>$GLOBALS['BE_USER']->doSomething();</pre>|Should have|X|X|
h3. UpperCase constant
|_.Sniff|_.Description|_.Notice|_.Implemented|_.PHPUnit Test|
|Generic.PHP.UpperCaseConstant|False: <pre>false / true / null
</pre>Correct: <pre>FALSE / TRUE / NULL
</pre>|Must have|X|X|
h2. Files
h3. One Class per file
|_.Sniff|_.Description|_.Notice|_.Implemented|_.PHPUnit Test|
|TYPO3.Files.OneClassPerFile|False: <pre><?php
class foo {
// Code
}
class bar {
// Code
}
?></pre>Correct:<pre><?php
class foo {
// Code
}
?></pre>|Must have|X|X|
h3. One interface per file
|_.Sniff|_.Description|_.Notice|_.Implemented|_.PHPUnit Test|
|TYPO3.Files.OneInterfacePerFile|False: <pre><?php
interface foo {
// Code
}
interface bar {
// Code
}
?></pre>Correct:<pre><?php
interface foo {
// Code
}
?></pre>|Must have|X|X|
h3. Line endings
|_.Sniff|_.Description|_.Notice|_.Implemented|_.PHPUnit Test|
|Generic.Files.LineEndings|UNIX Line endings only (\n)|Must have|X|X|
h3. Encoding UTF8
|_.Sniff|_.Description|_.Notice|_.Implemented|_.PHPUnit Test|
|TYPO3.Files.EncodingUtf8|Files must be encoded in UTF-8|Must have|X||
h2. Whitespace
h3. Trailing whitespace
|_.Sniff|_.Code Example|_.Notice|_.Implemented|_.PHPUnit Test|
|Squiz.WhiteSpace.SuperfluousWhitespace|False: <pre>
// In the next line there is whitespace after ...
// ... the word "code". Mark it to see it.
// Some code
</pre>Correct:<pre>
// In the next line there is _NO_ whitespace after ...
// ... the word "code". Mark it to see it.
// Some code
</pre>|Must have|X|X|
h3. Disallow space indent
|_.Sniff|_.Code Example|_.Notice|_.Implemented|_.PHPUnit Test|
|TYPO3.WhiteSpace.DisallowSpaceIndent|Indention with tabs (\t) ONLY|Must have|X|X|
h3. Semicolon spacing
|_.Sniff|_.Code Example|_.Notice|_.Implemented|_.PHPUnit Test|
|Squiz.WhiteSpace.SemicolonSpacing|False;<pre>functionCall() ;</pre>Correct:<pre>functionCall();</pre>|Should have|X|X|
h3. Logical operator spacing
|_.Sniff|_.Code Example|_.Notice|_.Implemented|_.PHPUnit Test|
|Squiz.WhiteSpace.LogicalOperatorSpacing|False: <pre>
if ($foo||$bar && $baz) {}
if ($foo|| $bar&&$baz) {}
</pre>Correct: <pre>
if ($foo || $bar) {}
if ($foo || $bar && $baz) {}
</pre>|Must have|X|X|
h3. Assignment arithmetic and comparison space
|_.Sniff|_.Code Example|_.Notice|_.Implemented|_.PHPUnit Test|
|TYPO3.WhiteSpace.AssignmentArithmeticAndComparisonSpace|False: <pre>$foo=$bar;
$foo=($bar+$baz)*5;
$foo=($bar==$baz?1:2);</pre>Correct: <pre>$foo = $bar;
$foo = ($bar + $baz) * 5;
$foo = ($bar == $baz ? 1 : 2);</pre>|Must have|X|X|
h3. Asteriks whitespaces
|_.Sniff|_.Code Example|_.Notice|_.Implemented|_.PHPUnit Test|
|TYPO3.Whitespace.AsteriksWhitespaces|False: <pre>/**
*This class provides XYZ plugin implementation.
*
*@author John Doe <john.doe@example.com>
*@author Jane Doe <jane.doe@example.com>
*/</pre>Correct: <pre>/**
* This class provides XYZ plugin implementation.
*
* @author John Doe <john.doe@example.com>
* @author Jane Doe <jane.doe@example.com>
*/</pre>|Must have|X|X|
h2. Comments
h3. Inline comment
|_.Sniff|_.Description|_.Notice|_.Implemented|_.PHPUnit Test|
|PEAR.Commenting.InlineComment|False: <pre># This is a perl-style comment.
</pre>Correct: <pre>// Better use this kind of comments
</pre>|Must have|X|X|
h3. Double slash comments in new line
|_.Sniff|_.Description|_.Notice|_.Implemented|_.PHPUnit Test|
|TYPO3.Commenting.DoubleSlashCommentsInNewLine|False: <pre>$foo = 42 // The answer of life, the universe and everything
</pre>Correct: <pre>// The answer of life, the universe and everything
$foo = 42
</pre>|Must have|X|X|
h3. Valid comment indent
|_.Sniff|_.Description|_.Notice|_.Implemented|_.PHPUnit Test|
|TYPO3.Commenting.ValidCommentIndent|False: <pre>// The answer of life, the universe and everything
$foo = 42
</pre>Correct: <pre> // The answer of life, the universe and everything
$foo = 42
</pre>|Must have|X|X|
h3. Space after double slash
|_.Sniff|_.Description|_.Notice|_.Implemented|_.PHPUnit Test|
|TYPO3.Commenting.SpaceAfterDoubleSlash|False: <pre> //The answer of life, the universe and everything</pre>Correct: <pre> // The answer of life, the universe and everything
</pre>|Must have|X|X|
h2. Control structures
h3. Inline control structure
|_.Sniff|_.Description|_.Notice|_.Implemented|_.PHPUnit Test|
|Generic.ControlStructures.InlineControlStructure|False: <pre>
if($foo == $bar)
$foo = $baz;
$this->doFunction();
</pre>Correct: <pre>
if($foo == $bar) {
$foo = $baz;
}
$this->doFunction();
</pre>|Must have|X|X|
h3. Opening braces must be on the same line
|_.Sniff|_.Description|_.Notice|_.Implemented|_.PHPUnit Test|
|Squiz.ControlStructures.ControlSignature|False: <pre>
if($foo == $bar)
{
$foo = $baz;
}
</pre>Correct: <pre>
if($foo == $bar) {
$foo = $baz;
}
</pre>|Must have|X|X|
h3. Space before the opening curly brace
|_.Sniff|_.Description|_.Notice|_.Implemented|_.PHPUnit Test|
|Squiz.ControlStructures.ControlSignature|False: <pre>
if($foo == $bar){
$foo = $baz;
}
</pre>Correct: <pre>
if($foo == $bar) {
$foo = $baz;
}
</pre>|Must have|X|X|
h3. Opening brace is followed by a new line
|_.Sniff|_.Description|_.Notice|_.Implemented|_.PHPUnit Test|
|Squiz.ControlStructures.ControlSignature|False: <pre>
if($foo == $bar) { $foo = $baz;
}
</pre>Correct: <pre>
if($foo == $bar) {
$foo = $baz;
}
</pre>|Must have|X|X|
h3. Disallow elseif construct
|_.Sniff|_.Description|_.Notice|_.Implemented|_.PHPUnit Test|
|TYPO3.ControlStructures.DisallowElseIfConstruct|False: <pre>
if ($this->processSubmission) {
// Process submission here
} else if ($this->internalError) {
// Handle internal error
}
</pre>Correct: <pre>
if ($this->processSubmission) {
// Process submission here
} elseif ($this->internalError) {
// Handle internal error
} else {
// Something else here
}
</pre>|Must have|X|X|
h2. Strings
h3. Unnecessary string concat
|_.Sniff|_.Description|_.Notice|_.Implemented|_.PHPUnit Test|
|Generic.Strings.UnnecessaryStringConcat|False: <pre>'A string concate' . 'with another string'
</pre>Correct: <pre>'Just a singe string'
</pre>|Must have|X|X|
h3. Double quote usage
|_.Sniff|_.Description|_.Notice|_.Implemented|_.PHPUnit Test|
|Squiz.Strings.DoubleQuoteUsage|False: <pre>echo "This is a string";
$content = "Hello $userName";
</pre>Correct: <pre>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;</pre>|Must have|X|X|
h3. Variables must not be embedded into strings
|_.Sniff|_.Description|_.Notice|_.Implemented|_.PHPUnit Test|
|Squiz.Strings.DoubleQuoteUsage|False: <pre>$content = "Hello $userName";</pre>Correct: <pre>$content = 'Hello ' . $userName;</pre>|Must have|X|X|
h2. Scope
h3. Member var scope
|_.Sniff|_.Description|_.Notice|_.Implemented|_.PHPUnit Test|
|Squiz.Scope.MemberVarScope|False: <pre>class foo {
var $foo;
}</pre>Correct: <pre>class foo {
private $foo;
protected $bar;
public $baz;
}
</pre>|Must have|X|X|
h3. Method scope
|_.Sniff|_.Description|_.Notice|_.Implemented|_.PHPUnit Test|
|Squiz.Scope.MethodScope|False: <pre>class foo {
function foo() {}
function bar() {}
function baz() {}
}</pre>Correct: <pre>class foo {
public function foo() {}
private function bar() {}
protected function baz() {}
}
</pre>|Must have|X|X|
h2. Naming conventions
h3. Constructor name
|_.Sniff|_.Description|_.Notice|_.Implemented|_.PHPUnit Test|
|Generic.NamingConventions.ConstructorName|False: <pre>function ClassName()</pre> Correct: <pre>function __construct()</pre>|Must have|X|X|
h3. UpperCase constant name
|_.Sniff|_.Description|_.Notice|_.Implemented|_.PHPUnit Test|
|Generic.NamingConventions.UpperCaseConstantName|False:
<pre>class foo {
const foo = 0;
const foo_bar = 1;
const fooBar = 2;
}</pre>
Correct: <pre>class foo {
const FOO = 0;
const FOO_BAR = 1;
const FOOBAR = 2;
}</pre>|Must have|X|X|
h2. Classes
h3. Lowercase class keywords
|_.Sniff|_.Description|_.Notice|_.Implemented|_.PHPUnit Test|
|TYPO3.Classes.LowercaseClassKeywords|False:<pre>CLASS test {
PRIVATE $foo;
PUBLIC $bar;
PROTECTED $baz;
}</pre>
Correct:<pre>class test {
private $foo;
public $bar;
protected $baz;
}</pre>|Should have|X|X|
h3. Self member reference
|_.Sniff|_.Description|_.Notice|_.Implemented|_.PHPUnit Test|
|Squiz.Classes.SelfMemberReference|<pre>Verifies that:
- self:: is used instead of Self::
- self:: is used for local static member reference
- self:: is used instead of self ::</pre>|Should have|X|X|
h2. Arrays
h3. Array bracket spacing
|_.Sniff|_.Description|_.Notice|_.Implemented|_.PHPUnit Test|
|Squiz.Arrays.ArrayBracketSpacing|False: <pre>$array[ 'Hello'];
$array ['Hello'];</pre>Correct: <pre>$array['Hello'];
</pre>|Must have|X|X|
h2. Functions
h3. Opening function brace Kernighan Ritchie
|_.Sniff|_.Description|_.Notice|_.Implemented|_.PHPUnit Test|
|Generic.Functions.OpeningFunctionBraceKernighanRitchie|False: <pre>if ($foo === 42)
{
do_something();
}
</pre>Correct: <pre>if ($foo === 42) {
do_something();
}
</pre>|Must have|X|X|
h3. Function call argument spacing
|_.Sniff|_.Description|_.Notice|_.Implemented|_.PHPUnit Test|
|Generic.Functions.FunctionCallArgumentSpacing|False: <pre>
$example = $this->doSomething($param1,$param2);</pre>Correct: <pre>
$example = $this->doSomething($param1, $param2);</pre>|Must have|X|X|