artemeon/cognitive-complexity 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

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

artemeon/cognitive-complexity

Composer 安装命令:

composer require artemeon/cognitive-complexity

包简介

PHPStan rules to measure cognitive complexity of your classes and methods

README 文档

README


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 or Sonar paper about cognitive complexity metrics that inspired this repository.


Install

composer require artemeon/cognitive-complexity --dev

The package is available on PHP 7.4+.


Usage

With 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

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!

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2026-01-04

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固