Rules uses default numeric parameters (some can be changed to match your needs).
TOC:
- 1 Classes
- 2 Commenting
- 3 Control Structures
- 4 Debug
- 5 Namespaces
- 6 Naming
- 7 PHP
- 8 Scope
- 9 WhiteSpace
- Opening brace for the class should be followed by 1 empty line
- Closing brace for the class should be preceded by 1 empty line
Correct
class SomeClass
{
public function run()
{
}
}
Wrong
class SomeClass
{
public function run()
{
}
}
- Non-abstract class that implements interface should be final.
- Except for Doctrine entities, they cannot be final.
Correct
final class SomeClass implements SomeInterface
{
/**
* {@inheritdoc}
*/
public function run()
{
}
}
Wrong
class SomeClass implements SomeInterface
{
/**
* {@inheritdoc}
*/
public function run()
{
}
}
- Block comment should be used instead of one liner
Correct
class SomeClass
{
/**
* @var int
*/
public $count;
}
Wrong
class SomeClass
{
/** @var int */
public $count;
}
- CreateComponent* method should have a doc comment
- CreateComponent* method should have a return tag
- Return tag should contain type
Correct
/**
* @return DisplayComponent
*/
protected function createComponentDisplay()
{
$this->displayComponentFactory->create();
}
Wrong
protected function createComponentDisplay()
{
$this->displayComponentFactory->create();
}
- Property should have docblock comment (except for {@inheritdoc}).
Correct
class SomeClass
{
/**
* @var int
*/
private $someProperty;
}
Wrong
class SomeClass
{
private $someProperty;
}
- Method without parameter typehints should have docblock comment.
Correct
class SomeClass
{
/**
* @param int $values
*/
public function count($values)
{
}
}
class SomeClass
{
public function count(array $values)
{
}
}
Wrong
class SomeClass
{
public function count($values)
{
}
}
- Getters should have @return tag (except for {@inheritdoc}).
Correct
class SomeClass
{
/**
* @return int
*/
public function getResult()
{
// ...
}
}
class SomeClass
{
/**
* {@inheritdoc}
*/
public function getResult()
{
// ...
}
}
Wrong
class SomeClass
{
/**
* This will return something.
*/
public function getResult()
{
}
}
- New class statement should not have empty parentheses
Correct
$someClass = new SomeNamespace\SomeClass;
$someClass = new SomeNamespace\SomeClass($keyHandler);
Wrong
$someClass = new SomeNamespace\SomeClass();
Correct
$suit = 'case';
switch ($suit) {
case 1:
echo 'ok';
break;
default:
echo 'not ok';
break;
}
Wrong
$suit = 'case';
switch ($suit) {
case 1:
echo 'ok';
break;
}
- Yoda condition should not be used; switch expression order
Correct
if ($i === TRUE) {
return;
}
$go = $decide === TRUE ?: FALSE;
Wrong
if (TRUE === $i) {
return;
}
$go = TRUE === $decide ?: FALSE;
- Strong comparison should be used instead of weak one, or commented with its purpose
Correct
if ($i == TRUE) { // intentionally ==, failure proof
return;
}
if ($i !== TRUE) {
return;
}
Wrong
if ($i == TRUE) {
return;
}
- Debug functions should not be left in the code
Wrong
dump('It works');
- There must be 2 empty lines after the namespace declaration or 1 empty line followed by use statement.
Correct
namespace SomeNamespace;
use PHP_CodeSniffer;
class SomeClass
{
}
or
namespace SomeNamespace;
class SomeClass
{
}
Wrong
namespace SomeNamespace;
use SomeNamespace;
class SomeClass
{
}
or
namespace SomeNamespace;
class SomeClass
{
}
- There must be one USE keyword per declaration
- There must be 2 blank lines after the last USE statement
Correct
namespace SomeNamespace;
use Sth;
use SthElse;
class SomeClass
{
}
Wrong
namespace SomeNamespace;
use Sth, SthElse;
class SomeClass
{
}
- Use statements should be in alphabetical order
Correct
use A;
use B;
use C;
Wrong
use C;
use A;
use B;
- Bool operator should be spelled "bool"
Correct
/** @var bool */
public $someProperty;
Wrong
/** @var boolean */
public $someProperty;
- Int operator should be spelled "int"
Correct
/** @var int */
public $someProperty;
Wrong
/** @var integer */
public $someProperty;
- Abstract class should have prefix "Abstract"
Correct
abstract class AbstractClass
{
}
Wrong
abstract class SomeClass
{
}
- Interface should have suffix "Interface"
Correct
interface SomeInterface
{
}
Wrong
interface Some
{
}
- Inheritdoc comment should be spelled "{@inheritdoc}"
Correct
class SomeClass
{
/**
* {@inheritdoc}
*/
public function getSome()
{
}
}
Wrong
class SomeClass
{
/**
* @{inheritDoc}
*/
public function getSome()
{
}
}
- Short array syntax should be used, instead of traditional one.
Correct
private $settings = [];
Wrong
private $settings = array();
- Function should have scope modifier
- Interface function should not have scope modifier
Correct
class SomeClass
{
public function run()
{
}
}
or
interface SomeInterface
{
function run();
}
Wrong
class SomeClass
{
function run()
{
}
}
or
interface SomeInterface
{
public function run();
}
- ConcatOperator (.) should be surrounded by spaces
Correct
$s = 'Ze' . 'n';
Wrong
$s = 'Ze'.'n';
- DocBlock lines should start with space (except first one)
Correct
/**
* Counts feelings.
*/
public function ...
Wrong
/**
* Counts feelings.
*/
public function ...
- Not operator (!) should be surrounded by spaces
Correct
if ( ! $s) {
return $s;
}
Wrong
if (!$s) {
return $s;
}
- Else/elseif/catch/finally statement should be preceded by 1 empty line
Correct
if ($i === 1) {
return $i;
} else {
return $i * 2;
}
Wrong
try (1 === 2) {
return 3;
} catch (2 === 3) {
return 4;
} finally (2 === 3) {
return 4;
}
- Method should have 2 empty lines after itself
Correct
class SomeClass
{
public function run()
{
}
public function go()
{
}
}
Wrong
class SomeClass
{
public function run()
{
}
public function go()
{
}
}
- Between properties and methods should be 2 empty lines
Correct
class SomeClass
{
private $jet;
public function run()
{
}
}
Wrong
class SomeClass
{
private $jet;
public function run()
{
}
}
- Operator should be surrounded by spaces or on new line
- Exceptions: Function's defaults, ?:, +=, &$var and similar
Correct
$result = 5 && 3 || 2;
$output = $tooLonLine
+ $anotherLongLine;
Wrong
$result = 5 &&3|| 2;
$car = 'wheels' +
'engine';