blib/lib/Object/InsideOut/Cumulative.pm

filter: Statementstmt 82.2% 167 / 203 Branchbran 63.4% 33 / 52 Conditioncond 41.3% 12 / 29 MC/DCmcdc 17.6% 3 / 17 Subroutinesub 87.5% 21 / 24 Podpod 0.0% 0 / 2 totaltotal 72.1% 236 / 327 SCAR 44.5
CC
48
Cov
75%
generate_CUMULATIVE
66.0
__ANON__
18.0
as_hash
2.0
CRAP 85.5
« blib/lib/Object/InsideOut/Autoload.pm ↑ Summary blib/lib/Object/InsideOut/Dynamic.pm »
1 package Object::InsideOut; {
2  
3 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶no warnings 'redefine';
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 2 my $GBL = {};
8  
9 sub generate_CUMULATIVE :Sub(Private)
10 {
11 2 ▶ ($GBL) = @_;
Subroutine generate_CUMULATIVE: called CC=34 SCAR=41.9
Pod undocumented
12 2 my $g_cu = $$GBL{'sub'}{'cumu'};
13 2 ▶ my $cumu_td = $$g_cu{'new'}{'td'} || [];
Condition100%2/2
$g_cu->{'new'}{'td'} || []
l!l
11
MC/DC100%1/1
$g_cu->{'new'}{'td'} || []
$g_cu->{'new'}{'td'}
Truth table100%2/2
A
A= $g_cu->{'new'}{'td'}
Aresult
11
00
14 2 ▶ my $cumu_bu = $$g_cu{'new'}{'bu'} || [];
Condition100%2/2
$g_cu->{'new'}{'bu'} || []
l!l
11
MC/DC100%1/1
$g_cu->{'new'}{'bu'} || []
$g_cu->{'new'}{'bu'}
Truth table100%2/2
A
A= $g_cu->{'new'}{'bu'}
Aresult
11
00
15 2 delete($$g_cu{'new'});
16 2 ▶ if (! exists($$g_cu{'td'})) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $g_cu->{"td"}) 2 0 2
17 2 $$GBL{'sub'}{'cumu'} = {
18 td => {}, # 'Top down'
19 bu => {}, # 'Bottom up'
20 restrict => {}, # :Restricted
21 };
22 2 $g_cu = $$GBL{'sub'}{'cumu'};
23 }
24 2 my $cu_td = $$g_cu{'td'};
25 2 my $cu_bu = $$g_cu{'bu'};
26 2 my $cu_restr = $$g_cu{'restrict'};
27  
28 # Get names for :CUMULATIVE methods
29 2 my (%cum_loc);
30 2 ▶ while (my $info = shift(@{$cumu_td})) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $info = shift @{$cumu_td;}) 3 2 5
31 3 ▶ $$info{'name'} ||= sub_name($$info{'code'}, ':CUMULATIVE', $$info{'loc'});
Condition33%1/3
$info->{"name"} ||= sub_name($info->{"code"}, ":CUMULATIVE", $info->{"loc"})
l!l&&r!l&&!r
030
MC/DC0%0/2
$info->{"name"} ||= sub_name($info->{"code"}, ":CUMULATIVE", $info->{"loc"})
$info->{"name"} sub_name($info->{"code"}, ":CUMULATIVE", $info->{"loc"})
Truth table33%1/3
A ||= B
A= $info->{"name"}B= sub_name($info->{"code"}, ":CUMULATIVE", $info->{"loc"})
ABresult
1X1
011
000
32 3 my $package = $$info{'pkg'};
33 3 my $name = $$info{'name'};
34  
35 3 $cum_loc{$name}{$package} = $$info{'loc'};
36  
37 3 $$cu_td{$name}{$package} = $$info{'wrap'};
38 3 ▶ if (exists($$info{'exempt'})) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $info->{"exempt"}) 3 0 3
39 3 push(@{$$cu_restr{$package}{$name}},
40 3 ▶ sort grep {$_} split(/[,'\s]+/, $$info{'exempt'} || ''));
Condition50%1/2
$info->{'exempt'} || ''
l!l
03
MC/DC0%0/1
$info->{'exempt'} || ''
$info->{'exempt'}
Truth table50%1/2
A
A= $info->{'exempt'}
Aresult
11
00
41 }
42 }
43  
44 # Get names for :CUMULATIVE(BOTTOM UP) methods
45 2 ▶ while (my $info = shift(@{$cumu_bu})) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $info = shift @{$cumu_bu;}) 2 2 4
46 2 ▶ $$info{'name'} ||= sub_name($$info{'code'}, ':CUMULATIVE(BOTTOM UP)', $$info{'loc'});
Condition33%1/3
$info->{"name"} ||= sub_name($info->{"code"}, ":CUMULATIVE(BOTTOM UP)", $info->{"loc"})
l!l&&r!l&&!r
020
MC/DC0%0/2
$info->{"name"} ||= sub_name($info->{"code"}, ":CUMULATIVE(BOTTOM UP)", $info->{"loc"})
$info->{"name"} sub_name($info->{"code"}, ":CUMULATIVE(BOTTOM UP)", $info->{"loc"})
Truth table33%1/3
A ||= B
A= $info->{"name"}B= sub_name($info->{"code"}, ":CUMULATIVE(BOTTOM UP)", $info->{"loc"})
ABresult
1X1
011
000
47 2 my $package = $$info{'pkg'};
48 2 my $name = $$info{'name'};
49  
50 # Check for conflicting definitions of 'name'
51 2 ▶ if ($$cu_td{$name}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cu_td->{$name}) 0 2 2
52 0 foreach my $other_package (keys(%{$$cu_td{$name}})) {
53 0 ▶ if ($other_package->isa($package) ||
Branch: 1 branch
BranchTrueFalseTotal
if ($other_package->isa($package) or $package->isa($other_package)) 0 0 0
Condition0%0/3
$other_package->isa($package) or $package->isa($other_package)
l!l&&r!l&&!r
000
MC/DC0%0/2
$other_package->isa($package) or $package->isa($other_package)
$other_package->isa($package) $package->isa($other_package)
Truth table0%0/3
A or B
A= $other_package->isa($package)B= $package->isa($other_package)
ABresult
1X1
011
000
54 $package->isa($other_package))
55 {
56 0 my ($pkg, $file, $line) = @{$cum_loc{$name}{$other_package}};
57 0 my ($pkg2, $file2, $line2) = @{$$info{'loc'}};
58 OIO::Attribute->die(
59 0 'location' => $$info{'loc'},
60 'message' => "Conflicting definitions for cumulative method '$name'",
61 'Info' => "Declared as :CUMULATIVE in class '$pkg' (file '$file', line $line), but declared as :CUMULATIVE(BOTTOM UP) in class '$pkg2' (file '$file2' line $line2)");
62 }
63 }
64 }
65  
66 2 $$cu_bu{$name}{$package} = $$info{'wrap'};
67 2 ▶ if (exists($$info{'exempt'})) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $info->{"exempt"}) 0 2 2
68 0 push(@{$$cu_restr{$package}{$name}},
69 0 ▶ sort grep {$_} split(/[,'\s]+/, $$info{'exempt'} || ''));
Condition0%0/2
$info->{'exempt'} || ''
l!l
00
MC/DC0%0/1
$info->{'exempt'} || ''
$info->{'exempt'}
Truth table0%0/2
A
A= $info->{'exempt'}
Aresult
11
00
70 }
71 }
72  
73 # Propagate restrictions
74 2 my $reapply = 1;
75 2 my $trees = $$GBL{'tree'}{'td'};
76 2 ▶ while ($reapply) {
Branch: 1 branch
BranchTrueFalseTotal
if ($reapply) 2 2 4
77 2 $reapply = 0;
78 2 foreach my $pkg (keys(%{$cu_restr})) {
79 3 foreach my $class (keys(%{$trees})) {
80 12 ▶ next if (! grep { $_ eq $pkg } @{$$trees{$class}});
Branch: 1 branch
BranchTrueFalseTotal
unless grep {$_ eq $pkg;} @{$$trees{$class};} 6 6 12
81 6 foreach my $p (@{$$trees{$class}}) {
82 14 foreach my $n (keys(%{$$cu_restr{$pkg}})) {
83 14 ▶ if (exists($$cu_restr{$p}{$n})) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $cu_restr->{$p}{$n}) { } 14 0 14
84 14 ▶ next if ($$cu_restr{$p}{$n} == $$cu_restr{$pkg}{$n});
Branch: 1 branch
BranchTrueFalseTotal
if $cu_restr->{$p}{$n} == $cu_restr->{$pkg}{$n} 12 2 14
85 2 my $equal = (@{$$cu_restr{$p}{$n}} == @{$$cu_restr{$pkg}{$n}});
86 2 ▶ if ($equal) {
Branch: 1 branch
BranchTrueFalseTotal
if ($equal) 2 0 2
87 2 for (1..@{$$cu_restr{$p}{$n}}) {
88 0 ▶ if ($$cu_restr{$pkg}{$n}[$_-1] ne $$cu_restr{$p}{$n}[$_-1]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cu_restr->{$pkg}{$n}[$_ - 1] ne $cu_restr->{$p}{$n}[$_ - 1]) 0 0 0
89 0 $equal = 0;
90 0 last;
91 }
92 }
93 }
94 2 ▶ if (! $equal) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($equal) 0 2 2
95 0 my %restr = map { $_ => 1 } @{$$cu_restr{$p}{$n}}, @{$$cu_restr{$pkg}{$n}};
96 0 $$cu_restr{$pkg}{$n} = [ sort(keys(%restr)) ];
97 0 $reapply = 1;
98 }
99 } else {
100 0 $reapply = 1;
101 }
102 2 $$cu_restr{$p}{$n} = $$cu_restr{$pkg}{$n};
103 }
104 }
105 }
106 }
107 }
108  
109 2 ▶ no warnings 'redefine';
Subroutine BEGIN: called CC=1 SCAR=0.0
110 2 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
111  
112 # Implement :CUMULATIVE methods
113 2 foreach my $name (keys(%{$cu_td})) {
114 1 my $code = create_CUMULATIVE($name, $trees, $$cu_td{$name});
115 1 foreach my $package (keys(%{$$cu_td{$name}})) {
116 3 *{$package.'::'.$name} = $code;
117 3 add_meta($package, $name, 'kind', 'cumulative');
118 3 ▶ if (exists($$cu_restr{$package}{$name})) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $cu_restr->{$package}{$name}) 3 0 3
119 3 add_meta($package, $name, 'restrict', 1);
120 }
121 }
122 }
123  
124 # Implement :CUMULATIVE(BOTTOM UP) methods
125 2 foreach my $name (keys(%{$cu_bu})) {
126 1 my $code = create_CUMULATIVE($name, $$GBL{'tree'}{'bu'}, $$cu_bu{$name});
127 1 foreach my $package (keys(%{$$cu_bu{$name}})) {
128 2 *{$package.'::'.$name} = $code;
129 2 add_meta($package, $name, 'kind', 'cumulative (bottom up)');
130 2 ▶ if (exists($$cu_restr{$package}{$name})) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $cu_restr->{$package}{$name}) 0 2 2
131 0 add_meta($package, $name, 'restrict', 1);
132 }
133 }
134 }
135 2 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
136  
137  
138 # Returns a closure back to initialize() that is used to setup CUMULATIVE
139 # and CUMULATIVE(BOTTOM UP) methods for a particular method name.
140 sub create_CUMULATIVE :Sub(Private)
141 {
142 # $name - method name
143 # $tree - either $GBL{'tree'}{'td'} or $GBL{'tree'}{'bu'}
144 # $code_refs - hash ref by package of code refs for a particular method name
145 4 ▶ my ($name, $tree, $code_refs) = @_;
Subroutine create_CUMULATIVE: called CC=1 SCAR=0.2
Pod undocumented
146  
147 return sub {
148 6 ▶ my $class = ref($_[0]) || $_[0];
Subroutine __ANON__: called CC=14 SCAR=28.9
Condition33%1/3
ref $_[0] || $_[0]
l!l&&r!l&&!r
600
MC/DC0%0/2
ref $_[0] || $_[0]
ref $_[0] $_[0]
Truth table33%1/3
A || B
A= ref $_[0]B= $_[0]
ABresult
1X1
011
000
149 6 ▶ if (! $class) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($class) 0 6 6
150 0 OIO::Method->die('message' => "Must call '$name' as a method");
151 }
152 6 my $list_context = wantarray;
153 6 my (@results, @classes);
154  
155 # Caller must be in class hierarchy
156 6 my $restr = $$GBL{'sub'}{'cumu'}{'restrict'};
157 6 ▶ if ($restr && exists($$restr{$class}{$name})) {
Branch: 1 branch
BranchTrueFalseTotal
if ($restr and exists $restr->{$class}{$name}) 1 5 6
Condition66%2/3
$restr and exists $restr->{$class}{$name}
!ll&&!rl&&r
051
MC/DC50%1/2
$restr and exists $restr->{$class}{$name}
$restr exists $restr->{$class}{$name}
Truth table66%2/3
A and B
A= $restrB= exists $restr->{$class}{$name}
ABresult
0X0
100
111
158 1 my $caller = caller();
159 1 ▶ if (! ((grep { $_ eq $caller } @{$$restr{$class}{$name}}) ||
Branch: 1 branch
BranchTrueFalseTotal
unless (grep {$_ eq $caller;} @{$$restr{$class}{$name};} or $caller->isa($class) or $class->isa($caller)) 0 1 1
Condition33%2/6
grep {$_ eq $caller;} @{$$restr{$class}{$name};} or $caller->isa($class) or $class->isa($caller)
l!l&&r!l&&!r
010
grep {$_ eq $caller;} @{$$restr{$class}{$name};} or $caller->isa($class)
l!l&&r!l&&!r
001
MC/DC0%0/3
grep {$_ eq $caller;} @{$$restr{$class}{$name};} or $caller->isa($class) or $class->isa($caller)
grep {$_ eq $caller;} @{$$restr{$class}{$name};} $caller->isa($class) $class->isa($caller)
Truth table25%1/4
A or B or C
A= grep {$_ eq $caller;} @{$$restr{$class}{$name};}B= $caller->isa($class)C= $class->isa($caller)
ABCresult
1XX1
01X1
0011
0000
160 $caller->isa($class) ||
161 $class->isa($caller)))
162 {
163 0 OIO::Method->die('message' => "Can't call restricted method '$class->$name' from class '$caller'");
164 }
165 }
166  
167 # Accumulate results
168 6 foreach my $pkg (@{$$tree{$class}}) {
169 14 ▶ if (my $code = $$code_refs{$pkg}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $code = $code_refs->{$pkg}) 13 1 14
170 13 local $SIG{'__DIE__'} = 'OIO::trap';
171 13 my @args = @_;
172 13 ▶ if (defined($list_context)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $list_context) { } 13 0 13
173 13 push(@classes, $pkg);
174 13 ▶ if ($list_context) {
Branch: 1 branch
BranchTrueFalseTotal
if ($list_context) { } 4 9 13
175 # List context
176 4 push(@results, $code->(@args));
177 } else {
178 # Scalar context
179 9 push(@results, scalar($code->(@args)));
180 }
181 } else {
182 # void context
183 0 $code->(@args);
184 }
185 }
186 }
187  
188 # Return results
189 6 ▶ if (defined($list_context)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $list_context) 6 0 6
190 6 ▶ if ($list_context) {
Branch: 1 branch
BranchTrueFalseTotal
if ($list_context) 3 3 6
191 # List context
192 3 return (@results);
193 }
194 # Scalar context - returns object
195 3 return (Object::InsideOut::Results->new('VALUES' => \@results,
196 'CLASSES' => \@classes));
197 }
198 4 };
199 2 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
200  
201 } # End of package's lexical scope
202  
203  
204 2 package Object::InsideOut::Results; {
205  
206 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
207 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
208  
209 2 our $VERSION = '4.04';
210 2 $VERSION = eval $VERSION;
211  
212 2 ▶use Object::InsideOut 4.04;
Subroutine BEGIN: called CC=1 SCAR=0.0
213 2 ▶use Object::InsideOut::Metadata 4.04;
Subroutine BEGIN: called CC=1 SCAR=0.0
214  
215 2 my @VALUES :Field :Arg(VALUES);
216 2 my @CLASSES :Field :Arg(CLASSES);
217 2 my @HASHES :Field;
218  
219 sub as_string :Stringify
220 {
221 0 ▶ return (join('', grep(defined, @{$VALUES[${$_[0]}]})));
Subroutine as_string: not called CC=1 SCAR=6.9
222 2 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
223  
224 sub count :Numerify
225 {
226 0 ▶ return (scalar(@{$VALUES[${$_[0]}]}));
Subroutine count: not called CC=1 SCAR=6.9
227 2 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
228  
229 sub have_any :Boolify
230 {
231 0 ▶ return (@{$VALUES[${$_[0]}]} > 0);
Subroutine have_any: not called CC=1 SCAR=6.9
232 2 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
233  
234 sub values :Arrayify
235 {
236 2 ▶ return ($VALUES[${$_[0]}]);
Subroutine values: called CC=1 SCAR=0.0
237 2 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
238  
239 sub as_hash :Hashify
240 {
241 1 ▶ my $self = $_[0];
Subroutine as_hash: called CC=2 SCAR=7.0
242  
243 1 ▶ if (! defined($HASHES[$$self])) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $HASHES[$$self]) 1 0 1
244 1 my %hash;
245 1 @hash{@{$CLASSES[$$self]}} = @{$VALUES[$$self]};
246 1 $self->set(\@HASHES, \%hash);
247 }
248  
249 1 return ($HASHES[$$self]);
250 2 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
251  
252 # Our metadata
253 2 add_meta('Object::InsideOut::Results', {
254 'new' => {'hidden' => 1},
255 'create_field' => {'hidden' => 1},
256 'add_class' => {'hidden' => 1},
257 });
258  
259 } # End of package's lexical scope
260  
261  
262 # Ensure correct versioning
263 2 ▶($Object::InsideOut::VERSION eq '4.04')
Branch: 1 branch
BranchTrueFalseTotal
$Object::InsideOut::VERSION eq "4.04" or die "Version mismatch\n" 0 2 2
264 or die("Version mismatch\n");
265  
266 # EOF
« blib/lib/Object/InsideOut/Autoload.pm ↑ Summary blib/lib/Object/InsideOut/Dynamic.pm »