GaryJones / phpcs-calisthenics-rules

Object Calisthenics rules for PHP_CodeSniffer

Home Page:http://williamdurand.fr/2013/06/03/object-calisthenics/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

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

Via CLI

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

The Best to Start With: Single Sniff via CLI

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

Implemented Rule Sniffs

❌

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

πŸ‘

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

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

Apply in CLI?

--sniffs=ObjectCalisthenics.Metrics.MaxNestingLevel

πŸ”§ Configurable


❌

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

πŸ‘

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

$this->advance();

Apply in CLI?

--sniffs=ObjectCalisthenics.ControlStructures.NoElseSniff

❌

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

πŸ‘

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

Apply in CLI?

--sniffs=ObjectCalisthenics.CodeAnalysis.OneObjectOperatorPerLine

πŸ”§ Configurable


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

❌

class EM
{
    // ...
}

πŸ‘

class EntityMailer
{
    // ...
}

Apply in CLI?

--sniffs=ObjectCalisthenics.NamingConventions.ElementNameMinimalLength

πŸ”§ Configurable


❌

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
}

Apply in CLI?

--sniffs=ObjectCalisthenics.Files.ClassTraitAndInterfaceLength,ObjectCalisthenics.Files.FunctionLengthSniff,ObjectCalisthenics.Metrics.MethodPerClassLimit,ObjectCalisthenics.Metrics.PropertyPerClassLimitSniff

πŸ”§ Configurable


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

Apply in CLI?

--sniffs=ObjectCalisthenics.Classes.ForbiddenPublicProperty,ObjectCalisthenics.NamingConventions.NoSetter

Not Implemented Rules - Too Strict, Vague or Annoying

While using in practise, we found these rule to be too strict, vague or even annoying, rather then 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 practise. 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.

About

Object Calisthenics rules for PHP_CodeSniffer

http://williamdurand.fr/2013/06/03/object-calisthenics/

License:MIT License


Languages

Language:PHP 100.0%