Collector.pm

filter: Statementstmt 96.5% 221 / 229 Branchbran 73.3% 66 / 90 Conditioncond 83.3% 5 / 6 MC/DCmcdc 50.0% 2 / 4 Subroutinesub 100% 36 / 36 Podpod 100% 16 / 16 totaltotal 90.8% 346 / 381 CC 64 SCAR 14.3
CC
64
Cov
90%
_generate_short_names
8.0
_get_args
8.0
_get_file_manifest
6.0
CRAP 4.2
↑ Summary Collector/Processor.pm »
1 package File::Collector ;
2 3 $File::Collector::VERSION = '0.038';
3 3 ▶use strict; use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 3 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 3 ▶use File::Basename;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶use Role::Tiny::With;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 # public methods
11  
12 41 ▶sub AUTOLOAD {
Subroutine AUTOLOAD: called CC=6 SCAR=17.9
13 41 our $AUTOLOAD;
14 41 my $s = shift;
15 41 ▶ $AUTOLOAD =~ /.*::(next_|isa_|get_)*(\w+)_files*$/ or
Branch: 1 branch
BranchTrueFalseTotal
$File::Collector::AUTOLOAD =~ /.*::(next_|isa_|get_)*(\w+)_files*$/ or croak("No such method: $File::Collector::AUTOLOAD") 40 1 41
16 croak "No such method: $AUTOLOAD";
17  
18 40 ▶ if (!$s->{_files}{"$2_files"}) { $s->_scroak("No such file category exists: '$2' at "); }
Branch: 1 branch
BranchTrueFalseTotal
if (not $s->{'_files'}{"$2_files"}) { } 1 39 40
19 39 ▶ else { return $s->{_files}{"$2_files"} if !$1; }
Branch: 1 branch
BranchTrueFalseTotal
unless $1 2 37 39
20  
21 37 ▶ if ($1 eq 'next_') {
Branch: 1 branch
BranchTrueFalseTotal
if ($1 eq "next_") 23 14 37
22 23 return $s->{_files}{"$2_files"}->next;
23 }
24  
25 14 ▶ if ($1 eq 'isa_') {
Branch: 1 branch
BranchTrueFalseTotal
if ($1 eq "isa_") 2 12 14
26 2 return $s->{_files}{"$2_files"}->_isa($s->selected);
27 }
28  
29 # must be a "get" method
30 12 my $cat = $2;
31 12 my $class = $s->{_processor_map}{$cat};
32 my $obj = $class->new($s->{_files}{all},
33 \($s->{selected}),
34 12 $s->{_files}{"${cat}_files"}{_files});
35 12 return $obj;
36 }
37  
38 11 ▶sub new {
Subroutine new: called CC=3 SCAR=11.0
Pod documented
39 11 my $class = shift;
40  
41 # process args
42 11 my ($user_opts, $classes, @resources) = _get_args(@_);
43  
44 # get options hash
45 6 my $default_opts = { recurse => 1 };
46 6 my %opts = (%$default_opts, %$user_opts);
47  
48 # construct object
49 6 my $s = bless {
50 _files => { all => {} },
51 _common_dir => '',
52 selected => '',
53 _options => \%opts,
54 _classes => $classes,
55 all => undef,
56 }, $class;
57  
58 # eval class code
59 6 foreach my $class ( @$classes ) {
60 5 eval "require $class";
61 5 ▶ die "Could not load class: $class, $@" if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 5 5
62 }
63  
64 # a bit of trickery to make Processor class code consistent with base class
65 6 $s->{all} = $s->{_files}{all};
66  
67 # add rersources and process files
68 6 $s->add_resources(@resources);
69  
70 5 return $s;
71 }
72  
73 6 ▶sub add_resources {
Subroutine add_resources: called CC=5 SCAR=16.1
Pod documented
74 6 my ($s, @resources) = @_;
75  
76 # collect the files
77 6 foreach my $resource (@resources) {
78 7 $s->_exists($resource);
79 6 ▶ $s->_add_file($resource) if -f $resource;
Branch: 1 branch
BranchTrueFalseTotal
if -f $resource 1 5 6
80 6 ▶ $s->_get_file_manifest($resource) if -d $resource;
Branch: 1 branch
BranchTrueFalseTotal
if -d $resource 5 1 6
81 }
82  
83 5 $s->_generate_short_names; # calculate the short names
84 5 $s->_init_all_processors;
85 5 foreach my $file (@{$s->{_files}{new}}) {
86 23 $s->{selected} = $file;
87 23 $s->_classify_all;
88 }
89 5 $s->_run_all;
90 5 undef $s->{selected};
91 5 undef $s->{_files}{new}; # clear the new_file array
92 }
93  
94 3 ▶sub get_count {
Subroutine get_count: called CC=1 SCAR=0.0
Pod documented
95 3 my $s = shift;
96 3 return (scalar keys %{$s->{_files}{all}})
97 }
98  
99 7 ▶sub get_files {
Subroutine get_files: called CC=1 SCAR=0.0
Pod documented
100 7 my $s = shift;
101  
102 7 my @files = sort keys %{$s->{_files}{all}};
103 7 return @files;
104 }
105  
106 2 ▶sub get_file {
Subroutine get_file: called CC=2 SCAR=6.9
Pod documented
107 2 my ($s, $file) = @_;
108 2 ▶ $s->_scroak('No file argument passed to method. Aborting.') if !$file;
Branch: 1 branch
BranchTrueFalseTotal
unless $file 1 1 2
109  
110 1 return $s->{_files}{all}{$file};
111 }
112  
113 1 ▶sub list_files_long {
Subroutine list_files_long: called CC=2 SCAR=6.9
Pod documented
114 1 my $s = shift;
115  
116 1 my @files = $s->get_files;
117 1 print $_ . "\n" for @files;
118 }
119  
120 2 ▶sub list_files {
Subroutine list_files: called CC=2 SCAR=6.9
Pod documented
121 2 my $s = shift;
122  
123 2 my @files = map { $s->{_files}{all}{$_}{short_path} } sort keys %{$s->{_files}{all}};
124 2 print "\nFiles found in '".$s->{_common_dir}."':\n\n";
125 2 print $_ . "\n" for @files;
126 }
127  
128 20 ▶sub DESTROY {
Subroutine DESTROY: called CC=1 SCAR=0.0
129 }
130  
131 # private methods meant for used by subclasses
132  
133 23 ▶sub _classify {
Subroutine _classify: called CC=4 SCAR=14.1
134 23 my ($s, @classes) = @_;
135 23 foreach my $type (@classes) {
136 23 my $t = $type . '_files';
137 23 my $file = $s->selected;
138  
139 # die if bad args given
140 23 ▶ die ("No $type argument sent to _classify method. Aborting.") if !$type;
Branch: 1 branch
BranchTrueFalseTotal
unless $type 0 23 23
141 23 ▶ die ("No processor called $type exists. Aborting.") if !$s->{_files}{$t};
Branch: 1 branch
BranchTrueFalseTotal
unless $s->{"_files"}{$t} 0 23 23
142  
143 23 $s->{_files}{$t}->_add_file($file, $s->{_files}{all}{$file});
144 }
145 }
146  
147 23 ▶sub _add_obj {
Subroutine _add_obj: called CC=2 SCAR=7.1
148 23 my ($s, $type, $obj) = @_;
149 23 ▶ $s->_scroak("Missing args to 'add_obj' method. Aborting.") if (!$obj);
Branch: 1 branch
BranchTrueFalseTotal
unless $obj 0 23 23
150  
151 23 $s->{_files}{all}{$s->selected}{"${type}_obj"} = $obj;
152 }
153  
154 # Methods for iterators
155  
156 121 ▶sub get_obj_prop {
Subroutine get_obj_prop: called CC=5 SCAR=16.7
Pod documented
157 121 my ($s, $obj, $prop) = @_;
158  
159 121 ▶ if (!$prop) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($prop) 1 120 121
160 1 $s->_scroak ("Missing arguments to get_obj_prop method");
161 }
162  
163 my $file = ref ($s->selected) eq 'HASH'
164 ? $s->selected->{full_path}
165 120 ▶ : $s->selected;
Branch: 1 branch
BranchTrueFalseTotal
ref $s->selected eq 'HASH' ? : 120 0 120
166 120 my $attr = "_$prop";
167 120 my $o = $obj . '_obj';
168 120 my $object = $s->{all}{$file}{$o};
169 120 ▶ if (! exists $object->{$attr} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $object->{$attr}) 0 120 120
170 0 $s->_scroak ("Non-existent $obj object attribute requested: '_$prop'");
171 }
172 120 my $value = $object->{$attr};
173 120 ▶ if (ref $value eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $value eq 'ARRAY') { } 0 120 120
174 0 return @$value;
175 } else {
176 120 return $value;
177 }
178 }
179  
180 23 ▶sub get_obj {
Subroutine get_obj: called CC=3 SCAR=11.8
Pod documented
181 23 my ($s, $obj) = @_;
182  
183 23 ▶ if (!$obj) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($obj) 0 23 23
184 0 _scroak ("Missing arguments to get_obj method");
185 }
186  
187 my $file = ref ($s->selected) eq 'HASH'
188 ? $s->selected->{full_path}
189 23 ▶ : $s->selected;
Branch: 1 branch
BranchTrueFalseTotal
ref $s->selected eq 'HASH' ? : 0 23 23
190 23 my $o = $obj . '_obj';
191  
192 23 return $s->{all}{$file}{$o};
193 }
194  
195 23 ▶sub set_obj_prop {
Subroutine set_obj_prop: called CC=3 SCAR=11.7
Pod documented
196 23 my ($s, $obj, $prop, $val) = @_;
197  
198 23 ▶ if (!$val) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($val) 0 23 23
199 0 $s->_scroak ("Missing arguments to set_obj_prop method");
200 }
201  
202 23 my $file = $s->selected;
203  
204 23 my $o = $obj . '_obj';
205 23 my $object = $s->{all}{$file}{$o};
206 23 my $attr = "_$prop";
207 23 ▶ if (! exists $object->{$attr} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $object->{$attr}) 0 23 23
208 0 $s->_scroak ("Non-existent $obj object attribute requested: '$prop'");
209 }
210  
211 23 $object->{$attr} = $val;
212 }
213  
214 23 ▶sub get_filename {
Subroutine get_filename: called CC=1 SCAR=0.0
Pod documented
215 23 my $s = shift;
216 23 my $file = $s->selected;
217  
218 23 return $s->{all}{$file}{filename};
219 }
220  
221 23 ▶sub obj_meth {
Subroutine obj_meth: called CC=4 SCAR=14.8
Pod documented
222 # Keep these args shifted individually
223 23 my $s = shift;
224 23 my $obj = shift;
225 23 my $meth = shift;
226 my $file = ref ($s->selected) eq 'HASH'
227 ? $s->selected->{full_path}
228 23 ▶ : $s->selected;
Branch: 1 branch
BranchTrueFalseTotal
ref $s->selected eq 'HASH' ? : 0 23 23
229  
230 23 ▶ if (!$meth) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($meth) 0 23 23
231 0 $s->_scroak ("Missing arguments to obj_meth method");
232 }
233  
234 23 my $o = $obj . '_obj';
235 23 $obj = $s->{all}{$file}{$o};
236  
237 23 ▶ if (! $obj->can($meth)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($obj->can($meth)) 0 23 23
238 0 _scroak ("Non-existent method on $obj object: '$meth'");
239 }
240 23 return $obj->$meth($s->_short_name, @_);
241 }
242  
243 932 ▶sub selected {
Subroutine selected: called CC=1 SCAR=0.0
Pod documented
244 932 my $s = shift;
245 932 $s->{selected};
246 }
247  
248 24 ▶sub has_obj {
Subroutine has_obj: called CC=3 SCAR=11.0
Pod documented
249 24 my ($s, $type) = @_;
250  
251 24 ▶ if (!$type) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($type) 1 23 24
252 1 $s->_scroak ("Missing argument to has method");
253 }
254  
255 23 my $to = "${type}_obj";
256 my $file = ref ($s->selected) eq 'HASH'
257 ? $s->selected->{full_path}
258 23 ▶ : $s->selected;
Branch: 1 branch
BranchTrueFalseTotal
ref $s->selected eq 'HASH' ? : 0 23 23
259 23 return defined $s->{all}{$file}{$to};
260 }
261  
262 23 ▶sub attr_defined {
Subroutine attr_defined: called CC=2 SCAR=7.2
Pod documented
263 23 my $s = shift;
264 23 my $obj = shift;
265 23 my $attr = shift;
266  
267 23 ▶ if (ref $s->selected eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $s->selected eq 'HASH') { } 0 23 23
268 0 return exists $s->selected->{"${obj}_obj"}->{"_${attr}"};
269 } else {
270 23 return exists $s->{_files}{all}{$s->selected}{"${obj}_obj"}->{"_${attr}"};
271 }
272 }
273  
274 30 ▶sub print_short_name {
Subroutine print_short_name: called CC=2 SCAR=7.1
Pod documented
275 30 my $s = shift;
276  
277 30 ▶ $s->_scroak ("The 'print_short_name' method does not accept methods") if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 30 30
278 30 print $s->_short_name . "\n";
279 }
280  
281 # private helper methods
282  
283 53 ▶sub _short_name {
Subroutine _short_name: called CC=2 SCAR=6.9
284 53 my $s = shift;
285 my $file = ref ($s->selected) eq 'HASH'
286 ? $s->selected->{full_path}
287 53 ▶ : $s->selected;
Branch: 1 branch
BranchTrueFalseTotal
ref $s->selected eq 'HASH' ? : 10 43 53
288 53 $s->{all}{$file}{short_path};
289 }
290  
291 7 ▶sub _exists {
Subroutine _exists: called CC=2 SCAR=6.9
292 7 my $s = shift;
293 7 ▶ $s->_scroak("'$_[0]' does not exist, aborting call from: ") if ! -e $_[0];
Branch: 1 branch
BranchTrueFalseTotal
if not -e $_[0] 1 6 7
294 }
295  
296 5 ▶sub _scroak {
Subroutine _scroak: called CC=1 SCAR=0.0
297 5 my $s = shift;
298 5 my $msg = shift;
299 5 croak($msg . ' ' . (fileparse((caller(1))[1]))[0] . ', line ' . (caller(1))[2] . "\n");
300 }
301  
302 10 ▶sub _get_file_manifest {
Subroutine _get_file_manifest: called CC=6 SCAR=18.0
303 10 my ($s, $dir) = @_;
304  
305 10 ▶ opendir (my $dh, $dir) or die "Can't opendir $dir: $!";
Branch: 1 branch
BranchTrueFalseTotal
opendir my $dh, $dir or die "Can't opendir ${dir}: $!" 10 0 10
306 10 my @dirs_and_files = grep { /^[^\.]/ } readdir($dh);
307  
308 10 my @files = grep { -f File::Spec->catfile($dir, $_) } @dirs_and_files;
309 10 $s->_add_file( File::Spec->catfile($dir, $_)) for @files;
310  
311 10 ▶ my @dirs = grep { -d File::Spec->catdir( $dir, $_ ) } @dirs_and_files if $s->{_options}{recurse};
Branch: 1 branch
BranchTrueFalseTotal
if $s->{"_options"}{"recurse"} 9 1 10
312 10 foreach my $tdir (@dirs) {
313 5 ▶ opendir (my $tdh, File::Spec->catdir($dir, $tdir)) || die "Can't opendir $tdir: $!";
Branch: 1 branch
BranchTrueFalseTotal
opendir my $tdh, "File::Spec"->catdir($dir, $tdir) or die "Can't opendir ${tdir}: $!" 5 0 5
314 5 $s->_get_file_manifest(File::Spec->catdir( $dir, $tdir ));
315 }
316 }
317  
318 5 ▶sub _run_all {
Subroutine _run_all: called CC=3 SCAR=11.1
319 5 my $s = shift;
320 5 my $classes = $s->{_classes};
321 5 foreach my $c ( @$classes ) {
322 5 my $role = Role::Tiny->apply_roles_to_object ($s, $c);
323 5 ▶ $role->_run_processes if $role->can('_run_processes');;
Branch: 1 branch
BranchTrueFalseTotal
if $role->can("_run_processes") 0 5 5
324 }
325 }
326  
327 5 ▶sub _generate_short_names {
Subroutine _generate_short_names: called CC=8 SCAR=20.8
328 5 my $s = shift;
329  
330 5 my @files = $s->get_files;
331 5 my $file = pop @files;
332 5 my @letters = split //, $file;
333 5 my ($new_string, $longest_string) = '';
334 5 foreach my $cfile (@files) {
335 18 my @cletters = split //, $cfile;
336 18 my $lc = 0;
337  
338 18 foreach my $cletter (@cletters) {
339 1252 ▶ if (defined $letters[$lc] && $cletters[$lc] eq $letters[$lc]) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $letters[$lc] and $cletters[$lc] eq $letters[$lc]) 1234 18 1252
Condition100%3/3
defined $letters[$lc] and $cletters[$lc] eq $letters[$lc]
!ll&&!rl&&r
1531234
MC/DC100%2/2
defined $letters[$lc] and $cletters[$lc] eq $letters[$lc]
defined $letters[$lc] $cletters[$lc] eq $letters[$lc]
Truth table100%3/3
A and B
A= defined $letters[$lc]B= $cletters[$lc] eq $letters[$lc]
ABresult
0X0
100
111
340 1234 $new_string .= $cletters[$lc++];
341 1234 next;
342 }
343 18 $longest_string = $new_string;
344 18 @letters = split //, $new_string;
345 18 $new_string = '';
346 18 last;
347 }
348 }
349  
350 5 ▶ $s->{_common_dir} = $longest_string || (fileparse($file))[1];
Condition66%2/3
$longest_string || (fileparse($file))[1]
l!l&&r!l&&!r
320
MC/DC0%0/2
$longest_string || (fileparse($file))[1]
$longest_string (fileparse($file))[1]
Truth table66%2/3
A || B
A= $longest_stringB= (fileparse($file))[1]
ABresult
1X1
011
000
351  
352 5 ▶ if (@files) {
Branch: 1 branch
BranchTrueFalseTotal
if (@files) { } 3 2 5
353 3 foreach my $file ( @files, $file ) {
354 21 my $tfile = $file;
355 21 $tfile =~ s/\Q$longest_string\E//;
356 21 $s->{_files}{all}{$file}{short_path} = $tfile;
357 }
358 } else {
359 2 $s->{_files}{all}{$file}{short_path} = $file;
360 }
361 }
362  
363 23 ▶sub _add_file {
Subroutine _add_file: called CC=2 SCAR=7.0
364 23 my ($s, $file) = @_;
365  
366 23 $file = File::Spec->rel2abs( $file );
367 23 $s->{_files}{all}{$file}{full_path} = $file;
368 23 my $filename = (fileparse($file))[0];
369 23 $s->{_files}{all}{$file}{filename} = $filename;
370  
371 23 ▶ push @{$s->{_files}{new}}, $file if !$s->{_files}{$file};
Branch: 1 branch
BranchTrueFalseTotal
if not $s->{"_files"}{$file} 23 0 23
372 }
373  
374 5 ▶sub _init_all_processors {
Subroutine _init_all_processors: called CC=5 SCAR=16.3
375 5 my $s = shift;
376  
377 5 foreach my $c ( @{ $s->{_classes} } ) {
378 5 ▶ my @processors = $c->_init_processors if $c->can('_init_processors');
Branch: 1 branch
BranchTrueFalseTotal
if $c->can("_init_processors") 5 0 5
379 5 my $it_class = $c . '::Processor';
380 5 foreach my $it ( @processors ) {
381 10 ▶ next if ($s->{_files}{"${it}_files"}); # don't overwrite existing processor
Branch: 1 branch
BranchTrueFalseTotal
if $s->{"_files"}{"${it}_files"} 0 10 10
382 10 $s->{_processor_map}{$it} = $it_class;
383 10 $s->{_files}{"${it}_files"} = $it_class->new($s->{_files}{all}, \($s->{selected}));
384 }
385 }
386 }
387  
388 23 ▶sub _classify_all {
Subroutine _classify_all: called CC=3 SCAR=11.1
389 23 my $s = shift;
390 23 foreach my $c ( @{ $s->{_classes} } ) {
391 23 my $role = Role::Tiny->apply_roles_to_object ($s, $c);
392 23 ▶ $role->_classify_file() if $role->can('_classify_file');
Branch: 1 branch
BranchTrueFalseTotal
if $role->can("_classify_file") 23 0 23
393 }
394 }
395  
396 11 ▶sub _get_args {
Subroutine _get_args: called CC=8 SCAR=20.8
397 11 my $user_opts = {};
398 11 my @resources;
399 my $classes;
400 11 foreach my $arg (@_) {
401 24 ▶ if (!ref $arg) {
Branch: 3 branches
BranchTrueFalseTotal
if (not ref $arg) { } 10 14 24
elsif (ref $arg eq 'HASH') { } 4 10 14
elsif (ref $arg eq 'ARRAY') { } 9 1 10
402 10 push @resources, $arg;
403 } elsif (ref($arg) eq 'HASH') {
404 4 ▶ die ('Only one option hash allowed in constructor. Aborting.') if %$user_opts;
Branch: 1 branch
BranchTrueFalseTotal
if %$user_opts 1 3 4
405 3 $user_opts = $arg;
406 } elsif (ref($arg) eq 'ARRAY') {
407 9 ▶ die ('Only one class array allowed in constructor. Aborting.') if $classes;
Branch: 1 branch
BranchTrueFalseTotal
if $classes 1 8 9
408 8 $classes = $arg;
409 } else {
410 1 die ('Unrecognized argument type passed to constructor');
411 }
412 }
413 8 ▶ die('No list of resources passed to constructor. Aborting.') if ! @resources;
Branch: 1 branch
BranchTrueFalseTotal
unless @resources 2 6 8
414 #die('No Collector class array passed to constructor. Aborting.') if !$classes;
415  
416 6 return ($user_opts, $classes, @resources);
417 }
418  
419 3 1; # Magic true value
420 # ABSTRACT: Base class for custom File::Collector classes for classifying files and calling File::Collector::Processor methods for processing files
421  
422 __END__
↑ Summary Collector/Processor.pm »