Object Calisthenics rules for PHP_CodeSniffer
Object Calisthenics are set of rules in object-oriented code, that focuses of maintainability, readability, testability and comprehensibility.
Are you interested in motivation and reasons behind them?
Do you prefer slides?
- Here are 58 of them in presentation by Guilherme Blanco, the founding father of this project.
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>
Read explanation with code examples.
This sniff is configurable:
<!-- ruleset.xml -->
<rule ref="ObjectCalisthenics.Metrics.MaxNestingLevel">
<properties>
<property name="maxNestingLevel" value="2"/>
</properties>
</rule>
Read explanation with code examples
Read explanation with code examples
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>
ObjectCalisthenics\Sniffs\NamingConventions\ClassNameLengthSniff
ObjectCalisthenics\Sniffs\NamingConventions\ConstantNameLengthSniff
ObjectCalisthenics\Sniffs\NamingConventions\FunctionNameLengthSniff
ObjectCalisthenics\Sniffs\NamingConventions\VariableNameLengthSniff
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>
ObjectCalisthenics\Sniffs\Files\ClassElementLengthSniff
ObjectCalisthenics\Sniffs\Metrics\MethodPerClassLimitSniff
ObjectCalisthenics\Sniffs\Metrics\PropertyPerClassLimitSniff
ObjectCalisthenics\Sniffs\Files\FunctionLengthSniff
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>
Read explanation with code examples
This sniff is configurable:
<!-- ruleset.xml -->
<rule ref="ObjectCalisthenics.CodeAnalysis.InstancePropertyPerClassLimit">
<properties>
<property name="maxCount" value="2"/>
</properties>
</rule>
Read explanation with code examples
- Classes should not contain public properties.
- Method should represent behavior, not set values.
ObjectCalisthenics\Sniffs\Classes\ForbiddenPublicPropertySniff
ObjectCalisthenics\Sniffs\NamingConventions\NoSetterSniff
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.
This rule makes sense, yet is too strict to be useful in practise. Even our code didn't pass it at all.
- 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.