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:
How exclude: and paths: are matched
v2 change. In v1 these keys were matched relative to the path you passed on the CLI. As of v2.0.0 they are matched relative to the project root and support glob wildcards. See
MIGRATION.md.
- Project root = the directory containing
cognitive.yaml(or theroot:key, or the current directory when no config file is used). Every pattern and every reported path is relative to it, socheck web/,check web/sites/andcheck .all match config the same way — the CLI argument only narrows what is walked. - Anchored.
tests/matches the top-leveltests/directory, notvendor/phpunit/phpunit/tests/. Prefix with**/to match at any depth. - Wildcards:
*(within a path segment),**(spans directories),?(one character). Naming a directory covers its whole subtree.
| Pattern | Matches | Does not match |
|---|---|---|
vendor/ |
vendor/autoload.php |
app/vendor/x.php |
**/files/php/ |
web/sites/aaa/files/php/twig/x.php |
web/sites/aaa/files/phpstan.php |
src/*/Legacy/ |
src/Billing/Legacy/X.php |
src/Billing/Sub/Legacy/X.php |
**/*.blade.php |
resources/views/home.blade.php |
— |
Notes:
--diffmode honoursexclude:(it did not in v1).- A
--configpath that does not exist is now an error (exit code 2), not a silent fallback to defaults. - Reported paths (console,
--format=json,--format=gitlab) and baseline file keys are project-root-relative. Regenerate baselines when upgrading from v1.
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