Skip to content

Cyclomatic Complexity not reported correctly #343

Closed
@fabianbadoi

Description

@fabianbadoi

Hi,

I've noticed PhpMetrics does not correctly report cyclomatic complexity and max method cyclomatic complexity.

The bug is due to CyclomaticComplexityVisitor::leaveNode(), line 61 not taking into account else branches, elseif branches and ternary operators in control statement conditions.

if (isset($node->stmts) && $node->stmts) {
    foreach ($node->stmts as $child) {
        $ccn += $cb($child);
    }
}

Here's an example:
Say the $cb function is analysing an if statement. All nodes in the $node->stmts will be evaluated, but these are only the statements in the true branch. Thus, the else branch and elseif branches are ignored.

I will make a PR with a fix shortly. Here.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions