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条)

报告相同问题?

悬赏问题

  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c