| 1 | package Analizo::Metric::AverageCycloComplexity; | ||||||||||
| 2 | 21 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 3 | 21 | ▶ | use parent qw(Class::Accessor::Fast Analizo::ModuleMetric); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 21 | ▶ | use Statistics::Descriptive; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | |||||||||||
| 6 | =head1 NAME | ||||||||||
| 7 | |||||||||||
| 8 | Analizo::Metric::AverageCycloComplexity - Average Cyclomatic Complexity per Method (ACCM) metric | ||||||||||
| 9 | |||||||||||
| 10 | =head1 DESCRIPTION | ||||||||||
| 11 | |||||||||||
| 12 | The metric calculation is based on the following article and calculates the | ||||||||||
| 13 | cyclomatic complexity of the program. | ||||||||||
| 14 | |||||||||||
| 15 | Article: | ||||||||||
| 16 | I<McCabe, Thomas J. "A complexity measure." IEEE Transactions on software Engineering 4 (1976): 308-320>. | ||||||||||
| 17 | |||||||||||
| 18 | The Average Cyclomatic Complexity per Method is calculated counting the | ||||||||||
| 19 | predicates (i.e., decision points, or conditional paths) on each method plus | ||||||||||
| 20 | one, then a mean of all methods is returned as the final value of ACCM. | ||||||||||
| 21 | |||||||||||
| 22 | The cyclomatic complexity of a program represented as a graph can be calculated | ||||||||||
| 23 | using a formula of graph theory: | ||||||||||
| 24 | |||||||||||
| 25 | v(G) = e - n + 2 | ||||||||||
| 26 | |||||||||||
| 27 | Where C<e> is the number of edges and C<n> is the number of nodes of the graph. | ||||||||||
| 28 | |||||||||||
| 29 | Another good reference is: | ||||||||||
| 30 | I<Woodward, Martin R., Michael A. Hennell, and David Hedley. "A measure of control flow complexity in program text." IEEE Transactions on Software Engineering 1 (1979): 45-50>. | ||||||||||
| 31 | |||||||||||
| 32 | =cut | ||||||||||
| 33 | |||||||||||
| 34 | 21 | __PACKAGE__->mk_accessors(qw( model )); | |||||||||
| 35 | |||||||||||
| 36 | 15 | ▶ | sub new { | ||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 37 | 15 | my ($package, %args) = @_; | |||||||||
| 38 | my @instance_variables = ( | ||||||||||
| 39 | model => $args{model} | ||||||||||
| 40 | 15 | ); | |||||||||
| 41 | 15 | return bless { @instance_variables }, $package; | |||||||||
| 42 | } | ||||||||||
| 43 | |||||||||||
| 44 | 4 | ▶ | sub description { | ||||||||
|
Subroutine
description: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 45 | 4 | return 'Average Cyclomatic Complexity per Method'; | |||||||||
| 46 | } | ||||||||||
| 47 | |||||||||||
| 48 | 10 | ▶ | sub calculate { | ||||||||
|
Subroutine
calculate: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||
| 49 | 10 | my ($self, $module) = @_; | |||||||||
| 50 | |||||||||||
| 51 | 10 | my @functions = $self->model->functions($module); | |||||||||
| 52 | 10 | ▶ | if (scalar(@functions) == 0) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 53 | 1 | return 0; | |||||||||
| 54 | } | ||||||||||
| 55 | |||||||||||
| 56 | 9 | my $statisticalCalculator = Statistics::Descriptive::Full->new(); | |||||||||
| 57 | 9 | for my $function (@functions) { | |||||||||
| 58 | 15 | $statisticalCalculator->add_data($self->model->{conditional_paths}->{$function} + 1); | |||||||||
| 59 | } | ||||||||||
| 60 | |||||||||||
| 61 | 9 | return $statisticalCalculator->mean(); | |||||||||
| 62 | } | ||||||||||
| 63 | |||||||||||
| 64 | 21 | 1; | |||||||||