PHP
Clone or download
TomasVotruba Merge pull request #78 from object-calisthenics/own-config
Decouple own config to allow importing the set
Latest commit 1f436d7 May 12, 2018

README.md

Object Calisthenics rules for PHP_CodeSniffer

Build Status Coverage Status Downloads

Object Calisthenics are set of rules in object-oriented code, that focuses of maintainability, readability, testability and comprehensibility. We're pragmatic first - they are easy to use all together or one by one.

Why Should You Use This in Your Project?

Read post by William Durand or check presentation by Guilherme Blanco.

Install

composer require object-calisthenics/phpcs-calisthenics-rules

Usage

In PHP_CodeSniffer

vendor/bin/phpcs src tests -sp \
--standard=vendor/object-calisthenics/phpcs-calisthenics-rules/src/ObjectCalisthenics/ruleset.xml

or

vendor/bin/phpcs src tests -sp \
--standard=vendor/object-calisthenics/phpcs-calisthenics-rules/src/ObjectCalisthenics/ruleset.xml \
--sniffs=ObjectCalisthenics.Classes.ForbiddenPublicProperty

In EasyCodingStandard

vendor/bin/ecs check src --config vendor/object-calisthenics/phpcs-calisthenics-rules/config/object-calisthenics.yml

or

# easy-coding-standard.yml
imports:
    - { resource: 'vendor/object-calisthenics/phpcs-calisthenics-rules/config/object-calisthenics.yml' }

Implemented Rule Sniffs

1. Only X Level of Indentation per Method

foreach ($sniffGroups as $sniffGroup) {
    foreach ($sniffGroup as $sniffKey => $sniffClass) {
        if (! $sniffClass instanceof Sniff) {
            throw new InvalidClassTypeException;
        }
    }
}

👍

foreach ($sniffGroups as $sniffGroup) {
    $this->ensureIsAllInstanceOf($sniffGroup, Sniff::class);
}

// ...
private function ensureIsAllInstanceOf(array $objects, string $type)
{
    // ...
}

Use Just This One?

--sniffs=ObjectCalisthenics.Metrics.MaxNestingLevel
# easy-coding-standard.yml
services:
    ObjectCalisthenics\Sniffs\Metrics\MaxNestingLevelSniff: ~

🔧 Configurable


2. Do Not Use "else" Keyword

if ($status === self::DONE) {
    $this->finish();
} else {
    $this->advance();
}

👍

if ($status === self::DONE) {
    $this->finish();
    return;
}

$this->advance();

Use Just This One?

--sniffs=ObjectCalisthenics.ControlStructures.NoElseSniff
# easy-coding-standard.yml
services:
    ObjectCalisthenics\Sniffs\ControlStructures\NoElseSniff: ~

5. Use Only One Object Operator (->) per Line

$this->container->getBuilder()->addDefinition(SniffRunner::class);

👍

$containerBuilder = $this->getContainerBuilder();
$containerBuilder->addDefinition(SniffRunner::class);

Use Just This One?

--sniffs=ObjectCalisthenics.CodeAnalysis.OneObjectOperatorPerLine
# easy-coding-standard.yml
services:
    ObjectCalisthenics\Sniffs\CodeAnalysis\OneObjectOperatorPerLineSniff: ~

🔧 Configurable


6. Do not Abbreviate

This is related to class, trait, interface, constant, function and variable names.

class EM
{
    // ...
}

👍

class EntityMailer
{
    // ...
}

Use Just This One?

--sniffs=ObjectCalisthenics.NamingConventions.ElementNameMinimalLength
# easy-coding-standard.yml
services:
    ObjectCalisthenics\Sniffs\NamingConventions\ElementNameMinimalLengthSniff: ~

🔧 Configurable


7. Keep Your Classes Small

class SimpleStartupController
{
    // 300 lines of code
}

👍

class SimpleStartupController
{
    // 50 lines of code
}

class SomeClass
{
    public function simpleLogic()
    {
        // 30 lines of code
    }
}

👍

class SomeClass
{
    public function simpleLogic()
    {
        // 10 lines of code
    }
}

class SomeClass
{
    // 20 properties
}

👍

class SomeClass
{
    // 5 properties
}

class SomeClass
{
    // 20 methods
}

👍

class SomeClass
{
    // 5 methods
}

Use Just These Ones?

--sniffs=ObjectCalisthenics.Files.ClassTraitAndInterfaceLength,ObjectCalisthenics.Files.FunctionLengthSniff,ObjectCalisthenics.Metrics.MethodPerClassLimit,ObjectCalisthenics.Metrics.PropertyPerClassLimitSniff
# easy-coding-standard.yml
services:
    ObjectCalisthenics\Sniffs\Files\ClassTraitAndInterfaceLengthSniff: ~
    ObjectCalisthenics\Sniffs\Files\FunctionLengthSniff: ~
    ObjectCalisthenics\Sniffs\Metrics\MethodPerClassLimitSniff: ~
    ObjectCalisthenics\Sniffs\Metrics\PropertyPerClassLimitSniff: ~

🔧 Configurable


9. Do not Use Getters and Setters

This rules is partially related to Domain Driven Design.

  • Classes should not contain public properties.
  • Method should represent behavior, not set values.

class ImmutableBankAccount
{
    public $currency = 'USD';
    private $amount;

    public function setAmount(int $amount)
    {
        $this->amount = $amount;
    }
}

👍

class ImmutableBankAccount
{
    private $currency = 'USD';
    private $amount;

    public function withdrawAmount(int $withdrawnAmount)
    {
        $this->amount -= $withdrawnAmount;
    }
}

Use Just These Ones?

--sniffs=ObjectCalisthenics.Classes.ForbiddenPublicProperty,ObjectCalisthenics.NamingConventions.NoSetter
# easy-coding-standard.yml
services:
    ObjectCalisthenics\Sniffs\Classes\ForbiddenPublicPropertySniff: ~
    ObjectCalisthenics\Sniffs\NamingConventions\NoSetterSniff: ~

Not Implemented Rules - Too Strict, Vague or Annoying

While using in practice, we found these rule to be too strict, vague or even annoying, rather than helping to write cleaner and more pragmatic code. They're also closely related with Domain Driven Design.

3. Wrap Primitive Types and Strings - Since PHP 7, you can use define(strict_types=1) and scalar type hints. For other cases, e.g. email, you can deal with that in your Domain via Value Objects.

4. Use First Class Collections - This rule makes sense, yet is too strict to be useful in practice. Even our code didn't pass it at all.

8. Do Not Use Classes With More Than Two Instance Variables - This depends on individual domain of each project. It doesn't make sense to make a rule for that.


3 Rules for Contributing

  • 1 feature per PR

  • every new feature must be covered by tests

  • all tests and style checks must pass

    composer complete-check

We will be happy to merge your feature then.