Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does this code have high cyclomatic complexity - or is it a bug in PHPMD in Jenkins?

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.

like image 559
matt Avatar asked Dec 03 '13 16:12

matt


People also ask

What is high cyclomatic complexity?

Cyclomatic complexity is a measure of code quality that takes into account the number of independent paths through a piece of code. A high cyclomatic complexity indicates that a piece of code is more difficult to understand and maintain, and is, therefore, more likely to contain errors.

What is Phpmd?

PHPMD is a spin-off project of PHP Depend and aims to be a PHP equivalent of the well known Java tool PMD. PHPMD can be seen as an user friendly frontend application for the raw metrics stream measured by PHP Depend. phpmd.org.

Does cyclomatic have a complexity?

Cyclomatic complexity of a code section is the quantitative measure of the number of linearly independent paths in it. It is a software metric used to indicate the complexity of a program. It is computed using the Control Flow Graph of the program.

What is NPath complexity?

NPath complexity is the number of execution paths possible for a given code i.e. the number of ways the given code can get executed (ignoring cycles).


1 Answers

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.

like image 91
Steven Scott Avatar answered Oct 08 '22 14:10

Steven Scott