定制 ncac/php-cognitive-complexity 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

ncac/php-cognitive-complexity

Composer 安装命令:

composer require ncac/php-cognitive-complexity

包简介

CLI tool for measuring PHP cognitive complexity (ISO SonarQube) — integrates with CI/CD pipelines and Husky pre-commit hooks

README 文档

README

CI codecov Packagist Version PHP Version License: MIT

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

composer require --dev ncac/php-cognitive-complexity

Usage

check — CI gate

Strict mode: exits 1 on any violation. Designed for pre-commit hooks and CI pipelines.

# Basic check
vendor/bin/cognitive-complexity check src/ --max=15

# With a config file
vendor/bin/cognitive-complexity check src/ --config=cognitive.yaml

# Only git-modified files (pre-commit)
vendor/bin/cognitive-complexity check src/ --max=15 --diff

# JSON output
vendor/bin/cognitive-complexity check src/ --format=json

# GitLab Code Quality artifact
vendor/bin/cognitive-complexity check src/ --format=gitlab > gl-code-quality-report.json

# Drupal/multi-extension projects
vendor/bin/cognitive-complexity check web/ --ext=php,module,inc,theme,install

analyse — interactive exploration

Developer-experience mode: coloured output grouped by file, severity labels, always exits 0.

# Show only violations (default)
vendor/bin/cognitive-complexity analyse src/

# Show all functions including those below threshold
vendor/bin/cognitive-complexity analyse src/ --all

# Sort by score descending (prioritise refactoring targets)
vendor/bin/cognitive-complexity analyse src/ --sort=score

# Custom threshold
vendor/bin/cognitive-complexity analyse src/ --max=20

# Drupal/multi-extension projects
vendor/bin/cognitive-complexity analyse web/ --ext=php,module,inc,theme,install

Example output:

── src/Service/OrderService.php
  [ 18]  processOrder:42 — minor
  [  4]  validateItems:91
  [  0]  formatResult:112

── src/Gateway/PaymentGateway.php
  [ 32]  validate:87 — moderate

✗ 2 violation(s) / 4 function(s) in 2 file(s) — threshold=15

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)

vendor/bin/cognitive-complexity baseline src/ --output=baseline.json
vendor/bin/cognitive-complexity check src/ --baseline=baseline.json

Configuration file

Create a cognitive.yaml at the root of your project:

# Default threshold
max_complexity: 15

# Per-path overrides
paths:
  src/Controller/: 10
  src/Service/: 12
  tests/: 20

# Excluded paths
exclude:
  - vendor/
  - cache/
  - legacy/

# File extensions to scan (default: php)
# CLI --ext overrides this list
extensions:
  - php
  - module
  - inc
  - theme
  - install

Console output example

✗ src/Service/OrderService.php::processOrder → 18 (max: 15) [line 42]
✗ src/Gateway/PaymentGateway.php::validate → 16 (max: 15) [line 87]

2 violation(s) found. Cognitive complexity threshold exceeded.

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:

{
  "scripts": {
    "cc": "cognitive-complexity check src/ --max=15",
    "cc:analyse": "cognitive-complexity analyse src/ --all --sort=score",
    "cc:baseline": "cognitive-complexity baseline src/ --max=15 --output=cognitive-baseline.json"
  }
}
composer cc              # CI gate — exit 1 on violations
composer cc:analyse      # interactive report sorted by score
composer cc:baseline     # regenerate the baseline after a refactoring

For ad-hoc analysis on a specific path, call cc — the short wrapper installed alongside the main binary:

# cc defaults to 'check' when no sub-command is given
vendor/bin/cc src/
vendor/bin/cc src/ --max=20
vendor/bin/cc src/ --diff                        # pre-commit: git-modified files only

# Explicit sub-commands
vendor/bin/cc analyse web/modules/custom/ --all
vendor/bin/cc analyse web/modules/custom/ --sort=score --ext=php,module,inc
vendor/bin/cc baseline src/ --output=cognitive-baseline.json

To ignore pre-existing violations during onboarding, generate a baseline first:

composer cc:baseline
# then add --baseline to your cc script:
# "cc": "cognitive-complexity check src/ --max=15 --baseline=cognitive-baseline.json"
# .husky/pre-commit
vendor/bin/cognitive-complexity check src/ --max=15

GitLab CI/CD integration

# .gitlab-ci.yml
cognitive_complexity:
  stage: quality
  script:
    - php vendor/bin/cognitive-complexity check src/ --format=gitlab > gl-code-quality-report.json
  artifacts:
    reports:
      codequality: gl-code-quality-report.json

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

git clone https://github.com/ncac/php-cognitive-complexity.git
cd php-cognitive-complexity
composer install

# Run tests
composer test

# Run all checks
composer check

# Coverage
composer test-coverage

License

MIT © Nicolas Catrice-Auzon Cape

ncac/php-cognitive-complexity 适用场景与选型建议

ncac/php-cognitive-complexity 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 153 次下载、GitHub Stars 达 1, 最近一次更新时间为 2026 年 05 月 12 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「php」 「cli」 「static-analysis」 「pre-commit」 「sonarqube」 「code-quality」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 ncac/php-cognitive-complexity 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 ncac/php-cognitive-complexity 我们能提供哪些服务?
定制开发 / 二次开发

基于 ncac/php-cognitive-complexity 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 153
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 1
  • 点击次数: 56
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 1
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2026-05-12