Download the PHP package ncac/php-cognitive-complexity without Composer
On this page you can find all versions of the php package ncac/php-cognitive-complexity. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download ncac/php-cognitive-complexity
More information about ncac/php-cognitive-complexity
Files in ncac/php-cognitive-complexity
Package php-cognitive-complexity
Short Description CLI tool for measuring PHP cognitive complexity (ISO SonarQube) — integrates with CI/CD pipelines and Husky pre-commit hooks
License MIT
Homepage https://github.com/ncac/php-cognitive-complexity
Informations about the package php-cognitive-complexity
PHP Cognitive Complexity
A modern PHP CLI tool for measuring cognitive complexity of PHP code — ISO SonarQube (G. Ann Campbell's specification).
Two commands under one binary: check for CI/hooks (strict gate, exit 1 on violation), analyse for interactive exploration (coloured output, severity levels, always exit 0).
Why cognitive complexity?
Cyclomatic complexity counts paths. Cognitive complexity measures how hard code is to read. It penalises deep nesting and rewards early returns — much closer to human perception of code quality.
This tool implements the SonarSource specification for PHP.
Installation
Usage
check — CI gate
Strict mode: exits 1 on any violation. Designed for pre-commit hooks and CI pipelines.
analyse — interactive exploration
Developer-experience mode: coloured output grouped by file, severity labels, always exits 0.
Example output:
Severity levels (multiples of threshold, default 15):
| Level | Score range |
|---|---|
minor |
threshold+1 → 2×threshold |
moderate |
2×threshold+1 → 3×threshold |
high |
3×threshold+1 → 50 |
critical |
> 50 |
Generate a baseline (ignore pre-existing violations)
Configuration file
Create a cognitive.yaml at the root of your project:
Console output example
This is the output of check. For richer output, use analyse.
Integration in your project
Add convenience scripts to your composer.json for fixed targets:
For ad-hoc analysis on a specific path, call cc — the short wrapper installed alongside the main binary:
To ignore pre-existing violations during onboarding, generate a baseline first:
GitLab CI/CD integration
Algorithm
Based on G. Ann Campbell's Cognitive Complexity specification:
| Structure | Increment |
|---|---|
if, else if, else |
+1 |
for, foreach, while, do-while |
+1 |
switch |
+1 |
catch |
+1 |
Ternary ?: |
+1 |
&&, \|\| (logical sequence changes) |
+1 |
| Each nested level | +level bonus |
Development
License
All versions of php-cognitive-complexity with dependencies
nikic/php-parser Version ^5.0
revolt/event-loop Version >=1.0.0 <1.0.7
symfony/console Version ^6.0 || ^7.0
symfony/finder Version ^6.0 || ^7.0
symfony/yaml Version ^6.0 || ^7.0