Visitor/LoadDirectoryTree.pm

filter: Statementstmt 100% 61 / 61 Branchbran 88.4% 23 / 26 Conditioncond 87.1% 27 / 31 MC/DCmcdc 75.0% 15 / 20 Subroutinesub 100% 14 / 14 Podpod 100% 5 / 5 totaltotal 92.3% 145 / 157 CC 25 SCAR 16.1
CC
25
Cov
94%
_recursiveLoad
9.1
visit
6.0
__ANON__
5.0
CRAP 5.0
« Visitor/LoadClassHierarchy.pm ↑ Summary Visitor/FromNestedArray.pm »
1 package Tree::Simple::Visitor::LoadDirectoryTree;
2  
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  
6 1 our $VERSION = '0.16';
7  
8 1 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Scalar::Util qw(blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 1 ▶use base qw(Tree::Simple::Visitor);
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 4 ▶sub new {
Subroutine new: called CC=2 SCAR=7.1
Pod documented
14 4 my ($_class) = @_;
15 4 ▶ my $class = ref($_class) || $_class;
Condition33%1/3
ref $_class || $_class
l!l&&r!l&&!r
040
MC/DC0%0/2
ref $_class || $_class
ref $_class $_class
Truth table33%1/3
A || B
A= ref $_classB= $_class
ABresult
1X1
011
000
16 4 my $visitor = {};
17 4 bless($visitor, $class);
18 4 $visitor->_init();
19 4 return $visitor;
20 }
21  
22 4 ▶sub _init {
Subroutine _init: called CC=1 SCAR=0.0
23 4 my ($self) = @_;
24 4 $self->{sort_function} = undef;
25 4 $self->SUPER::_init();
26 }
27  
28 # pre-built sort functions
29 1 ▶sub SORT_FILES_FIRST {
Subroutine SORT_FILES_FIRST: called CC=1 SCAR=0.0
Pod documented
30 172 ▶ return sub ($$$) {
Subroutine __ANON__: called CC=5 SCAR=16.1
31 172 my ($path, $left, $right) = @_;
32 172 $left = File::Spec->catdir($path, $left);
33 172 $right = File::Spec->catdir($path, $right);
34 172 ▶ return ((-d $left && -f $right) ? 1 : # file beats directory
Branch: 2 branches
BranchTrueFalseTotal
-d $left && -f $right ? : 8 164 172
-d $right && -f $left ? : 7 157 164
Condition100%6/6
-d $left and -f $right
!ll&&!rl&&r
15688
-d $right and -f $left
!ll&&!rl&&r
14987
MC/DC100%2/2
-d $left and -f $right
-d $left -f $right
MC/DC100%2/2
-d $right and -f $left
-d $right -f $left
Truth table100%3/3
A and B
A= -d $leftB= -f $right
ABresult
0X0
100
111
Truth table100%3/3
A and B
A= -d $rightB= -f $left
ABresult
0X0
100
111
35 (-d $right && -f $left) ? -1 : # file beats directory
36 (lc($left) cmp lc($right))) # otherwise just sort 'em
37 }
38 1 }
39  
40 1 ▶sub SORT_DIRS_FIRST {
Subroutine SORT_DIRS_FIRST: called CC=1 SCAR=0.0
Pod documented
41 169 ▶ return sub ($$$) {
Subroutine __ANON__: called CC=5 SCAR=16.1
42 169 my ($path, $left, $right) = @_;
43 169 $left = File::Spec->catdir($path, $left);
44 169 $right = File::Spec->catdir($path, $right);
45 169 ▶ return ((-d $left && -f $right) ? -1 : # directory beats file
Branch: 2 branches
BranchTrueFalseTotal
-d $left && -f $right ? : 7 162 169
-d $right && -f $left ? : 5 157 162
Condition100%6/6
-d $left and -f $right
!ll&&!rl&&r
15487
-d $right and -f $left
!ll&&!rl&&r
14985
MC/DC100%2/2
-d $left and -f $right
-d $left -f $right
MC/DC100%2/2
-d $right and -f $left
-d $right -f $left
Truth table100%3/3
A and B
A= -d $leftB= -f $right
ABresult
0X0
100
111
Truth table100%3/3
A and B
A= -d $rightB= -f $left
ABresult
0X0
100
111
46 (-d $right && -f $left) ? 1 : # directory beats file
47 (lc($left) cmp lc($right))) # otherwise just sort 'em
48 }
49 1 }
50  
51 5 ▶sub setSortStyle {
Subroutine setSortStyle: called CC=3 SCAR=11.0
Pod documented
52 5 my ($self, $sort_function) = @_;
53 5 ▶ (defined($sort_function) && ref($sort_function) eq "CODE")
Branch: 1 branch
BranchTrueFalseTotal
defined $sort_function and ref $sort_function eq "CODE" or die "Insufficient Arguments : sort function argument must be a subroutine reference" 2 3 5
Condition100%3/3
defined $sort_function and ref $sort_function eq "CODE"
!ll&&!rl&&r
122
MC/DC100%2/2
defined $sort_function and ref $sort_function eq "CODE"
defined $sort_function ref $sort_function eq "CODE"
Truth table100%3/3
A and B
A= defined $sort_functionB= ref $sort_function eq "CODE"
ABresult
0X0
100
111
54 || die "Insufficient Arguments : sort function argument must be a subroutine reference";
55 2 $self->{sort_function} = $sort_function;
56 }
57  
58 9 ▶sub visit {
Subroutine visit: called CC=6 SCAR=17.9
Pod documented
59 9 my ($self, $tree) = @_;
60 9 ▶ (blessed($tree) && $tree->isa("Tree::Simple"))
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($tree) and $tree->isa("Tree::Simple") or die "Insufficient Arguments : You must supply a valid Tree::Simple object" 5 4 9
Condition100%3/3
builtin::blessed($tree) and $tree->isa("Tree::Simple")
!ll&&!rl&&r
315
MC/DC100%2/2
builtin::blessed($tree) and $tree->isa("Tree::Simple")
builtin::blessed($tree) $tree->isa("Tree::Simple")
Truth table100%3/3
A and B
A= builtin::blessed($tree)B= $tree->isa("Tree::Simple")
ABresult
0X0
100
111
61 || die "Insufficient Arguments : You must supply a valid Tree::Simple object";
62 # it must be a leaf
63 5 ▶ ($tree->isLeaf()) || die "Illegal Operation : The tree must be a leaf node to load a directory";
Branch: 1 branch
BranchTrueFalseTotal
$tree->isLeaf or die "Illegal Operation : The tree must be a leaf node to load a directory" 4 1 5
64 # check that our directory is valid
65 4 my $root_dir = $tree->getNodeValue();
66 4 ▶ (-e $root_dir && -d $root_dir)
Branch: 1 branch
BranchTrueFalseTotal
-e $root_dir and -d $root_dir or die "Incorrect Type : The tree's node value must be a valid directory" 3 1 4
Condition66%2/3
-e $root_dir and -d $root_dir
!ll&&!rl&&r
103
MC/DC50%1/2
-e $root_dir and -d $root_dir
-e $root_dir -d $root_dir
Truth table66%2/3
A and B
A= -e $root_dirB= -d $root_dir
ABresult
0X0
100
111
67 || die "Incorrect Type : The tree's node value must be a valid directory";
68 # and load it recursively
69 3 $self->_recursiveLoad($tree, $root_dir);
70 }
71  
72 18 ▶sub _recursiveLoad {
Subroutine _recursiveLoad: called CC=9 SCAR=22.1
73 18 my ($self, $t, $path) = @_;
74 # get a node filter if we have one
75 18 my $filter = $self->getNodeFilter();
76  
77 # get the contents of the directory
78 18 ▶ opendir(DIR, $path) || die "IO Error : Could not open directory : $!";
Branch: 1 branch
BranchTrueFalseTotal
opendir DIR, $path or die "IO Error : Could not open directory : $!" 18 0 18
79 # avoid the . and .. symbolic links
80 my @dir_contents = grep {
81 18 ▶ $_ ne File::Spec->curdir() && $_ ne File::Spec->updir()
Condition100%3/3
$_ ne 'File::Spec'->curdir && $_ ne 'File::Spec'->updir
!ll&&!rl&&r
1818177
MC/DC100%2/2
$_ ne 'File::Spec'->curdir && $_ ne 'File::Spec'->updir
$_ ne 'File::Spec'->curdir $_ ne 'File::Spec'->updir
Truth table100%3/3
A && B
A= $_ ne 'File::Spec'->curdirB= $_ ne 'File::Spec'->updir
ABresult
0X0
100
111
82 } readdir(DIR);
83 18 close(DIR);
84  
85 # sort them if we need to with full paths
86 @dir_contents = sort {
87 341 $self->{sort_function}->($path, $a, $b)
88 18 ▶ } @dir_contents if $self->{sort_function};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"sort_function"} 12 6 18
89  
90 # now traverse ...
91 18 foreach my $item (@dir_contents) {
92 # filter based on the item name
93 177 ▶ $filter->($item) || next if defined($filter);
Branch: 1 branch
BranchTrueFalseTotal
if defined $filter 177 0 177
Condition75%3/4
defined $filter && (&$filter($item) || (next))
!ll
0177
&$filter($item) or next
l!l
14136
MC/DC0%0/2
defined $filter && (&$filter($item) || (next))
defined $filter &$filter($item)
Truth table33%1/3
A && B
A= defined $filterB= &$filter($item)
ABresult
0X0
100
111
94 # get the full path for checking
95 # the item type and recursion
96 141 my $full_path = File::Spec->catdir($path, $item);
97 141 ▶ if (-d $full_path) {
Branch: 2 branches
BranchTrueFalseTotal
if (-d $full_path) { } 15 126 141
elsif (-f $full_path) { } 126 0 126
98 15 my $new_tree = $t->new($item);
99 15 $t->addChild($new_tree);
100 15 $self->_recursiveLoad($new_tree, $full_path);
101 }
102 elsif (-f $full_path) {
103 126 $t->addChild($t->new($item));
104 }
105 }
106 }
107  
108 1 1;
109  
110 __END__
« Visitor/LoadClassHierarchy.pm ↑ Summary Visitor/FromNestedArray.pm »