blib/lib/Test/Perl/Metrics/Lite.pm

filter: Statementstmt 94.5% 87 / 92 Branchbran 63.6% 14 / 22 Conditioncond 87.5% 7 / 8 MC/DCmcdc 75.0% 3 / 4 Subroutinesub 100% 18 / 18 Podpod 0.0% 0 / 3 totaltotal 87.7% 129 / 147 CC 18 SCAR 9.5
CC
18
Cov
89%
_all_files_metric_ok
5.0
all_code_files
3.1
_sub_metric_ok
3.1
CRAP 2.6
↑ Summary
1 package Test::Perl::Metrics::Lite;
2 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 4 our $VERSION = '0.2';
4  
5 4 ▶use List::MoreUtils qw(any);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 4 ▶use Perl::Metrics::Lite;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 4 ▶use Test::More ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8 4 ▶use Test::Builder;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 4 my %METRICS_ARGS;
11  
12 4 my $TEST = Test::Builder->new;
13  
14 4 ▶sub import {
Subroutine import: called CC=3 SCAR=11.0
15 4 my ( $self, %args ) = @_;
16  
17 4 my $caller = caller;
18 {
19 4 ▶ no strict 'refs'; ## no critic qw(ProhibitNoStrict)
Subroutine BEGIN: called CC=1 SCAR=0.0
20 4 *{ $caller . '::all_metrics_ok' } = \&all_metrics_ok;
21 }
22  
23 4 $TEST->exported_to($caller);
24  
25 4 %METRICS_ARGS = %args;
26 4 ▶ $METRICS_ARGS{-mccabe_complexity} ||= 10;
Condition100%2/2
$METRICS_ARGS{"-mccabe_complexity"} ||= 10
l!l
13
MC/DC100%1/1
$METRICS_ARGS{"-mccabe_complexity"} ||= 10
$METRICS_ARGS{"-mccabe_complexity"}
Truth table100%2/2
A
A= $METRICS_ARGS{"-mccabe_complexity"}
Aresult
11
00
27 4 ▶ $METRICS_ARGS{-loc} ||= 60;
Condition100%2/2
$METRICS_ARGS{"-loc"} ||= 60
l!l
13
MC/DC100%1/1
$METRICS_ARGS{"-loc"} ||= 60
$METRICS_ARGS{"-loc"}
Truth table100%2/2
A
A= $METRICS_ARGS{"-loc"}
Aresult
11
00
28  
29 4 return 1;
30 }
31  
32 3 ▶sub all_code_files {
Subroutine all_code_files: called CC=3 SCAR=11.4
Pod undocumented
33 3 ▶ my @exceptions = @{ $METRICS_ARGS{-except_dir} || [] };
Condition50%1/2
$METRICS_ARGS{'-except_dir'} || []
l!l
03
MC/DC0%0/1
$METRICS_ARGS{'-except_dir'} || []
$METRICS_ARGS{'-except_dir'}
Truth table50%1/2
A
A= $METRICS_ARGS{'-except_dir'}
Aresult
11
00
34 3 my @dirs = @_;
35 3 ▶ if ( not @dirs ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@dirs) 0 3 3
36 0 @dirs = _starting_points();
37 }
38 3 @dirs = grep { !is_excluded( $_, @exceptions ) } @dirs;
39 3 return \@dirs;
40 }
41  
42 8 ▶sub is_excluded {
Subroutine is_excluded: called CC=1 SCAR=0.0
Pod undocumented
43 8 my ( $path, @exceptions ) = @_;
44 8 ▶ any { $path eq $_ || $path =~ /$_/ } @exceptions;
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
$path eq $_ or $path =~ /$_/ 1 1 2
45 }
46  
47 1 ▶sub _starting_points {
Subroutine _starting_points: called CC=2 SCAR=7.6
48 1 ▶ return -e 'blib' ? 'blib' : 'lib';
Branch: 1 branch
BranchTrueFalseTotal
-e 'blib' ? : 1 0 1
49 }
50  
51 3 ▶sub all_metrics_ok {
Subroutine all_metrics_ok: called CC=2 SCAR=6.9
Pod undocumented
52 3 my @dirs = @_;
53  
54 3 Test::More::plan('no_plan');
55  
56 3 ▶ if ( not @dirs ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@dirs) 1 2 3
57 1 @dirs = _starting_points();
58 }
59 3 my $files = all_code_files(@dirs);
60  
61 3 my $analysis = _analyze_metrics($files);
62 3 my $ok = _all_files_metric_ok( $analysis->sub_stats );
63 3 return $ok;
64 }
65  
66 3 ▶sub _analyze_metrics {
Subroutine _analyze_metrics: called CC=1 SCAR=0.0
67 3 my $libs = shift;
68 3 my $analzyer = Perl::Metrics::Lite->new;
69 3 my $analysis = $analzyer->analyze_files(@$libs);
70 3 return $analysis;
71 }
72  
73 3 ▶sub _all_files_metric_ok {
Subroutine _all_files_metric_ok: called CC=5 SCAR=16.1
74 3 my $sub_stats = shift;
75 3 my $ok = 0;
76 3 foreach my $file_path ( keys %{$sub_stats} ) {
77 5 ▶ my @except_files = @{ $METRICS_ARGS{-except_file} || [] };
Condition100%2/2
$METRICS_ARGS{'-except_file'} || []
l!l
23
MC/DC100%1/1
$METRICS_ARGS{'-except_file'} || []
$METRICS_ARGS{'-except_file'}
Truth table100%2/2
A
A= $METRICS_ARGS{'-except_file'}
Aresult
11
00
78 5 ▶ next if is_excluded( $file_path, @except_files );
Branch: 1 branch
BranchTrueFalseTotal
if is_excluded($file_path, @except_files) 1 4 5
79  
80 4 my $sub_metrics = $sub_stats->{$file_path};
81 4 ▶ $ok = $ok or _all_sub_metrics_ok($sub_metrics);
Branch: 1 branch
BranchTrueFalseTotal
$ok = $ok or _all_sub_metrics_ok($sub_metrics) 0 4 4
82 }
83 3 return $ok;
84 }
85  
86 4 ▶sub _all_sub_metrics_ok {
Subroutine _all_sub_metrics_ok: called CC=3 SCAR=11.0
87 4 my $sub_metrics = shift;
88 4 my @rows = ();
89 4 my $ok = 0;
90 4 foreach my $sub_metric ( @{$sub_metrics} ) {
91 17 ▶ $ok = $ok or _sub_metric_ok($sub_metric);
Branch: 1 branch
BranchTrueFalseTotal
$ok = $ok or _sub_metric_ok($sub_metric) 0 17 17
92 }
93 4 return $ok;
94 }
95  
96 17 ▶sub _sub_metric_ok {
Subroutine _sub_metric_ok: called CC=3 SCAR=11.3
97 17 my $sub_metric = shift;
98  
99 17 my $ok = 0;
100 17 ▶ $ok = $ok or _sub_loc_ok($sub_metric);
Branch: 1 branch
BranchTrueFalseTotal
$ok = $ok or _sub_loc_ok($sub_metric) 0 17 17
101 17 ▶ $ok = $ok or _sub_cc_ok($sub_metric);
Branch: 1 branch
BranchTrueFalseTotal
$ok = $ok or _sub_cc_ok($sub_metric) 0 17 17
102 17 return $ok;
103 }
104  
105 17 ▶sub _sub_cc_ok {
Subroutine _sub_cc_ok: called CC=2 SCAR=7.6
106 17 my $sub_metric = shift;
107  
108 17 my $cc = $sub_metric->{mccabe_complexity};
109 17 ▶ if ( $cc < $METRICS_ARGS{-mccabe_complexity} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cc < $METRICS_ARGS{'-mccabe_complexity'}) { } 17 0 17
110 17 $TEST->ok( 1, $sub_metric->{name} . " cc is ok" );
111 17 return 0;
112 }
113 else {
114 0 $TEST->ok( 0,
115 "The method is to complex! Detail: Path: $sub_metric->{path}, Method: $sub_metric->{name}, CC: ${cc}"
116 );
117 0 return 1;
118 }
119 }
120  
121 17 ▶sub _sub_loc_ok {
Subroutine _sub_loc_ok: called CC=2 SCAR=7.6
122 17 my $sub_metric = shift;
123  
124 17 my $sloc = $sub_metric->{lines};
125 17 ▶ if ( $sloc < $METRICS_ARGS{-loc} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sloc < $METRICS_ARGS{'-loc'}) { } 17 0 17
126 17 $TEST->ok( 1, $sub_metric->{name} . " sloc is ok" );
127 17 return 0;
128 }
129 else {
130 0 $TEST->ok( 0,
131 "The method is too long! Detail: Path: $sub_metric->{path} ,Method: $sub_metric->{name}, SLOC: ${sloc}"
132 );
133 0 return 1;
134 }
135 }
136  
137 4 1;
138 __END__
↑ Summary