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.0.0(1y ago)1475.0M↑13.5%3[1 issues](https://github.com/TomasVotruba/cognitive-complexity/issues)20MITPHPPHP ^7.4 || ^8.0CI passing

Since Dec 23Pushed 1mo 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 1mo ago

READMEChangelogDependencies (1)Versions (20)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

57

—

FairBetter than 98% of packages

Maintenance67

Regular maintenance activity

Popularity58

Moderate usage in the ecosystem

Community31

Small or concentrated contributor base

Maturity60

Established project with proven stability

 Bus Factor1

Top contributor holds 85.3% 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 ~40 days

Recently: every ~115 days

Total

19

Last Release

521d ago

Major Versions

0.2.3 → 1.0.02024-12-13

PHP version history (4 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

### 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 (29 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)")

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

[larastan/larastan

Larastan - Discover bugs in your code without running it. A phpstan/phpstan extension for Laravel

6.4k43.5M5.2k](/packages/larastan-larastan)[phpstan/phpstan-symfony

Symfony Framework extensions and rules for PHPStan

78768.9M1.5k](/packages/phpstan-phpstan-symfony)[phpstan/phpstan-doctrine

Doctrine extensions for PHPStan

66766.6M1.1k](/packages/phpstan-phpstan-doctrine)[phpat/phpat

PHP Architecture Tester

1.2k3.5M32](/packages/phpat-phpat)[spaze/phpstan-disallowed-calls

PHPStan rules to detect disallowed method &amp; function calls, constant, namespace, attribute, property &amp; superglobal usages, with powerful rules to re-allow a call or a usage in places where it should be allowed.

33320.0M375](/packages/spaze-phpstan-disallowed-calls)[mglaman/phpstan-drupal

Drupal extension and rules for PHPStan

20829.0M124](/packages/mglaman-phpstan-drupal)

PHPackages © 2026

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