{"id":13826491,"url":"https://github.com/object-calisthenics/phpcs-calisthenics-rules","last_synced_at":"2025-05-15T09:00:19.388Z","repository":{"id":19712669,"uuid":"22968104","full_name":"object-calisthenics/phpcs-calisthenics-rules","owner":"object-calisthenics","description":"[DEPRECATED] Object Calisthenics rules for PHP_CodeSniffer ","archived":false,"fork":false,"pushed_at":"2021-06-23T11:19:29.000Z","size":429,"stargazers_count":600,"open_issues_count":0,"forks_count":56,"subscribers_count":31,"default_branch":"master","last_synced_at":"2025-04-13T12:48:49.568Z","etag":null,"topics":["calisthenics-rules","coding-standard","php","php-codesniffer","solid","static-analysis"],"latest_commit_sha":null,"homepage":"https://tomasvotruba.com/blog/2017/06/26/php-object-calisthenics-rules-made-simple-version-3-0-is-out-now/","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/object-calisthenics.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2014-08-14T20:46:49.000Z","updated_at":"2025-03-14T12:22:25.000Z","dependencies_parsed_at":"2022-08-07T09:15:31.289Z","dependency_job_id":null,"html_url":"https://github.com/object-calisthenics/phpcs-calisthenics-rules","commit_stats":null,"previous_names":[],"tags_count":21,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/object-calisthenics%2Fphpcs-calisthenics-rules","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/object-calisthenics%2Fphpcs-calisthenics-rules/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/object-calisthenics%2Fphpcs-calisthenics-rules/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/object-calisthenics%2Fphpcs-calisthenics-rules/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/object-calisthenics","download_url":"https://codeload.github.com/object-calisthenics/phpcs-calisthenics-rules/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254310509,"owners_count":22049467,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["calisthenics-rules","coding-standard","php","php-codesniffer","solid","static-analysis"],"created_at":"2024-08-04T09:01:38.809Z","updated_at":"2025-05-15T09:00:19.354Z","avatar_url":"https://github.com/object-calisthenics.png","language":"PHP","funding_links":[],"categories":["\u003ca id=\"7bf0f5839fb2827fdc1b93ae6ac7f53d\"\u003e\u003c/a\u003e工具"],"sub_categories":["\u003ca id=\"32739127f0c38d61b14448c66a797098\"\u003e\u003c/a\u003e嗅探\u0026\u0026Sniff"],"readme":"# Object Calisthenics rules for [PHP_CodeSniffer](https://github.com/squizlabs/PHP_CodeSniffer)\n\n\n**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)**.\n\n**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 👇**\n\n```\nincludes:\n    - vendor/symplify/phpstan-rules/packages/object-calisthenics/config/object-calisthenics-rules.neon\n    - vendor/symplify/phpstan-rules/packages/object-calisthenics/config/object-calisthenics-services.neon\n```\n\n\u003cbr\u003e\n\u003cbr\u003e\n\u003cbr\u003e\n\n[![Downloads](https://img.shields.io/packagist/dt/object-calisthenics/phpcs-calisthenics-rules.svg?style=flat-square)](https://packagist.org/packages/object-calisthenics/phpcs-calisthenics-rules)\n\nObject 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.\n\n### Why Should You Use This in Your Project?\n\n[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).\n\n\n## Install\n\n```sh\ncomposer require object-calisthenics/phpcs-calisthenics-rules --dev\n```\n\n## Usage\n\nIf you know what you want, jump right to the specific rule:\n\n- [1. Only X Level of Indentation per Method](#1-only-x-level-of-indentation-per-method)\n- [2. Do Not Use \"else\" Keyword](#2-do-not-use-else-keyword)\n- [5. Use Only One Object Operator (-\u003e) per Statement](#5-use-only-one-object-operator---per-line)\n- [6. Do not Abbreviate](#6-do-not-abbreviate)\n- [7. Keep Your Classes Small](#7-keep-your-classes-small)\n- [9. Do not Use Getters and Setters](#9-do-not-use-getters-and-setters)\n\n## How to quickly check 1 rule?\n\nIn [PHP_CodeSniffer](https://github.com/squizlabs/PHP_CodeSniffer)\n\n```bash\nvendor/bin/phpcs src tests -sp \\\n--standard=vendor/object-calisthenics/phpcs-calisthenics-rules/src/ObjectCalisthenics/ruleset.xml \\\n--sniffs=ObjectCalisthenics.Classes.ForbiddenPublicProperty\n```\n\nIn [EasyCodingStandard](https://github.com/symplify/easyCodingStandard/)\n\n```yml\n# ecs.yaml\nservices:\n    ObjectCalisthenics\\Sniffs\\Classes\\ForbiddenPublicPropertySniff: ~\n```\n\nthen\n\n```bash\nvendor/bin/ecs check src\n```\n\n---\n\n## Implemented Rule Sniffs\n\n### 1. [Only `X` Level of Indentation per Method](http://williamdurand.fr/2013/06/03/object-calisthenics/#1-only-one-level-of-indentation-per-method)\n\n:x:\n\n```php\nforeach ($sniffGroups as $sniffGroup) {\n    foreach ($sniffGroup as $sniffKey =\u003e $sniffClass) {\n        if (! $sniffClass instanceof Sniff) {\n            throw new InvalidClassTypeException;\n        }\n    }\n}\n```\n\n:+1:\n\n```php\nforeach ($sniffGroups as $sniffGroup) {\n    $this-\u003eensureIsAllInstanceOf($sniffGroup, Sniff::class);\n}\n\n// ...\nprivate function ensureIsAllInstanceOf(array $objects, string $type)\n{\n    // ...\n}\n```\n\n#### Use Only This Rule?\n\nIn PHP_CodeSniffer:\n\n```bash\nvendor/bin/phpcs ... --sniffs=ObjectCalisthenics.Metrics.MaxNestingLevel\n```\n\nIn ECS:\n\n```yml\n# ecs.yaml\nservices:\n    ObjectCalisthenics\\Sniffs\\Metrics\\MaxNestingLevelSniff: ~\n```\n\n#### :wrench: Configurable\n\nIn PHP_CodeSniffer:\n\n```xml\n\u003c?xml version=\"1.0\"?\u003e\n\u003cruleset name=\"my-project\"\u003e\n    \u003crule ref=\"ObjectCalisthenics.Metrics.MaxNestingLevel\"\u003e\n        \u003cproperties\u003e\n            \u003cproperty name=\"maxNestingLevel\" value=\"2\"/\u003e\n        \u003c/properties\u003e\n    \u003c/rule\u003e\n\u003c/ruleset\u003e\n```\n\nIn ECS:\n\n```yaml\nservices:\n    ObjectCalisthenics\\Sniffs\\Metrics\\MaxNestingLevelSniff:\n        maxNestingLevel: 2\n```\n\n---\n\n### 2. [Do Not Use \"else\" Keyword](http://williamdurand.fr/2013/06/03/object-calisthenics/#2-dont-use-the-else-keyword)\n\n:x:\n\n```php\nif ($status === self::DONE) {\n    $this-\u003efinish();\n} else {\n    $this-\u003eadvance();\n}\n```\n\n:+1:\n\n```php\nif ($status === self::DONE) {\n    $this-\u003efinish();\n    return;\n}\n\n$this-\u003eadvance();\n```\n\n#### Use Only This Rule?\n\nIn PHP_CodeSniffer:\n\n```bash\nvendor/bin/phpcs ... --sniffs=ObjectCalisthenics.ControlStructures.NoElse\n```\n\nIn ECS:\n\n```yml\n# ecs.yaml\nservices:\n    ObjectCalisthenics\\Sniffs\\ControlStructures\\NoElseSniff: ~\n```\n\n---\n\n### 5. [Use Only One Object Operator (`-\u003e`) per Statement](http://williamdurand.fr/2013/06/03/object-calisthenics/#5-one-dot-per-line)\n\n:x:\n\n```php\n$this-\u003econtainer-\u003egetBuilder()-\u003eaddDefinition(SniffRunner::class);\n```\n\n:+1:\n\n```php\n$containerBuilder = $this-\u003egetContainerBuilder();\n$containerBuilder-\u003eaddDefinition(SniffRunner::class);\n```\n\n#### Use Only This Rule?\n\nIn PHP_CodeSniffer:\n\n```bash\nvendor/bin/phpcs ... --sniffs=ObjectCalisthenics.CodeAnalysis.OneObjectOperatorPerLine\n```\n\nIn ECS:\n\n```yml\n# ecs.yaml\nservices:\n    ObjectCalisthenics\\Sniffs\\CodeAnalysis\\OneObjectOperatorPerLineSniff: ~\n```\n\n#### :wrench: Configurable\n\nIn PHP_CodeSniffer:\n\n```xml\n\u003c?xml version=\"1.0\"?\u003e\n\u003cruleset name=\"my-project\"\u003e\n    \u003crule ref=\"ObjectCalisthenics.CodeAnalysis.OneObjectOperatorPerLine\"\u003e\n        \u003cproperties\u003e\n            \u003cproperty name=\"variablesHoldingAFluentInterface\" type=\"array\" value=\"$queryBuilder,$containerBuilder\"/\u003e\n            \u003cproperty name=\"methodsStartingAFluentInterface\" type=\"array\" value=\"createQueryBuilder\"/\u003e\n            \u003cproperty name=\"methodsEndingAFluentInterface\" type=\"array\" value=\"execute,getQuery\"/\u003e\n        \u003c/properties\u003e\n    \u003c/rule\u003e\n\u003c/ruleset\u003e\n```\n\nIn ECS:\n\n```yaml\nservices:\n    ObjectCalisthenics\\Sniffs\\CodeAnalysis\\OneObjectOperatorPerLineSniff:\n        variablesHoldingAFluentInterface: [\"$queryBuilder\", \"$containerBuilder\"]\n        methodsStartingAFluentInterface: [\"createQueryBuilder\"]\n        methodsEndingAFluentInterface: [\"execute\", \"getQuery\"]\n```\n\n---\n\n### 6. [Do not Abbreviate](http://williamdurand.fr/2013/06/03/object-calisthenics/#6-dont-abbreviate)\n\nThis is related to class, trait, interface, constant, function and variable names.\n\n:x:\n\n```php\nclass EM\n{\n    // ...\n}\n```\n\n:+1:\n\n```php\nclass EntityMailer\n{\n    // ...\n}\n```\n\n#### Use Only This Rule?\n\nIn PHP_CodeSniffer:\n\n```bash\nvendor/bin/phpcs ... --sniffs=ObjectCalisthenics.NamingConventions.ElementNameMinimalLength\n```\n\nIn ECS:\n\n```yml\n# ecs.yaml\nservices:\n    ObjectCalisthenics\\Sniffs\\NamingConventions\\ElementNameMinimalLengthSniff: ~\n```\n\n#### :wrench: Configurable\n\nIn PHP_CodeSniffer:\n\n```xml\n\u003c?xml version=\"1.0\"?\u003e\n\u003cruleset name=\"my-project\"\u003e\n    \u003crule ref=\"ObjectCalisthenics.NamingConventions.ElementNameMinimalLength\"\u003e\n        \u003cproperties\u003e\n            \u003cproperty name=\"minLength\" value=\"3\"/\u003e\n            \u003cproperty name=\"allowedShortNames\" type=\"array\" value=\"i,id,to,up\"/\u003e\n        \u003c/properties\u003e\n    \u003c/rule\u003e\n\u003c/ruleset\u003e\n```\n\nIn ECS:\n\n```yaml\n# ecs.yaml\nservices:\n    ObjectCalisthenics\\Sniffs\\NamingConventions\\ElementNameMinimalLengthSniff:\n        minLength: 3\n        allowedShortNames: [\"i\", \"id\", \"to\", \"up\"]\n```\n\n---\n\n### 7. [Keep Your Classes Small](http://williamdurand.fr/2013/06/03/object-calisthenics/#7-keep-all-entities-small)\n\n:x:\n\n```php\nclass SimpleStartupController\n{\n    // 300 lines of code\n}\n```\n\n:+1:\n\n```php\nclass SimpleStartupController\n{\n    // 50 lines of code\n}\n```\n\n:x:\n\n```php\nclass SomeClass\n{\n    public function simpleLogic()\n    {\n        // 30 lines of code\n    }\n}\n```\n\n:+1:\n\n```php\nclass SomeClass\n{\n    public function simpleLogic()\n    {\n        // 10 lines of code\n    }\n}\n```\n\n:x:\n\n```php\nclass SomeClass\n{\n    // 20 properties\n}\n```\n\n:+1:\n\n```php\nclass SomeClass\n{\n    // 5 properties\n}\n```\n\n:x:\n\n```php\nclass SomeClass\n{\n    // 20 methods\n}\n```\n\n:+1:\n\n```php\nclass SomeClass\n{\n    // 5 methods\n}\n```\n\n\n#### Use Only This Rule?\n\nIn PHP_CodeSniffer:\n\n```bash\nvendor/bin/phpcs ... --sniffs=ObjectCalisthenics.Files.ClassTraitAndInterfaceLength,ObjectCalisthenics.Files.FunctionLength,ObjectCalisthenics.Metrics.MethodPerClassLimit,ObjectCalisthenics.Metrics.PropertyPerClassLimit\n```\n\nIn ECS:\n\n```yml\n# ecs.yaml\nservices:\n    ObjectCalisthenics\\Sniffs\\Files\\ClassTraitAndInterfaceLengthSniff: ~\n    ObjectCalisthenics\\Sniffs\\Files\\FunctionLengthSniff: ~\n    ObjectCalisthenics\\Sniffs\\Metrics\\MethodPerClassLimitSniff: ~\n    ObjectCalisthenics\\Sniffs\\Metrics\\PropertyPerClassLimitSniff: ~\n```\n\n#### :wrench: Configurable\n\nIn PHP_CodeSniffer:\n\n```xml\n\u003c?xml version=\"1.0\"?\u003e\n\u003cruleset name=\"my-project\"\u003e\n    \u003crule ref=\"ObjectCalisthenics.Files.ClassTraitAndInterfaceLength\"\u003e\n        \u003cproperties\u003e\n            \u003cproperty name=\"maxLength\" value=\"200\"/\u003e\n        \u003c/properties\u003e\n    \u003c/rule\u003e\n    \u003crule ref=\"ObjectCalisthenics.Files.FunctionLength\"\u003e\n        \u003cproperties\u003e\n            \u003cproperty name=\"maxLength\" value=\"20\"/\u003e\n        \u003c/properties\u003e\n    \u003c/rule\u003e\n    \u003crule ref=\"ObjectCalisthenics.Metrics.PropertyPerClassLimit\"\u003e\n        \u003cproperties\u003e\n            \u003cproperty name=\"maxCount\" value=\"10\"/\u003e\n        \u003c/properties\u003e\n    \u003c/rule\u003e\n    \u003crule ref=\"ObjectCalisthenics.Metrics.MethodPerClassLimit\"\u003e\n        \u003cproperties\u003e\n            \u003cproperty name=\"maxCount\" value=\"10\"/\u003e\n        \u003c/properties\u003e\n    \u003c/rule\u003e\n\u003c/ruleset\u003e\n```\n\nIn ECS:\n\n```yaml\n# ecs.yaml\nservices:\n    ObjectCalisthenics\\Sniffs\\Files\\ClassTraitAndInterfaceLengthSniff:\n        maxLength: 200\n    ObjectCalisthenics\\Sniffs\\Files\\FunctionLengthSniff:\n        maxLength: 20\n    ObjectCalisthenics\\Sniffs\\Metrics\\PropertyPerClassLimitSniff:\n        maxCount: 10\n    ObjectCalisthenics\\Sniffs\\Metrics\\MethodPerClassLimitSniff:\n        maxCount: 10\n```\n\n---\n\n### 9. [Do not Use Getters and Setters](http://williamdurand.fr/2013/06/03/object-calisthenics/#9-no-getterssettersproperties)\n\nThis rules is partially related to [Domain Driven Design](https://github.com/dddinphp).\n\n- Classes should not contain public properties.\n- Method should [represent behavior](http://whitewashing.de/2012/08/22/building_an_object_model__no_setters_allowed.html), not set values.\n\n:x:\n\n```php\nclass ImmutableBankAccount\n{\n    public $currency = 'USD';\n```\n```php\n    private $amount;\n\n    public function setAmount(int $amount)\n    {\n        $this-\u003eamount = $amount;\n    }\n}\n```\n\n:+1:\n\n```php\nclass ImmutableBankAccount\n{\n    private $currency = 'USD';\n```\n```php\n    private $amount;\n\n    public function withdrawAmount(int $withdrawnAmount)\n    {\n        $this-\u003eamount -= $withdrawnAmount;\n    }\n}\n```\n\n#### Use Only This Rule?\n\nIn PHP_CodeSniffer:\n\n```bash\nvendor/bin/phpcs ... --sniffs=ObjectCalisthenics.Classes.ForbiddenPublicProperty,ObjectCalisthenics.NamingConventions.NoSetter\n```\n\nIn ECS:\n\n```yml\n# ecs.yaml\nservices:\n    ObjectCalisthenics\\Sniffs\\Classes\\ForbiddenPublicPropertySniff: ~\n    ObjectCalisthenics\\Sniffs\\NamingConventions\\NoSetterSniff: ~\n```\n\n#### :wrench: Configurable\n\nIn PHP_CodeSniffer:\n\n```xml\n\u003c?xml version=\"1.0\"?\u003e\n\u003cruleset name=\"my-project\"\u003e\n    \u003crule ref=\"ObjectCalisthenics.NamingConventions.NoSetter\"\u003e\n        \u003cproperties\u003e\n            \u003cproperty name=\"allowedClasses\" type=\"array\" value=\"*\\DataObject\"/\u003e\n        \u003c/properties\u003e\n    \u003c/rule\u003e\n\u003c/ruleset\u003e\n```\n\nIn ECS:\n\n```yaml\n# ecs.yaml\nservices:\n    ObjectCalisthenics\\Sniffs\\NamingConventions\\NoSetterSniff:\n        allowedClasses: \n            - '*\\DataObject'\n```\n\n---\n\n### Not Implemented Rules - Too Strict, Vague or Annoying\n\nWhile 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).\n\n**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).\n\n**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.\n\n**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)** -\nThis depends on individual domain of each project. It doesn't make sense to make a rule for that.\n\n---\n\n## 3 Rules for Contributing\n\n- **1 feature per PR**\n- every new feature **must be covered by tests**\n- **all tests** and **style checks must pass**\n\n    ```bash\n    composer complete-check\n    ```\n\nWe will be happy to merge your feature then.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fobject-calisthenics%2Fphpcs-calisthenics-rules","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fobject-calisthenics%2Fphpcs-calisthenics-rules","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fobject-calisthenics%2Fphpcs-calisthenics-rules/lists"}