2017 © Pedro Peláez
 

phpcodesniffer-standard phpcs-calisthenics-rules

PHP CodeSniffer Object Calisthenics rules/sniffs

image

object-calisthenics/phpcs-calisthenics-rules

PHP CodeSniffer Object Calisthenics rules/sniffs

  • Sunday, July 22, 2018
  • by guilhermeblanco
  • Repository
  • 30 Watchers
  • 470 Stars
  • 49,372 Installations
  • PHP
  • 21 Dependents
  • 0 Suggesters
  • 51 Forks
  • 0 Open issues
  • 21 Versions
  • 16 % Grown

The README.md

Object Calisthenics rules for PHP_CodeSniffer

DEPRECATED: PHP_CodeSniffer is great for handling spaces and char positions. Yet these rules are about code architecture and structure. In 2020, there is tool that suits this perfectly - PHPStan., (*1)

Saying that, object calisthenics were implemented as PHPStan rules in a symplify/phpstan-rules package. Use it instead 👇, (*2)

includes:
    - vendor/symplify/phpstan-rules/packages/object-calisthenics/config/object-calisthenics-rules.neon
    - vendor/symplify/phpstan-rules/packages/object-calisthenics/config/object-calisthenics-services.neon




, (*3)

Downloads, (*4)

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

Why Should You Use This in Your Project?

Read post by William Durand or check presentation by Guilherme Blanco., (*6)

Install

composer require object-calisthenics/phpcs-calisthenics-rules --dev

Usage

If you know what you want, jump right to the specific rule:, (*7)

How to quickly check 1 rule?

In PHP_CodeSniffer, (*8)

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

In EasyCodingStandard, (*9)

# ecs.yaml
services:
    ObjectCalisthenics\Sniffs\Classes\ForbiddenPublicPropertySniff: ~

then, (*10)

vendor/bin/ecs check src

Implemented Rule Sniffs

1. Only X Level of Indentation per Method

:x:, (*11)

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

:+1:, (*12)

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

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

Use Only This Rule?

In PHP_CodeSniffer:, (*13)

vendor/bin/phpcs ... --sniffs=ObjectCalisthenics.Metrics.MaxNestingLevel

In ECS:, (*14)

# ecs.yaml
services:
    ObjectCalisthenics\Sniffs\Metrics\MaxNestingLevelSniff: ~

:wrench: Configurable

In PHP_CodeSniffer:, (*15)

<?xml version="1.0"?>
<ruleset name="my-project">
    <rule ref="ObjectCalisthenics.Metrics.MaxNestingLevel">
        <properties>
            <property name="maxNestingLevel" value="2"/>
        </properties>
    </rule>
</ruleset>

In ECS:, (*16)

services:
    ObjectCalisthenics\Sniffs\Metrics\MaxNestingLevelSniff:
        maxNestingLevel: 2

2. Do Not Use "else" Keyword

:x:, (*17)

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

:+1:, (*18)

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

$this->advance();

Use Only This Rule?

In PHP_CodeSniffer:, (*19)

vendor/bin/phpcs ... --sniffs=ObjectCalisthenics.ControlStructures.NoElse

In ECS:, (*20)

# ecs.yaml
services:
    ObjectCalisthenics\Sniffs\ControlStructures\NoElseSniff: ~

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

:x:, (*21)

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

:+1:, (*22)

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

Use Only This Rule?

In PHP_CodeSniffer:, (*23)

vendor/bin/phpcs ... --sniffs=ObjectCalisthenics.CodeAnalysis.OneObjectOperatorPerLine

In ECS:, (*24)

# ecs.yaml
services:
    ObjectCalisthenics\Sniffs\CodeAnalysis\OneObjectOperatorPerLineSniff: ~

:wrench: Configurable

In PHP_CodeSniffer:, (*25)

<?xml version="1.0"?>
<ruleset name="my-project">
    <rule ref="ObjectCalisthenics.CodeAnalysis.OneObjectOperatorPerLine">
        <properties>
            <property name="variablesHoldingAFluentInterface" type="array" value="$queryBuilder,$containerBuilder"/>
            <property name="methodsStartingAFluentInterface" type="array" value="createQueryBuilder"/>
            <property name="methodsEndingAFluentInterface" type="array" value="execute,getQuery"/>
        </properties>
    </rule>
</ruleset>

In ECS:, (*26)

services:
    ObjectCalisthenics\Sniffs\CodeAnalysis\OneObjectOperatorPerLineSniff:
        variablesHoldingAFluentInterface: ["$queryBuilder", "$containerBuilder"]
        methodsStartingAFluentInterface: ["createQueryBuilder"]
        methodsEndingAFluentInterface: ["execute", "getQuery"]

6. Do not Abbreviate

This is related to class, trait, interface, constant, function and variable names., (*27)

:x:, (*28)

class EM
{
    // ...
}

:+1:, (*29)

class EntityMailer
{
    // ...
}

Use Only This Rule?

In PHP_CodeSniffer:, (*30)

vendor/bin/phpcs ... --sniffs=ObjectCalisthenics.NamingConventions.ElementNameMinimalLength

In ECS:, (*31)

# ecs.yaml
services:
    ObjectCalisthenics\Sniffs\NamingConventions\ElementNameMinimalLengthSniff: ~

:wrench: Configurable

In PHP_CodeSniffer:, (*32)

<?xml version="1.0"?>
<ruleset name="my-project">
    <rule ref="ObjectCalisthenics.NamingConventions.ElementNameMinimalLength">
        <properties>
            <property name="minLength" value="3"/>
            <property name="allowedShortNames" type="array" value="i,id,to,up"/>
        </properties>
    </rule>
</ruleset>

In ECS:, (*33)

# ecs.yaml
services:
    ObjectCalisthenics\Sniffs\NamingConventions\ElementNameMinimalLengthSniff:
        minLength: 3
        allowedShortNames: ["i", "id", "to", "up"]

7. Keep Your Classes Small

:x:, (*34)

class SimpleStartupController
{
    // 300 lines of code
}

:+1:, (*35)

class SimpleStartupController
{
    // 50 lines of code
}

:x:, (*36)

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

:+1:, (*37)

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

:x:, (*38)

class SomeClass
{
    // 20 properties
}

:+1:, (*39)

class SomeClass
{
    // 5 properties
}

:x:, (*40)

class SomeClass
{
    // 20 methods
}

:+1:, (*41)

class SomeClass
{
    // 5 methods
}

Use Only This Rule?

In PHP_CodeSniffer:, (*42)

vendor/bin/phpcs ... --sniffs=ObjectCalisthenics.Files.ClassTraitAndInterfaceLength,ObjectCalisthenics.Files.FunctionLength,ObjectCalisthenics.Metrics.MethodPerClassLimit,ObjectCalisthenics.Metrics.PropertyPerClassLimit

In ECS:, (*43)

# ecs.yaml
services:
    ObjectCalisthenics\Sniffs\Files\ClassTraitAndInterfaceLengthSniff: ~
    ObjectCalisthenics\Sniffs\Files\FunctionLengthSniff: ~
    ObjectCalisthenics\Sniffs\Metrics\MethodPerClassLimitSniff: ~
    ObjectCalisthenics\Sniffs\Metrics\PropertyPerClassLimitSniff: ~

:wrench: Configurable

In PHP_CodeSniffer:, (*44)

<?xml version="1.0"?>
<ruleset name="my-project">
    <rule ref="ObjectCalisthenics.Files.ClassTraitAndInterfaceLength">
        <properties>
            <property name="maxLength" value="200"/>
        </properties>
    </rule>
    <rule ref="ObjectCalisthenics.Files.FunctionLength">
        <properties>
            <property name="maxLength" value="20"/>
        </properties>
    </rule>
    <rule ref="ObjectCalisthenics.Metrics.PropertyPerClassLimit">
        <properties>
            <property name="maxCount" value="10"/>
        </properties>
    </rule>
    <rule ref="ObjectCalisthenics.Metrics.MethodPerClassLimit">
        <properties>
            <property name="maxCount" value="10"/>
        </properties>
    </rule>
</ruleset>

In ECS:, (*45)

# ecs.yaml
services:
    ObjectCalisthenics\Sniffs\Files\ClassTraitAndInterfaceLengthSniff:
        maxLength: 200
    ObjectCalisthenics\Sniffs\Files\FunctionLengthSniff:
        maxLength: 20
    ObjectCalisthenics\Sniffs\Metrics\PropertyPerClassLimitSniff:
        maxCount: 10
    ObjectCalisthenics\Sniffs\Metrics\MethodPerClassLimitSniff:
        maxCount: 10

9. Do not Use Getters and Setters

This rules is partially related to Domain Driven Design., (*46)

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

:x:, (*47)

class ImmutableBankAccount
{
    public $currency = 'USD';

```php private $amount;, (*48)

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

}, (*49)


:+1: ```php class ImmutableBankAccount { private $currency = 'USD';

```php private $amount;, (*50)

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

}, (*51)


#### Use Only This Rule? In PHP_CodeSniffer: ```bash vendor/bin/phpcs ... --sniffs=ObjectCalisthenics.Classes.ForbiddenPublicProperty,ObjectCalisthenics.NamingConventions.NoSetter

In ECS:, (*52)

# ecs.yaml
services:
    ObjectCalisthenics\Sniffs\Classes\ForbiddenPublicPropertySniff: ~
    ObjectCalisthenics\Sniffs\NamingConventions\NoSetterSniff: ~

:wrench: Configurable

In PHP_CodeSniffer:, (*53)

<?xml version="1.0"?>
<ruleset name="my-project">
    <rule ref="ObjectCalisthenics.NamingConventions.NoSetter">
        <properties>
            <property name="allowedClasses" type="array" value="*\DataObject"/>
        </properties>
    </rule>
</ruleset>

In ECS:, (*54)

# ecs.yaml
services:
    ObjectCalisthenics\Sniffs\NamingConventions\NoSetterSniff:
        allowedClasses: 
            - '*\DataObject'

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

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

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

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


3 Rules for Contributing

  • 1 feature per PR
  • every new feature must be covered by tests
  • all tests and style checks must pass, (*59)

    composer complete-check
    

We will be happy to merge your feature then., (*60)

The Versions

12/05 2018
01/08 2017

2.0.x-dev

2.0.9999999.9999999-dev https://github.com/object-calisthenics/phpcs-calisthenics-rules

PHP CodeSniffer Object Calisthenics rules/sniffs

  Sources   Download

MIT

The Requires

 

The Development Requires

01/08 2017
10/03 2017

v3.0.0-RC1

3.0.0.0-RC1

PHP CodeSniffer Object Calisthenics rules/sniffs

  Sources   Download

MIT

The Requires

 

The Development Requires

10/03 2017

dev-UFOMelkor-feature/fluent-interface-detection

dev-UFOMelkor-feature/fluent-interface-detection

PHP CodeSniffer Object Calisthenics rules/sniffs

  Sources   Download

MIT

The Requires

 

The Development Requires

12/11 2016
21/08 2014

v1.0.1

1.0.1.0 https://github.com/object-calisthenics/phpcs-calisthenics-rules

PHP CodeSniffer Object Calisthenics rules/sniffs

  Sources   Download

MIT

The Requires

  • php ~5.3

 

The Development Requires

phpcs style code standard rules standards coding cs rule sniffer sniff sniffs

20/08 2014

v1.0.0

1.0.0.0 https://github.com/object-calisthenics/phpcs-calisthenics-rules

PHP CodeSniffer Object Calisthenics rules/sniffs

  Sources   Download

MIT

The Requires

  • php ~5.3

 

The Development Requires

phpcs style code standard rules standards coding cs rule sniffer sniff sniffs