Debian Codegraph @

lib/Analizo/Metric/CouplingBetweenObjects.pm

raw
1 package Analizo::Metric::CouplingBetweenObjects;
2 use strict;
3 use parent qw(Class::Accessor::Fast Analizo::ModuleMetric);
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 __PACKAGE__->mk_accessors(qw( model analized_module calls_to ));
29
30 sub new {
31 my ($package, %args) = @_;
32 my @instance_variables = (
33 model => $args{model},
34 analized_module => undef,
35 calls_to => {},
36 );
37 return bless { @instance_variables }, $package;
38 }
39
40 sub description {
41 return "Coupling Between Objects";
42 }
43
44 sub calculate {
45 my ($self, $module) = @_;
46 $self->analized_module($module);
47 $self->calls_to({});
48
49 my $number_of_calls_to_other_modules = $self->_number_of_calls_to_other_modules();
50 return $number_of_calls_to_other_modules;
51 }
52
53 sub _number_of_calls_to_other_modules {
54 my ($self) = @_;
55
56 for my $caller_function ($self->model->functions($self->analized_module)) {
57 $self->_add_number_of_calls_to_other_modules($caller_function);
58 }
59 return (scalar keys(%{$self->calls_to}));
60 }
61
62 sub _add_number_of_calls_to_other_modules {
63 my ($self, $caller_function) = @_;
64
65 for my $called_function (keys(%{$self->model->calls->{$caller_function}})) {
66 $self->_add_function_module_other_then_analized_module($called_function);
67 }
68 }
69
70 sub _add_function_module_other_then_analized_module {
71 my ($self, $called_function) = @_;
72
73 my $called_module = $self->model->members->{$called_function};
74 $self->calls_to->{$called_module}++ if ($called_module && $called_module ne $self->analized_module);
75 }
76
77 1;
78