| 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
MC/DC100%1/1
$METRICS_ARGS{"-mccabe_complexity"} ||= 10
$METRICS_ARGS{"-mccabe_complexity"}
Truth table100%2/2
A
A= $METRICS_ARGS{"-mccabe_complexity"}
| |||||||||||||
| 27 | 4 | ▶ | $METRICS_ARGS{-loc} ||= 60; | ||||||||||
|
Condition100%2/2
$METRICS_ARGS{"-loc"} ||= 60
MC/DC100%1/1
$METRICS_ARGS{"-loc"} ||= 60
$METRICS_ARGS{"-loc"}
Truth table100%2/2
A
A= $METRICS_ARGS{"-loc"}
| |||||||||||||
| 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'} || []
MC/DC0%0/1
$METRICS_ARGS{'-except_dir'} || []
$METRICS_ARGS{'-except_dir'}
Truth table50%1/2
A
A= $METRICS_ARGS{'-except_dir'}
| |||||||||||||
| 34 | 3 | my @dirs = @_; | |||||||||||
| 35 | 3 | ▶ | if ( not @dirs ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 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
| |||||||||||||
| 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
| |||||||||||||
| 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
| |||||||||||||
| 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'} || []
MC/DC100%1/1
$METRICS_ARGS{'-except_file'} || []
$METRICS_ARGS{'-except_file'}
Truth table100%2/2
A
A= $METRICS_ARGS{'-except_file'}
| |||||||||||||
| 78 | 5 | ▶ | next if is_excluded( $file_path, @except_files ); | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 79 | |||||||||||||
| 80 | 4 | my $sub_metrics = $sub_stats->{$file_path}; | |||||||||||
| 81 | 4 | ▶ | $ok = $ok or _all_sub_metrics_ok($sub_metrics); | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 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
| |||||||||||||
| 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
| |||||||||||||
| 101 | 17 | ▶ | $ok = $ok or _sub_cc_ok($sub_metric); | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 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
| |||||||||||||
| 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
| |||||||||||||
| 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__ | ||||||||||||