| 1 | package Analizo::Metric::CouplingBetweenObjects; | ||||||||||||||||||||||||||||
| 2 | 22 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | 22 | ▶ | use parent qw(Class::Accessor::Fast Analizo::ModuleMetric); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | =head1 NAME | ||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | Analizo::Metric::CouplingBetweenObjects - Coupling Between Objects (CBO) metric | ||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | The metric calculation is based on the following article and calculates the | ||||||||||||||||||||||||||||
| 12 | number of calls to other modules. | ||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | Article: I<A metrics suite for object oriented design> by Shyam R. Chidamber | ||||||||||||||||||||||||||||
| 15 | and Chris F. Kemerer. | ||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | See the paragraph about Coupling Between Objects in the article: | ||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | Theoretical Basis: CBO relates to the notion that an object is coupled to | ||||||||||||||||||||||||||||
| 20 | another object if one of them acts on the other, i.e., methods of one use | ||||||||||||||||||||||||||||
| 21 | method or instance variables of another. As stated earlier, since objects of | ||||||||||||||||||||||||||||
| 22 | the same class have the same properties, two classes are coupled when methods | ||||||||||||||||||||||||||||
| 23 | declared in on class use methods or instance variables defined by the other | ||||||||||||||||||||||||||||
| 24 | class. | ||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | =cut | ||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | 22 | __PACKAGE__->mk_accessors(qw( model analized_module calls_to )); | |||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | 18 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 31 | 18 | my ($package, %args) = @_; | |||||||||||||||||||||||||||
| 32 | my @instance_variables = ( | ||||||||||||||||||||||||||||
| 33 | model => $args{model}, | ||||||||||||||||||||||||||||
| 34 | 18 | analized_module => undef, | |||||||||||||||||||||||||||
| 35 | calls_to => {}, | ||||||||||||||||||||||||||||
| 36 | ); | ||||||||||||||||||||||||||||
| 37 | 18 | return bless { @instance_variables }, $package; | |||||||||||||||||||||||||||
| 38 | } | ||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | 4 | ▶ | sub description { | ||||||||||||||||||||||||||
|
Subroutine
description: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 41 | 4 | return "Coupling Between Objects"; | |||||||||||||||||||||||||||
| 42 | } | ||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | 13 | ▶ | sub calculate { | ||||||||||||||||||||||||||
|
Subroutine
calculate: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 45 | 13 | my ($self, $module) = @_; | |||||||||||||||||||||||||||
| 46 | 13 | $self->analized_module($module); | |||||||||||||||||||||||||||
| 47 | 13 | $self->calls_to({}); | |||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | 13 | my $number_of_calls_to_other_modules = $self->_number_of_calls_to_other_modules(); | |||||||||||||||||||||||||||
| 50 | 13 | return $number_of_calls_to_other_modules; | |||||||||||||||||||||||||||
| 51 | } | ||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | 13 | ▶ | sub _number_of_calls_to_other_modules { | ||||||||||||||||||||||||||
|
Subroutine
_number_of_calls_to_other_modules: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 54 | 13 | my ($self) = @_; | |||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | 13 | for my $caller_function ($self->model->functions($self->analized_module)) { | |||||||||||||||||||||||||||
| 57 | 17 | $self->_add_number_of_calls_to_other_modules($caller_function); | |||||||||||||||||||||||||||
| 58 | } | ||||||||||||||||||||||||||||
| 59 | 13 | return (scalar keys(%{$self->calls_to})); | |||||||||||||||||||||||||||
| 60 | } | ||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | 17 | ▶ | sub _add_number_of_calls_to_other_modules { | ||||||||||||||||||||||||||
|
Subroutine
_add_number_of_calls_to_other_modules: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 63 | 17 | my ($self, $caller_function) = @_; | |||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | 17 | for my $called_function (keys(%{$self->model->calls->{$caller_function}})) { | |||||||||||||||||||||||||||
| 66 | 24 | $self->_add_function_module_other_then_analized_module($called_function); | |||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | 24 | ▶ | sub _add_function_module_other_then_analized_module { | ||||||||||||||||||||||||||
|
Subroutine
_add_function_module_other_then_analized_module: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 71 | 24 | my ($self, $called_function) = @_; | |||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | 24 | my $called_module = $self->model->members->{$called_function}; | |||||||||||||||||||||||||||
| 74 | 24 | ▶ | $self->calls_to->{$called_module}++ if ($called_module && $called_module ne $self->analized_module); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$called_module and $called_module ne $self->analized_module
MC/DC100%2/2
$called_module and $called_module ne $self->analized_module
$called_module
$called_module ne $self->analized_module
Truth table100%3/3
A and B
A= $called_moduleB= $called_module ne $self->analized_module
| |||||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | 22 | 1; | |||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||