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

03.5k—8.1%PHP

Since Nov 4Pushed 6mo 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 1mo ago

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 32% of packages

Maintenance47

Moderate activity, may be stable

Popularity23

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://www.gravatar.com/avatar/5c87366e3237b8eed0a163e8a3c14a76418d7a82d34911d205585457a361ea8d?d=identicon)[artemeon](/maintainers/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

[phpspec/prophecy

Highly opinionated mocking framework for PHP 5.3+

8.5k551.7M682](/packages/phpspec-prophecy)[brianium/paratest

Parallel testing for PHP

2.5k118.8M754](/packages/brianium-paratest)[beberlei/assert

Thin assertion library for input validation in business models.

2.4k96.9M570](/packages/beberlei-assert)[mikey179/vfsstream

Virtual file system to mock the real file system in unit tests.

1.4k108.0M2.7k](/packages/mikey179-vfsstream)[orchestra/testbench

Laravel Testing Helper for Packages Development

2.2k39.1M32.1k](/packages/orchestra-testbench)[phpspec/phpspec

Specification-oriented BDD framework for PHP 7.1+

1.9k36.7M3.1k](/packages/phpspec-phpspec)

PHPackages © 2026

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