PHPackages                             phauthentic/cognitive-code-analysis - 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. [Utility &amp; Helpers](/categories/utility)
4. /
5. phauthentic/cognitive-code-analysis

ActiveProject[Utility &amp; Helpers](/categories/utility)

phauthentic/cognitive-code-analysis
===================================

1.11.1(2w ago)5112.8k↓75.8%3[3 issues](https://github.com/Phauthentic/cognitive-code-analysis/issues)GPL-3.0-onlyPHPPHP ^8.1CI failing

Since Aug 17Pushed 2w ago3 watchersCompare

[ Source](https://github.com/Phauthentic/cognitive-code-analysis)[ Packagist](https://packagist.org/packages/phauthentic/cognitive-code-analysis)[ RSS](/packages/phauthentic-cognitive-code-analysis/feed)WikiDiscussions master Synced 1w ago

READMEChangelog (10)Dependencies (47)Versions (34)Used By (0)

Cognitive Complexity Analysis
=============================

[](#cognitive-complexity-analysis)

Cognitive Code Analysis is an approach to understanding and improving code by focusing on how human cognition interacts with code. It emphasizes making code more readable, understandable, and maintainable by considering the cognitive processes of the developers who write and work with the code.

> "Human short-term or working memory was estimated to be limited to 7 ± 2 variables in the 1950s. A more current estimate is 4 ± 1 constructs. Decision quality generally becomes degraded once this limit of four constructs is exceeded."

[Source: Human Cognitive Limitations. Broad, Consistent, Clinical Application of Physiological Principles Will Require Decision Support](https://www.ncbi.nlm.nih.gov/pmc/articles/PMC5822395/)

---

A [c# version](https://github.com/floriankraemer/csharp-cognitive-code-analysis) and a [Java version](https://github.com/floriankraemer/cognitive-code-analysis-java) are also available.

Features 💎
----------

[](#features-)

- Cognitive Complexity Analysis:
    - Calculates a cognitive complexity score for each class and method
    - Provides detailed cognitive complexity metrics
    - Generate reports in various formats (JSON, CSV, HTML, Markdown, Checkstyle XML, JUnit XML, SARIF, GitLab Code Quality, GitHub Actions)
    - Baseline comparison to track complexity changes over time
    - Configurable thresholds and weights for complexity analysis
    - Optional result cache for faster subsequent runs (must be enabled in config)
    - Custom report generators
    - Also provides Halstead Complexity Metrics (must be enabled in config)
    - Also provides Cyclomatic Complexity Metrics (must be enabled in config)
- Cognitive Complexity Churn Analysis to identify hotspots in the codebase
    - Generate reports in various formats (JSON, CSV, HTML)
    - Custom report generators

Installation ⚙️
---------------

[](#installation-️)

```
composer require --dev phauthentic/cognitive-code-analysis
```

Running it 🧑‍💻
--------------

[](#running-it-‍)

Create a project configuration file:

```
bin/phpcca init                  # interactive setup → creates phpcca.yaml in current directory
bin/phpcca init --silent         # non-interactive, all defaults
bin/phpcca init --path=/path/to/phpcca.yaml
```

When `phpcca.yaml` exists in the current working directory, `analyse` and `churn` load it automatically. Use `--config` to specify a different file.

Cognitive Complexity Analysis

```
bin/phpcca analyse
bin/phpcca analyse  --config=custom.yaml  # explicit config overrides auto-discovery
```

Generate a report, supported types are `json`, `csv`, `html`, `markdown`, `checkstyle`, `junit`, `sarif`, `gitlab-codequality`, `github-actions`.

```
bin/phpcca analyse  --report-type json --report-file cognitive.json
```

You can also pass a baseline file to compare the results to. The JSON report is used as baseline. The output will now show a delta if a value was changed.

```
bin/phpcca analyse  --baseline cognitive.json
```

### Finding Hotspots

[](#finding-hotspots)

Churn is a measure of how much code has changed over time. It helps to find the most changed and complex areas in your codebase, which are often the most error-prone and difficult to maintain. Read the [Churn - Finding Hotspots](./docs/Churn-Finding-Hotspots.md) documentation for more details.

Note that this requires a version control system (VCS) to be set up, such as Git.

```
bin/phpcca churn
```

Documentation 📚
---------------

[](#documentation-)

- [Cognitive Complexity Analysis](./docs/Cognitive-Complexity-Analysis.md#cognitive-complexity-analysis)
    - [Why bother?](./docs/Cognitive-Complexity-Analysis.md#why-bother)
    - [What is the difference to Cyclomatic Complexity?](./docs/Cognitive-Complexity-Analysis.md#what-is-the-difference-to-cyclomatic-complexity)
    - [How is Cognitive Complexity calculated?](./docs/Cognitive-Complexity-Analysis.md#how-is-cognitive-complexity-calculated)
    - [Metrics Collected](./docs/Cognitive-Complexity-Analysis.md#metrics-collected)
    - [Result Interpretation](./docs/Cognitive-Complexity-Analysis.md#result-interpretation)
    - [Churn - Finding Hotspots](./docs/Churn-Finding-Hotspots.md)
    - [Sorting and Filtering](./docs/Sorting-and-Filtering.md)
    - [Configuration](./docs/Configuration.md#configuration)
        - [Tuning the calculation](./docs/Configuration.md#tuning-the-calculation)
    - [CI Integration](./docs/CI-Integration.md)
    - [Examples](#examples)
        - [Wordpress WP\_Debug\_Data](#wordpress-wp_debug_data)
        - [Doctrine Paginator](#doctrine-paginator)
    - [Reporting Issues](#reporting-issues)

Resources 🔗
-----------

[](#resources-)

These pages and papers provide more information on cognitive limitations and readability and the impact on the business.

- **Cognitive Complexity**
    - [Cognitive Complexity Wikipedia](https://en.wikipedia.org/wiki/Cognitive_complexity)
    - [Cognitive Complexity and Its Effect on the Code](https://www.baeldung.com/java-cognitive-complexity) by Emanuel Trandafir.
    - [Human Cognitive Limitations. Broad, Consistent, Clinical Application of Physiological Principles Will Require Decision Support](https://www.ncbi.nlm.nih.gov/pmc/articles/PMC5822395/) by Alan H. Morris.
    - [The Magical Number 4 in Short-Term Memory: A Reconsideration of Mental Storage Capacity](https://www.researchgate.net/publication/11830840_The_Magical_Number_4_in_Short-Term_Memory_A_Reconsideration_of_Mental_Storage_Capacity) by Nelson Cowan
    - [Neural substrates of cognitive capacity limitations](https://www.ncbi.nlm.nih.gov/pmc/articles/PMC3131328/) by Timothy J. Buschman,a,1 Markus Siegel,a,b Jefferson E. Roy, and Earl K. Millera.
    - [Code Readability Testing, an Empirical Study](https://www.researchgate.net/publication/299412540_Code_Readability_Testing_an_Empirical_Study) by Todd Sedano.
    - [An Empirical Validation of Cognitive Complexity as a Measure of Source Code Understandability](https://arxiv.org/pdf/2007.12520) by Marvin Muñoz Barón, Marvin Wyrich, and Stefan Wagner.
- **Halstead Complexity**
    - [Halstead Complexity](https://en.wikipedia.org/wiki/Halstead_complexity_measures)
- **Cyclomatic Complexity**
    - [Cyclomatic Complexity](https://en.wikipedia.org/wiki/Cyclomatic_complexity)
- **Sonars Understandability / Cognitive Complexity**
    - [{Cognitive Complexity} a new way of measuring understandability](https://www.sonarsource.com/docs/CognitiveComplexity.pdf)

Calculated Metrics
------------------

[](#calculated-metrics)

The tool computes complementary complexity metrics at class and method level. Optional metrics are enabled in [`phpcca.yaml`](./docs/Configuration.md#configuration).

CategoryMetricDescription[Cognitive Complexity](./docs/Cognitive-Complexity-Analysis.md)**Score**Weighted sum of structural inputs (lines, arguments, variables, branching, etc.); each input contributes logarithmically above a configurable threshold[Understandability](./docs/Understandability.md)**Score**Sonar-style control-flow complexity, separate from the weighted cognitive score; risk bands: 0–5 low, 6–10 medium, 11–15 high, 16+ very high[Cyclomatic Complexity](./docs/Cyclomatic-Complexity-Analysis.md)**Score**Number of linearly independent paths: base 1 plus decision points and logical operators; risk bands: 1–5 low, 6–10 medium, 11–15 high, 16+ very high[Halstead](./docs/Halstead-Analysis.md)VolumeImplementation size derived from operators and operands (`N × log₂(n)`)HalsteadDifficultyEffort required to understand the code (`(n₁ / 2) × (N₂ / n₂)`)HalsteadEffortMental effort to develop or maintain the code (`D × V`)Examples 📖
----------

[](#examples-)

### Cognitive Metrics

[](#cognitive-metrics)

#### Wordpress WP\_Debug\_Data

[](#wordpress-wp_debug_data)

```
Class: \WP_Debug_Data
┌───────────────────┬──────────────┬───────────┬─────────┬─────────────┬────────────┬────────────┬────────────┬────────────┬────────────┐
│ Method Name       │ Lines        │ Arguments │ Returns │ Variables   │ Property   │ If         │ If Nesting │ Else       │ Cognitive  │
│                   │              │           │         │             │ Accesses   │            │ Level      │            │ Complexity │
├───────────────────┼──────────────┼───────────┼─────────┼─────────────┼────────────┼────────────┼────────────┼────────────┼────────────┤
│ check_for_updates │ 6 (0)        │ 0 (0)     │ 0 (0)   │ 0 (0)       │ 0 (0)      │ 0 (0)      │ 0 (0)      │ 0 (0)      │ 0          │
│ debug_data        │ 1230 (6.373) │ 0 (0)     │ 1 (0)   │ 105 (3.073) │ 20 (0.788) │ 58 (4.025) │ 3 (1.099)  │ 33 (3.497) │ 18.855     │
│ get_wp_constants  │ 144 (3.761)  │ 0 (0)     │ 1 (0)   │ 9 (0.875)   │ 0 (0)      │ 5 (1.099)  │ 1 (0)      │ 5 (1.609)  │ 7.345      │
│ get_wp_filesystem │ 60 (0)       │ 0 (0)     │ 1 (0)   │ 9 (0.875)   │ 0 (0)      │ 1 (0)      │ 1 (0)      │ 0 (0)      │ 0.875      │
│ get_mysql_var     │ 15 (0)       │ 1 (0)     │ 2 (0)   │ 2 (0)       │ 0 (0)      │ 1 (0)      │ 1 (0)      │ 0 (0)      │ 0          │
│ format            │ 60 (0)       │ 2 (0)     │ 1 (0)   │ 11 (1.03)   │ 0 (0)      │ 5 (1.099)  │ 1 (0)      │ 5 (1.609)  │ 3.738      │
│ get_database_size │ 14 (0)       │ 0 (0)     │ 1 (0)   │ 4 (0.336)   │ 1 (0)      │ 1 (0)      │ 1 (0)      │ 0 (0)      │ 0.336      │
│ get_sizes         │ 125 (3.512)  │ 0 (0)     │ 1 (0)   │ 14 (1.224)  │ 0 (0)      │ 9 (1.946)  │ 2 (0.693)  │ 5 (1.609)  │ 8.984      │
└───────────────────┴──────────────┴───────────┴─────────┴─────────────┴────────────┴────────────┴────────────┴────────────┴────────────┘
```

#### Doctrine Paginator

[](#doctrine-paginator)

```
Class: Doctrine\ORM\Tools\Pagination\Paginator
┌───────────────────────────────────────────┬────────┬───────────┬─────────┬───────────┬──────────┬───────┬────────────┬───────────┬────────────┐
│ Method Name                               │ Lines  │ Arguments │ Returns │ Variables │ Property │ If    │ If Nesting │ Else      │ Cognitive  │
│                                           │        │           │         │           │ Accesses │       │ Level      │           │ Complexity │
├───────────────────────────────────────────┼────────┼───────────┼─────────┼───────────┼──────────┼───────┼────────────┼───────────┼────────────┤
│ __construct                               │ 10 (0) │ 2 (0)     │ 0 (0)   │ 1 (0)     │ 1 (0)    │ 1 (0) │ 1 (0)      │ 0 (0)     │ 0          │
│ getQuery                                  │ 4 (0)  │ 0 (0)     │ 1 (0)   │ 1 (0)     │ 1 (0)    │ 0 (0) │ 0 (0)      │ 0 (0)     │ 0          │
│ getFetchJoinCollection                    │ 4 (0)  │ 0 (0)     │ 1 (0)   │ 1 (0)     │ 1 (0)    │ 0 (0) │ 0 (0)      │ 0 (0)     │ 0          │
│ getUseOutputWalkers                       │ 4 (0)  │ 0 (0)     │ 1 (0)   │ 1 (0)     │ 1 (0)    │ 0 (0) │ 0 (0)      │ 0 (0)     │ 0          │
│ setUseOutputWalkers                       │ 6 (0)  │ 1 (0)     │ 1 (0)   │ 1 (0)     │ 1 (0)    │ 0 (0) │ 0 (0)      │ 0 (0)     │ 0          │
│ count                                     │ 12 (0) │ 0 (0)     │ 1 (0)   │ 1 (0)     │ 1 (0)    │ 1 (0) │ 1 (0)      │ 0 (0)     │ 0          │
│ getIterator                               │ 46 (0) │ 0 (0)     │ 2 (0)   │ 9 (0.875) │ 2 (0)    │ 3 (0) │ 2 (0.693)  │ 2 (0.693) │ 2.262      │
│ cloneQuery                                │ 13 (0) │ 1 (0)     │ 1 (0)   │ 3 (0.182) │ 0 (0)    │ 0 (0) │ 0 (0)      │ 0 (0)     │ 0.182      │
│ useOutputWalker                           │ 8 (0)  │ 1 (0)     │ 2 (0)   │ 1 (0)     │ 1 (0)    │ 1 (0) │ 1 (0)      │ 0 (0)     │ 0          │
│ appendTreeWalker                          │ 11 (0) │ 2 (0)     │ 0 (0)   │ 1 (0)     │ 0 (0)    │ 1 (0) │ 1 (0)      │ 0 (0)     │ 0          │
│ getCountQuery                             │ 25 (0) │ 0 (0)     │ 1 (0)   │ 4 (0.336) │ 1 (0)    │ 2 (0) │ 1 (0)      │ 1 (0)     │ 0.336      │
│ unbindUnusedQueryParams                   │ 17 (0) │ 1 (0)     │ 0 (0)   │ 6 (0.588) │ 0 (0)    │ 1 (0) │ 1 (0)      │ 0 (0)     │ 0.588      │
│ convertWhereInIdentifiersToDatabaseValues │ 11 (0) │ 1 (0)     │ 1 (0)   │ 5 (0.47)  │ 1 (0)    │ 0 (0) │ 0 (0)      │ 0 (0)     │ 0.47       │
└───────────────────────────────────────────┴────────┴───────────┴─────────┴───────────┴──────────┴───────┴────────────┴───────────┴────────────┘
```

Reporting Issues 🪲
------------------

[](#reporting-issues-)

If you find a bug or have a feature request, please open an issue on the [GitHub repository](https://github.com/Phauthentic/cognitive-code-analysis/issues/new).

Especially the AST-parser used under the hood to analyse the code might have issues with certain code constructs, so please provide a minimal example that reproduces the issue.

License ⚖️
----------

[](#license-️)

Copyright [Florian Krämer](https://florian-kraemer.net/)

Licensed under the [GPL3 license](LICENSE).

###  Health Score

55

—

FairBetter than 97% of packages

Maintenance96

Actively maintained with recent releases

Popularity38

Limited adoption so far

Community10

Small or concentrated contributor base

Maturity60

Established project with proven stability

 Bus Factor1

Top contributor holds 100% 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 ~29 days

Recently: every ~57 days

Total

23

Last Release

16d ago

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/4996022?v=4)[Florian Krämer](/maintainers/floriankraemer)[@floriankraemer](https://github.com/floriankraemer)

---

Top Contributors

[![floriankraemer](https://avatars.githubusercontent.com/u/4996022?v=4)](https://github.com/floriankraemer "floriankraemer (82 commits)")

---

Tags

churnchurn-analysiscode-analysiscode-analyzercognitive-analysiscognitive-metricshalsteadphp

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Code StylePHP\_CodeSniffer

Type Coverage Yes

### Embed Badge

![Health badge](/badges/phauthentic-cognitive-code-analysis/health.svg)

```
[![Health](https://phpackages.com/badges/phauthentic-cognitive-code-analysis/health.svg)](https://phpackages.com/packages/phauthentic-cognitive-code-analysis)
```

###  Alternatives

[sulu/sulu

Core framework that implements the functionality of the Sulu content management system

1.3k1.4M195](/packages/sulu-sulu)[shopware/core

Shopware platform is the core for all Shopware ecommerce products.

585.4M506](/packages/shopware-core)[aeliot/todo-registrar

Register TODOs from source code in issue tracker

153.0k](/packages/aeliot-todo-registrar)

PHPackages © 2026

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