iperlmoddir/Utils.pm

filter: Statementstmt 100% 143 / 143 Branchbran 72.9% 35 / 48 Conditioncond 33.3% 2 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 100% 22 / 22 Podpod 0.0% 0 / 2 totaltotal 89.7% 202 / 225 CC 35 SCAR 16.3
CC
35
Cov
91%
_sort_cols_AoA_by_neighbour
9.1
get_inspected_modules_list
8.4
parse_modules
8.2
CRAP 5.1
« iperlmoddir.pm ↑ Summary
1 package App::iperlmoddir::Utils;
2 1 $App::iperlmoddir::Utils::VERSION = '1.02';
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use feature 'say';
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 1 ▶use Module::Info;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Module::Metadata;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 # use Module::Load;
11 1 ▶use Module::Util qw(module_path);
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 1 ▶use File::Basename;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use List::Compare;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 1 ▶use List::Util qw(max uniq);
Subroutine BEGIN: called CC=1 SCAR=0.0
16 1 ▶use List::MoreUtils qw(each_arrayref);
Subroutine BEGIN: called CC=1 SCAR=0.0
17 1 ▶use Package::Constants;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 1 ▶use Data::Dump qw(dd);
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 1 require Exporter;
23 1 our @ISA = qw(Exporter);
24 1 our @EXPORT_OK = qw(
25 get_inspected_modules_list
26 parse_modules
27 _extract_base
28 _validate_module_fullname
29 _substr_aldc
30 _cols2rows
31 _sort_cols_AoA_by_neighbour
32 _rm_header_from_cols_AoA
33 _add_header_to_cols_AoA
34 );
35 1 our %EXPORT_TAGS = ( 'all' => [@EXPORT_OK] );
36  
37  
38 1 ▶sub get_inspected_modules_list {
Subroutine get_inspected_modules_list: called CC=8 SCAR=21.3
Pod undocumented
39 1 my ( $dir, $exclude_list, $v ) = @_;
40  
41 1 ▶ say "Inspecting modules in " . $dir if $v;
Branch: 1 branch
BranchTrueFalseTotal
if $v 0 1 1
42 1 ▶ say "Skip modules : " . join( ',', @$exclude_list )
Branch: 1 branch
BranchTrueFalseTotal
if $v and @$exclude_list 0 1 1
Condition33%1/3
$v and @$exclude_list
!ll&&!rl&&r
100
MC/DC0%0/2
$v and @$exclude_list
$v @$exclude_list
Truth table33%1/3
A and B
A= $vB= @$exclude_list
ABresult
0X0
100
111
43 if ( $v && @$exclude_list );
44  
45 1 ▶ opendir( my $dh, $dir ) or die $!;
Branch: 1 branch
BranchTrueFalseTotal
opendir my $dh, $dir or die $! 1 0 1
46  
47 1 my @files;
48 1 ▶ while ( my $file = readdir($dh) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $file = readdir $dh)) 9 1 10
49 9 ▶ next unless ( -f "$dir/$file" );
Branch: 1 branch
BranchTrueFalseTotal
unless -f "$dir/$file" 2 7 9
50 7 ▶ next unless ( $file =~ m/\.pm$/ );
Branch: 1 branch
BranchTrueFalseTotal
unless $file =~ /\.pm$/ 1 6 7
51  
52 # $file=~ s/(\w+).pm/$1/;
53 6 push @files, $file;
54 }
55 1 closedir($dh);
56  
57 1 my $lc = List::Compare->new( \@files, $exclude_list );
58 1 return $lc->get_Lonly_ref;
59 }
60  
61 23 ▶sub _substr_aldc {
Subroutine _substr_aldc: called CC=2 SCAR=6.9
62 23 my ($str) = @_;
63 23 ▶ return if !defined $str;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $str 1 22 23
64 22 my @x = split( '::', $str );
65 22 return $x[-1];
66 }
67  
68 8 ▶sub _extract_base {
Subroutine _extract_base: called CC=4 SCAR=14.0
69 8 my (%p) = @_;
70  
71 8 ▶ croak "Abs path is not defined" if !defined $p{abs};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $p{"abs"} 0 8 8
72 8 ▶ croak "Rel path is not defined" if !defined $p{rel};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $p{"rel"} 0 8 8
73  
74 # https://stackoverflow.com/questions/7283274/check-whether-a-string-contains-a-substring
75 8 ▶ if ( index( $p{abs}, $p{rel} ) == -1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (index($p{"abs"}, $p{"rel"}) == -1) 2 6 8
76 croak "Relative path "
77 . $p{rel}
78 . " is not part of full path "
79 . $p{abs}
80 2 . ", maybe corresponded module placed in wrong dir";
81 }
82  
83 6 my $to_rel_idx = length( $p{abs} ) - length( $p{rel} ) - 1;
84 6 substr $p{abs}, 0, $to_rel_idx;
85 }
86  
87 9 ▶sub _validate_module_fullname {
Subroutine _validate_module_fullname: called CC=3 SCAR=11.0
88 9 my ($fullname) = @_;
89 9 my ( $name, $path, $suffix ) = fileparse( $fullname, 'pm' );
90  
91 9 ▶ carp
Branch: 1 branch
BranchTrueFalseTotal
if $path ne "./" 1 8 9
92 "Provided module name ($fullname) contain path so results may not correct"
93 if ( $path ne './' );
94  
95 # croak "Provided module name ($fullname) is not perl module" if ( $suffix ne 'pm' );
96 9 ▶ return 0 if ( $suffix ne 'pm' );
Branch: 1 branch
BranchTrueFalseTotal
if $suffix ne "pm" 1 8 9
97  
98 8 return $name . $suffix;
99 }
100  
101  
102 1 ▶sub parse_modules {
Subroutine parse_modules: called CC=8 SCAR=21.0
Pod undocumented
103 1 my ( $files, $v ) = @_;
104  
105 1 ▶ say "Start parsing modules : \n" . join( "\n", @$files ) if $v;
Branch: 1 branch
BranchTrueFalseTotal
if $v 0 1 1
106  
107 1 my @res = ();
108 1 for my $f ( sort @$files ) {
109  
110 6 _validate_module_fullname($f);
111  
112 6 my $info = Module::Metadata->new_from_file($f);
113  
114 6 my $name = $info->name;
115  
116 6 ▶ say "Module::Metadata can not define module name of $f"
Branch: 1 branch
BranchTrueFalseTotal
if $v and not defined $name 0 6 6
Condition33%1/3
$v and not defined $name
!ll&&!rl&&r
600
MC/DC0%0/2
$v and not defined $name
$v not defined $name
Truth table33%1/3
A and not(B)
A= $vB= not defined $name
ABresult
0X0
100
111
117 if ( $v && !defined $name );
118 6 ▶ next if !defined $name;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $name 0 6 6
119  
120 6 my $abs_filename = $info->filename;
121 6 my $rel_filename = module_path $name;
122  
123 eval {
124 6 my $inc =
125 _extract_base( abs => $abs_filename, rel => $rel_filename );
126 5 push @INC, $inc;
127 5 1;
128 6 ▶ } or do {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { my $inc = _extract_base("abs", $abs_filename, "rel", $rel_filename); push @INC, $inc; 1 } } or do { say "Module $name skipped from analysis. Exception : " . $@; next } 5 1 6
129 1 say "Module $name skipped from analysis. Exception : " . $@;
130 1 next;
131 };
132  
133 # Package::Constants need module to be loaded
134 5 require $f;
135  
136 # or autoload $name;
137  
138 5 my $mod = Module::Info->new_from_module($name);
139  
140 5 ▶ croak "Problems with getting Module::Info at module " . $name
Branch: 1 branch
BranchTrueFalseTotal
unless defined $mod 0 5 5
141 if !defined $mod;
142  
143 5 my %subs = $mod->subroutines;
144 5 my @used = sort { "\L$a" cmp "\L$b" } $mod->modules_used;
145  
146 5 my @consts = Package::Constants->list($name);
147 push @res,
148 {
149 name => $name,
150 subs =>
151 9 [ map { _substr_aldc $_ } sort { "\L$a" cmp "\L$b" } keys %subs ],
152 used => \@used,
153 5 consts => [ sort { "\L$a" cmp "\L$b" } @consts ]
154 };
155  
156 }
157  
158 1 return \@res;
159 }
160  
161 1 ▶sub _cols2rows {
Subroutine _cols2rows: called CC=4 SCAR=13.9
162 1 my ($colls_arr) = @_;
163  
164 1 my @sizes;
165 my @rows;
166  
167 1 for my $max_idx (@$colls_arr) {
168 3 push @sizes, $#$max_idx;
169 }
170  
171 1 for my $i ( 0 .. $#$colls_arr ) {
172 3 for my $j ( 0 .. max @sizes ) {
173 12 $rows[$j][$i] = _substr_aldc( $colls_arr->[$i][$j] );
174 }
175 }
176  
177 1 return \@rows;
178 }
179  
180 1 ▶sub _add_header_to_cols_AoA {
Subroutine _add_header_to_cols_AoA: called CC=3 SCAR=11.0
181 1 my ( $AoA, $header ) = @_;
182  
183 1 ▶ croak "Different size of header and table rows amount"
Branch: 1 branch
BranchTrueFalseTotal
if $AoA->$#* != $header->$#* 0 1 1
184 if ( $#$AoA != $#$header );
185  
186 1 my $ea = each_arrayref( $AoA, $header );
187 1 ▶ while ( my ( $a, $b ) = $ea->() ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = &$ea()) 3 1 4
188 3 unshift @$a, $b;
189 }
190  
191 1 return $AoA;
192 }
193  
194 1 ▶sub _rm_header_from_cols_AoA {
Subroutine _rm_header_from_cols_AoA: called CC=2 SCAR=6.9
195 1 my ($AoA) = @_;
196  
197 1 my @header;
198 1 for my $col (@$AoA) {
199 3 push @header, $col->[0];
200 3 shift @$col;
201 }
202 1 return \@header;
203 }
204  
205 # See more
206 # https://stackoverflow.com/questions/54333145/sort-table-or-2-dimensional-array-by-same-values-in-column
207 # https://perldoc.pl/perllol
208  
209 2 ▶sub _sort_cols_AoA_by_neighbour {
Subroutine _sort_cols_AoA_by_neighbour: called CC=9 SCAR=22.1
210 2 my ( $data, $has_header ) = @_;
211  
212 # if first element is column header start idx = 1
213 2 ▶ my $i = ( $has_header ? 1 : 0 );
Branch: 1 branch
BranchTrueFalseTotal
$has_header ? : 0 2 2
214 2 my @all;
215  
216 2 for my $col_arr (@$data) {
217 6 push @all, @$col_arr[ $i .. $#$col_arr ];
218 }
219  
220 2 ▶ my $header = _rm_header_from_cols_AoA($data) if $has_header;
Branch: 1 branch
BranchTrueFalseTotal
if $has_header 0 2 2
221 2 @all = sort { "\L$a" cmp "\L$b" } grep { $_ } uniq @all;
222  
223 2 for my $ary (@$data) {
224  
225 6 my $cmp_at = 0;
226 6 my @res;
227 6 for my $i ( 0 .. $#all ) {
228  
229 24 ▶ if ( !defined $ary->[$cmp_at] ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $ary->[$cmp_at]) { } 3 21 24
elsif ($ary->[$cmp_at] eq $all[$i]) { } 12 9 21
230 3 push @res, undef;
231 }
232 elsif ( $ary->[$cmp_at] eq $all[$i] ) {
233 12 push @res, $ary->[$cmp_at];
234 12 ++$cmp_at;
235 }
236 else {
237 9 push @res, undef;
238 }
239 }
240 6 $ary = \@res;
241 }
242  
243 2 ▶ _add_header_to_cols_AoA( $data, $header ) if $has_header;
Branch: 1 branch
BranchTrueFalseTotal
if $has_header 0 2 2
244  
245 2 return $data;
246 }
247  
248 1 1;
249  
250 __END__
« iperlmoddir.pm ↑ Summary