lib/Project/Easy/Config.pm

filter: Statementstmt 81.2% 113 / 139 Branchbran 58.3% 49 / 84 Conditioncond 46.5% 27 / 58 MC/DCmcdc 21.4% 6 / 28 Subroutinesub 84.6% 11 / 13 Podpod 0.0% 0 / 8 totaltotal 62.4% 206 / 330 CC 71 SCAR 31.7
CC
71
Cov
67%
patch
37.7
patch
37.7
_recursive_traverse_struct
17.1
CRAP 23.7
« lib/Project/Easy/Helper/DB.pm ↑ Summary lib/Project/Easy/Helper.pm »
1 package Project::Easy::Config;
2  
3 14 ▶use Class::Easy;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 6 our %nonexistent_keys_in_config = ();
6 6 our @curr_patch_config_path = ();
7  
8 #sub patch ($$);
9  
10 6 ▶sub parse {
Subroutine parse: called CC=1 SCAR=0.0
Pod undocumented
11 5 ▶ my $class = shift;
Subroutine parse: called CC=1 SCAR=0.0
Pod undocumented
12 5 my $core = shift;
13 5 my $instance = shift;
14
15 2 my $path = $core->conf_path;
16 2 my $fixup = $core->fixup_path_instance ($instance);
17
18 # TODO: replace to real splitpath and join '/' for windows users
19 2 my $root_path = $core->root->path;
20 2 $root_path =~ s/\\/\//g;
21  
22 # here we want to expand some generic params
23 2 my $expansion = {
24 root => $root_path,
25 id => $core->id,
26 instance => $core->instance,
27 };
28
29 2 my $conf = $path->deserialize ($expansion);
30 2 my $alt = $fixup->deserialize ($expansion);
31
32 2 patch ($conf, $alt);
33
34 2 return $conf;
35 }
36  
37 6 my $ext_syn = {
38 'pl' => 'perl',
39 'js' => 'json',
40 };
41  
42 21 ▶sub serializer {
Subroutine serializer: called CC=3 SCAR=11.1
Pod undocumented
43 2 ▶ shift;
Subroutine serializer: called CC=3 SCAR=11.1
Pod undocumented
44 2 my $type = shift;
45
46 $type = $ext_syn->{$type}
47 2 ▶ if exists $ext_syn->{$type};
Branch: 2 branches
BranchTrueFalseTotal
if exists $ext_syn->{$type} 4 10 14
if exists $ext_syn->{$type} 0 21 21
48
49 14 my $pack = "Project::Easy::Config::Format::$type";
50
51 14 ▶ die ('no such serializer: ', $type)
Branch: 2 branches
BranchTrueFalseTotal
unless try_to_use($pack) 0 14 14
unless try_to_use($pack) 0 21 21
52 unless try_to_use ($pack);
53
54 14 return $pack->new;
55 }
56  
57 34 ▶sub string_from_template {
Subroutine string_from_template: called CC=4 SCAR=14.1
Pod undocumented
58  
59 14 ▶ my $template = shift;
Subroutine string_from_template: called CC=4 SCAR=14.1
Pod undocumented
60 14 my $expansion = shift;
61  
62 14 ▶ return unless $template;
Branch: 2 branches
BranchTrueFalseTotal
unless $template 0 18 18
unless $template 0 34 34
63  
64 18 foreach (keys %$expansion) {
65 18 ▶ next unless defined $expansion->{$_};
Branch: 2 branches
BranchTrueFalseTotal
unless defined $expansion->{$_} 0 50 50
unless defined $expansion->{$_} 0 112 112
66  
67 18 $template =~ s/\{\$$_\}/$expansion->{$_}/sg;
68 }
69  
70 18 return $template;
71 }
72  
73 15 ▶sub patch {
Subroutine patch: called CC=21 SCAR=36.3
Pod undocumented
74 50 ▶ my $struct = shift;
Subroutine patch: called CC=21 SCAR=36.3
Pod undocumented
75 50 my $patch = shift;
76 18 ▶ my $algorithm = shift || 'ordinary_patch';
Condition100%4/4
shift() || 'ordinary_patch'
l!l
79
shift() || 'ordinary_patch'
l!l
69
MC/DC100%1/1
shift() || 'ordinary_patch'
shift()
MC/DC100%1/1
shift() || 'ordinary_patch'
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
Truth table100%2/2
A
A= shift()
Aresult
11
00
77
78 # $algorithm = ordinary_patch || undef_keys_in_patch || store_nonexistent_keys_in_struct
79
80 16 ▶ return if ref $struct ne 'HASH' and ref $patch ne 'HASH';
Branch: 2 branches
BranchTrueFalseTotal
if ref $struct ne "HASH" and ref $patch ne "HASH" 6 10 16
if ref $struct ne "HASH" and ref $patch ne "HASH" 0 15 15
Condition50%3/6
ref $struct ne "HASH" and ref $patch ne "HASH"
!ll&&!rl&&r
1006
ref $struct ne "HASH" and ref $patch ne "HASH"
!ll&&!rl&&r
1500
MC/DC50%1/2
ref $struct ne "HASH" and ref $patch ne "HASH"
ref $struct ne "HASH" ref $patch ne "HASH"
MC/DC0%0/2
ref $struct ne "HASH" and ref $patch ne "HASH"
ref $struct ne "HASH" ref $patch ne "HASH"
Truth table66%2/3
A and B
A= ref $struct ne "HASH"B= ref $patch ne "HASH"
ABresult
0X0
100
111
Truth table33%1/3
A and B
A= ref $struct ne "HASH"B= ref $patch ne "HASH"
ABresult
0X0
100
111
81  
82 16 ▶ unless ( scalar keys %$struct ) {
Branch: 2 branches
BranchTrueFalseTotal
unless (scalar keys %$struct) 3 7 10
unless (scalar keys %$struct) 6 9 15
83 16 %$struct = %$patch;
84 16 return;
85 }
86
87 10 my $algo_id = {
88 ordinary_patch => 1,
89 undef_keys_in_patch => 2,
90 store_nonexistent_keys_in_struct => 3,
91 };
92
93 3 foreach my $k (keys %$patch) {
94
95 3 push @curr_patch_config_path, $k;
96  
97 7 ▶ if (! exists $struct->{$k}) {
Branch: 8 branches
BranchTrueFalseTotal
if (not exists $struct->{$k}) { } 3 8 11
elsif (not ref $patch->{$k} || ref $struct->{$k} or ref $patch->{$k} eq 'ARRAY' and ref $struct->{$k} eq 'ARRAY' or ref $patch->{$k} eq 'Regexp' and ref $struct->{$k} eq 'Regexp') { } 7 1 8
elsif (ref $patch->{$k} eq 'HASH' and ref $struct->{$k} eq 'HASH') { } 1 0 1
elsif (ref $patch->{$k} eq 'CODE' and ref $struct->{$k} eq 'CODE' || !defined($struct->{$k})) { } 0 0 0
if (not exists $struct->{$k}) { } 2 7 9
elsif (not ref $patch->{$k} || ref $struct->{$k} or ref $patch->{$k} eq 'ARRAY' and ref $struct->{$k} eq 'ARRAY' or ref $patch->{$k} eq 'Regexp' and ref $struct->{$k} eq 'Regexp') { } 1 6 7
elsif (ref $patch->{$k} eq 'HASH' and ref $struct->{$k} eq 'HASH') { } 6 0 6
elsif (ref $patch->{$k} eq 'CODE' and ref $struct->{$k} eq 'CODE' || !defined($struct->{$k})) { } 0 0 0
98 7 ▶ if ( $algo_id->{$algorithm} == 2 ) {
Branch: 4 branches
BranchTrueFalseTotal
if ($algo_id->{$algorithm} == 2) { } 1 2 3
elsif ($algo_id->{$algorithm} == 3) { } 1 1 2
if ($algo_id->{$algorithm} == 2) { } 0 2 2
elsif ($algo_id->{$algorithm} == 3) { } 0 2 2
99 0 $struct->{$k} = _recursive_undef_struct($patch->{$k});
100 }
101 elsif ( $algo_id->{$algorithm} == 3 ) {
102 0 _recursive_traverse_struct($patch->{$k}, join('.', @curr_patch_config_path));
103 }
104 else {
105 3 $struct->{$k} = $patch->{$k};
106 }
107
108 ▶ } elsif (
Condition60%18/30
not ref $patch->{$k} || ref $struct->{$k} or ref $patch->{$k} eq "ARRAY" and ref $struct->{$k} eq "ARRAY" or ref $patch->{$k} eq "Regexp" and ref $struct->{$k} eq "Regexp"
l!l&&r!l&&!r
701
not ref $patch->{$k} || ref $struct->{$k} or ref $patch->{$k} eq "ARRAY" and ref $struct->{$k} eq "ARRAY"
l!l&&r!l&&!r
611
ref $patch->{$k} || ref $struct->{$k}
l!l&&r!l&&!r
206
ref $patch->{$k} eq "ARRAY" and ref $struct->{$k} eq "ARRAY"
!ll&&!rl&&r
101
ref $patch->{$k} eq "Regexp" and ref $struct->{$k} eq "Regexp"
!ll&&!rl&&r
100
not ref $patch->{$k} || ref $struct->{$k} or ref $patch->{$k} eq "ARRAY" and ref $struct->{$k} eq "ARRAY" or ref $patch->{$k} eq "Regexp" and ref $struct->{$k} eq "Regexp"
l!l&&r!l&&!r
106
not ref $patch->{$k} || ref $struct->{$k} or ref $patch->{$k} eq "ARRAY" and ref $struct->{$k} eq "ARRAY"
l!l&&r!l&&!r
106
ref $patch->{$k} || ref $struct->{$k}
l!l&&r!l&&!r
601
ref $patch->{$k} eq "ARRAY" and ref $struct->{$k} eq "ARRAY"
!ll&&!rl&&r
600
ref $patch->{$k} eq "Regexp" and ref $struct->{$k} eq "Regexp"
!ll&&!rl&&r
600
MC/DC33%2/6
not ref $patch->{$k} || ref $struct->{$k} or ref $patch->{$k} eq "ARRAY" and ref $struct->{$k} eq "ARRAY" or ref $patch->{$k} eq "Regexp" and ref $struct->{$k} eq "Regexp"
ref $patch->{$k} ref $struct->{$k} ref $patch->{$k} eq "ARRAY" ref $struct->{$k} eq "ARRAY" ref $patch->{$k} eq "Regexp" ref $struct->{$k} eq "Regexp"
MC/DC16%1/6
not ref $patch->{$k} || ref $struct->{$k} or ref $patch->{$k} eq "ARRAY" and ref $struct->{$k} eq "ARRAY" or ref $patch->{$k} eq "Regexp" and ref $struct->{$k} eq "Regexp"
ref $patch->{$k} ref $struct->{$k} ref $patch->{$k} eq "ARRAY" ref $struct->{$k} eq "ARRAY" ref $patch->{$k} eq "Regexp" ref $struct->{$k} eq "Regexp"
Truth table20%3/15
not(A || B) or C and D or E and F
A= ref $patch->{$k}B= ref $struct->{$k}C= ref $patch->{$k} eq "ARRAY"D= ref $struct->{$k} eq "ARRAY"E= ref $patch->{$k} eq "Regexp"F= ref $struct->{$k} eq "Regexp"
ABCDEFresult
00XXXX1
1X11XX1
0111XX1
1X0X111
1X10111
010X111
0110111
1X0X0X0
1X0X100
1X100X0
1X10100
010X0X0
010X100
01100X0
0110100
Truth table13%2/15
not(A || B) or C and D or E and F
A= ref $patch->{$k}B= ref $struct->{$k}C= ref $patch->{$k} eq "ARRAY"D= ref $struct->{$k} eq "ARRAY"E= ref $patch->{$k} eq "Regexp"F= ref $struct->{$k} eq "Regexp"
ABCDEFresult
00XXXX1
1X11XX1
0111XX1
1X0X111
1X10111
010X111
0110111
1X0X0X0
1X0X100
1X100X0
1X10100
010X0X0
010X100
01100X0
0110100
109 (! ref $patch->{$k} && ! ref $struct->{$k})
110 || (ref $patch->{$k} eq 'ARRAY' && (ref $struct->{$k} eq 'ARRAY'))
111 || (ref $patch->{$k} eq 'Regexp' && (ref $struct->{$k} eq 'Regexp'))
112 ) {
113 1 ▶ if ( $algo_id->{$algorithm} == 2 ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($algo_id->{$algorithm} == 2) { } 4 3 7
if ($algo_id->{$algorithm} == 2) { } 0 1 1
114 0 patch ($struct->{$k}, $patch->{$k}, $algorithm);
115 }
116 else {
117 1 $struct->{$k} = $patch->{$k};
118 }
119 ▶ } elsif (ref $patch->{$k} eq 'HASH' && (ref $struct->{$k} eq 'HASH')) {
Condition33%2/6
ref $patch->{$k} eq "HASH" and ref $struct->{$k} eq "HASH"
!ll&&!rl&&r
001
ref $patch->{$k} eq "HASH" and ref $struct->{$k} eq "HASH"
!ll&&!rl&&r
006
MC/DC0%0/2
ref $patch->{$k} eq "HASH" and ref $struct->{$k} eq "HASH"
ref $patch->{$k} eq "HASH" ref $struct->{$k} eq "HASH"
MC/DC0%0/2
ref $patch->{$k} eq "HASH" and ref $struct->{$k} eq "HASH"
ref $patch->{$k} eq "HASH" ref $struct->{$k} eq "HASH"
Truth table33%1/3
A and B
A= ref $patch->{$k} eq "HASH"B= ref $struct->{$k} eq "HASH"
ABresult
0X0
100
111
Truth table33%1/3
A and B
A= ref $patch->{$k} eq "HASH"B= ref $struct->{$k} eq "HASH"
ABresult
0X0
100
111
120 7 patch ($struct->{$k}, $patch->{$k}, $algorithm);
121 ▶ } elsif (ref $patch->{$k} eq 'CODE' && (ref $struct->{$k} eq 'CODE' || ! defined $struct->{$k})) {
Condition0%0/12
ref $patch->{$k} eq "CODE" and ref $struct->{$k} eq "CODE" || !defined($struct->{$k})
!ll&&!rl&&r
000
ref $struct->{$k} eq "CODE" or not defined $struct->{$k}
l!l&&r!l&&!r
000
ref $patch->{$k} eq "CODE" and ref $struct->{$k} eq "CODE" || !defined($struct->{$k})
!ll&&!rl&&r
000
ref $struct->{$k} eq "CODE" or not defined $struct->{$k}
l!l&&r!l&&!r
000
MC/DC0%0/3
ref $patch->{$k} eq "CODE" and ref $struct->{$k} eq "CODE" || !defined($struct->{$k})
ref $patch->{$k} eq "CODE" ref $struct->{$k} eq "CODE" not defined $struct->{$k}
MC/DC0%0/3
ref $patch->{$k} eq "CODE" and ref $struct->{$k} eq "CODE" || !defined($struct->{$k})
ref $patch->{$k} eq "CODE" ref $struct->{$k} eq "CODE" not defined $struct->{$k}
Truth table0%0/4
A and B or not(C)
A= ref $patch->{$k} eq "CODE"B= ref $struct->{$k} eq "CODE"C= not defined $struct->{$k}
ABCresult
0XX0
1000
11X1
1011
Truth table0%0/4
A and B or not(C)
A= ref $patch->{$k} eq "CODE"B= ref $struct->{$k} eq "CODE"C= not defined $struct->{$k}
ABCresult
0XX0
1000
11X1
1011
122 0 $struct->{$k} = $patch->{$k};
123 }
124 }
125 }
126  
127 0 ▶sub _recursive_undef_struct {
Subroutine _recursive_undef_struct: not called CC=4 SCAR=19.3
128 0 ▶ my $data = shift;
Subroutine _recursive_undef_struct: called CC=4 SCAR=19.3
129
130 0 ▶ if ( ! ref $data ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not ref $data) { } 15 6 21
if (not ref $data) { } 0 0 0
131 0 $data = undef;
132 }
133 else {
134 0 ▶ if ( ref $data eq 'ARRAY' ) {
Branch: 4 branches
BranchTrueFalseTotal
if (ref $data eq 'ARRAY') { } 3 3 6
elsif (ref $data eq 'HASH') { } 3 0 3
if (ref $data eq 'ARRAY') { } 0 0 0
elsif (ref $data eq 'HASH') { } 0 0 0
135 0 @$data = map { _recursive_undef_struct($_) } @$data;
136 }
137 elsif ( ref $data eq 'HASH' ) {
138 0 %$data = map { $_ => _recursive_undef_struct($data->{$_}) } keys %$data;
139 }
140 }
141
142 0 return $data;
143 }
144  
145 0 ▶sub _recursive_traverse_struct {
Subroutine _recursive_traverse_struct: not called CC=8 SCAR=28.4
146 0 ▶ my $data = shift;
Subroutine _recursive_traverse_struct: called CC=8 SCAR=28.4
147 0 my $name = shift;
148
149 0 ▶ if ( ! ref $data) {
Branch: 6 branches
BranchTrueFalseTotal
if (not ref $data) { } 6 5 11
elsif (ref $data eq 'ARRAY') { } 1 4 5
elsif (ref $data eq 'HASH') { } 4 0 4
if (not ref $data) { } 0 0 0
elsif (ref $data eq 'ARRAY') { } 0 0 0
elsif (ref $data eq 'HASH') { } 0 0 0
150 0 $nonexistent_keys_in_config{$name} = 1;
151 }
152 elsif ( ref $data eq 'ARRAY' ) {
153 0 foreach my $element (@$data) {
154 0 ▶ if (! ref $element) {
Branch: 2 branches
BranchTrueFalseTotal
if (not ref $element) { } 3 1 4
if (not ref $element) { } 0 0 0
155 0 $nonexistent_keys_in_config{$name} = 'ARRAY of ' . scalar @$data . ' elements';
156 }
157 else {
158 0 _recursive_traverse_struct($element, $name);
159 }
160 }
161 }
162 elsif ( ref $data eq 'HASH' ) {
163 0 foreach my $key ( keys %$data ) {
164 0 ▶ $nonexistent_keys_in_config{"$name.$key"} = 1 if (! ref $data->{$key});
Branch: 2 branches
BranchTrueFalseTotal
unless ref $data->{$key} 5 3 8
unless ref $data->{$key} 0 0 0
165  
166 0 _recursive_traverse_struct($data->{$key}, "$name.$key");
167 }
168 }
169  
170 0 return $data;
171 }
172  
173 6 1;
« lib/Project/Easy/Helper/DB.pm ↑ Summary lib/Project/Easy/Helper.pm »