Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

V2.4.0: Cyclomatic complexity calculation for a class fails #359

Closed
niconoe- opened this issue Jul 9, 2018 · 7 comments
Closed

V2.4.0: Cyclomatic complexity calculation for a class fails #359

niconoe- opened this issue Jul 9, 2018 · 7 comments

Comments

@niconoe-
Copy link
Contributor

niconoe- commented Jul 9, 2018

Hi guys,

With the release of the version 2.4.0 today, I saw that my classes have a higher cyclomatic complexity level.
This is due to the fact that methods are increasing the CCN for a class, although it shouldn't.
(See In the CyclomaticComplexityVisitor, after running the $cb lambda function).

Can you please fix this?
Thanks a lot.

Best regards,

@UFOMelkor
Copy link
Member

Please help me understandig the problem. Are you graph the CCN over time or why is this a problem?

@niconoe-
Copy link
Contributor Author

niconoe- commented Jul 9, 2018

You can try to run PHPMetrics on a single class containing no conditions nor decision points, but 20 methods into. The CCN should be 1 but PHPMetrics will say 20.

Currently, the CCN for a class is the sum of the CCN for all its methods, which must not be the case.

@UFOMelkor
Copy link
Member

Yes, the formula until 2.4 was 1 + Σ (cc(m_i)-1) and has changed to Σ (cc(m_i).

I could not find any way use case for calculating the CC on classes. All papers / books i consulted applied the CC to procedures / methods. The only way to apply it to classes is the Weighted Method Count with the CC as weight metric, which is Σ (cc(m_i).

But perhaps it's better to add the WMC as own metric and drop the ccn for classes in a major release?
Or if you have any any sources for calculating CC for classes I would really appreciate it.

@niconoe-
Copy link
Contributor Author

niconoe- commented Jul 9, 2018

I agree about the use cases on CC Classes, I also found only references on CC for functions/methods.
But the PHPMetrics bubble graph displays all classes with the maintainability index for color and CC of the class for size, giving it much value.

Perhaps another metric should be used in the PHPMetrics bubble graph in a major release, but for now, we should keep using the ccn for classes for this graph and create the WMC as its own metric, like you purposed.

Thanks a lot.

@niconoe-
Copy link
Contributor Author

niconoe- commented Jul 9, 2018

Another way to define a cyclomatic complexity for a class could be the average of the CCN for all methods.
This way, having 3 methods with CC = 2 and 4 methods with CC = 1, you would have a CC_Class = (32 + 41) / (3+4) = 1.42857

It also offers consistancy with the average CC project displayed in the HTML output, as the average CC_Classes must be the average CC_Project.

This could be used as a CC_Class metrics for the PHPMetrics bubble graph without having a change in the reports between 2.3.x and 2.4.x (no compatibility failure) and the WMC could still be stored in another metrics

UFOMelkor added a commit to UFOMelkor/PhpMetrics that referenced this issue Jul 10, 2018
CCN by Class is deprecated and will be removed in v3.
WMC should be used instead.
To be BC compatible the CCN by Class will be retained until v3.

Fixes phpmetrics#359
@UFOMelkor
Copy link
Member

Average CCN sounds interesting too, but would also need a rebalance of the bublbe graph because of another value range.

Please have a look at #362
I've re-added the old ccn by class and added the wmc as own metric. Does it look good for you?

@niconoe-
Copy link
Contributor Author

I understand about rebalancing the bubble graph.
I looked at #362 and it looks good to me.

Thanks again.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants