blib/lib/MooseX/MungeHas.pm

filter: Statementstmt 81.9% 150 / 183 Branchbran 52.7% 39 / 74 Conditioncond 46.6% 14 / 30 MC/DCmcdc 27.7% 5 / 18 Subroutinesub 89.4% 17 / 19 Podpod n/a totaltotal 69.4% 225 / 324 CC 53 SCAR 33.2
CC
53
Cov
71%
__ANON__
90.0
__detect_oo
63.1
_compile_munger_code
12.0
CRAP 27.6
↑ Summary
1 package MooseX::MungeHas;
2  
3 7 ▶use 5.008;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 7 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 7 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 7 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
8 7 $MooseX::MungeHas::AUTHORITY = 'cpan:TOBYINK';
9 7 $MooseX::MungeHas::VERSION = '0.011';
10 };
11  
12 7 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
13 7 ▶use Scalar::Util qw(blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 7 ▶BEGIN {
Subroutine BEGIN: called CC=4 SCAR=14.8
16 7 for my $backend (qw/ Eval::TypeTiny Eval::Closure /)
17 {
18 last if eval(
19 7 ▶ "require $backend; *eval_closure = \\&$backend\::eval_closure;"
Branch: 1 branch
BranchTrueFalseTotal
if eval "require $backend; *eval_closure = \\&${backend}::eval_closure;" 7 0 7
20 );
21 }
22 7 ▶ exists(&eval_closure)
Branch: 1 branch
BranchTrueFalseTotal
exists &MooseX::MungeHas::eval_closure or croak("Could not load Eval::TypeTiny") 7 0 7
23 or croak "Could not load Eval::TypeTiny";
24 };
25  
26 sub import
27 9 ▶{
Subroutine import: called CC=5 SCAR=16.1
28 7 ▶ no strict qw(refs);
Subroutine BEGIN: called CC=1 SCAR=0.0
29
30 9 my $class = shift;
31 9 my $caller = caller;
32
33 9 ▶ my $orig = \&{"$caller\::has"}
Branch: 1 branch
BranchTrueFalseTotal
my $orig = \&{"${caller}::has";} or croak("$caller does not have a 'has' function to munge") 9 0 9
34 or croak "$caller does not have a 'has' function to munge";
35
36 my %export = (@_ == 1 and ref($_[0]) eq "HASH")
37 9 ▶ ? %{ $_[0] }
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 && ref $_[0] eq 'HASH' ? : 1 8 9
Condition100%3/3
@_ == 1 and ref $_[0] eq "HASH"
!ll&&!rl&&r
711
MC/DC100%2/2
@_ == 1 and ref $_[0] eq "HASH"
@_ == 1 ref $_[0] eq "HASH"
Truth table100%3/3
A and B
A= @_ == 1B= ref $_[0] eq "HASH"
ABresult
0X0
100
111
38 : ( has => [@_] );
39
40 9 for my $f (sort keys %export)
41 {
42 7 ▶ no warnings qw(redefine prototype);
Subroutine BEGIN: called CC=1 SCAR=0.0
43
44 10 *{"$caller\::$f"} = $class->_make_has(
45 $caller,
46 10 $class->_make_munger($caller, @{$export{$f}}),
47 $orig,
48 );
49 }
50 }
51  
52 {
53 sub __detect_oo
54 9 ▶ {
Subroutine __detect_oo: called CC=13 SCAR=41.4
55 9 my $package = $_[0];
56
57 9 ▶ if ($INC{'Moo.pm'})
Branch: 1 branch
BranchTrueFalseTotal
if ($INC{"Moo.pm"}) 3 6 9
58 {
59 3 ▶ return "Moo" if $Moo::MAKERS{$package};
Branch: 1 branch
BranchTrueFalseTotal
if $Moo::MAKERS{$package} 2 1 3
60 1 ▶ return "Moo" if $Moo::Role::INFO{$package};
Branch: 1 branch
BranchTrueFalseTotal
if $Moo::Role::INFO{$package} 1 0 1
61 }
62
63 6 ▶ if ($INC{'Moose.pm'})
Branch: 1 branch
BranchTrueFalseTotal
if ($INC{"Moose.pm"}) 6 0 6
64 {
65 6 require Moose::Util;
66 6 ▶ return "Moose" if Moose::Util::find_meta($package);
Branch: 1 branch
BranchTrueFalseTotal
if Moose::Util::find_meta($package) 6 0 6
67 }
68
69 0 ▶ if ($INC{'Mouse.pm'})
Branch: 1 branch
BranchTrueFalseTotal
if ($INC{"Mouse.pm"}) 0 0 0
70 {
71 0 require Mouse::Util;
72 0 ▶ return "Mouse" if Mouse::Util::find_meta($package);
Branch: 1 branch
BranchTrueFalseTotal
if Mouse::Util::find_meta($package) 0 0 0
73 }
74
75 0 my $meta;
76 0 ▶ eval { $meta = $package->meta } or return "?";
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $meta = $package->meta } } or return "?" 0 0 0
77
78 0 ▶ return "Moo" if ref($meta) eq "Moo::HandleMoose::FakeMetaClass";
Branch: 1 branch
BranchTrueFalseTotal
if ref $meta eq "Moo::HandleMoose::FakeMetaClass" 0 0 0
79 0 ▶ return "Mouse" if $meta->isa("Mouse::Meta::Module");
Branch: 1 branch
BranchTrueFalseTotal
if $meta->isa("Mouse::Meta::Module") 0 0 0
80 0 ▶ return "Moose" if $meta->isa("Moose::Meta::Class");
Branch: 1 branch
BranchTrueFalseTotal
if $meta->isa("Moose::Meta::Class") 0 0 0
81 0 ▶ return "Moose" if $meta->isa("Moose::Meta::Role");
Branch: 1 branch
BranchTrueFalseTotal
if $meta->isa("Moose::Meta::Role") 0 0 0
82 0 return "?";
83 }
84
85 7 my %_cache;
86 47 ▶ sub _detect_oo { $_cache{$_[0]} ||= __detect_oo(@_) };
Subroutine _detect_oo: called CC=2 SCAR=7.2
Condition66%2/3
$_cache{$_[0]} ||= __detect_oo(@_)
l!l&&r!l&&!r
3890
MC/DC0%0/2
$_cache{$_[0]} ||= __detect_oo(@_)
$_cache{$_[0]} __detect_oo(@_)
Truth table66%2/3
A ||= B
A= $_cache{$_[0]}B= __detect_oo(@_)
ABresult
1X1
011
000
87 }
88  
89 sub _make_munger
90 7 ▶{
Subroutine _make_munger: called CC=1 SCAR=0.0
91 10 my $class = shift;
92 10 return $class->_compile_munger_code(@_);
93 }
94  
95 sub _compile_munger_code
96 10 ▶{
Subroutine _compile_munger_code: called CC=12 SCAR=24.9
97 10 my $class = shift;
98 10 my ($caller, @features) = @_;
99 10 my %features = map +($_ => 1), grep !ref, @features;
100 10 my @subs = grep ref, @features;
101
102 10 my @code = "sub {";
103
104 10 ▶ if (_detect_oo($caller) =~ /^Mo[ou]se$/)
Branch: 1 branch
BranchTrueFalseTotal
if (_detect_oo($caller) =~ /^Mo[ou]se$/) 7 3 10
105 {
106 7 push @code, ' if (exists($_{isa}) && !ref($_{isa})) {';
107 7 push @code, ' $_{isa} = '._detect_oo($caller).'::Util::TypeConstraints::find_or_create_isa_type_constraint($_{isa});';
108 7 push @code, ' }';
109 }
110
111 10 for my $is (qw/ro rw rwp lazy bare/)
112 {
113 50 ▶ if (delete $features{"is_$is"})
Branch: 1 branch
BranchTrueFalseTotal
if (delete $features{"is_$is"}) 7 43 50
114 {
115 7 push @code, ' $_{is} ||= "'.$is.'";';
116 }
117 }
118
119 10 push @code, ' if (exists($_{_is})) {';
120 10 push @code, ' my $_is = delete($_{_is});';
121 10 push @code, ' $_{is} ||= $_is;';
122 10 push @code, ' }';
123  
124 10 push @code, 'if ( ref $_{lazy} eq q(CODE) ) {';
125 10 push @code, ' $_{builder} ||= $_{lazy};';
126 10 push @code, '}';
127
128 10 push @code, ' if (ref($_{builder}) eq q(CODE)) {';
129 10 push @code, ' no strict qw(refs);';
130 10 push @code, ' require Sub::Util;';
131 10 push @code, ' my $short_name = "_build_$_";';
132 10 push @code, ' my $name = "$_{__CALLER__}::$short_name";';
133 10 push @code, ' *$name = Sub::Util::set_subname($name, $_{builder});';
134 10 push @code, ' $_{builder} = $short_name;';
135 10 push @code, ' }';
136
137 10 ▶ unless (_detect_oo($caller) eq "Moo")
Branch: 1 branch
BranchTrueFalseTotal
unless (_detect_oo($caller) eq "Moo") 7 3 10
138 {
139 7 push @code, ' my $is = $_{is} || "";';
140  
141 7 push @code, ' if ($is eq q(lazy)) {';
142 7 push @code, ' $_{is} = "ro";';
143 7 push @code, ' $_{lazy} = 1 unless exists($_{lazy});';
144 7 push @code, ' $_{builder} = "_build_$_" if $_{lazy} && !exists($_{builder}) && !exists($_{default});';
145 7 push @code, ' }';
146
147 7 push @code, ' if ($is eq q(rwp)) {';
148 7 push @code, ' $_{is} = "ro";';
149 7 push @code, ' $_{writer} = "_set_$_" unless exists($_{writer});';
150 7 push @code, ' }';
151
152 7 push @code, ' if (ref($_{isa}) eq q(CODE)) {';
153 7 push @code, ' require Type::Tiny;';
154 7 push @code, ' my $code = $_{isa};';
155 7 push @code, ' my $safe = sub { !!eval { $code->($_); 1 } };';
156 7 push @code, ' $_{isa} = "Type::Tiny"->new(constraint => $safe);';
157 7 push @code, ' }';
158 }
159
160 10 delete $features{"eq_1"};
161 10 push @code, ' my ($pfx, $name) = ($_ =~ /^(_*)(.+)$/);';
162 10 push @code, ' $_{builder} = "_build_$_" if exists($_{builder}) && $_{builder} eq q(1);';
163 10 push @code, ' $_{clearer} = "${pfx}clear_${name}" if exists($_{clearer}) && $_{clearer} eq q(1);';
164 10 push @code, ' $_{predicate} = "${pfx}has_${name}" if exists($_{predicate}) && $_{predicate} eq q(1);';
165 10 push @code, ' if (exists($_{trigger}) && $_{trigger} eq q(1)) {';
166 10 push @code, ' my $method = "_trigger_$_";';
167 10 push @code, ' $_{trigger} = sub { shift->$method(@_) };';
168 10 push @code, ' }';
169
170 10 ▶ if (delete $features{"always_coerce"})
Branch: 1 branch
BranchTrueFalseTotal
if (delete $features{"always_coerce"}) 2 8 10
171 {
172 2 push @code, ' if (exists($_{isa}) and !exists($_{coerce}) and Scalar::Util::blessed($_{isa}) and $_{isa}->can("has_coercion") and $_{isa}->has_coercion) {';
173 2 push @code, ' $_{coerce} = 1;';
174 2 push @code, ' }';
175 }
176
177 10 ▶ if (delete $features{"always_required"})
Branch: 1 branch
BranchTrueFalseTotal
if (delete $features{"always_required"}) 2 8 10
178 {
179 2 push @code, ' if (!exists($_{required}) and !exists($_{default}) and !exists($_{builder})) {';
180 2 push @code, ' $_{required} = 1;';
181 2 push @code, ' }';
182 }
183  
184 10 ▶ if (_detect_oo($caller) eq "Moo")
Branch: 1 branch
BranchTrueFalseTotal
if (_detect_oo($caller) eq "Moo") 3 7 10
185 {
186 3 push @code, ' if (defined($_{coerce}) and !ref($_{coerce}) and $_{coerce} eq "1") {';
187 3 push @code, ' Scalar::Util::blessed($_{isa})';
188 3 push @code, ' and $_{isa}->isa("Type::Tiny")';
189 3 push @code, ' and ($_{coerce} = $_{isa}->coercion);';
190 3 push @code, ' }';
191 3 push @code, ' elsif (exists($_{coerce}) and not $_{coerce}) {';
192 3 push @code, ' delete($_{coerce});';
193 3 push @code, ' }';
194 }
195
196 10 ▶ if (delete $features{"no_isa"})
Branch: 1 branch
BranchTrueFalseTotal
if (delete $features{"no_isa"}) 0 10 10
197 {
198 0 push @code, ' delete($_{isa}) if !exists($_{coerce});';
199 }
200
201 10 ▶ if (delete $features{"simple_isa"})
Branch: 1 branch
BranchTrueFalseTotal
if (delete $features{"simple_isa"}) 2 8 10
202 {
203 2 push @code, ' $_{isa} = "'.$class.'"->_simplify_isa($_{isa}) if Scalar::Util::blessed($_{isa}) && !$_{coerce};';
204 }
205
206 10 push @code, sprintf(' $subs[%d]->(@_);', $_) for 0..$#subs;
207 #push @code, ' ::diag(::explain($_, \%_));';
208 10 push @code, ' delete($_{__CALLER__});';
209 10 push @code, "}";
210
211 10 ▶ croak sprintf("Did not understand mungers: %s", join(q[, ], sort keys %features))
Branch: 1 branch
BranchTrueFalseTotal
if keys %features 0 10 10
212 if keys %features;
213
214 10 return eval_closure(
215 source => \@code,
216 environment => { '@subs' => \@subs },
217 );
218 }
219  
220 sub _simplify_isa
221 2 ▶{
Subroutine _simplify_isa: called CC=4 SCAR=17.9
222 2 my $class = shift;
223 2 my ($t) = @_;
224
225 2 ▶ until ($t->can_be_inlined)
Branch: 1 branch
BranchTrueFalseTotal
unless ($t->can_be_inlined) 4 2 6
226 {
227 4 ▶ if ($t->has_parent)
Branch: 1 branch
BranchTrueFalseTotal
if ($t->has_parent) 4 0 4
228 {
229 4 $t = $t->parent;
230 4 next;
231 }
232
233 0 ▶ if ($t->isa("Type::Tiny::Intersection"))
Branch: 1 branch
BranchTrueFalseTotal
if ($t->isa("Type::Tiny::Intersection")) 0 0 0
234 {
235 0 require Type::Tiny::Intersection;
236 0 my (@can_be_inlined) = grep $_->can_be_inlined, @$t;
237 0 $t = "Type::Tiny::Intersection"->new(type_constraints => \@can_be_inlined);
238 0 next;
239 }
240
241 0 require Type::Tiny;
242 0 return "Type::Tiny"->new;
243 }
244
245 2 return $t;
246 }
247  
248 sub _make_has
249 10 ▶{
Subroutine _make_has: called CC=2 SCAR=7.0
250 10 my $class = shift;
251 10 my ($caller, $coderef, $orig) = @_;
252
253 10 ▶ return $class->_make_has_mouse(@_) if _detect_oo($caller) eq "Mouse";
Branch: 1 branch
BranchTrueFalseTotal
if _detect_oo($caller) eq "Mouse" 0 10 10
254
255 return sub
256 24 ▶ {
Subroutine __ANON__: called CC=10 SCAR=23.3
257 my ($attr, %spec) = (
258 (@_ == 2 and ref($_[1]) eq "CODE") ? ($_[0], _is => "ro", lazy => 1, builder => $_[1]) :
259 0 ▶ (@_ == 2 and ref($_[1]) eq "HASH") ? ($_[0], %{$_[1]}) :
Branch: 3 branches
BranchTrueFalseTotal
@_ == 2 && ref $_[1] eq 'CODE' ? : 5 19 24
@_ == 2 && ref $_[1] eq 'HASH' ? : 0 19 19
@_ == 2 && builtin::blessed($_[1]) && $_[1]->can('check') ? : 4 15 19
Condition75%9/12
@_ == 2 and ref $_[1] eq "CODE"
!ll&&!rl&&r
1545
@_ == 2 and ref $_[1] eq "HASH"
!ll&&!rl&&r
1540
@_ == 2 and builtin::blessed($_[1]) and $_[1]->can("check")
!ll&&!rl&&r
1504
@_ == 2 and builtin::blessed($_[1])
!ll&&!rl&&r
1504
MC/DC100%2/2
@_ == 2 and ref $_[1] eq "CODE"
@_ == 2 ref $_[1] eq "CODE"
MC/DC0%0/2
@_ == 2 and ref $_[1] eq "HASH"
@_ == 2 ref $_[1] eq "HASH"
MC/DC33%1/3
@_ == 2 and builtin::blessed($_[1]) and $_[1]->can("check")
@_ == 2 builtin::blessed($_[1]) $_[1]->can("check")
Truth table100%3/3
A and B
A= @_ == 2B= ref $_[1] eq "CODE"
ABresult
0X0
100
111
Truth table66%2/3
A and B
A= @_ == 2B= ref $_[1] eq "HASH"
ABresult
0X0
100
111
Truth table50%2/4
A and B and C
A= @_ == 2B= builtin::blessed($_[1])C= $_[1]->can("check")
ABCresult
0XX0
10X0
1100
1111
260 (@_ == 2 and blessed($_[1]) and $_[1]->can('check')) ? ($_[0], _is => "ro", isa => $_[1]) :
261 @_
262 );
263
264 24 $spec{"__CALLER__"} = $caller;
265
266 24 ▶ if (ref($attr) eq q(ARRAY))
Branch: 1 branch
BranchTrueFalseTotal
if (ref $attr eq 'ARRAY') { } 1 23 24
267 {
268 1 my @attrs = @$attr;
269 1 for my $attr (@attrs)
270 {
271 2 local %_ = %spec;
272 2 local $_ = $attr;
273 2 $coderef->($attr, %_);
274 2 $orig->($attr, %_);
275 }
276 }
277 else
278 {
279 23 local %_ = %spec;
280 23 local $_ = $attr;
281 23 $coderef->($attr, %_);
282 23 return $orig->($attr, %_);
283 }
284 10 };
285 }
286  
287 sub _make_has_mouse
288 0 ▶{
Subroutine _make_has_mouse: not called CC=1 SCAR=6.9
289 0 my $class = shift;
290 0 my ($caller, $coderef, $orig) = @_;
291
292 return sub
293 0 ▶ {
Subroutine __ANON__: not called CC=9 SCAR=45.0
294 my ($attr, %spec) = (
295 (@_ == 2 and ref($_[1]) eq "CODE") ? ($_[0], _is => "ro", lazy => 1, builder => $_[1]) :
296 0 ▶ (@_ == 2 and ref($_[1]) eq "HASH") ? ($_[0], %{$_[1]}) :
Branch: 3 branches
BranchTrueFalseTotal
@_ == 2 && ref $_[1] eq 'CODE' ? : 0 0 0
@_ == 2 && ref $_[1] eq 'HASH' ? : 0 0 0
@_ == 2 && builtin::blessed($_[1]) && $_[1]->can('check') ? : 0 0 0
Condition0%0/12
@_ == 2 and ref $_[1] eq "CODE"
!ll&&!rl&&r
000
@_ == 2 and ref $_[1] eq "HASH"
!ll&&!rl&&r
000
@_ == 2 and builtin::blessed($_[1]) and $_[1]->can("check")
!ll&&!rl&&r
000
@_ == 2 and builtin::blessed($_[1])
!ll&&!rl&&r
000
MC/DC0%0/2
@_ == 2 and ref $_[1] eq "CODE"
@_ == 2 ref $_[1] eq "CODE"
MC/DC0%0/2
@_ == 2 and ref $_[1] eq "HASH"
@_ == 2 ref $_[1] eq "HASH"
MC/DC0%0/3
@_ == 2 and builtin::blessed($_[1]) and $_[1]->can("check")
@_ == 2 builtin::blessed($_[1]) $_[1]->can("check")
Truth table0%0/3
A and B
A= @_ == 2B= ref $_[1] eq "CODE"
ABresult
0X0
100
111
Truth table0%0/3
A and B
A= @_ == 2B= ref $_[1] eq "HASH"
ABresult
0X0
100
111
Truth table0%0/4
A and B and C
A= @_ == 2B= builtin::blessed($_[1])C= $_[1]->can("check")
ABCresult
0XX0
10X0
1100
1111
297 (@_ == 2 and blessed($_[1]) and $_[1]->can('check')) ? ($_[0], _is => "ro", isa => $_[1]) :
298 @_
299 );
300
301 0 $spec{"__CALLER__"} = $caller;
302
303 0 ▶ if (ref($attr) eq q(ARRAY))
Branch: 1 branch
BranchTrueFalseTotal
if (ref $attr eq 'ARRAY') { } 0 0 0
304 {
305 0 croak "MooseX::MungeHas does not support has \\\@array for Mouse";
306 }
307 else
308 {
309 0 local %_ = %spec;
310 0 local $_ = $attr;
311 0 $coderef->($attr, %_);
312 0 @_ = ($attr, %_);
313 0 goto $orig;
314 }
315 0 };
316 }
317  
318 7 1;
319  
320 __END__
↑ Summary