Debian Codegraph @

lib/Analizo/Metric/NumberOfPublicAttributes.pm

raw
1 package Analizo::Metric::NumberOfPublicAttributes;
2 use strict;
3 use parent qw(Class::Accessor::Fast Analizo::ModuleMetric);
4
5 =head1 NAME
6
7 Analizo::Metric::NumberOfPublicAttributes - Number of Public Attributes (NPA) metric
8
9 =head1 DESCRIPTION
10
11 The metric calculation is based on the following article and calculates the
12 number of public attributes of a class.
13
14 Article: I<Monitoring of source code metrics in open source projects> by Paulo
15 Roberto Miranda Meirelles.
16
17 See the adaptation of the paragraph about Number of Public Attributes in the
18 article:
19
20 It measures the encapsulation. The attributes of a class must only serve to the
21 functionalities of itself. Thus, good programming practices recommend that the
22 attributes of a class must be manipulated using the access methods (Beck,
23 1997), the attributes of a class must be private, indicating that the ideal
24 number for this metric is zero.
25
26 =cut
27
28 __PACKAGE__->mk_accessors(qw( model ));
29
30 sub new {
31 my ($package, %args) = @_;
32 my @instance_variables = (
33 model => $args{model}
34 );
35 return bless { @instance_variables }, $package;
36 }
37
38 sub description {
39 return "Number of Public Attributes";
40 }
41
42 sub calculate {
43 my ($self, $module) = @_;
44
45 my $count = 0;
46 for my $attribute ($self->model->variables($module)) {
47 $count += 1 if $self->_is_public($attribute);
48 }
49 return $count;
50 }
51
52 sub _is_public {
53 my ($self, $attribute) = @_;
54 return $self->model->{protection}->{$attribute} && $self->model->{protection}->{$attribute} eq "public";
55 }
56
57 1;
58