PHPackages                             tomasvotruba/cognitive-complexity - 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. tomasvotruba/cognitive-complexity

ActivePhpstan-extension[Testing &amp; Quality](/categories/testing)

tomasvotruba/cognitive-complexity
=================================

PHPStan rules to measure cognitive complexity of your classes and methods

1.2.0(3w ago)1635.6M↓34.5%320MITPHPPHP ^8.3CI passing

Since Dec 23Pushed 3w ago3 watchersCompare

[ Source](https://github.com/TomasVotruba/cognitive-complexity)[ Packagist](https://packagist.org/packages/tomasvotruba/cognitive-complexity)[ Fund](https://www.paypal.me/rectorphp)[ GitHub Sponsors](https://github.com/tomasvotruba)[ RSS](/packages/tomasvotruba-cognitive-complexity/feed)WikiDiscussions main Synced 2d ago

READMEChangelogDependencies (14)Versions (23)Used By (20)

Keep Cognitive Complexity Down
==============================

[](#keep-cognitive-complexity-down)

Cognitive complexity tells us, how difficult code is to understand by a reader.

**How is cognitive complexity measured?**

```
function get_words_from_number(int $number): string
{
    $amountInWords = '';

    if ($number === 1) {            // + 1
        $amountInWords = 'one';
    } elseif ($number === 2) {      // + 1
        $amountInWords = 'couple';
    } elseif ($number === 3) {      // + 1
        $amountInWords = 'a few';
    } else {                        // + 1
        $amountInWords = 'a lot';
    }

    return $amountInWords;
}
```

This function uses nesting, conditions and continue back and forth. It's hard to read and results in **cognitive complexity of 4**.

How to keep **cognitive complexity on 1**? Read [Cognitive load is what matters](https://minds.md/zakirullin/cognitive) or [Sonar paper about cognitive complexity metrics](https://www.sonarsource.com/docs/CognitiveComplexity.pdf) that inspired this repository.

[![](https://github.com/zakirullin/cognitive-load/raw/main/img/cognitiveloadv5paper.png?raw=true)](https://github.com/zakirullin/cognitive-load/blob/main/img/cognitiveloadv5paper.png?raw=true)

Install
-------

[](#install)

```
composer require tomasvotruba/cognitive-complexity --dev
```

The package is available on PHP 7.4+.

Usage
-----

[](#usage)

With [PHPStan extension installer](https://github.com/phpstan/extension-installer), everything is ready to run.

Enable each item on their own with simple configuration:

```
# phpstan.neon
parameters:
    cognitive_complexity:
        class: 50
        function: 8
```

Detect complex Class Dependency Trees
-------------------------------------

[](#detect-complex-class-dependency-trees)

In classes like controllers, Rector rules, PHPStan rules or other services of specific type, the complexity can be hidden in the \_\_construct() dependencies. Simple class with 10 dependencies is more complex than complex class with 2 dependencies.

That's why there is a rule to detect these dependency trees. It checks:

- complexity of **current class**
- **constructor dependencies and their class complexity** together

Final number is compared and used as a final complexity:

```
# phpstan.neon
parameters:
    cognitive_complexity:
        dependency_tree: 150
        dependency_tree_types:
            # only these explicit types are checked, nothing else
            - Rector\Contract\Rector\RectorInterface
```

Happy coding!

###  Health Score

68

—

FairBetter than 99% of packages

Maintenance95

Actively maintained with recent releases

Popularity59

Moderate usage in the ecosystem

Community33

Small or concentrated contributor base

Maturity71

Established project with proven stability

 Bus Factor1

Top contributor holds 85.7% 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 ~60 days

Recently: every ~221 days

Total

22

Last Release

25d ago

Major Versions

0.2.3 → 1.0.02024-12-13

PHP version history (5 changes)0.0.1PHP ^8.1

0.0.1.72PHP ^7.2 || 8.0.\*

0.1.2PHP ^7.2 || ^8.0

1.0.0PHP ^7.4 || ^8.0

1.2.0PHP ^8.3

### Community

Maintainers

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

---

Top Contributors

[![TomasVotruba](https://avatars.githubusercontent.com/u/924196?v=4)](https://github.com/TomasVotruba "TomasVotruba (30 commits)")[![staabm](https://avatars.githubusercontent.com/u/120441?v=4)](https://github.com/staabm "staabm (4 commits)")[![SatoshiFujinaga](https://avatars.githubusercontent.com/u/38089943?v=4)](https://github.com/SatoshiFujinaga "SatoshiFujinaga (1 commits)")

###  Code Quality

TestsPHPUnit

Static AnalysisRector

Code StyleECS

### Embed Badge

![Health badge](/badges/tomasvotruba-cognitive-complexity/health.svg)

```
[![Health](https://phpackages.com/badges/tomasvotruba-cognitive-complexity/health.svg)](https://phpackages.com/packages/tomasvotruba-cognitive-complexity)
```

###  Alternatives

[behat/behat

Scenario-oriented BDD framework for PHP

4.0k101.8M2.2k](/packages/behat-behat)[phpstan/phpstan-symfony

Symfony Framework extensions and rules for PHPStan

79475.7M2.2k](/packages/phpstan-phpstan-symfony)[phpstan/phpstan-doctrine

Doctrine extensions for PHPStan

67272.8M1.4k](/packages/phpstan-phpstan-doctrine)[shipmonk/dead-code-detector

Dead code detector to find unused PHP code via PHPStan extension. Can automatically remove dead PHP code. Supports libraries like Symfony, Doctrine, PHPUnit etc. Detects dead cycles. Can detect dead code that is tested.

4853.5M91](/packages/shipmonk-dead-code-detector)[phparkitect/phparkitect

Enforce architectural constraints in your PHP applications

9224.3M28](/packages/phparkitect-phparkitect)[rector/rector-src

Instant Upgrade and Automated Refactoring of any PHP code

136406.3k14](/packages/rector-rector-src)

PHPackages © 2026

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