Api icon indicating copy to clipboard operation
Api copied to clipboard

Cyclomatic Complexity

Open Gisleburt opened this issue 9 years ago • 1 comments

Currently Documentation::getDescription has a cycolmatic complexity of 12. The mess detector rules say this must be below 10

    /**
     * Gets a methods description.
     * This is an example of a description. In PSR-5 it follows the summary.
     * @param string[] $lines
     * @return string
     */
    protected function getDescription(array $lines)
    {
        $description = '';
        $summaryFound = false;
        $summaryPassed = false;
        foreach ($lines as $line) {
            if ($line && !$summaryPassed) {
                $summaryFound = true;
                if (substr(trim($line), -1) == '.') {
                    $summaryPassed = true;
                }
                continue;
            }
            if (!$line && $summaryFound && !$summaryPassed) {
                $summaryPassed = true;
                continue;
            }
            if ($line && $line[0] == '@') {
                break;
            }
            if ($line && $summaryPassed) {
                $description .= $line."\n";
            }
        }
        return trim($description);
    }

This can be reduced by moving some of the complex comparisons into seperate methods, eg:

    private function isEndOfSummary($line, $summaryFound, $summaryPassed)
    {
        return (!$line || substr(trim($line), -1) == '.') && $summaryFound && !$summaryPassed;
    }

    private function hasDescriptionEnded($line)
    {
        return $line && $line[0] == '@';
    }

    /**
     * Gets a methods description.
     * This is an example of a description. In PSR-5 it follows the summary.
     * @param string[] $lines
     * @return string
     */
    protected function getDescription(array $lines)
    {
        $description = '';
        $summaryFound = false;
        $summaryPassed = false;

        foreach ($lines as $line) {
            if ($line && !$summaryPassed) {
                $summaryFound = true;
            }
            if ($this->isEndOfSummary($line, $summaryFound, $summaryPassed)) {
                $summaryPassed = true;
                continue;
            }
            if ($this->hasDescriptionEnded($line)) {
                break;
            }
            if ($line && $summaryPassed) {
                $description .= $line."\n";
            }
        }
        return trim($description);
    }

This takes the complexity below 10 and still passes the tests, though could be reduced further by moving the other if's out.

The question is, is it worth it, and do there methods belong here in the Documentation class?

Gisleburt avatar Feb 08 '16 15:02 Gisleburt