Object/MakeMethods.pm

filter: Statementstmt 95.7% 113 / 118 Branchbran 74.1% 46 / 62 Conditioncond 44.4% 16 / 36 MC/DCmcdc 9.5% 2 / 21 Subroutinesub 100% 12 / 12 Podpod 50.0% 2 / 4 totaltotal 75.4% 191 / 253 CC 46 SCAR 24.7
CC
46
Cov
81%
__make_methods
18.0
_normalize_args
16.5
apparently_made_method
9.8
CRAP 11.8
« Class/MakeMethods/Set.pm ↑ Summary Object/MakeMethods/Generic.pm »
1 package Rose::Object::MakeMethods;
2  
3 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 4 ▶use Carp();
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 4 our $VERSION = '0.856';
8  
9 4 __PACKAGE__->allow_apparent_reload(1);
10  
11 4 our %Made_Method_Custom;
12  
13 sub import
14 33 ▶{
Subroutine import: called CC=3 SCAR=11.2
15 33 my($class) = shift;
16  
17 33 ▶ return 1 unless(@_);
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 16 17 33
18  
19 17 my($options, $args) = $class->_normalize_args(@_);
20  
21 17 ▶ $options->{'target_class'} ||= (caller)[0];
Condition33%1/3
$options->{"target_class"} ||= (caller)[0]
l!l&&r!l&&!r
0170
MC/DC0%0/2
$options->{"target_class"} ||= (caller)[0]
$options->{"target_class"} (caller)[0]
Truth table33%1/3
A ||= B
A= $options->{"target_class"}B= (caller)[0]
ABresult
1X1
011
000
22  
23 17 $class->make_methods($options, $args);
24  
25 17 return 1;
26 }
27  
28 sub make_methods
29 17 ▶{
Subroutine make_methods: called CC=3 SCAR=11.2
Pod documented
30 17 my($class) = shift;
31  
32 17 my($options, $args) = $class->_normalize_args(@_);
33  
34 17 ▶ $options->{'target_class'} ||= (caller)[0];
Condition33%1/3
$options->{"target_class"} ||= (caller)[0]
l!l&&r!l&&!r
1700
MC/DC0%0/2
$options->{"target_class"} ||= (caller)[0]
$options->{"target_class"} (caller)[0]
Truth table33%1/3
A ||= B
A= $options->{"target_class"}B= (caller)[0]
ABresult
1X1
011
000
35  
36 #use Data::Dumper;
37 #print STDERR Dumper($options);
38 #print STDERR Dumper($args);
39  
40 17 ▶ while(@$args)
Branch: 1 branch
BranchTrueFalseTotal
if (@$args) 49 17 66
41 {
42 49 $class->__make_methods($options, shift(@$args), shift(@$args));
43 }
44  
45 17 return 1;
46 }
47  
48 # Can't use the class method maker easily here due to a chicken/egg
49 # situation, so this code is manually inlined.
50 4 my %Inheritable_Scalar;
51  
52 sub allow_apparent_reload
53 5 ▶{
Subroutine allow_apparent_reload: called CC=7 SCAR=20.0
Pod documented
54 5 ▶ my($class) = ref($_[0]) ? ref(shift) : shift;
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 0 5 5
55  
56 5 ▶ if(@_)
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 4 1 5
57 {
58 4 return $Inheritable_Scalar{$class}{'allow_apparent_reload'} = shift;
59 }
60  
61 return $Inheritable_Scalar{$class}{'allow_apparent_reload'}
62 1 ▶ if(exists $Inheritable_Scalar{$class}{'allow_apparent_reload'});
Branch: 1 branch
BranchTrueFalseTotal
if exists $Inheritable_Scalar{$class}{"allow_apparent_reload"} 0 1 1
63  
64 1 my @parents = ($class);
65  
66 1 ▶ while(my $parent = shift(@parents))
Branch: 1 branch
BranchTrueFalseTotal
if (my $parent = shift @parents) 1 0 1
67 {
68 4 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
69 1 foreach my $subclass (@{$parent . '::ISA'})
70 {
71 1 push(@parents, $subclass);
72  
73 1 ▶ if(exists $Inheritable_Scalar{$subclass}{'allow_apparent_reload'})
Branch: 1 branch
BranchTrueFalseTotal
if (exists $Inheritable_Scalar{$subclass}{"allow_apparent_reload"}) 1 0 1
74 {
75 1 return $Inheritable_Scalar{$subclass}{'allow_apparent_reload'}
76 }
77 }
78 }
79  
80 0 return undef;
81 }
82  
83 # XXX: This nasty hack should be unneeded now and will probably
84 # XXX: be removed some time in the future.
85 4 our $Preserve_Existing = 0;
86  
87 sub __make_methods
88 49 ▶{
Subroutine __make_methods: called CC=15 SCAR=28.9
89 49 my($class) = shift;
90  
91 #my $options;
92  
93 #if(ref $_[0] eq 'HASH')
94 #{
95 # $options = shift;
96 #}
97 #else { $options = {} }
98  
99 #$options->{'target_class'} ||= (caller)[0];
100  
101 49 my $options = shift;
102 49 my $method_type = shift;
103 49 my $methods = shift;
104  
105 49 my $target_class = $options->{'target_class'};
106  
107 49 ▶ while(@$methods)
Branch: 1 branch
BranchTrueFalseTotal
if (@$methods) 125 49 174
108 {
109 125 my $method_name = shift(@$methods);
110 125 my $method_args = shift(@$methods);
111  
112 125 ▶ my $make = $class->$method_type($method_name => $method_args, $options ||= {});
Condition50%1/2
$options ||= {}
l!l
1250
MC/DC0%0/1
$options ||= {}
$options
Truth table50%1/2
A
A= $options
Aresult
11
00
113  
114 125 ▶ Carp::croak "${class}::method_type(...) didn't return a hash ref!"
Branch: 1 branch
BranchTrueFalseTotal
unless ref $make eq "HASH" 0 125 125
115 unless(ref $make eq 'HASH');
116  
117 4 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
118  
119 125 ▶ METHOD: while(my($name, $code) = each(%$make))
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$make) 301 125 426
120 {
121 Carp::croak "${class}::method_type(...) - key for $name is not a code ref!"
122 301 ▶ unless(ref $code eq 'CODE' || (ref $code eq 'HASH' && $code->{'make_method'}));
Branch: 1 branch
BranchTrueFalseTotal
unless ref $code eq "CODE" or ref $code eq "HASH" and $code->{"make_method"} 0 301 301
Condition50%3/6
ref $code eq "CODE" or ref $code eq "HASH" and $code->{"make_method"}
l!l&&r!l&&!r
29650
ref $code eq "HASH" and $code->{"make_method"}
!ll&&!rl&&r
005
MC/DC0%0/3
ref $code eq "CODE" or ref $code eq "HASH" and $code->{"make_method"}
ref $code eq "CODE" ref $code eq "HASH" $code->{"make_method"}
Truth table50%2/4
A or B and C
A= ref $code eq "CODE"B= ref $code eq "HASH"C= $code->{"make_method"}
ABCresult
1XX1
0111
00X0
0100
123  
124 301 ▶ if(my $code = $target_class->can($name))
Branch: 1 branch
BranchTrueFalseTotal
if (my $code = $target_class->can($name)) 1 300 301
125 {
126 1 ▶ if($options->{'preserve_existing'} || $Preserve_Existing)
Branch: 1 branch
BranchTrueFalseTotal
if ($options->{"preserve_existing"} or $Rose::Object::MakeMethods::Preserve_Existing) 0 1 1
Condition33%1/3
$options->{"preserve_existing"} or $Rose::Object::MakeMethods::Preserve_Existing
l!l&&r!l&&!r
001
MC/DC0%0/2
$options->{"preserve_existing"} or $Rose::Object::MakeMethods::Preserve_Existing
$options->{"preserve_existing"} $Rose::Object::MakeMethods::Preserve_Existing
Truth table33%1/3
A or B
A= $options->{"preserve_existing"}B= $Rose::Object::MakeMethods::Preserve_Existing
ABresult
1X1
011
000
127 {
128 0 next METHOD;
129 }
130  
131 1 ▶ unless($options->{'override_existing'})
Branch: 1 branch
BranchTrueFalseTotal
unless ($options->{"override_existing"}) 1 0 1
132 {
133 1 ▶ if($class->allow_apparent_reload && $class->apparently_made_method($code))
Branch: 1 branch
BranchTrueFalseTotal
if ($class->allow_apparent_reload and $class->apparently_made_method($code)) 1 0 1
Condition33%1/3
$class->allow_apparent_reload and $class->apparently_made_method($code)
!ll&&!rl&&r
001
MC/DC0%0/2
$class->allow_apparent_reload and $class->apparently_made_method($code)
$class->allow_apparent_reload $class->apparently_made_method($code)
Truth table33%1/3
A and B
A= $class->allow_apparent_reloadB= $class->apparently_made_method($code)
ABresult
0X0
100
111
134 {
135 1 next METHOD;
136 }
137  
138 0 Carp::croak "Cannot create method ${target_class}::$name - method already exists";
139 }
140 }
141  
142 4 ▶ no warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
143  
144 300 ▶ if(ref $code eq 'CODE')
Branch: 1 branch
BranchTrueFalseTotal
if (ref $code eq 'CODE') { } 296 4 300
145 {
146 296 *{"${target_class}::$name"} = $code;
147 }
148 else
149 {
150 # XXX: Must track these separately because they do not show up as
151 # XXX: being named __ANON__ when fetching the sub_identity()
152 4 $Made_Method_Custom{$target_class}{$name}++;
153 4 $code->{'make_method'}($name, $target_class, $options);
154 }
155 }
156 }
157  
158 49 return 1;
159 }
160  
161 sub apparently_made_method
162 1 ▶{
Subroutine apparently_made_method: called CC=6 SCAR=22.8
Pod undocumented
163 1 my($class, $code) = @_;
164  
165 1 my($mm_class, $name) = $class->sub_identity($code);
166 1 ▶ return 0 unless($class && $name);
Branch: 1 branch
BranchTrueFalseTotal
unless $class and $name 0 1 1
Condition33%1/3
$class and $name
!ll&&!rl&&r
001
MC/DC0%0/2
$class and $name
$class $name
Truth table33%1/3
A and B
A= $classB= $name
ABresult
0X0
100
111
167 # XXX: RT 54444 - The formerly constant "__ANON__" sub name looks
168 # XXX: like this in newer versions of perl when running under the
169 # XXX: debugger: "__ANON__[/usr/lib/perl5/.../Some/Module.pm:123]"
170 return (($mm_class eq $class && $name =~ /^__ANON__/) ||
171 1 ▶ $Made_Method_Custom{$mm_class}{$name}) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$mm_class eq $class && $name =~ /^__ANON__/ || $Rose::Object::MakeMethods::Made_Method_Custom{$mm_class}{$name} ? : 1 0 1
Condition33%2/6
$mm_class eq $class and $name =~ /^__ANON__/ or $Rose::Object::MakeMethods::Made_Method_Custom{$mm_class}{$name}
l!l&&r!l&&!r
010
$mm_class eq $class and $name =~ /^__ANON__/
!ll&&!rl&&r
100
MC/DC0%0/3
$mm_class eq $class and $name =~ /^__ANON__/ or $Rose::Object::MakeMethods::Made_Method_Custom{$mm_class}{$name}
$mm_class eq $class $name =~ /^__ANON__/ $Rose::Object::MakeMethods::Made_Method_Custom{$mm_class}{$name}
Truth table20%1/5
A and B or C
A= $mm_class eq $classB= $name =~ /^__ANON__/C= $Rose::Object::MakeMethods::Made_Method_Custom{$mm_class}{$name}
ABCresult
11X1
0X11
1011
0X00
1000
172 }
173  
174 # Code from Sub::Identify
175 sub sub_identity
176 1 ▶{
Subroutine sub_identity: called CC=2 SCAR=6.9
Pod undocumented
177 1 my($class, $code) = @_;
178  
179 1 my @id;
180  
181 TRY:
182 {
183 1 local $@;
184  
185 eval # if this fails, the identity is undefined
186 1 {
187 1 require B;
188 1 my $cv = B::svref_2object($code);
189 1 ▶ return unless($cv->isa('B::CV'));
Branch: 1 branch
BranchTrueFalseTotal
unless $cv->isa("B::CV") 0 1 1
190 1 @id = ($cv->GV->STASH->NAME, $cv->GV->NAME);
191 };
192  
193 # Ignore errors
194 }
195  
196 1 return @id;
197 }
198  
199 # Given the example method types "bitfield" and "scalar", _normalize_args()
200 # takes args in any of these forms:
201 #
202 # { ... }, # Class options (optionally) go here
203 #
204 # scalar => 'foo',
205 #
206 # 'bitfield --opt' => [ 'a', 'b' ],
207 #
208 # 'scalar --opt2=blah' => [ 'foo' => { opt => 4, opt2 => 'blee' } ],
209 #
210 # scalar => [ 'a' => { default => 5 }, 'b' ],
211 #
212 # bitfield =>
213 # [
214 # bar => { size => 8 },
215 # baz => { size => 5, default => '00011' },
216 # ],
217 #
218 # and returns an options hashref (possibly empty) and a reference
219 # to an array that is normalized to look like this:
220 #
221 # [
222 # [
223 # 'scalar' => [ 'foo' => {} ],
224 #
225 # 'bitfield' =>
226 # [
227 # 'a' => { opt => 1 },
228 # 'b' => { opt => 1 }
229 # ],
230 #
231 # 'scalar' => [ 'foo' => { 'opt' => 4, 'opt2' => 'blee' } ],
232 #
233 # 'scalar'=>
234 # [
235 # 'a' => { 'default' => 5 },
236 # 'b' => {}
237 # ],
238 #
239 # 'bitfield' =>
240 # [
241 # 'bar' => { 'size' => 8 },
242 # 'baz' => { 'default' => '00011', 'size' => 5 }
243 # ]
244 # ]
245 # ]
246  
247 sub _normalize_args
248 34 ▶{
Subroutine _normalize_args: called CC=16 SCAR=28.0
249 34 my($class) = shift;
250  
251 34 my $i = 0;
252  
253 34 my(@normalized_args, $options);
254  
255 34 ▶ while(@_)
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 132 34 166
256 {
257 132 ▶ my $method_type = shift || last;
Condition50%1/2
shift() || (last)
l!l
1320
MC/DC0%0/1
shift() || (last)
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
258  
259 132 ▶ if(ref $method_type)
Branch: 1 branch
BranchTrueFalseTotal
if (ref $method_type) 34 98 132
260 {
261 34 ▶ if(ref $method_type eq 'HASH')
Branch: 2 branches
BranchTrueFalseTotal
if (ref $method_type eq 'HASH') { } 17 17 34
elsif (ref $method_type eq 'ARRAY') { } 17 0 17
262 {
263 17 ▶ Carp::croak "Options hash ref provided more than once"
Branch: 1 branch
BranchTrueFalseTotal
if $options 0 17 17
264 if($options);
265  
266 17 $options = $method_type;
267 17 next;
268 }
269 elsif(ref $method_type eq 'ARRAY')
270 {
271 17 unshift(@_, @$method_type);
272 17 next;
273 }
274 }
275  
276 98 my %method_options;
277  
278 98 my $i = 0;
279  
280 98 ▶ while($method_type =~ s/\s+--(\w+)(?:=(\w+))?//)
Branch: 1 branch
BranchTrueFalseTotal
if ($method_type =~ s/\s+--(\w+)(?:=(\w+))?//) 8 98 106
281 {
282 8 ▶ if($i++ || defined $2)
Branch: 1 branch
BranchTrueFalseTotal
if ($i++ or defined $2) { } 4 4 8
Condition66%2/3
$i++ or defined $2
l!l&&r!l&&!r
044
MC/DC50%1/2
$i++ or defined $2
$i++ defined $2
Truth table66%2/3
A or B
A= $i++B= defined $2
ABresult
1X1
011
000
283 {
284 4 $method_options{$1} = $2;
285 }
286 else
287 {
288 4 $method_options{'interface'} = $1;
289 }
290 }
291  
292 98 push(@normalized_args, $method_type);
293  
294 98 my $args = shift;
295  
296 98 ▶ if(!ref $args)
Branch: 2 branches
BranchTrueFalseTotal
if (not ref $args) { } 21 77 98
elsif (ref $args ne 'ARRAY') { } 0 77 77
297 {
298 21 $args = [ $args ];
299 }
300 elsif(ref $args ne 'ARRAY')
301 {
302 0 Carp::croak "Bad invocation of Rose::Object::MakeMethods";
303 }
304  
305 98 my @method_args;
306  
307 98 ▶ while(@$args)
Branch: 1 branch
BranchTrueFalseTotal
if (@$args) 250 98 348
308 {
309 250 my $method_name = shift(@$args);
310  
311 250 ▶ if(ref $args->[0])
Branch: 1 branch
BranchTrueFalseTotal
if (ref $args->[0]) { } 221 29 250
312 {
313 221 ▶ unless(ref $args->[0] eq 'HASH')
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $args->[0] eq "HASH") 0 221 221
314 {
315 0 Carp::croak "Expected hash ref or scalar after method name, but found $args->[0]";
316 }
317  
318 221 push(@method_args, $method_name => { %method_options, %{shift(@$args)} });
319 }
320 else
321 {
322 29 push(@method_args, $method_name => { %method_options });
323 }
324 }
325  
326 98 push(@normalized_args, \@method_args);
327 }
328  
329 34 ▶ return($options || {}, \@normalized_args);
Condition100%2/2
$options || {}
l!l
1717
MC/DC100%1/1
$options || {}
$options
Truth table100%2/2
A
A= $options
Aresult
11
00
330 }
331  
332 4 1;
333  
334 __END__
« Class/MakeMethods/Set.pm ↑ Summary Object/MakeMethods/Generic.pm »