UFOMelkor / phpcs-calisthenics-rules

PHP CodeSniffer Object Calisthenics Sniffs/Rules

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Object Calisthenics rules for PHP_CodeSniffer

Build Status Code Coverage Downloads PHPStan

Object Calisthenics are set of rules in object-oriented code, that focuses of maintainability, readability, testability and comprehensibility.

Where to read more about Object Calisthenics?

Are you interested in motivation and reasons behind them?

Do you prefer slides?

Install

Via composer:

composer require object-calisthenics/phpcs-calisthenics-rules "squizlabs/php_codesniffer:3.0.0RC4" --dev

Then, enable it as part of your CodeSniffer ruleset (ie. ruleset.xml in root project directory):

<!-- ruleset.xml -->
<?xml version="1.0" encoding="UTF-8"?>
<ruleset name="Project">
    <rule ref="vendor/object-calisthenics/phpcs-calisthenics-rules/src/ObjectCalisthenics/ruleset.xml"/>
</ruleset>

Implemented Rules

1. Only One Level of Indentation per Method

Read explanation with code examples.

Sniff

This sniff is configurable:

<!-- ruleset.xml -->
<rule ref="ObjectCalisthenics.Metrics.MaxNestingLevel">
    <properties>
        <property name="maxNestingLevel" value="2"/>
    </properties>
</rule>

2. Do Not Use "else" Keyword

Read explanation with code examples

Sniff

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

Read explanation with code examples

Sniff

This sniff is configurable:

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

6. Do not Abbreviate

Read explanation

Sniffs

These sniffs are configurable:

<!-- ruleset.xml -->
<rule ref="ObjectCalisthenics.NamingConventions.ClassNameLength">
    <properties>
        <property name="minLength" value="3"/>
    </properties>
</rule>
<rule ref="ObjectCalisthenics.NamingConventions.ConstantNameLength">
    <properties>
        <property name="minLength" value="3"/>
    </properties>
</rule>
<rule ref="ObjectCalisthenics.NamingConventions.FunctionNameLength">
    <properties>
        <property name="minLength" value="3"/>
    </properties>
</rule>
<rule ref="ObjectCalisthenics.NamingConventions.VariableNameLength">
    <properties>
        <property name="minLength" value="3"/>
        <property name="propertiesToBeSkipped" type="array"
                  value="id"
        />
    </properties>
</rule>

7. Keep Your Classes Small

Read explanation

Sniffs

These sniffs are configurable:

<!-- ruleset.xml -->
<rule ref="ObjectCalisthenics.Files.ClassElementLength">
    <properties>
        <property name="maxLength" value="200"/>
    </properties>
</rule>
<rule ref="ObjectCalisthenics.Metrics.MethodPerClassLimit">
    <properties>
        <property name="maxLength" value="10"/>
    </properties>
</rule>
<rule ref="ObjectCalisthenics.Metrics.PropertyPerClassLimit">
    <properties>
        <property name="maxLength" value="10"/>
    </properties>
</rule>
<rule ref="ObjectCalisthenics.Files.FunctionLength">
    <properties>
        <property name="maxLength" value="20"/>
    </properties>
</rule>

8. Do Not Use Classes With More Than Two Instance Variables

Read explanation with code examples

Sniff

This sniff is configurable:

<!-- ruleset.xml -->
<rule ref="ObjectCalisthenics.CodeAnalysis.InstancePropertyPerClassLimit">
    <properties>
        <property name="maxCount" value="2"/>
    </properties>
</rule>

9. Do not Use Getters and Setters

Read explanation with code examples

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

Sniffs


Not Implemented Rules

3. Wrap Primitive Types and Strings

Read explanation

Since PHP 7, you can use define(strict_types=1) and scalar type hints:

define(strict_types=1);

final class Resolver
{
    public function resolveFromRoute(string $route): string
    {
        // ...
    }
}

For other cases, e.g. email, you can deal with that in your Domain via Value Objects.

4. Use First Class Collections

Read explanation

This rule makes sense, yet is too strict to be useful in practise. Even our code didn't pass it at all.

3 Rules for Contributing

  • 1 feature per PR
  • every new feature must be covered by tests
  • all tests and style checks must pass
# runs PHPStan, coding standard check and tests,
# see "scripts" section in composer.json for more
composer complete-check

We will be happy to merge your feature then.

About

PHP CodeSniffer Object Calisthenics Sniffs/Rules

License:MIT License


Languages

Language:PHP 100.0%