duannao3819 2013-12-03 16:59
浏览 42
已采纳

为什么这段代码具有很高的圈复杂性 - 或者它是Jenkins中的PHPMD中的错误?

I'm trying to understand how Cyclomatic Complexity works and how I can avoid the warnings. Yes, I understand that the goal of writing code is not to avoid arbitrary warnings, but I'd at least like to know what's going on so I can decide if the code I'm seeing is good or bad.

I have a function that looks like:

protected function update($uuid, $data, $householdUuid, $androidId) {
    $household = $this->householdService->getHouseholdByUuid($householdUuid);

    $this->updatePeriod($household, $data);
    $this->updateNickname($household, $data, $androidId);
    $this->updateDateOrder($household, $data);
    $this->updateCurrency($household, $data);
    $this->updateAccounts($household, $data);

    $household->save();
    return $this->respondUpdated();
}

This gets flagged as having a cyclomatic complexity of 10. How is that possible? From the documentation, I would count this as a 1. The only possibility is that PHPMD is descending down into the various method calls.

But if that's so, then I have no way to "fix" this method. Generally I would reduce the complexity of a method by extracting out smaller helper methods. This method got refactored into those various update() methods already, to eliminate a bunch of conditional updating that happens. The original method had a cyclomatic complexity of 10 as well, and the refactor did nothing.

Or maybe the problem is simpler -- I'm running PHPMD through a continuous integration set up with Jenkins. Could there be an issue where PHPMD is not using the most recent code? I have had somewhat similar issues where it will flag a class as having too many lines, after I had already refactored the class below the line number limit.

  • 写回答

2条回答 默认 最新

  • duanbo6871 2013-12-12 19:36
    关注

    I might think that each function call is what is adding the +1 to the complexity, since it is technically a pass through the code, but it should not do this according to the documentation. Even the definition for Cyclomatic Complexity does not support this count.

    I would think this is a bug in PHP Mess Detector, as the PHP_CodeSniffer complexity calculation does not give a 10 on this.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥30 关于用python写支付宝扫码付异步通知收不到的问题
  • ¥50 vue组件中无法正确接收并处理axios请求
  • ¥15 隐藏系统界面pdf的打印、下载按钮
  • ¥15 MATLAB联合adams仿真卡死如何解决(代码模型无问题)
  • ¥15 基于pso参数优化的LightGBM分类模型
  • ¥15 安装Paddleocr时报错无法解决
  • ¥15 python中transformers可以正常下载,但是没有办法使用pipeline
  • ¥50 分布式追踪trace异常问题
  • ¥15 人在外地出差,速帮一点点
  • ¥15 如何使用canvas在图片上进行如下的标注,以下代码不起作用,如何修改