2017 © Pedro Peláez
 

library coding-standard

Set of rules for PHP_CodeSniffer preferring tabs and based on Nette coding standard.

image

zenify/coding-standard

Set of rules for PHP_CodeSniffer preferring tabs and based on Nette coding standard.

  • PHP
  • 14 Dependents
  • 0 Suggesters
  • 4 Forks
  • 0 Open issues
  • 58 Versions
  • 0 % Grown

The README.md

Coding Standard

Build Status Quality Score Code Coverage Downloads Latest stable, (*1)

Set of rules for PHP_CodeSniffer preferring tabs and based on Nette coding standard., (*2)

Check rules overview for examples., (*3)

Install

$ composer require zenify/coding-standard --dev

Usage

Run with Php_CodeSniffer:, (*4)

vendor/bin/phpcs src --standard=vendor/zenify/coding-standard/src/ZenifyCodingStandard/ruleset.xml -p

That's all!, (*5)

How to be both Lazy and Safe

Composer hook

In case you don't want to use Php_CodeSniffer manually for every change in the code you make, you can add pre-commit hook via composer.json:, (*6)

"scripts": {
    "post-install-cmd": [
        "Zenify\\CodingStandard\\Composer\\ScriptHandler::addPhpCsToPreCommitHook"
    ],
    "post-update-cmd": [
        "Zenify\\CodingStandard\\Composer\\ScriptHandler::addPhpCsToPreCommitHook"
    ]
}

Every time you try to commit, Php_CodeSniffer will run on changed .php files only., (*7)

This is much faster than checking whole project, running manually or wait for CI., (*8)

Pretty cool, huh?, (*9)

Testing

composer check-cs
vendor/bin/phpunit

Contributing

Rules are simple:, (*10)

  • new feature needs tests
  • all tests must pass
  • 1 feature per PR

We would be happy to merge your feature then!, (*11)


Rules Overview

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

TOC:, (*13)


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, (*14)

class SomeClass
{

    public function run()
    {

    }

}

Wrong, (*15)

class SomeClass
{
    public function run()
    {

    }
}

FinalInterfaceSniff

  • Non-abstract class that implements interface should be final.
  • Except for Doctrine entities, they cannot be final.

Correct, (*16)

final class SomeClass implements SomeInterface
{

    public function run()
    {

    }

}

Wrong, (*17)

class SomeClass implements SomeInterface
{

    public function run()
    {

    }

}

2 Commenting

BlockPropertyCommentSniff

  • Block comment should be used instead of one liner

Correct, (*18)

class SomeClass
{

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

}

Wrong, (*19)

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, (*20)

/**
 * @return DisplayComponent
 */
protected function createComponentDisplay()
{
    $this->displayComponentFactory->create();
}

Wrong, (*21)

protected function createComponentDisplay()
{
    $this->displayComponentFactory->create();
}

VarPropertyCommentSniff

  • Property should have docblock comment.

Correct, (*22)

class SomeClass
{

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

}

Wrong, (*23)

class SomeClass
{

    private $someProperty;

}

MethodCommentSniff

  • Method without parameter typehints should have docblock comment.

Correct, (*24)

class SomeClass
{

    /**
     * @param int $values
     */
    public function count($values)
    {
    }

}
class SomeClass
{

    public function count(array $values)
    {
    }

}

Wrong, (*25)

class SomeClass
{

    public function count($values)
    {
    }

}

MethodCommentReturnTagSniff

  • Getters should have @return tag or return type.

Correct, (*26)

class SomeClass
{

    /**
     * @return int
     */
    public function getResult()
    {
        // ...
    }

}

Wrong, (*27)

class SomeClass
{

    /**
     * This will return something.
     */
    public function getResult()
    {
    }

}

3 Control Structures

NewClassSniff

  • New class statement should not have empty parentheses

Correct, (*28)

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

Wrong, (*29)

$someClass = new SomeNamespace\SomeClass();

SwitchDeclarationSniff

Correct, (*30)

$suit = 'case';

switch ($suit) {
    case 1:
        echo 'ok';
        break;
    default:
        echo 'not ok';
        break;
}

Wrong, (*31)

$suit = 'case';

switch ($suit) {
case 1:
    echo 'ok';
    break;
}

YodaConditionSniff

  • Yoda condition should not be used; switch expression order

Correct, (*32)

if ($i === TRUE) {
    return;
}

$go = $decide === TRUE ?: FALSE;

Wrong, (*33)

if (TRUE === $i) {
    return;
}

$go = TRUE === $decide ?: FALSE;

4 Debug

DebugFunctionCallSniff

  • Debug functions should not be left in the code

Wrong, (*34)

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, (*35)

namespace SomeNamespace;

use PHP_CodeSniffer;


class SomeClass
{

}

or, (*36)

namespace SomeNamespace;


class SomeClass
{

}

Wrong, (*37)

namespace SomeNamespace;


use SomeNamespace;


class SomeClass
{

}

or, (*38)

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, (*39)

namespace SomeNamespace;

use Sth;
use SthElse;


class SomeClass
{

}

Wrong, (*40)

namespace SomeNamespace;

use Sth, SthElse;

class SomeClass
{

}

6 Naming

AbstractClassNameSniff

  • Abstract class should have prefix "Abstract"

Correct, (*41)

abstract class AbstractClass
{

}

Wrong, (*42)

abstract class SomeClass
{

}

InterfaceNameSniff

  • Interface should have suffix "Interface"

Correct, (*43)

interface SomeInterface
{

}

Wrong, (*44)

interface Some
{

}

7 WhiteSpace

DocBlockSniff

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

Correct, (*45)

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

Wrong, (*46)

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

ExclamationMarkSniff

  • Not operator (!) should be surrounded by spaces

Correct, (*47)

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

Wrong, (*48)

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

IfElseTryCatchFinallySniff

  • Else/elseif/catch/finally statement should be preceded by 1 empty line

Correct, (*49)

if ($i === 1) {
    return $i;

} else {
    return $i * 2;
}

Wrong, (*50)

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, (*51)

class SomeClass
{

    public function run()
    {
    }


    public function go()
    {
    }

}

Wrong, (*52)

class SomeClass
{

    public function run()
    {
    }

    public function go()
    {
    }

}

PropertiesMethodsMutualSpacingSniff

  • Between properties and methods should be 2 empty lines

Correct, (*53)

class SomeClass
{

    private $jet;


    public function run()
    {
    }

}

Wrong, (*54)

class SomeClass
{

    private $jet;

    public function run()
    {
    }

}

The Versions

30/12 2016

dev-master

9999999-dev

Set of rules for PHP_CodeSniffer preferring tabs and based on Nette coding standard.

  Sources   Download

MIT

The Requires

 

The Development Requires

30/12 2016

v4.1

4.1.0.0

Set of rules for PHP_CodeSniffer preferring tabs and based on Nette coding standard.

  Sources   Download

MIT

The Requires

 

The Development Requires

30/12 2016

v4.2

4.2.0.0

Set of rules for PHP_CodeSniffer preferring tabs and based on Nette coding standard.

  Sources   Download

MIT

The Requires

 

The Development Requires

01/11 2016

v4.0.1

4.0.1.0

Set of rules for PHP_CodeSniffer preferring tabs and based on Nette coding standard.

  Sources   Download

MIT

The Requires

 

The Development Requires

01/11 2016

v4.0.0

4.0.0.0

Set of rules for PHP_CodeSniffer preferring tabs and based on Nette coding standard.

  Sources   Download

MIT

The Requires

 

The Development Requires

30/11 2015

v3.3.4

3.3.4.0

Set of rules for PHP_CodeSniffer preferring tabs and based on Nette coding standard.

  Sources   Download

MIT

The Requires

 

27/10 2015

v3.3.3

3.3.3.0

Set of rules for PHP_CodeSniffer preferring tabs and based on Nette coding standard.

  Sources   Download

MIT

The Requires

 

27/10 2015

v3.3.2

3.3.2.0

Set of rules for PHP_CodeSniffer preferring tabs and based on Nette coding standard.

  Sources   Download

MIT

The Requires

 

26/10 2015

v3.3.1

3.3.1.0

Set of rules for PHP_CodeSniffer preferring tabs and based on Nette coding standard.

  Sources   Download

MIT

The Requires

 

08/10 2015

v3.3.0

3.3.0.0

Set of rules for PHP_CodeSniffer preferring tabs and based on Nette coding standard.

  Sources   Download

MIT

The Requires

 

07/10 2015

v3.2.2

3.2.2.0

Set of rules for PHP_CodeSniffer preferring tabs and based on Nette coding standard.

  Sources   Download

MIT

The Requires

 

28/09 2015

v3.2.1

3.2.1.0

Set of rules for PHP_CodeSniffer preferring tabs and based on Nette coding standard.

  Sources   Download

MIT

The Requires

 

16/09 2015

v3.2.0

3.2.0.0

Set of rules for PHP_CodeSniffer preferring tabs and based on Nette coding standard.

  Sources   Download

MIT

The Requires

 

15/06 2015

v3.1.1

3.1.1.0

Set of rules for PHP_CodeSniffer preferring tabs and based on Nette coding standard.

  Sources   Download

MIT

The Requires

 

21/02 2015

v3.1.0

3.1.0.0

Set of rules for PHP_CodeSniffer preferring tabs and based on Nette coding standard.

  Sources   Download

MIT

The Requires

 

18/02 2015

v3.0.15

3.0.15.0

Set of rules for PHP_CodeSniffer preferring tabs and based on Nette coding standard.

  Sources   Download

MIT

The Requires

 

03/02 2015

v3.0.14

3.0.14.0

Set of rules for PHP_CodeSniffer preferring tabs and based on Nette coding standard.

  Sources   Download

MIT

The Requires

 

03/02 2015

v3.0.13

3.0.13.0

Set of rules for PHP_CodeSniffer preferring tabs and based on Nette coding standard.

  Sources   Download

MIT

The Requires

 

02/02 2015

v3.0.12

3.0.12.0

Set of rules for PHP_CodeSniffer preferring tabs and based on Nette coding standard.

  Sources   Download

MIT

The Requires

 

02/02 2015

v3.0.11

3.0.11.0

Set of rules for PHP_CodeSniffer preferring tabs and based on Nette coding standard.

  Sources   Download

MIT

The Requires

 

23/01 2015

v3.0.10

3.0.10.0

Set of rules for PHP_CodeSniffer preferring tabs and based on Nette coding standard.

  Sources   Download

MIT

The Requires

 

The Development Requires

25/12 2014

v3.0.9

3.0.9.0

Set of rules for PHP_CodeSniffer preferring tabs and based on Nette coding standard.

  Sources   Download

MIT

The Requires

 

The Development Requires

24/12 2014

v3.0.8

3.0.8.0

Set of rules for PHP_CodeSniffer preferring tabs and based on Nette coding standard.

  Sources   Download

MIT

The Requires

 

The Development Requires

24/12 2014

v3.0.7

3.0.7.0

Set of rules for PHP_CodeSniffer preferring tabs and based on Nette coding standard.

  Sources   Download

MIT

The Requires

 

The Development Requires

24/12 2014

v3.0.6

3.0.6.0

Set of rules for PHP_CodeSniffer preferring tabs and based on Nette coding standard.

  Sources   Download

MIT

The Requires

 

The Development Requires

16/12 2014

v3.0.5

3.0.5.0

Set of rules for PHP_CodeSniffer based on PGS-2 (http://php-fg.com) and Nette CS.

  Sources   Download

MIT

The Requires

 

The Development Requires

15/12 2014

v3.0.4

3.0.4.0

Set of rules for PHP_CodeSniffer based on PGS-2 (http://php-fg.com) and Nette CS.

  Sources   Download

MIT

The Requires

 

The Development Requires

11/12 2014

v3.0.3

3.0.3.0

Set of rules for PHP_CodeSniffer based on PGS-2 (http://php-fg.com) and Nette CS.

  Sources   Download

MIT

The Requires

 

The Development Requires

11/12 2014

v3.0.1

3.0.1.0

Set of rules for PHP_CodeSniffer based on PGS-2 (http://php-fg.com) and Nette CS.

  Sources   Download

MIT

The Requires

 

The Development Requires

11/12 2014

v3.0.2

3.0.2.0

Set of rules for PHP_CodeSniffer based on PGS-2 (http://php-fg.com) and Nette CS.

  Sources   Download

MIT

The Requires

 

The Development Requires

10/12 2014

v3.0.0

3.0.0.0

Set of rules for PHP_CodeSniffer based on PGS-2 (http://php-fg.com) and Nette CS.

  Sources   Download

MIT

The Requires

 

The Development Requires

08/12 2014

v2.1.4

2.1.4.0

Set of rules for PHP_CodeSniffer based on PGS-2 (http://php-fg.com) and Nette CS.

  Sources   Download

MIT

The Requires

 

The Development Requires

08/12 2014

v2.1.3

2.1.3.0

Set of rules for PHP_CodeSniffer based on PGS-2 (http://php-fg.com) and Nette CS.

  Sources   Download

MIT

The Requires

 

The Development Requires

07/12 2014

v2.1.2

2.1.2.0

Set of rules for PHP_CodeSniffer based on PGS-2 (http://php-fg.com) and Nette CS.

  Sources   Download

MIT

The Requires

 

The Development Requires

07/12 2014

v2.1.1

2.1.1.0

Set of rules for PHP_CodeSniffer based on PGS-2 (http://php-fg.com) and Nette CS.

  Sources   Download

MIT

The Requires

 

The Development Requires

05/12 2014

v2.1.0

2.1.0.0

Set of rules for PHP_CodeSniffer based on PGS-2 (http://php-fg.com) and Nette CS.

  Sources   Download

MIT

The Requires

 

The Development Requires

14/11 2014

v2.0.0

2.0.0.0

Set of rules for PHP_CodeSniffer based on PGS-2 (http://php-fg.com) and Nette CS.

  Sources   Download

MIT

The Requires

 

The Development Requires

06/11 2014

v1.0.0

1.0.0.0

Set of rules for PHP_CodeSniffer based on PGS-2 (http://php-fg.com) and Nette CS.

  Sources   Download

MIT

The Requires

 

The Development Requires

03/11 2014

v0.3.1

0.3.1.0

Set of rules for PHP_CodeSniffer based on PGS-2 (http://php-fg.com) and Nette CS.

  Sources   Download

MIT

The Requires

 

The Development Requires

03/11 2014

v0.3.0

0.3.0.0

Set of rules for PHP_CodeSniffer based on PGS-2 (http://php-fg.com) and Nette CS.

  Sources   Download

MIT

The Requires

 

The Development Requires

26/10 2014

v0.2.8

0.2.8.0

Set of rules for PHP_CodeSniffer based on PGS-2 (http://php-fg.com) and Nette CS.

  Sources   Download

MIT

The Requires

 

The Development Requires

26/10 2014

v0.2.7

0.2.7.0

Set of rules for PHP_CodeSniffer based on PGS-2 (http://php-fg.com) and Nette CS.

  Sources   Download

MIT

The Requires

 

The Development Requires

26/10 2014

v0.2.6

0.2.6.0

Set of rules for PHP_CodeSniffer based on PGS-2 (http://php-fg.com) and Nette CS.

  Sources   Download

MIT

The Requires

 

The Development Requires

26/10 2014

v0.2.5

0.2.5.0

Set of rules for PHP_CodeSniffer based on PGS-2 (http://php-fg.com) and Nette CS.

  Sources   Download

MIT

The Requires

 

The Development Requires

25/10 2014

v0.2.4

0.2.4.0

Set of rules for PHP_CodeSniffer based on PGS-2 (http://php-fg.com) and Nette CS.

  Sources   Download

MIT

The Requires

 

The Development Requires

25/10 2014

v0.2.3

0.2.3.0

Set of rules for PHP_CodeSniffer based on PGS-2 (http://php-fg.com) and Nette CS.

  Sources   Download

MIT

The Requires

 

The Development Requires

15/10 2014

v0.2.2

0.2.2.0

Set of rules for PHP_CodeSniffer based on PGS-2 (http://php-fg.com) and Nette CS.

  Sources   Download

MIT

The Requires

 

The Development Requires

15/10 2014

v0.2.1

0.2.1.0

Set of rules for PHP_CodeSniffer based on PGS-2 (http://php-fg.com) and Nette CS.

  Sources   Download

MIT

The Requires

 

The Development Requires

15/10 2014

v0.2.0

0.2.0.0

Set of rules for PHP_CodeSniffer based on PGS-2 (http://php-fg.com) and Nette CS.

  Sources   Download

MIT

The Requires

 

The Development Requires

14/10 2014

v0.1.7

0.1.7.0

Set of rules for PHP_CodeSniffer based on PGS-2 (http://php-fg.com) and Nette CS.

  Sources   Download

MIT

The Requires

 

The Development Requires

14/10 2014

v0.1.6

0.1.6.0

Set of rules for PHP_CodeSniffer based on PGS-2 (http://php-fg.com) and Nette CS.

  Sources   Download

MIT

The Requires

 

The Development Requires

14/10 2014

v0.1.5

0.1.5.0

Set of rules for PHP_CodeSniffer based on PGS-2 (http://php-fg.com) and Nette CS.

  Sources   Download

MIT

The Requires

 

The Development Requires

13/10 2014

v0.1.4

0.1.4.0

Set of rules for PHP_CodeSniffer based on PGS-2 (http://php-fg.com) and Nette CS.

  Sources   Download

MIT

The Requires

 

The Development Requires

13/10 2014

v0.1.3

0.1.3.0

Set of rules for PHP_CodeSniffer based on PGS-2 (http://php-fg.com) and Nette CS.

  Sources   Download

MIT

The Requires

 

The Development Requires

12/10 2014

v0.1.2

0.1.2.0

Set of rules for PHP_CodeSniffer based on PGS-2 (http://php-fg.com) and Nette CS.

  Sources   Download

MIT

The Requires

 

The Development Requires

12/10 2014

v0.1.1

0.1.1.0

Set of rules for PHP_CodeSniffer based on PGS-2 (http://php-fg.com) and Nette CS.

  Sources   Download

MIT

The Requires

 

The Development Requires

12/10 2014

v0.1.0

0.1.0.0

Set of rules for PHP_CodeSniffer based on PGS-2 (http://php-fg.com) and Nette CS.

  Sources   Download

MIT

The Requires

 

The Development Requires

11/10 2014

v0.0.1

0.0.1.0

Set of rules for PHP_CodeSniffer based on PGS-2 (http://php-fg.com) and Nette CS.

  Sources   Download

MIT

The Requires

 

The Development Requires