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

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

artemeon/cognitive-complexity
=============================

PHPStan rules to measure cognitive complexity of your classes and methods

07.5k↓14.5%PHP

Since Nov 4Pushed 8mo agoCompare

[ Source](https://github.com/artemeon/cognitive-complexity)[ Packagist](https://packagist.org/packages/artemeon/cognitive-complexity)[ RSS](/packages/artemeon-cognitive-complexity/feed)WikiDiscussions main Synced yesterday

READMEChangelogDependenciesVersions (1)Used By (0)

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 artemeon/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

24

—

LowBetter than 31% of packages

Maintenance43

Moderate activity, may be stable

Popularity25

Limited adoption so far

Community9

Small or concentrated contributor base

Maturity13

Early-stage or recently created project

 Bus Factor1

Top contributor holds 78.4% 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.

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/16920430?v=4)[ARTEMEON](/maintainers/artemeon)[@artemeon](https://github.com/artemeon)

---

Top Contributors

[![TomasVotruba](https://avatars.githubusercontent.com/u/924196?v=4)](https://github.com/TomasVotruba "TomasVotruba (29 commits)")[![marcreichel](https://avatars.githubusercontent.com/u/7645035?v=4)](https://github.com/marcreichel "marcreichel (4 commits)")[![staabm](https://avatars.githubusercontent.com/u/120441?v=4)](https://github.com/staabm "staabm (4 commits)")

### Embed Badge

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

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

###  Alternatives

[dms/phpunit-arraysubset-asserts

This package provides ArraySubset and related asserts once deprecated in PHPUnit 8

14429.2M361](/packages/dms-phpunit-arraysubset-asserts)[gong023/assert_chain

enable you to use phpunit assert with method chain

1478.9k3](/packages/gong023-assert-chain)

PHPackages © 2026

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