Skip to content

Latest commit

 

History

History
915 lines (584 loc) · 8.21 KB

zenify-rules-overview.md

File metadata and controls

915 lines (584 loc) · 8.21 KB

Zenify Rules Overview

Rules uses default numeric parameters (some can be changed to match your needs).

TOC:


1 Classes

ClassDeclarationSniff

  • 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()
	{

	}
}

FinalInterfaceSniff

  • 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()
	{

	}

}

2 Commenting

BlockPropertyCommentSniff

  • Block comment should be used instead of one liner

Correct

class SomeClass
{

	/**
	 * @var int
	 */
	public $count;

}

Wrong

class SomeClass
{

	/** @var int */
	public $count;

}

ComponentFactoryCommentSniff

  • 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();
}

VarPropertyCommentSniff

  • Property should have docblock comment (except for {@inheritdoc}).

Correct

class SomeClass
{

	/**
	 * @var int
	 */
	private $someProperty;

}

Wrong

class SomeClass
{

	private $someProperty;

}

MethodCommentSniff

  • 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)
	{
	}

}

MethodCommentReturnTagSniff

  • 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()
	{
	}

}

3 Control Structures

NewClassSniff

  • New class statement should not have empty parentheses

Correct

$someClass = new SomeNamespace\SomeClass;
$someClass = new SomeNamespace\SomeClass($keyHandler);

Wrong

$someClass = new SomeNamespace\SomeClass();

SwitchDeclarationSniff

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;
}

YodaConditionSniff

  • 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;

WeakTypeComparisonWithExplanationSniff

  • 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;
}

4 Debug

DebugFunctionCallSniff

  • Debug functions should not be left in the code

Wrong

dump('It works');

5 Namespaces

NamespaceDeclarationSniff

  • 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
{

}

UseDeclarationSniff

  • 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
{

}

UseInAlphabeticalOrderSniff

  • Use statements should be in alphabetical order

Correct

use A;
use B;
use C;

Wrong

use C;
use A;
use B;

6 Naming

BoolSniff

  • Bool operator should be spelled "bool"

Correct

/** @var bool */
public $someProperty;

Wrong

/** @var boolean */
public $someProperty;

IntSniff

  • Int operator should be spelled "int"

Correct

/** @var int */
public $someProperty;

Wrong

/** @var integer */
public $someProperty;

AbstractClassNameSniff

  • Abstract class should have prefix "Abstract"

Correct

abstract class AbstractClass
{

}

Wrong

abstract class SomeClass
{

}

InterfaceNameSniff

  • Interface should have suffix "Interface"

Correct

interface SomeInterface
{

}

Wrong

interface Some
{

}

InheritDocSniff

  • Inheritdoc comment should be spelled "{@inheritdoc}"

Correct

class SomeClass
{

	/**
	 * {@inheritdoc}
	 */
	public function getSome()
	{
	}

}

Wrong

class SomeClass
{

	/**
	 * @{inheritDoc}
	 */
	public function getSome()
	{
	}

}

7 PHP

ShortArraySyntaxSniff

  • Short array syntax should be used, instead of traditional one.

Correct

private $settings = [];

Wrong

private $settings = array();

8 Scope

MethodScopeSniff

  • 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();

}

9 WhiteSpace

ConcatOperatorSniff

  • ConcatOperator (.) should be surrounded by spaces

Correct

$s = 'Ze' . 'n';

Wrong

$s = 'Ze'.'n';

DocBlockSniff

  • DocBlock lines should start with space (except first one)

Correct

/**
 * Counts feelings.
 */
public function ...

Wrong

/**
* Counts feelings.
*/
public function ...

ExclamationMarkSniff

  • Not operator (!) should be surrounded by spaces

Correct

if ( ! $s) {
	return $s;
}

Wrong

if (!$s) {
	return $s;
}

IfElseTryCatchFinallySniff

  • 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;
}

InBetweenMethodSpacingSniff

  • 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()
	{
	}

}

PropertiesMethodsMutualSpacingSniff

  • 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()
	{
	}

}

OperatorSpacingSniff

  • 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';