PHPackages                             object-calisthenics/phpcs-calisthenics-rules - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Testing &amp; Quality](/categories/testing)
4. /
5. object-calisthenics/phpcs-calisthenics-rules

Abandoned → [symplify/phpstan-rules](/?search=symplify%2Fphpstan-rules)Phpcodesniffer-standard[Testing &amp; Quality](/categories/testing)

object-calisthenics/phpcs-calisthenics-rules
============================================

PHP CodeSniffer Object Calisthenics rules/sniffs

v3.9.1(5y ago)5962.6M—8.8%54[1 PRs](https://github.com/object-calisthenics/phpcs-calisthenics-rules/pulls)20MITPHPPHP ^7.4|^8.0

Since Aug 20Pushed 4y ago30 watchersCompare

[ Source](https://github.com/object-calisthenics/phpcs-calisthenics-rules)[ Packagist](https://packagist.org/packages/object-calisthenics/phpcs-calisthenics-rules)[ RSS](/packages/object-calisthenics-phpcs-calisthenics-rules/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (10)Dependencies (13)Versions (22)Used By (20)

Object Calisthenics rules for [PHP\_CodeSniffer](https://github.com/squizlabs/PHP_CodeSniffer)
==============================================================================================

[](#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](github.com/phpstan/phpstan)**.

**Saying that, [object calisthenics were implemented as PHPStan rules](https://github.com/symplify/phpstan-rules/blob/main/docs/rules_overview.md#nochainmethodcallrule) in a `symplify/phpstan-rules` package. Use it instead 👇**

```
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

```

[![Downloads](https://camo.githubusercontent.com/b3d38cd7ee634cf66917d832b90da735fd8fe2dadf7f36e4aeb13847f363481a/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f6f626a6563742d63616c69737468656e6963732f70687063732d63616c69737468656e6963732d72756c65732e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/object-calisthenics/phpcs-calisthenics-rules)

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?

[](#why-should-you-use-this-in-your-project)

[Read post by *William Durand*](http://williamdurand.fr/2013/06/03/object-calisthenics/) or [check presentation by *Guilherme Blanco*](https://www.slideshare.net/guilhermeblanco/object-calisthenics-applied-to-php).

Install
-------

[](#install)

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

Usage
-----

[](#usage)

If you know what you want, jump right to the specific rule:

- [1. Only X Level of Indentation per Method](#1-only-x-level-of-indentation-per-method)
- [2. Do Not Use "else" Keyword](#2-do-not-use-else-keyword)
- [5. Use Only One Object Operator (-&gt;) per Statement](#5-use-only-one-object-operator---per-line)
- [6. Do not Abbreviate](#6-do-not-abbreviate)
- [7. Keep Your Classes Small](#7-keep-your-classes-small)
- [9. Do not Use Getters and Setters](#9-do-not-use-getters-and-setters)

How to quickly check 1 rule?
----------------------------

[](#how-to-quickly-check-1-rule)

In [PHP\_CodeSniffer](https://github.com/squizlabs/PHP_CodeSniffer)

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

In [EasyCodingStandard](https://github.com/symplify/easyCodingStandard/)

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

then

```
vendor/bin/ecs check src
```

---

Implemented Rule Sniffs
-----------------------

[](#implemented-rule-sniffs)

### 1. [Only `X` Level of Indentation per Method](http://williamdurand.fr/2013/06/03/object-calisthenics/#1-only-one-level-of-indentation-per-method)

[](#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 Only This Rule?

[](#use-only-this-rule)

In PHP\_CodeSniffer:

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

In ECS:

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

#### 🔧 Configurable

[](#wrench-configurable)

In PHP\_CodeSniffer:

```

```

In ECS:

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

---

### 2. [Do Not Use "else" Keyword](http://williamdurand.fr/2013/06/03/object-calisthenics/#2-dont-use-the-else-keyword)

[](#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 Only This Rule?

[](#use-only-this-rule-1)

In PHP\_CodeSniffer:

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

In ECS:

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

---

### 5. [Use Only One Object Operator (`->`) per Statement](http://williamdurand.fr/2013/06/03/object-calisthenics/#5-one-dot-per-line)

[](#5-use-only-one-object-operator---per-statement)

❌

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

👍

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

#### Use Only This Rule?

[](#use-only-this-rule-2)

In PHP\_CodeSniffer:

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

In ECS:

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

#### 🔧 Configurable

[](#wrench-configurable-1)

In PHP\_CodeSniffer:

```

```

In ECS:

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

---

### 6. [Do not Abbreviate](http://williamdurand.fr/2013/06/03/object-calisthenics/#6-dont-abbreviate)

[](#6-do-not-abbreviate)

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

❌

```
class EM
{
    // ...
}
```

👍

```
class EntityMailer
{
    // ...
}
```

#### Use Only This Rule?

[](#use-only-this-rule-3)

In PHP\_CodeSniffer:

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

In ECS:

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

#### 🔧 Configurable

[](#wrench-configurable-2)

In PHP\_CodeSniffer:

```

```

In ECS:

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

---

### 7. [Keep Your Classes Small](http://williamdurand.fr/2013/06/03/object-calisthenics/#7-keep-all-entities-small)

[](#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 Only This Rule?

[](#use-only-this-rule-4)

In PHP\_CodeSniffer:

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

In ECS:

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

#### 🔧 Configurable

[](#wrench-configurable-3)

In PHP\_CodeSniffer:

```

```

In ECS:

```
# 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](http://williamdurand.fr/2013/06/03/object-calisthenics/#9-no-getterssettersproperties)

[](#9-do-not-use-getters-and-setters)

This rules is partially related to [Domain Driven Design](https://github.com/dddinphp).

- Classes should not contain public properties.
- Method should [represent behavior](http://whitewashing.de/2012/08/22/building_an_object_model__no_setters_allowed.html), 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 Only This Rule?

[](#use-only-this-rule-5)

In PHP\_CodeSniffer:

```
vendor/bin/phpcs ... --sniffs=ObjectCalisthenics.Classes.ForbiddenPublicProperty,ObjectCalisthenics.NamingConventions.NoSetter
```

In ECS:

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

#### 🔧 Configurable

[](#wrench-configurable-4)

In PHP\_CodeSniffer:

```

```

In ECS:

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

---

### Not Implemented Rules - Too Strict, Vague or Annoying

[](#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](https://github.com/dddinphp).

**3. [Wrap Primitive Types and Strings](http://williamdurand.fr/2013/06/03/object-calisthenics/#3-wrap-all-primitives-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](http://williamdurand.fr/2013/06/03/object-calisthenics/#3-wrap-all-primitives-and-strings).

**4. [Use First Class Collections](http://williamdurand.fr/2013/06/03/object-calisthenics/#4-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](http://williamdurand.fr/2013/06/03/object-calisthenics/#8-no-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
------------------------

[](#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.

###  Health Score

53

—

FairBetter than 97% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity61

Solid adoption and visibility

Community44

Growing community involvement

Maturity77

Established project with proven stability

 Bus Factor1

Top contributor holds 81.8% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~110 days

Recently: every ~75 days

Total

21

Last Release

2078d ago

Major Versions

v1.0.1 → v2.0.02016-11-12

v2.0.0 → v3.0.0-RC12017-03-10

v2.0.1 → v3.1.02017-10-19

PHP version history (6 changes)v1.0.0PHP ~5.3

v2.0.0PHP ^7.0

v3.0.0-RC1PHP ^7.1

v3.6.0PHP ^7.2

v3.9.0PHP ^7.4

v3.9.1PHP ^7.4|^8.0

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/924196?v=4)[Tomas Votruba](/maintainers/TomasVotruba)[@TomasVotruba](https://github.com/TomasVotruba)

![](https://www.gravatar.com/avatar/14e40fc4e4cbd5c5ebf5eb88ecfebbebc4dba76e671fc8468601139eeb129566?d=identicon)[guilhermeblanco](/maintainers/guilhermeblanco)

---

Top Contributors

[![TomasVotruba](https://avatars.githubusercontent.com/u/924196?v=4)](https://github.com/TomasVotruba "TomasVotruba (327 commits)")[![guilhermeblanco](https://avatars.githubusercontent.com/u/208883?v=4)](https://github.com/guilhermeblanco "guilhermeblanco (38 commits)")[![mihaeu](https://avatars.githubusercontent.com/u/2168701?v=4)](https://github.com/mihaeu "mihaeu (6 commits)")[![UFOMelkor](https://avatars.githubusercontent.com/u/571106?v=4)](https://github.com/UFOMelkor "UFOMelkor (6 commits)")[![EmanueleMinotto](https://avatars.githubusercontent.com/u/417201?v=4)](https://github.com/EmanueleMinotto "EmanueleMinotto (4 commits)")[![Th3Mouk](https://avatars.githubusercontent.com/u/5006899?v=4)](https://github.com/Th3Mouk "Th3Mouk (3 commits)")[![jeroennoten](https://avatars.githubusercontent.com/u/4370753?v=4)](https://github.com/jeroennoten "jeroennoten (3 commits)")[![greg0ire](https://avatars.githubusercontent.com/u/657779?v=4)](https://github.com/greg0ire "greg0ire (3 commits)")[![jeromegamez](https://avatars.githubusercontent.com/u/67554?v=4)](https://github.com/jeromegamez "jeromegamez (2 commits)")[![michaelvickersuk](https://avatars.githubusercontent.com/u/13500452?v=4)](https://github.com/michaelvickersuk "michaelvickersuk (1 commits)")[![frenck](https://avatars.githubusercontent.com/u/195327?v=4)](https://github.com/frenck "frenck (1 commits)")[![krizon](https://avatars.githubusercontent.com/u/880695?v=4)](https://github.com/krizon "krizon (1 commits)")[![afilina](https://avatars.githubusercontent.com/u/199835?v=4)](https://github.com/afilina "afilina (1 commits)")[![noplanman](https://avatars.githubusercontent.com/u/9423417?v=4)](https://github.com/noplanman "noplanman (1 commits)")[![nubs](https://avatars.githubusercontent.com/u/57673?v=4)](https://github.com/nubs "nubs (1 commits)")[![roukmoute](https://avatars.githubusercontent.com/u/2140469?v=4)](https://github.com/roukmoute "roukmoute (1 commits)")[![stof](https://avatars.githubusercontent.com/u/439401?v=4)](https://github.com/stof "stof (1 commits)")

---

Tags

calisthenics-rulescoding-standardphpphp-codesniffersolidstatic-analysis

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan, Rector

Type Coverage Yes

### Embed Badge

![Health badge](/badges/object-calisthenics-phpcs-calisthenics-rules/health.svg)

```
[![Health](https://phpackages.com/badges/object-calisthenics-phpcs-calisthenics-rules/health.svg)](https://phpackages.com/packages/object-calisthenics-phpcs-calisthenics-rules)
```

###  Alternatives

[slevomat/coding-standard

Slevomat Coding Standard for PHP\_CodeSniffer complements Consistence Coding Standard by providing sniffs with additional checks.

1.5k123.5M1.8k](/packages/slevomat-coding-standard)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
