File Coverage

blib/lib/Perl/Metrics/Lite/Report/Text.pm
Criterion Covered Total %
statement 18 74 24.3
branch 3 18 16.6
condition 0 3 0.0
subroutine 4 15 26.6
pod 0 5 0.0
total 25 115 21.7


line stmt bran cond sub pod time code
1             package Perl::Metrics::Lite::Report::Text;
2 4     4   28 use strict;
  4         8  
  4         124  
3 4     4   21 use warnings;
  4         7  
  4         103  
4 4     4   2388 use Text::ASCIITable;
  4         38819  
  4         3687  
5              
6             my $DEFAULT_MAX_SUB_LINES = 60;
7             my $DEFAULT_MAX_MCCABE_COMPLEXITY = 10;
8              
9             sub new {
10 3     3 0 10 my ( $class, %args ) = @_;
11 3         10 my $self = bless( {}, $class );
12              
13             my $max_sub_lines
14             = exists $args{max_sub_lines}
15             ? $args{max_sub_lines}
16 3 50       11 : $DEFAULT_MAX_SUB_LINES;
17             my $max_sub_mccabe_complexity = $args{max_sub_mccabe_complexity}
18             = exists $args{max_sub_mccabe_complexity}
19             ? $args{max_sub_mccabe_complexity}
20 3 50       12 : $DEFAULT_MAX_MCCABE_COMPLEXITY;
21             my $show_only_error = $args{show_only_error}
22             = exists $args{show_only_error}
23             ? $args{show_only_error}
24 3 50       11 : 0;
25              
26 3         15 $self->{max_sub_lines} = $max_sub_lines;
27 3         7 $self->{max_sub_mccabe_complexity} = $max_sub_mccabe_complexity;
28 3         8 $self->{show_only_error} = $show_only_error;
29              
30 3         9 return $self;
31             }
32              
33             sub report {
34 0     0 0   my ( $self, $analysis ) = @_;
35              
36 0           my $file_stats = $analysis->file_stats;
37 0           $self->report_file_stats($file_stats);
38              
39 0           my $sub_stats = $analysis->sub_stats;
40 0           $self->report_sub_stats($sub_stats);
41             }
42              
43             sub report_file_stats {
44 0     0 0   my ( $self, $file_stats ) = @_;
45 0           _print_file_stats_report_header();
46              
47 0           my @rows = ();
48 0           foreach my $file_stat ( @{$file_stats} ) {
  0            
49             push @rows,
50             {
51             path => $file_stat->{path},
52             packages => $file_stat->{main_stats}->{packages},
53             loc => $file_stat->{main_stats}->{lines},
54             subs => $file_stat->{main_stats}->{number_of_methods}
55 0           };
56             }
57 0 0         if (@rows) {
58 0           my $keys = [ "path", "loc", "subs", "packages" ];
59 0           my $table = $self->_create_table( $keys, \@rows );
60 0           print $table;
61             }
62             }
63              
64             sub _print_file_stats_report_header {
65 0     0     print "#======================================#\n";
66 0           print "# File Metrics #\n";
67 0           print "#======================================#\n";
68             }
69              
70             sub report_sub_stats {
71 0     0 0   my ( $self, $sub_stats ) = @_;
72 0           $self->_print_sub_stats_report_header;
73 0           foreach my $file_path ( keys %{$sub_stats} ) {
  0            
74 0           my $sub_metrics = $sub_stats->{$file_path};
75 0           $self->_report_sub_metrics( $file_path, $sub_metrics );
76             }
77             }
78              
79             sub _print_sub_stats_report_header {
80 0     0     print "#======================================#\n";
81 0           print "# Subroutine Metrics #\n";
82 0           print "#======================================#\n";
83             }
84              
85             sub _report_sub_metrics {
86 0     0     my ( $self, $path, $sub_metrics ) = @_;
87 0           my $table = $self->_create_ascii_table_for_submetrics($sub_metrics);
88 0 0         if ($table) {
89 0           $self->_print_table( $path, $table );
90             }
91             }
92              
93             sub _print_table {
94 0     0     my ( $self, $path, $table ) = @_;
95              
96 0           print "\nPath: ${path}\n";
97 0           print $table;
98             }
99              
100             sub _create_ascii_table_for_submetrics {
101 0     0     my ( $self, $sub_metrics ) = @_;
102 0           my @rows = ();
103 0           foreach my $sub_metric ( @{$sub_metrics} ) {
  0            
104             next
105             if $self->{show_only_error}
106 0 0 0       && $self->is_sub_metric_ok($sub_metric);
107 0           push @rows, $self->_create_row($sub_metric);
108             }
109              
110 0           my $table;
111 0 0         if (@rows) {
112 0           my $keys = [ "method", "loc", "mccabe_complexity" ];
113 0           $table = $self->_create_table( $keys, \@rows );
114             }
115 0           return $table;
116             }
117              
118             sub is_sub_metric_ok {
119 0     0 0   my ( $self, $sub_metric ) = @_;
120 0 0         return 1 if $sub_metric->{lines} < $self->{max_sub_lines};
121             return 1
122             if $sub_metric->{mccabe_complexity}
123 0 0         < $self->{max_sub_mccabe_complexity};
124 0           return 0;
125             }
126              
127             sub _create_row {
128 0     0     my ( $self, $sub_metric ) = @_;
129             return {
130             method => $sub_metric->{name},
131             loc => $sub_metric->{lines},
132             mccabe_complexity => $sub_metric->{mccabe_complexity}
133 0           };
134             }
135              
136             sub _create_table {
137 0     0     my ( $self, $keys, $rows ) = @_;
138 0           my $t = Text::ASCIITable->new();
139 0           $t->setCols(@$keys);
140 0           $t->addRow( @$_{@$keys} ) for @$rows;
141 0           $t;
142             }
143              
144             1;
145              
146             __END__