blib/lib/Sub/MultiMethod.pm

filter: Statementstmt 88.4% 404 / 457 Branchbran 71.0% 169 / 238 Conditioncond 64.4% 76 / 118 MC/DCmcdc 41.4% 29 / 70 Subroutinesub 91.6% 55 / 60 Podpod 92.8% 13 / 14 totaltotal 77.9% 746 / 957 CC 176 SCAR 33.7
CC
176
Cov
80%
__ANON__
179.5
pick_candidate
28.0
install_candidate
27.0
CRAP 29.0
↑ Summary
1 23 ▶use 5.008001;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 23 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 23 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 package Sub::MultiMethod;
6  
7 23 our $AUTHORITY = 'cpan:TOBYINK';
8 23 our $VERSION = '1.003';
9  
10 23 ▶use B ();
Subroutine BEGIN: called CC=1 SCAR=0.0
11 23 ▶use Eval::TypeTiny qw( set_subname );
Subroutine BEGIN: called CC=1 SCAR=0.0
12 23 ▶use Exporter::Shiny qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
13 multimethod monomethod
14 multifunction monofunction
15 VOID SCALAR LIST NONVOID NONSCALAR NONLIST
16 23 );
17 23 ▶use Role::Hooks;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 23 ▶use List::Util qw( max min any );
Subroutine BEGIN: called CC=1 SCAR=0.0
19 23 ▶use Scalar::Util qw( refaddr );
Subroutine BEGIN: called CC=1 SCAR=0.0
20 23 ▶use Type::Params ();
Subroutine BEGIN: called CC=1 SCAR=0.0
21 23 ▶use Types::TypeTiny qw( TypeTiny );
Subroutine BEGIN: called CC=1 SCAR=0.0
22 23 ▶use Types::Standard qw( -types -is );
Subroutine BEGIN: called CC=1 SCAR=0.0
23  
24 23 ▶use constant {
Subroutine BEGIN: called CC=1 SCAR=0.0
25 23 VOID => 'VOID',
26 LIST => 'LIST',
27 SCALAR => 'SCALAR',
28 NONVOID => '~VOID',
29 NONLIST => '~LIST',
30 NONSCALAR => '~SCALAR',
31 23 };
32  
33 # Options other than these will be passed through to
34 # Type::Params.
35 #
36 23 my %KNOWN_OPTIONS = (
37 alias => 1,
38 code => 1,
39 compiled => 1,
40 copied => 1,
41 declaration_order => 1,
42 die => 1,
43 height => 1,
44 if => 1,
45 is_monomethod => 1,
46 method => 1,
47 named => 'legacy',
48 no_dispatcher => 1,
49 return => 1,
50 score => 1,
51 signature => 'legacy',
52 want => 1,
53 );
54  
55 # But not these!
56 #
57 23 my %BAD_OPTIONS = (
58 want_details => 1,
59 want_object => 1,
60 want_source => 1,
61 goto_next => 1,
62 on_die => 1,
63 message => 1,
64 );
65  
66 23 my %CACHE = ();
67  
68 {
69 23 my %CANDIDATES;
70 450 ▶ sub _get_multimethods_ref {
Subroutine _get_multimethods_ref: called CC=2 SCAR=6.9
71 450 my ($me, $target) = @_;
72 450 ▶ if ( not $CANDIDATES{$target} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($CANDIDATES{$target}) 68 382 450
73 68 $CANDIDATES{$target} = {};
74 }
75 450 $CANDIDATES{$target};
76 }
77 }
78  
79 23 ▶sub get_multimethods {
Subroutine get_multimethods: called CC=1 SCAR=0.0
Pod documented
80 34 my ($me, $target) = @_;
81 34 sort keys %{ $me->_get_multimethods_ref($target) };
82 }
83  
84 410 ▶sub _get_multimethod_candidates_ref {
Subroutine _get_multimethod_candidates_ref: called CC=3 SCAR=11.0
85 410 my ($me, $target, $method_name) = @_;
86 410 ▶ my ( $package_key, $method_key ) = ref( $method_name )
Branch: 1 branch
BranchTrueFalseTotal
ref $method_name ? : 12 398 410
87 ? ( '__CODE__', refaddr( $method_name ) )
88 : ( $target, $method_name );
89 410 my $mm = $me->_get_multimethods_ref( $package_key );
90 410 ▶ $mm->{$method_key} ||= [];
Condition100%2/2
$mm->{$method_key} ||= []
l!l
33476
MC/DC100%1/1
$mm->{$method_key} ||= []
$mm->{$method_key}
Truth table100%2/2
A
A= $mm->{$method_key}
Aresult
11
00
91 }
92  
93 4 ▶sub _clear_multimethod_candidates_ref {
Subroutine _clear_multimethod_candidates_ref: called CC=2 SCAR=7.0
94 4 my ( $me, $target, $method_name ) = ( shift, @_ );
95 4 $me->clear_cache;
96 4 ▶ my ( $package_key, $method_key ) = ref( $method_name )
Branch: 1 branch
BranchTrueFalseTotal
ref $method_name ? : 0 4 4
97 ? ( '__CODE__', refaddr( $method_name ) )
98 : ( $target, $method_name );
99 4 my $mm = $me->_get_multimethods_ref( $package_key );
100 4 delete $mm->{$method_key};
101 4 return $me;
102 }
103
104 90 ▶sub get_multimethod_candidates {
Subroutine get_multimethod_candidates: called CC=1 SCAR=0.0
Pod documented
105 90 my ($me, $target, $method_name) = @_;
106 90 @{ $me->_get_multimethod_candidates_ref($target, $method_name) };
107 }
108  
109 94 ▶sub has_multimethod_candidates {
Subroutine has_multimethod_candidates: called CC=1 SCAR=0.0
Pod documented
110 94 my ($me, $target, $method_name) = @_;
111 94 scalar @{ $me->_get_multimethod_candidates_ref($target, $method_name) };
112 }
113  
114 226 ▶sub _add_multimethod_candidate {
Subroutine _add_multimethod_candidate: called CC=3 SCAR=11.4
115 226 my ($me, $target, $method_name, $spec) = @_;
116 226 $me->clear_cache;
117 226 my $mmc = $me->_get_multimethod_candidates_ref($target, $method_name);
118 23 ▶ no warnings 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
119 226 ▶ if ( @$mmc and $spec->{method} != $mmc->[0]{method} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$mmc and $spec->{"method"} != $mmc->[0]{"method"}) 0 226 226
Condition66%2/3
@$mmc and $spec->{"method"} != $mmc->[0]{"method"}
!ll&&!rl&&r
521740
MC/DC0%0/2
@$mmc and $spec->{"method"} != $mmc->[0]{"method"}
@$mmc $spec->{"method"} != $mmc->[0]{"method"}
Truth table66%2/3
A and B
A= @$mmcB= $spec->{"method"} != $mmc->[0]{"method"}
ABresult
0X0
100
111
120 0 require Carp;
121 Carp::carp(sprintf(
122 "Added multimethod candidate for %s with method=>%d but expected method=>%d",
123 $method_name,
124 $spec->{method},
125 $mmc->[0]{method},
126 0 ));
127 }
128 226 push @$mmc, $spec;
129 226 $me;
130 }
131  
132 230 ▶sub clear_cache {
Subroutine clear_cache: called CC=1 SCAR=0.0
Pod documented
133 230 %CACHE = ();
134 }
135  
136 0 ▶sub get_cache {
Subroutine get_cache: not called CC=1 SCAR=6.9
Pod documented
137 0 return \%CACHE;
138 }
139  
140 38 ▶sub get_all_multimethod_candidates {
Subroutine get_all_multimethod_candidates: called CC=11 SCAR=24.0
Pod documented
141 38 my ($me, $target, $method_name, $is_method) = @_;
142
143 # Figure out which packages to consider when finding candidates.
144 38 my (@packages, $is_coderef_method);
145 38 ▶ if (is_Int $method_name or is_ScalarRef $method_name) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_Int($method_name) or is_ScalarRef($method_name)) { } 2 36 38
Condition66%2/3
is_Int($method_name) or is_ScalarRef($method_name)
l!l&&r!l&&!r
2036
MC/DC50%1/2
is_Int($method_name) or is_ScalarRef($method_name)
is_Int($method_name) is_ScalarRef($method_name)
Truth table66%2/3
A or B
A= is_Int($method_name)B= is_ScalarRef($method_name)
ABresult
1X1
011
000
146 2 @packages = '__CODE__';
147 2 $is_coderef_method = 1;
148 }
149 else {
150 @packages = $is_method
151 36 ▶ ? @{ mro::get_linear_isa($target) }
Branch: 1 branch
BranchTrueFalseTotal
$is_method ? : 32 4 36
152 : $target;
153 }
154
155 38 my $curr_height = @packages;
156
157 # Find candidates from each package
158 38 my @candidates;
159 38 my $final_fallback = undef;
160 38 ▶ PACKAGE: while (@packages) {
Branch: 1 branch
BranchTrueFalseTotal
if (@packages) 94 34 128
161 94 my $p = shift @packages;
162 94 my @c;
163 94 my $found = $me->has_multimethod_candidates($p, $method_name);
164 94 ▶ if ($found) {
Branch: 2 branches
BranchTrueFalseTotal
if ($found) { } 56 38 94
elsif (not $is_coderef_method) { } 38 0 38
165 56 @c = $me->get_multimethod_candidates($p, $method_name);
166 }
167 elsif (not $is_coderef_method) {
168 23 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
169 38 ▶ if (exists &{"$p\::$method_name"}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists &{"${p}::$method_name";}) 4 34 38
170 # We found a potential monomethod.
171 4 my $coderef = \&{"$p\::$method_name"};
172 4 ▶ if (!$me->known_dispatcher($coderef)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($me->known_dispatcher($coderef)) 4 0 4
173 # Definite monomethod. Stop falling back.
174 4 $final_fallback = $coderef;
175 4 last PACKAGE;
176 }
177 }
178 34 @c = ();
179 }
180 # Record their height in case we need it later
181 90 $_->{height} = $curr_height for @c;
182 90 push @candidates, @c;
183 90 --$curr_height;
184 }
185
186 # If a monomethod was found, use it as last resort
187 38 ▶ if (defined $final_fallback) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $final_fallback) 4 34 38
188 push @candidates, {
189 16 ▶ signature => sub { @_ },
Subroutine __ANON__: called CC=1 SCAR=0.0
190 4 code => $final_fallback,
191 };
192 }
193
194 38 return @candidates;
195 }
196  
197 {
198 23 my %DISPATCHERS;
199
200 52 ▶ sub known_dispatcher {
Subroutine known_dispatcher: called CC=1 SCAR=0.0
Pod documented
201 52 my ($me, $coderef) = @_;
202 52 $DISPATCHERS{refaddr($coderef)};
203 }
204
205 48 ▶ sub _mark_as_dispatcher {
Subroutine _mark_as_dispatcher: called CC=1 SCAR=0.0
206 48 my ($me, $coderef) = @_;
207 48 $DISPATCHERS{refaddr($coderef)} = 1;
208 48 $me;
209 }
210
211 0 ▶ sub _unmark_as_dispatcher {
Subroutine _unmark_as_dispatcher: not called CC=1 SCAR=6.9
212 0 my ($me, $coderef) = @_;
213 0 $DISPATCHERS{refaddr($coderef)} = 0;
214 0 $me;
215 }
216 }
217  
218 23 ▶sub _generate_exported_function {
Subroutine _generate_exported_function: called CC=5 SCAR=22.8
219 80 my ( $me, $name, $args, $globals ) = ( shift, @_ );
220
221 80 my $target = $globals->{into};
222 80 ▶ if ( ref $target or not defined $target ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $target or not defined $target) 0 80 80
Condition33%1/3
ref $target or not defined $target
l!l&&r!l&&!r
0080
MC/DC0%0/2
ref $target or not defined $target
ref $target not defined $target
Truth table33%1/3
A or not(B)
A= ref $targetB= not defined $target
ABresult
1X1
011
000
223 0 require Carp;
224 0 Carp::croak( "Function $name can only be installed into a package by package name" );
225 }
226
227 80 ▶ my %defaults = %{ $args->{defaults} || {} };
Condition50%1/2
$args->{'defaults'} || {}
l!l
800
MC/DC0%0/1
$args->{'defaults'} || {}
$args->{'defaults'}
Truth table50%1/2
A
A= $args->{'defaults'}
Aresult
11
00
228 80 ▶ my $api_call = $args->{api_call} || 'install_candidate';
Condition100%2/2
$args->{'api_call'} || 'install_candidate'
l!l
2258
MC/DC100%1/1
$args->{'api_call'} || 'install_candidate'
$args->{'api_call'}
Truth table100%2/2
A
A= $args->{'api_call'}
Aresult
11
00
229
230 106 ▶ return sub {
Subroutine __ANON__: called CC=25 SCAR=51.9
231 106 my $sub_name = shift;
232
233 106 my @tmp_sigs;
234 106 ▶ while ( is_ArrayRef $_[0] or is_HashRef $_[0] ) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_ArrayRef($_[0]) or is_HashRef($_[0])) 0 106 106
Condition33%1/3
is_ArrayRef($_[0]) or is_HashRef($_[0])
l!l&&r!l&&!r
00106
MC/DC0%0/2
is_ArrayRef($_[0]) or is_HashRef($_[0])
is_ArrayRef($_[0]) is_HashRef($_[0])
Truth table33%1/3
A or B
A= is_ArrayRef($_[0])B= is_HashRef($_[0])
ABresult
1X1
011
000
235 0 push @tmp_sigs, shift;
236 }
237
238 106 my @tmp_code;
239 106 ▶ if ( @_ % 2 == 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ % 2 == 1) 0 106 106
240 0 ▶ if ( is_CodeRef $_[-1] ) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_CodeRef($_[-1])) { } 0 0 0
241 0 push @tmp_code, pop;
242 }
243 else {
244 0 require Carp;
245 0 Carp::croak( "Odd-length list passed to $name; should be key-value pairs" );
246 }
247 }
248
249 106 my %spec = @_;
250 106 ▶ $spec{positional} ||= delete $spec{pos} if exists $spec{pos};
Branch: 1 branch
BranchTrueFalseTotal
if exists $spec{"pos"} 0 106 106
Condition0%0/3
$spec{"positional"} ||= delete $spec{"pos"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$spec{"positional"} ||= delete $spec{"pos"}
$spec{"positional"} delete $spec{"pos"}
Truth table0%0/3
A ||= B
A= $spec{"positional"}B= delete $spec{"pos"}
ABresult
1X1
011
000
251 106 ▶ $spec{multiple} ||= delete $spec{multi} if exists $spec{multi};
Branch: 1 branch
BranchTrueFalseTotal
if exists $spec{"multi"} 0 106 106
Condition0%0/3
$spec{"multiple"} ||= delete $spec{"multi"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$spec{"multiple"} ||= delete $spec{"multi"}
$spec{"multiple"} delete $spec{"multi"}
Truth table0%0/3
A ||= B
A= $spec{"multiple"}B= delete $spec{"multi"}
ABresult
1X1
011
000
252
253 106 ▶ while ( my $sig = shift @tmp_sigs ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $sig = shift @tmp_sigs) 0 106 106
254 0 ▶ if ( is_ArrayRef $sig ) {
Branch: 2 branches
BranchTrueFalseTotal
if (is_ArrayRef($sig)) { } 0 0 0
elsif (is_HashRef($sig)) { } 0 0 0
255 0 ▶ if ( $spec{positional} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($spec{'positional'}) { } 0 0 0
256 0 require Carp;
257 0 Carp::croak( "Leading arrayref passed as argument to $name; unexpected positional signature found in list of key-value pairs" );
258 }
259 else {
260 0 $spec{positional} = $sig;
261 }
262 }
263 elsif ( is_HashRef $sig ) {
264 0 ▶ if ( $spec{named} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($spec{'named'}) { } 0 0 0
265 0 require Carp;
266 0 Carp::croak( "Leading hashref passed as argument to $name; unexpected named signature found in list of key-value pairs" );
267 }
268 else {
269 0 $spec{named} = [ %$sig ];
270 }
271 }
272 }
273
274 106 ▶ if ( $spec{named} and $spec{positional} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($spec{"named"} and $spec{"positional"}) 0 106 106
Condition66%2/3
$spec{"named"} and $spec{"positional"}
!ll&&!rl&&r
10420
MC/DC0%0/2
$spec{"named"} and $spec{"positional"}
$spec{"named"} $spec{"positional"}
Truth table66%2/3
A and B
A= $spec{"named"}B= $spec{"positional"}
ABresult
0X0
100
111
275 0 ▶ push @{ $spec{multiple} ||= [] }, (
Condition0%0/2
$spec{"multiple"} ||= []
l!l
00
MC/DC0%0/1
$spec{"multiple"} ||= []
$spec{"multiple"}
Truth table0%0/2
A
A= $spec{"multiple"}
Aresult
11
00
276 { named => delete $spec{named}, named_to_list => !!1 },
277 { positional => delete $spec{positional} },
278 0 );
279 }
280
281 106 ▶ if ( @tmp_code ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@tmp_code) 0 106 106
282 0 ▶ if ( $spec{code} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($spec{"code"}) 0 0 0
283 0 require Carp;
284 0 Carp::croak( "Trailing coderef passed as argument to $name; unexpected code key found in list of key-value pairs" );
285 }
286 0 $spec{code} = shift @tmp_code;
287 }
288
289 106 ▶ unless ( exists $spec{code} or exists $spec{return} or exists $spec{die} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $spec{"code"} or exists $spec{"return"} or exists $spec{"die"}) 0 106 106
Condition50%3/6
exists $spec{"code"} or exists $spec{"return"} or exists $spec{"die"}
l!l&&r!l&&!r
10600
exists $spec{"code"} or exists $spec{"return"}
l!l&&r!l&&!r
10420
MC/DC0%0/3
exists $spec{"code"} or exists $spec{"return"} or exists $spec{"die"}
exists $spec{"code"} exists $spec{"return"} exists $spec{"die"}
Truth table50%2/4
A or B or C
A= exists $spec{"code"}B= exists $spec{"return"}C= exists $spec{"die"}
ABCresult
1XX1
01X1
0011
0000
290 0 require Carp;
291 0 Carp::croak( "Missing 'code', 'return', or 'die'" );
292 }
293
294 106 ▶ if ( ref $spec{return} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $spec{"return"}) 0 106 106
295 0 require Carp;
296 0 Carp::croak( "Setting 'return' to a reference is not supported" );
297 }
298
299 106 ▶ if ( $defaults{no_dispatcher} eq 'auto' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($defaults{"no_dispatcher"} eq "auto") 46 60 106
300 46 $defaults{no_dispatcher} = 0+!! 'Role::Hooks'->is_role( $target );
301 }
302
303 106 ▶ $me->$api_call(
Branch: 1 branch
BranchTrueFalseTotal
ref $sub_name ? : 12 94 106
304 $target,
305 $sub_name,
306 %defaults,
307 'package' => $target,
308 'subname' => ( ref($sub_name) ? '__ANON__' : $sub_name ),
309 %spec,
310 );
311 80 };
312 }
313  
314 46 ▶sub _generate_multimethod {
Subroutine _generate_multimethod: called CC=3 SCAR=11.4
315 46 my ( $me, $name, $args, $globals ) = ( shift, @_ );
316 46 ▶ $args->{defaults}{no_dispatcher} = 'auto' unless exists $args->{defaults}{no_dispatcher};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $args->{"defaults"}{"no_dispatcher"} 46 0 46
317 46 ▶ $args->{defaults}{method} = !!1 unless exists $args->{defaults}{method};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $args->{"defaults"}{"method"} 46 0 46
318 46 return $me->_generate_exported_function( $name, $args, $globals );
319 }
320  
321 12 ▶sub _generate_monomethod {
Subroutine _generate_monomethod: called CC=3 SCAR=11.3
322 12 my ( $me, $name, $args, $globals ) = ( shift, @_ );
323 12 ▶ $args->{defaults}{no_dispatcher} = !!1 unless exists $args->{defaults}{no_dispatcher};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $args->{"defaults"}{"no_dispatcher"} 12 0 12
324 12 ▶ $args->{defaults}{method} = !!1 unless exists $args->{defaults}{method};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $args->{"defaults"}{"method"} 12 0 12
325 12 $args->{api_call} = 'install_monomethod';
326 12 return $me->_generate_exported_function( $name, $args, $globals );
327 }
328  
329 12 ▶sub _generate_multifunction {
Subroutine _generate_multifunction: called CC=3 SCAR=11.4
330 12 my ( $me, $name, $args, $globals ) = ( shift, @_ );
331 12 ▶ $args->{defaults}{no_dispatcher} = 'auto' unless exists $args->{defaults}{no_dispatcher};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $args->{"defaults"}{"no_dispatcher"} 12 0 12
332 12 ▶ $args->{defaults}{method} = !!0 unless exists $args->{defaults}{method};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $args->{"defaults"}{"method"} 12 0 12
333 12 return $me->_generate_exported_function( $name, $args, $globals );
334 }
335  
336 10 ▶sub _generate_monofunction {
Subroutine _generate_monofunction: called CC=3 SCAR=11.3
337 10 my ( $me, $name, $args, $globals ) = ( shift, @_ );
338 10 ▶ $args->{defaults}{no_dispatcher} = !!1 unless exists $args->{defaults}{no_dispatcher};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $args->{"defaults"}{"no_dispatcher"} 10 0 10
339 10 ▶ $args->{defaults}{method} = !!0 unless exists $args->{defaults}{method};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $args->{"defaults"}{"method"} 10 0 10
340 10 $args->{api_call} = 'install_monomethod';
341 10 return $me->_generate_exported_function( $name, $args, $globals );
342 }
343  
344 106 ▶sub _extract_type_params_spec {
Subroutine _extract_type_params_spec: called CC=21 SCAR=31.8
345 106 my ( $me, $target, $sub_name, $spec ) = ( shift, @_ );
346
347 106 my %tp = ( method => 1 );
348 106 ▶ $tp{method} = $spec->{method} if defined $spec->{method};
Branch: 1 branch
BranchTrueFalseTotal
if defined $spec->{"method"} 106 0 106
349
350 106 ▶ if ( is_ArrayRef $spec->{signature} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_ArrayRef($spec->{'signature'})) { } 51 55 106
351 51 ▶ my $key = $spec->{named} ? 'named' : 'positional';
Branch: 1 branch
BranchTrueFalseTotal
$spec->{'named'} ? : 1 50 51
352 51 $tp{$key} = delete $spec->{signature};
353 }
354 else {
355 55 ▶ $tp{named} = $spec->{named} if ref $spec->{named};
Branch: 1 branch
BranchTrueFalseTotal
if ref $spec->{"named"} 1 54 55
356 }
357
358 # Options which are not known by this module must be intended for
359 # Type::Params instead.
360 106 for my $key ( keys %$spec ) {
361
362 614 ▶ next if ( $KNOWN_OPTIONS{$key} or $key =~ /^_/ );
Branch: 1 branch
BranchTrueFalseTotal
if $KNOWN_OPTIONS{$key} or $key =~ /^_/ 350 264 614
Condition100%3/3
$KNOWN_OPTIONS{$key} or $key =~ /^_/
l!l&&r!l&&!r
3428264
MC/DC100%2/2
$KNOWN_OPTIONS{$key} or $key =~ /^_/
$KNOWN_OPTIONS{$key} $key =~ /^_/
Truth table100%3/3
A or B
A= $KNOWN_OPTIONS{$key}B= $key =~ /^_/
ABresult
1X1
011
000
363
364 264 ▶ if ( $BAD_OPTIONS{$key} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($BAD_OPTIONS{$key}) 0 264 264
365 0 require Carp;
366 0 Carp::carp( "Unsupported option: $key" );
367 0 next;
368 }
369
370 264 $tp{$key} = delete $spec->{$key};
371 }
372
373 106 ▶ $tp{package} ||= $target;
Condition33%1/3
$tp{"package"} ||= $target
l!l&&r!l&&!r
10600
MC/DC0%0/2
$tp{"package"} ||= $target
$tp{"package"} $target
Truth table33%1/3
A ||= B
A= $tp{"package"}B= $target
ABresult
1X1
011
000
374 106 ▶ $tp{subname} ||= ref( $sub_name ) ? '__ANON__' : $sub_name;
Branch: 1 branch
BranchTrueFalseTotal
ref $sub_name ? : 0 4 4
Condition66%2/3
$tp{"subname"} ||= ref $sub_name ? "__ANON__" : $sub_name
l!l&&r!l&&!r
10204
MC/DC50%1/2
$tp{"subname"} ||= ref $sub_name ? "__ANON__" : $sub_name
$tp{"subname"} ref $sub_name ? "__ANON__" : $sub_name
Truth table66%2/3
A ||= B
A= $tp{"subname"}B= ref $sub_name ? "__ANON__" : $sub_name
ABresult
1X1
011
000
375
376 # Historically we allowed method=2, etc
377 106 ▶ if ( is_Int $tp{method} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_Int($tp{"method"})) 98 8 106
378 98 ▶ if ( $tp{method} > 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tp{"method"} > 1) 4 94 98
379 4 my $excess = $tp{method} - 1;
380 4 $tp{method} = 1;
381 0 ▶ ref( $tp{head} ) ? push( @{ $tp{head} }, ( Any ) x $excess ) : ( $tp{head} += $excess );
Branch: 1 branch
BranchTrueFalseTotal
ref $tp{'head'} ? : 0 4 4
382 }
383 98 ▶ if ( $tp{method} == 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tp{"method"} == 1) 86 12 98
384 86 $tp{method} = Any;
385 }
386 }
387  
388 106 ▶ if ( not ( $tp{named} or $tp{pos} or $tp{positional} or $tp{multi} or $tp{multiple} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($tp{"named"} or $tp{"pos"} or $tp{"positional"} or $tp{"multi"} or $tp{"multiple"}) 4 102 106
Condition75%9/12
$tp{"named"} or $tp{"pos"} or $tp{"positional"} or $tp{"multi"} or $tp{"multiple"}
l!l&&r!l&&!r
10204
$tp{"named"} or $tp{"pos"} or $tp{"positional"} or $tp{"multi"}
l!l&&r!l&&!r
10204
$tp{"named"} or $tp{"pos"} or $tp{"positional"}
l!l&&r!l&&!r
21004
$tp{"named"} or $tp{"pos"}
l!l&&r!l&&!r
20104
MC/DC40%2/5
$tp{"named"} or $tp{"pos"} or $tp{"positional"} or $tp{"multi"} or $tp{"multiple"}
$tp{"named"} $tp{"pos"} $tp{"positional"} $tp{"multi"} $tp{"multiple"}
Truth table50%3/6
A or B or C or D or E
A= $tp{"named"}B= $tp{"pos"}C= $tp{"positional"}D= $tp{"multi"}E= $tp{"multiple"}
ABCDEresult
1XXXX1
01XXX1
001XX1
0001X1
000011
000000
389 4 $tp{pos} = [ Slurpy[Any] ];
390 4 $spec->{smiple} = 1;
391 }
392
393 106 $spec->{signature_spec} = \%tp;
394 }
395  
396 23 my %delete_while_copying = (
397 _id => '_id should be unique',
398 alias => 'alias should only be installed into package where originally declared',
399 copied => 'this will be set after copying',
400 height => 'this should never be kept anyway',
401 is_monomethod => 'if it has been copied, it is no longer mono!',
402 no_dispatcher => 'after a candidate gets copied from a role to a class, there SHOULD be a dispatcher',
403 );
404 20 ▶sub copy_package_candidates {
Subroutine copy_package_candidates: called CC=5 SCAR=16.1
Pod documented
405 20 my $me = shift;
406 20 my (@sources) = @_;
407 20 my $target = pop @sources;
408
409 20 for my $source (@sources) {
410 20 for my $method_name ($me->get_multimethods($source)) {
411 20 for my $candidate ($me->get_multimethod_candidates($source, $method_name)) {
412 my %new = map {
413 128 $delete_while_copying{$_}
414 ? ()
415 642 ▶ : ( $_ => $candidate->{$_} )
Branch: 1 branch
BranchTrueFalseTotal
$delete_while_copying{$_} ? : 130 512 642
416 } keys %$candidate;
417 128 $new{copied} = 1;
418 128 $me->_add_multimethod_candidate($target, $method_name, \%new);
419 }
420 }
421 }
422 }
423  
424 14 ▶sub install_missing_dispatchers {
Subroutine install_missing_dispatchers: called CC=3 SCAR=11.1
Pod documented
425 14 my $me = shift;
426 14 my ($target) = @_;
427
428 14 for my $method_name ($me->get_multimethods($target)) {
429 14 my ($first) = $me->get_multimethod_candidates($target, $method_name);
430 $me->install_dispatcher(
431 $target,
432 $method_name,
433 14 ▶ $first ? $first->{'method'} : 0,
Branch: 1 branch
BranchTrueFalseTotal
$first ? : 14 0 14
434 );
435 }
436 }
437  
438 4 ▶sub install_monomethod {
Subroutine install_monomethod: called CC=3 SCAR=11.2
Pod documented
439 4 my ( $me, $target, $sub_name, %spec ) = ( shift, @_ );
440
441 4 ▶ $spec{alias} ||= [];
Condition50%1/2
$spec{"alias"} ||= []
l!l
04
MC/DC0%0/1
$spec{"alias"} ||= []
$spec{"alias"}
Truth table50%1/2
A
A= $spec{"alias"}
Aresult
11
00
442 4 ▶ $spec{alias} = [$spec{alias}] if !ref $spec{alias};
Branch: 1 branch
BranchTrueFalseTotal
unless ref $spec{"alias"} 0 4 4
443 4 unshift @{$spec{alias}}, $sub_name;
444
445 4 $me->install_candidate($target, undef, no_dispatcher => 1, %spec, is_monomethod => 1);
446 }
447  
448 23 my %hooked;
449 23 my $DECLARATION_ORDER = 0;
450 106 ▶sub install_candidate {
Subroutine install_candidate: called CC=23 SCAR=33.0
Pod documented
451 106 my ( $me, $target, $sub_name, %spec ) = ( shift, @_ );
452 106 $me->_extract_type_params_spec( $target, $sub_name, \%spec );
453  
454 106 my $is_method = $spec{method};
455
456 106 ▶ if ( $spec{want} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($spec{"want"}) 1 105 106
457 my @canonical =
458 map {
459 1 ( my $x = $_ ) =~ s/^NON/~/;
460 1 ▶ $_ eq '~VOID' ? qw( SCALAR LIST ) :
Branch: 3 branches
BranchTrueFalseTotal
$_ eq '~VOID' ? : 0 1 1
$_ eq '~LIST' ? : 0 1 1
$_ eq '~SCALAR' ? : 0 1 1
461 $_ eq '~LIST' ? qw( SCALAR VOID ) :
462 $_ eq '~SCALAR' ? qw( LIST VOID ) : $_
463 }
464 1 map { split /,/, $_ }
465 1 ▶ map { uc $_ }
Branch: 1 branch
BranchTrueFalseTotal
is_ArrayRef($spec{'want'}) ? : 0 1 1
466 0 is_ArrayRef($spec{want}) ? @{$spec{want}} : $spec{want};
467 1 $spec{want} = \@canonical;
468 }
469
470 106 $spec{declaration_order} = ++$DECLARATION_ORDER;
471
472 106 ▶ $me->_add_multimethod_candidate($target, $sub_name, \%spec)
Branch: 1 branch
BranchTrueFalseTotal
if defined $sub_name 98 8 106
473 if defined $sub_name;
474
475 106 ▶ if ($spec{alias}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($spec{"alias"}) 16 90 106
476 my @aliases = is_ArrayRef( $spec{alias} )
477 4 ? @{ $spec{alias} }
478 16 ▶ : $spec{alias};
Branch: 1 branch
BranchTrueFalseTotal
is_ArrayRef($spec{'alias'}) ? : 4 12 16
479
480 16 my ($check, @sig);
481 16 ▶ if (is_CodeRef $spec{signature}) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_CodeRef($spec{"signature"})) 0 16 16
482 0 $check = $spec{signature};
483 }
484
485 my %sig_spec = (
486 16 ▶ %{ $spec{signature_spec} },
Branch: 3 branches
BranchTrueFalseTotal
defined $spec{'code'} ? : 16 0 16
is_CodeRef($spec{'die'}) ? : 0 0 0
defined $spec{'die'} ? : 0 0 0
487 goto_next =>
488 defined($spec{code}) ? $spec{code} :
489 0 ▶ is_CodeRef($spec{die}) ? sub { require Carp; Carp::croak($spec{die}->()) } :
Subroutine __ANON__: not called CC=1 SCAR=6.9
490 0 ▶ defined($spec{die}) ? sub { require Carp; Carp::croak($spec{die}) } :
Subroutine __ANON__: not called CC=1 SCAR=6.9
491 0 ▶ sub { $spec{return} },
Subroutine __ANON__: not called CC=1 SCAR=6.9
492 16 );
493 16 my $code = sprintf(
494 q{
495 package %s;
496 sub {
497 $check ||= Type::Params::signature( %%sig_spec );
498 goto $check;
499 }
500 },
501 $target,
502 );
503 16 my $coderef = do {
504 16 local $@;
505 16 ▶ eval $code or die $@,
Subroutine stringify_str: called CC=2 SCAR=7.2
Condition60%3/5
eval $code || die($@)
l!l
160
$check ||= Type::Params::signature(%sig_spec)
l!l&&r!l&&!r
26100
MC/DC0%0/1
eval $code || die($@)
eval $code
MC/DC0%0/2
$check ||= Type::Params::signature(%sig_spec)
$check Type::Params::signature(%sig_spec)
Truth table50%1/2
A
A= eval $code
Aresult
11
00
Truth table66%2/3
A ||= B
A= $checkB= Type::Params::signature(%sig_spec)
ABresult
1X1
011
000
506 };
507 16 for my $alias (@aliases) {
508 16 my $existing = do {
509 23 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
510 16 exists(&{"$target\::$alias"})
511 16 ▶ ? \&{"$target\::$alias"}
Branch: 1 branch
BranchTrueFalseTotal
exists &{"${target}::$alias";} ? : 4 12 16
512 : undef;
513 };
514 16 ▶ if ($existing) {
Branch: 1 branch
BranchTrueFalseTotal
if ($existing) 4 12 16
515 4 ▶ my $kind = ($spec{is_monomethod} && ($alias eq $aliases[0]))
Branch: 1 branch
BranchTrueFalseTotal
$spec{'is_monomethod'} && $alias eq $aliases[0] ? : 2 2 4
Condition66%2/3
$spec{"is_monomethod"} and $alias eq $aliases[0]
!ll&&!rl&&r
202
MC/DC50%1/2
$spec{"is_monomethod"} and $alias eq $aliases[0]
$spec{"is_monomethod"} $alias eq $aliases[0]
Truth table66%2/3
A and B
A= $spec{"is_monomethod"}B= $alias eq $aliases[0]
ABresult
0X0
100
111
516 ? 'Monomethod'
517 : 'Alias';
518 4 require Carp;
519 4 Carp::croak("$kind conflicts with existing method $target\::$alias, bailing out");
520 }
521 12 $me->_install_coderef( $target, $alias, $coderef );
522 }
523 }
524
525 $me->install_dispatcher($target, $sub_name, $is_method)
526 102 ▶ if defined $sub_name && !$spec{no_dispatcher};
Branch: 1 branch
BranchTrueFalseTotal
if defined $sub_name and not $spec{"no_dispatcher"} 72 30 102
Condition100%3/3
defined $sub_name and not $spec{"no_dispatcher"}
!ll&&!rl&&r
42672
MC/DC100%2/2
defined $sub_name and not $spec{"no_dispatcher"}
defined $sub_name not $spec{"no_dispatcher"}
Truth table100%3/3
A and not(B)
A= defined $sub_nameB= not $spec{"no_dispatcher"}
ABresult
0X0
100
111
527
528 98 ▶ if ( !$hooked{$target} and 'Role::Hooks'->is_role($target) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $hooked{$target} and "Role::Hooks"->is_role($target)) 10 88 98
Condition100%3/3
not $hooked{$target} and "Role::Hooks"->is_role($target)
!ll&&!rl&&r
167210
MC/DC100%2/2
not $hooked{$target} and "Role::Hooks"->is_role($target)
not $hooked{$target} "Role::Hooks"->is_role($target)
Truth table100%3/3
not(A) and B
A= not $hooked{$target}B= "Role::Hooks"->is_role($target)
ABresult
0X0
100
111
529 20 ▶ 'Role::Hooks'->after_apply($target, sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
530 20 my ($rolepkg, $consumerpkg) = @_;
531 20 $me->copy_package_candidates($rolepkg => $consumerpkg);
532 20 ▶ $me->install_missing_dispatchers($consumerpkg)
Branch: 1 branch
BranchTrueFalseTotal
unless "Role::Hooks"->is_role($consumerpkg) 14 6 20
533 unless 'Role::Hooks'->is_role($consumerpkg);
534 10 });
535 10 $hooked{$target}++;
536 }
537 }
538  
539 {
540 23 my %CLEANUP;
541
542 60 ▶ sub _install_coderef {
Subroutine _install_coderef: called CC=7 SCAR=20.8
543 60 my $me = shift;
544 60 my ($target, $sub_name, $coderef) = @_;
545 60 ▶ if (is_ScalarRef $sub_name) {
Branch: 2 branches
BranchTrueFalseTotal
if (is_ScalarRef($sub_name)) { } 14 46 60
elsif (is_Str($sub_name)) { } 46 0 46
546 14 ▶ if (is_Undef $$sub_name) {
Branch: 2 branches
BranchTrueFalseTotal
if (is_Undef($$sub_name)) { } 4 10 14
elsif (is_CodeRef($$sub_name) or is_Object($$sub_name)) { } 10 0 10
547 4 set_subname("$target\::__ANON__", $coderef);
548 4 bless( $coderef, $me );
549 4 $CLEANUP{"$coderef"} = [ $target, refaddr($sub_name) ];
550 4 return( $$sub_name = $coderef );
551 }
552 ▶ elsif (is_CodeRef $$sub_name or is_Object $$sub_name) {
Condition33%1/3
is_CodeRef($$sub_name) or is_Object($$sub_name)
l!l&&r!l&&!r
0100
MC/DC0%0/2
is_CodeRef($$sub_name) or is_Object($$sub_name)
is_CodeRef($$sub_name) is_Object($$sub_name)
Truth table33%1/3
A or B
A= is_CodeRef($$sub_name)B= is_Object($$sub_name)
ABresult
1X1
011
000
553 10 ▶ if ( $me->known_dispatcher($$sub_name) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($me->known_dispatcher($$sub_name)) { } 10 0 10
554 10 return $$sub_name;
555 }
556 else {
557 0 require Carp;
558 0 Carp::croak(sprintf(
559 'Sub name was a reference to an unknown coderef or object: %s',
560 $$sub_name,
561 ));
562 }
563 }
564 }
565 elsif (is_Str $sub_name) {
566 23 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
567 46 my $qname = "$target\::$sub_name";
568 46 *$qname = set_subname($qname, $coderef);
569 46 return $coderef;
570 }
571 0 require Carp;
572 0 Carp::croak(sprintf(
573 'Expected string or reference to coderef as sub name, but got: %s %s',
574 $sub_name,
575 ));
576 }
577
578 4 ▶ sub DESTROY {
Subroutine DESTROY: called CC=4 SCAR=15.0
579 4 my $blessed_coderef = shift;
580 4 ▶ my ( $target, $sub_name ) = @{ $CLEANUP{"$blessed_coderef"} or [] };
Condition50%1/2
$CLEANUP{"$blessed_coderef"} || []
l!l
40
MC/DC0%0/1
$CLEANUP{"$blessed_coderef"} || []
$CLEANUP{"$blessed_coderef"}
Truth table50%1/2
A
A= $CLEANUP{"$blessed_coderef"}
Aresult
11
00
581 4 ▶ if ( $target and $sub_name ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($target and $sub_name) 4 0 4
Condition33%1/3
$target and $sub_name
!ll&&!rl&&r
004
MC/DC0%0/2
$target and $sub_name
$target $sub_name
Truth table33%1/3
A and B
A= $targetB= $sub_name
ABresult
0X0
100
111
582 4 $blessed_coderef->_clear_multimethod_candidates_ref($target, $sub_name);
583 }
584 4 return;
585 }
586 }
587  
588 23 ▶sub install_dispatcher {
Subroutine install_dispatcher: called CC=11 SCAR=24.3
Pod documented
589 86 my $me = shift;
590 86 my ($target, $sub_name, $is_method) = @_;
591
592 exists &mro::get_linear_isa
593 0 or eval { require mro }
594 0 ▶ or do { require MRO::Compat };
Branch: 1 branch
BranchTrueFalseTotal
exists &mro::get_linear_isa or eval { do { require mro } } or do { require MRO::Compat } 86 0 86
Condition33%1/3
exists &mro::get_linear_isa or eval { do { require mro } }
l!l&&r!l&&!r
8600
MC/DC0%0/2
exists &mro::get_linear_isa or eval { do { require mro } }
exists &mro::get_linear_isa eval { do { require mro } }
Truth table33%1/3
A or B
A= exists &mro::get_linear_isaB= eval { do { require mro } }
ABresult
1X1
011
000
595
596 86 my $existing = do {
597 23 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
598 86 exists(&{"$target\::$sub_name"})
599 86 ▶ ? \&{"$target\::$sub_name"}
Branch: 1 branch
BranchTrueFalseTotal
exists &{"${target}::$sub_name";} ? : 38 48 86
600 : undef;
601 };
602
603 86 ▶ return if !defined $sub_name;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $sub_name 0 86 86
604
605 86 ▶ if ($existing and $me->known_dispatcher($existing)) {
Branch: 2 branches
BranchTrueFalseTotal
if ($existing and $me->known_dispatcher($existing)) { } 34 52 86
elsif ($existing) { } 4 48 52
Condition100%3/3
$existing and $me->known_dispatcher($existing)
!ll&&!rl&&r
48434
MC/DC100%2/2
$existing and $me->known_dispatcher($existing)
$existing $me->known_dispatcher($existing)
Truth table100%3/3
A and B
A= $existingB= $me->known_dispatcher($existing)
ABresult
0X0
100
111
606 34 return $me; # already installed
607 }
608 elsif ($existing) {
609 4 require Carp;
610 4 Carp::croak("Multimethod conflicts with monomethod $target\::$sub_name, bailing out");
611 }
612
613 48 ▶ my $code = sprintf(
Branch: 1 branch
BranchTrueFalseTotal
ref $sub_name ? : 12 36 48
Condition100%2/2
$is_method || 0
l!l
3216
MC/DC100%1/1
$is_method || 0
$is_method
Truth table100%2/2
A
A= $is_method
Aresult
11
00
614 q{
615 package %s;
616 sub {
617 @_ = (%s, %s, %s, %d, [@_], wantarray);
618 goto $next;
619 }
620 },
621 $target, # package %s
622 B::perlstring($me), # $_[0]
623 B::perlstring($target), # $_[1]
624 ref($sub_name) # $_[2]
625 ? refaddr($sub_name)
626 : B::perlstring("$sub_name"),
627 $is_method || 0, # $_[3]
628 );
629
630 48 my $coderef = do {
631 48 local $@;
632 48 my $next = $me->can('dispatch');
633 48 ▶ eval $code or die $@;
Subroutine stringify: called CC=1 SCAR=0.1
Condition50%1/2
eval $code || die($@)
l!l
480
MC/DC0%0/1
eval $code || die($@)
eval $code
Truth table50%1/2
A
A= eval $code
Aresult
11
00
634 };
635
636 48 $me->_install_coderef($target, $sub_name, $coderef);
637 48 $me->_mark_as_dispatcher($coderef);
638 48 return $coderef;
639 }
640  
641 149 ▶sub dispatch {
Subroutine dispatch: called CC=13 SCAR=26.9
Pod undocumented
642 149 my $me = shift;
643 149 my ($pkg, $method_name, $is_method, $argv, $wantarray) = @_;
644 139 ▶ $wantarray = wantarray if @_ < 5;
Branch: 1 branch
BranchTrueFalseTotal
if @_ < 5 0 123 123
645
646 139 my $search_from = $pkg;
647 137 ▶ if ( $is_method and is_Object $argv->[0] ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($is_method and is_Object($argv->[0])) { } 78 45 123
elsif ($is_method and &is_ClassName($argv->[0])) { } 10 66 76
Condition100%3/3
$is_method and is_Object($argv->[0])
!ll&&!rl&&r
311478
MC/DC100%2/2
$is_method and is_Object($argv->[0])
$is_method is_Object($argv->[0])
Truth table100%3/3
A and B
A= $is_methodB= is_Object($argv->[0])
ABresult
0X0
100
111
648 # object method; reset package search from invocant class
649 92 $search_from = ref $argv->[0];
650 }
651 ▶ elsif ( $is_method and is_ClassName $argv->[0] ) {
Condition100%3/3
$is_method and &is_ClassName($argv->[0])
!ll&&!rl&&r
31410
MC/DC100%2/2
$is_method and &is_ClassName($argv->[0])
$is_method &is_ClassName($argv->[0])
Truth table100%3/3
A and B
A= $is_methodB= &is_ClassName($argv->[0])
ABresult
0X0
100
111
652 # class method; reset package search from invocant class
653 14 $search_from = $argv->[0];
654 }
655
656 my ($winner, $new_argv) = $me->pick_candidate(
657 $CACHE{"$pkg/$search_from/$method_name/$is_method"} ||=
658 [ $me->get_all_multimethod_candidates($search_from, $method_name, $is_method) ],
659 $argv,
660 $wantarray ? LIST : defined($wantarray) ? SCALAR : VOID,
661 127 ▶ ) or do {
Branch: 3 branches
BranchTrueFalseTotal
() = $me->pick_candidate($CACHE{"$pkg/$search_from/$method_name/$is_method"} ||= [$me->get_all_multimethod_candidates($search_from, $method_name, $is_method)], $argv, $wantarray ? "LIST" : (defined $wantarray ? "SCALAR" : "VOID")) or do { require Carp; Carp::croak("Multimethod could not find candidate to dispatch to, stopped") } 117 6 123
$wantarray ? : 36 87 123
defined $wantarray ? : 79 8 87
Condition100%2/2
$CACHE{"$pkg/$search_from/$method_name/$is_method"} ||= [$me->get_all_multimethod_candidates($search_from, $method_name, $is_method)]
l!l
8538
MC/DC100%1/1
$CACHE{"$pkg/$search_from/$method_name/$is_method"} ||= [$me->get_all_multimethod_candidates($search_from, $method_name, $is_method)]
$CACHE{"$pkg/$search_from/$method_name/$is_method"}
Truth table100%2/2
A
A= $CACHE{"$pkg/$search_from/$method_name/$is_method"}
Aresult
11
00
662 12 require Carp;
663 12 Carp::croak('Multimethod could not find candidate to dispatch to, stopped');
664 };
665
666 131 ▶ if ( my $next = $winner->{code} ) {
Branch: 2 branches
BranchTrueFalseTotal
if (my $next = $winner->{'code'}) { } 117 0 117
elsif (defined $winner->{'die'}) { } 0 0 0
667 131 @_ = @$new_argv;
668 117 goto $next;
669 }
670 elsif ( defined $winner->{die} ) {
671 0 require Carp;
672 0 ▶ Carp::croak( is_CodeRef( $winner->{die} ) ? $winner->{die}->() : $winner->{die} );
Branch: 1 branch
BranchTrueFalseTotal
is_CodeRef($winner->{'die'}) ? : 0 0 0
673 }
674 else {
675 0 return $winner->{return};
676 }
677 }
678  
679 # Optimization for simple signatures: those consisting of only non-coercing positional parameters.
680 23 my $smiple_keys = Enum[qw/ package subname method pos positional /];
681 178 ▶sub _maybe_make_smiple {
Subroutine _maybe_make_smiple: called CC=11 SCAR=25.7
682 162 my ( $me, $candidate ) = @_;
683 162 ▶ return if $candidate->{smiple};
Branch: 1 branch
BranchTrueFalseTotal
if $candidate->{"smiple"} 4 158 162
684 158 ▶ return unless $smiple_keys->all( keys %{ $candidate->{signature_spec} } );
Branch: 1 branch
BranchTrueFalseTotal
unless $smiple_keys->all(keys %{$$candidate{"signature_spec"};}) 6 152 158
685 my @types =
686 244 ▶ map { is_Bool( $_ ) ? ( $_ ? Any : Optional[Any] ) : $_ }
Branch: 2 branches
BranchTrueFalseTotal
is_Bool($_) ? : 0 244 244
$_ ? : 0 0 0
687 152 ▶ @{ $candidate->{signature_spec}{pos} or $candidate->{signature_spec}{positional} or [] };
Condition40%2/5
$candidate->{'signature_spec'}{'pos'} || $candidate->{'signature_spec'}{'positional'} || []
l!l
1520
$candidate->{"signature_spec"}{"pos"} or $candidate->{"signature_spec"}{"positional"}
l!l&&r!l&&!r
01520
MC/DC0%0/2
$candidate->{'signature_spec'}{'pos'} || $candidate->{'signature_spec'}{'positional'} || []
$candidate->{"signature_spec"}{"pos"} $candidate->{"signature_spec"}{"positional"}
Truth table33%1/3
A or B
A= $candidate->{"signature_spec"}{"pos"}B= $candidate->{"signature_spec"}{"positional"}
ABresult
1X1
011
000
688 152 ▶ return unless TypeTiny->all( @types );
Branch: 1 branch
BranchTrueFalseTotal
unless &TypeTiny()->all(@types) 0 152 152
689 152 ▶ if ( TypeTiny->check( $candidate->{signature_spec}{method} ) ) {
Branch: 2 branches
BranchTrueFalseTotal
if (&TypeTiny()->check($candidate->{'signature_spec'}{'method'})) { } 136 16 152
elsif ($candidate->{'signature_spec'}{'method'}) { } 0 16 16
690 136 unshift @types, $candidate->{signature_spec}{method};
691 }
692 elsif ( $candidate->{signature_spec}{method} ) {
693 0 unshift @types, Any;
694 }
695 152 ▶ return if grep { $_->has_coercion } @types;
Branch: 1 branch
BranchTrueFalseTotal
if grep {$_->has_coercion;} @types 4 148 152
696 148 $candidate->{smiple} = Tuple->of( @types )->compiled_check;
697 }
698  
699 137 ▶sub pick_candidate {
Subroutine pick_candidate: called CC=28 SCAR=33.3
Pod documented
700 123 my ( $me, $candidates, $argv, $wantarray ) = ( shift, @_ );
701
702 123 my @remaining = @{ $candidates };
703
704 # Compile signatures into something useful. (Cached.)
705 #
706
707 123 for my $candidate (@remaining) {
708 754 ▶ next if $candidate->{compiled};
Branch: 1 branch
BranchTrueFalseTotal
if $candidate->{"compiled"} 588 166 754
709 166 ▶ if ( is_CodeRef $candidate->{signature} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_CodeRef($candidate->{'signature'})) { } 4 162 166
710 4 $candidate->{compiled}{closure} = $candidate->{signature};
711 4 $candidate->{compiled}{min_args} = 0;
712 4 $candidate->{compiled}{max_args} = undef;
713 }
714 else {
715 $candidate->{compiled} = Type::Params::signature(
716 162 %{ $candidate->{signature_spec} },
717 want_details => 1,
718 );
719 162 $me->_maybe_make_smiple( $candidate );
720 }
721 }
722
723 # Weed out signatures that cannot match because of
724 # argument count.
725 #
726
727 123 my $argc = @$argv;
728
729 @remaining =
730 735 ▶ grep { $_->{if} ? &{$_->{if}} : 1 }
Branch: 1 branch
BranchTrueFalseTotal
$_->{'if'} ? : 3 732 735
731 736 ▶ grep { ($_->{want} and $wantarray) ? (!!any { $wantarray eq $_ } @{$_->{want}}) : 1 }
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
$_->{'want'} && $wantarray ? : 2 734 736
Condition66%2/3
$_->{"want"} and $wantarray
!ll&&!rl&&r
73402
MC/DC50%1/2
$_->{"want"} and $wantarray
$_->{"want"} $wantarray
Truth table66%2/3
A and B
A= $_->{"want"}B= $wantarray
ABresult
0X0
100
111
732 grep {
733 123 (defined $_->{compiled}{min_args} and $_->{compiled}{min_args} > $argc) ? 0 :
734 754 ▶ (defined $_->{compiled}{max_args} and $_->{compiled}{max_args} < $argc) ? 0 : 1;
Branch: 2 branches
BranchTrueFalseTotal
defined $_->{'compiled'}{'min_args'} && $_->{'compiled'}{'min_args'} > $argc ? : 14 740 754
defined $_->{'compiled'}{'max_args'} && $_->{'compiled'}{'max_args'} < $argc ? : 4 736 740
Condition83%5/6
defined $_->{"compiled"}{"min_args"} and $_->{"compiled"}{"min_args"} > $argc
!ll&&!rl&&r
074014
defined $_->{"compiled"}{"max_args"} and $_->{"compiled"}{"max_args"} < $argc
!ll&&!rl&&r
287084
MC/DC50%1/2
defined $_->{"compiled"}{"min_args"} and $_->{"compiled"}{"min_args"} > $argc
defined $_->{"compiled"}{"min_args"} $_->{"compiled"}{"min_args"} > $argc
MC/DC100%2/2
defined $_->{"compiled"}{"max_args"} and $_->{"compiled"}{"max_args"} < $argc
defined $_->{"compiled"}{"max_args"} $_->{"compiled"}{"max_args"} < $argc
Truth table66%2/3
A and B
A= defined $_->{"compiled"}{"min_args"}B= $_->{"compiled"}{"min_args"} > $argc
ABresult
0X0
100
111
Truth table100%3/3
A and B
A= defined $_->{"compiled"}{"max_args"}B= $_->{"compiled"}{"max_args"} < $argc
ABresult
0X0
100
111
735 }
736 @remaining;
737
738 # Weed out signatures that cannot match because
739 # they fail type checks, etc
740 #
741
742 123 my %returns;
743
744 @remaining = grep {
745 123 ▶ if ( my $smiple = $_->{smiple} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $smiple = $_->{'smiple'}) { } 667 66 733
746 667 ▶ !ref($smiple) || $smiple->($argv) ? ($returns{"$_"} = $argv) : ();
Branch: 1 branch
BranchTrueFalseTotal
!ref($smiple) || &$smiple($argv) ? : 193 474 667
Condition100%3/3
not ref $smiple or &$smiple($argv)
l!l&&r!l&&!r
7186474
MC/DC100%2/2
not ref $smiple or &$smiple($argv)
not ref $smiple &$smiple($argv)
Truth table100%3/3
not(A) or B
A= not ref $smipleB= &$smiple($argv)
ABresult
1X1
011
000
747 }
748 else {
749 66 eval {
750 66 $returns{"$_"} = [ $_->{compiled}{closure}->(@$argv) ];
751 30 1;
752 };
753 }
754 } @remaining;
755
756 # Various techniques to cope with @remaining > 1...
757 #
758
759 123 ▶ if (@remaining > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@remaining > 1) 58 65 123
760 23 ▶ no warnings qw(uninitialized numeric);
Subroutine BEGIN: called CC=1 SCAR=0.0
761 # Calculate signature constrainedness score. (Cached.)
762 58 my $max_score;
763 58 for my $candidate (@remaining) {
764 164 my $score = $candidate->{score};
765 164 ▶ if ( not defined $score ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $score) 74 90 164
766 74 my $slurpyAny = Slurpy[Any];
767 74 $score = 0;
768 my @sig = map {
769 74 ▶ is_ArrayRef( $candidate->{signature_spec}{$_} ) ? @{ $candidate->{signature_spec}{$_} } : ();
Branch: 1 branch
BranchTrueFalseTotal
is_ArrayRef($candidate->{'signature_spec'}{$_}) ? : 70 152 222
770 } qw(positional pos named);
771 74 foreach my $type ( @sig ) {
772 100 ▶ next unless is_Object $type;
Branch: 1 branch
BranchTrueFalseTotal
unless is_Object($type) 0 100 100
773 100 ▶ next if $type == $slurpyAny;
Branch: 1 branch
BranchTrueFalseTotal
if $type == $slurpyAny 4 96 100
774 96 my @real_parents = grep !$_->_is_null_constraint, $type, $type->parents;
775 96 $score += @real_parents;
776 }
777 74 ▶ $score += 100_000 if $candidate->{want} || $candidate->{if};
Branch: 1 branch
BranchTrueFalseTotal
if $candidate->{"want"} or $candidate->{"if"} 2 72 74
Condition100%3/3
$candidate->{"want"} or $candidate->{"if"}
l!l&&r!l&&!r
1172
MC/DC100%2/2
$candidate->{"want"} or $candidate->{"if"}
$candidate->{"want"} $candidate->{"if"}
Truth table100%3/3
A or B
A= $candidate->{"want"}B= $candidate->{"if"}
ABresult
1X1
011
000
778 74 $candidate->{score} = $score;
779 };
780 164 $max_score = max( grep defined, $score, $max_score );
781 }
782 # Only keep those with (equal) highest score
783 58 @remaining = grep { $_->{score} == $max_score } @remaining;
784 }
785
786 123 ▶ if (@remaining > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@remaining > 1) 20 103 123
787 # Only keep those from the most derived class
788 23 ▶ no warnings qw(uninitialized numeric);
Subroutine BEGIN: called CC=1 SCAR=0.0
789 20 my $max_score = max( map $_->{height}, @remaining );
790 20 @remaining = grep { $_->{height} == $max_score } @remaining;
791 }
792
793 123 ▶ if (@remaining > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@remaining > 1) 10 113 123
794 # Only keep those from the most non-role-like packages
795 23 ▶ no warnings qw(uninitialized numeric);
Subroutine BEGIN: called CC=1 SCAR=0.0
796 10 my $min_score = min( map $_->{copied}, @remaining );
797 10 @remaining = grep { $_->{copied} == $min_score } @remaining;
798 }
799
800 123 ▶ if (@remaining > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@remaining > 1) 8 115 123
801 # Argh! Still got multiple candidates! Just choose whichever
802 # was declared first...
803 23 ▶ no warnings qw(uninitialized numeric);
Subroutine BEGIN: called CC=1 SCAR=0.0
804 8 my $min_score = min( map $_->{declaration_order}, @remaining );
805 8 @remaining = grep { $_->{declaration_order} == $min_score } @remaining;
806 }
807
808 123 ▶ wantarray or die 'MUST BE CALLED IN LIST CONTEXT';
Branch: 1 branch
BranchTrueFalseTotal
wantarray or die "MUST BE CALLED IN LIST CONTEXT" 123 0 123
809
810 123 ▶ return unless @remaining;
Branch: 1 branch
BranchTrueFalseTotal
unless @remaining 6 117 123
811 117 return ( $remaining[0], $returns{''.$remaining[0]} );
812 }
813  
814 23 1;
815  
816 __END__
↑ Summary