zenify / coding-standard
Set of rules for PHP_CodeSniffer preferring tabs and based on Nette coding standard.
Installs: 37 618
Dependents: 12
Suggesters: 0
Security: 0
Stars: 12
Watchers: 5
Forks: 4
Open Issues: 0
Requires
- php: ^7.0
- squizlabs/php_codesniffer: ~2.7
Requires (Dev)
- phpunit/phpunit: ^5.6
- dev-master
- v4.2
- v4.1
- v4.0.1
- v4.0.0
- v3.3.4
- v3.3.3
- v3.3.2
- v3.3.1
- v3.3.0
- v3.2.2
- v3.2.1
- v3.2.0
- v3.1.1
- v3.1.0
- v3.0.15
- v3.0.14
- v3.0.13
- v3.0.12
- v3.0.11
- v3.0.10
- v3.0.9
- v3.0.8
- v3.0.7
- v3.0.6
- v3.0.5
- v3.0.4
- v3.0.3
- v3.0.2
- v3.0.1
- v3.0.0
- v2.1.4
- v2.1.3
- v2.1.2
- v2.1.1
- v2.1.0
- v2.0.0
- v1.0.0
- v0.3.1
- v0.3.0
- v0.2.8
- v0.2.7
- v0.2.6
- v0.2.5
- v0.2.4
- v0.2.3
- v0.2.2
- v0.2.1
- v0.2.0
- v0.1.7
- v0.1.6
- v0.1.5
- v0.1.4
- v0.1.3
- v0.1.2
- v0.1.1
- v0.1.0
- v0.0.1
This package is auto-updated.
Last update: 2022-01-27 10:58:32 UTC
README
Set of rules for PHP_CodeSniffer preferring tabs and based on Nette coding standard.
Check rules overview for examples.
Install
$ composer require zenify/coding-standard --dev
Usage
Run with Php_CodeSniffer:
vendor/bin/phpcs src --standard=vendor/zenify/coding-standard/src/ZenifyCodingStandard/ruleset.xml -p
That's all!
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
:
"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.
This is much faster than checking whole project, running manually or wait for CI.
Pretty cool, huh?
Testing
composer check-cs vendor/bin/phpunit
Contributing
Rules are simple:
- new feature needs tests
- all tests must pass
- 1 feature per PR
We would be happy to merge your feature then!
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 { public function run() { } }
Wrong
class SomeClass implements SomeInterface { 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.
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 or return type.
Correct
class SomeClass { /** * @return int */ 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;
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 { }
6 Naming
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 { }
7 WhiteSpace
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() { } }