Kavorka/Multi.pm

filter: Statementstmt 96.6% 199 / 206 Branchbran 75.0% 69 / 92 Conditioncond 68.1% 15 / 22 MC/DCmcdc 30.7% 4 / 13 Subroutinesub 91.4% 32 / 35 Podpod n/a totaltotal 86.6% 319 / 368 CC 71 SCAR 24.3
CC
71
Cov
88%
foo
23.3
foo
23.3
__compile
7.2
CRAP 11.4
« Kavorka/Signature.pm ↑ Summary Kavorka/Sub.pm »
1 6 ▶use 5.014;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 6 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 6 ▶use Sub::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 package Kavorka::Multi;
8  
9 15 our $AUTHORITY = 'cpan:TOBYINK';
10 15 our $VERSION = '0.039';
11  
12 6 ▶use Parse::Keyword {};
Subroutine __ANON__: called CC=1 SCAR=0.0
13 6 ▶use Parse::KeywordX;
Subroutine __ANON__: called CC=1 SCAR=0.0
14  
15 6 ▶use Moo;
Subroutine __ANON__: called CC=1 SCAR=0.0
16 15 with 'Kavorka::Sub';
17 6 ▶use namespace::sweep;
Subroutine __ANON__: called CC=1 SCAR=0.0
18  
19 15 has multi_type => (is => 'ro', required => 1);
20 13 has declared_long_name => (is => 'rwp');
21 13 has qualified_long_name => (is => 'rwp');
22  
23 around parse => sub
24 13 ▶{
Subroutine __ANON__: called CC=1 SCAR=0.0
25 13 my $next = shift;
26 13 my $class = shift;
27
28 13 lex_read_space;
29 13 my $type = parse_name('keyword', 0);
30 13 lex_read_space;
31
32 13 $class->multi_parse($next, $type, @_);
33 13 };
34  
35 sub multi_parse
36 13 ▶{
Subroutine multi_parse: called CC=2 SCAR=7.1
37 13 my $class = shift;
38 13 my ($parse_method, $keyword, @args) = @_;
39
40 13 my $implementation;
41 13 ▶ if ($^H{Kavorka} =~ /\b$keyword=(\S+)/)
Branch: 1 branch
BranchTrueFalseTotal
if ($^H{'Kavorka'} =~ /\b$keyword=(\S+)/u) { } 13 0 13
42 {
43 13 $implementation = $1;
44 }
45 else
46 {
47 0 Carp::croak("Could not resolve keyword '$keyword'");
48 }
49
50 13 return $class->$parse_method(@args, multi_type => $implementation);
51 }
52  
53 after parse_attributes => sub
54 13 ▶{
Subroutine __ANON__: called CC=3 SCAR=11.0
55 13 my $self = shift;
56
57 13 my @attr = @{$self->attributes};
58
59 13 my @filtered;
60  
61 $_->[0] eq 'long'
62 ? ($self->_set_declared_long_name($_->[1]), $self->_set_qualified_long_name(scalar Kavorka::_fqname $_->[1]))
63 : push(@filtered, $_)
64 13 ▶ for @attr;
Branch: 1 branch
BranchTrueFalseTotal
$_->[0] eq 'long' ? : 1 0 1
65
66 13 @{$self->attributes} = @filtered;
67 13 };
68  
69 after parse_signature => sub
70 13 ▶{
Subroutine __ANON__: called CC=3 SCAR=11.0
71 13 my $self = shift;
72 13 my $sig = $self->signature;
73
74 13 for my $param (@{$sig->params})
75 {
76 Carp::croak("Type constraints for parameters cannot be 'assumed' in a multi sub")
77 13 ▶ if $param->traits->{assumed};
Branch: 1 branch
BranchTrueFalseTotal
if $param->traits->{"assumed"} 0 13 13
78 }
79
80 13 $self->signature->_set_nobble_checks(1);
81 9 };
82  
83 0 ▶sub allow_anonymous { 0 }
Subroutine allow_anonymous: not called CC=1 SCAR=6.9
84 0 ▶sub allow_lexical { 0 }
Subroutine allow_lexical: not called CC=1 SCAR=6.9
85  
86 sub default_attributes
87 13 ▶{
Subroutine default_attributes: called CC=1 SCAR=0.0
88 13 my $code = $_[0]->multi_type->can('default_attributes');
89 13 goto $code;
90 }
91  
92 sub default_invocant
93 13 ▶{
Subroutine default_invocant: called CC=1 SCAR=0.0
94 13 my $code = $_[0]->multi_type->can('default_invocant');
95 13 goto $code;
96 }
97  
98 sub forward_declare
99 0 ▶{
Subroutine forward_declare: not called CC=1 SCAR=6.9
100 0 my $code = $_[0]->multi_type->can('forward_declare');
101 0 goto $code;
102 }
103  
104 sub invocation_style
105 22 ▶{
Subroutine invocation_style: called CC=2 SCAR=7.6
106 22 ▶ $_[0]->multi_type->invocation_style
Branch: 1 branch
BranchTrueFalseTotal
$_[0]->multi_type->invocation_style or Carp::croak("No invocation style defined") 22 0 22
107 or Carp::croak("No invocation style defined");
108 }
109  
110 10 our %DISPATCH_TABLE;
111 10 our %DISPATCH_STYLE;
112 10 our %INVALIDATION;
113  
114 sub __gather_candidates
115 14 ▶{
Subroutine __gather_candidates: called CC=4 SCAR=13.9
116 14 my ($pkg, $subname, $args) = @_;
117
118 14 ▶ if ($DISPATCH_STYLE{$pkg}{$subname} eq 'fun')
Branch: 1 branch
BranchTrueFalseTotal
if ($Kavorka::Multi::DISPATCH_STYLE{$pkg}{$subname} eq "fun") 3 11 14
119 {
120 3 return @{$DISPATCH_TABLE{$pkg}{$subname}};
121 }
122
123 11 require mro;
124 11 ▶ my $invocant = ref($args->[0]) || $args->[0];
Condition33%1/3
ref $args->[0] || $args->[0]
l!l&&r!l&&!r
0110
MC/DC0%0/2
ref $args->[0] || $args->[0]
ref $args->[0] $args->[0]
Truth table33%1/3
A || B
A= ref $args->[0]B= $args->[0]
ABresult
1X1
011
000
125 11 ▶ return map @{$DISPATCH_TABLE{$_}{$subname} || [] }, @{ $invocant->mro::get_linear_isa };
Condition100%2/2
$Kavorka::Multi::DISPATCH_TABLE{$_}{$subname} || []
l!l
206
MC/DC100%1/1
$Kavorka::Multi::DISPATCH_TABLE{$_}{$subname} || []
$Kavorka::Multi::DISPATCH_TABLE{$_}{$subname}
Truth table100%2/2
A
A= $Kavorka::Multi::DISPATCH_TABLE{$_}{$subname}
Aresult
11
00
126 }
127  
128 sub __dispatch
129 6 ▶{
Subroutine __dispatch: called CC=3 SCAR=11.0
130 6 my ($pkg, $subname) = @{ +shift };
131
132 6 for my $c ( __gather_candidates($pkg, $subname, \@_) )
133 {
134 7 my @copy = @_;
135 7 ▶ next unless $c->signature->check(@copy);
Branch: 1 branch
BranchTrueFalseTotal
unless $c->signature->check(@copy) 1 6 7
136 6 my $body = $c->body;
137 6 goto $body;
138 }
139
140 0 Carp::croak("Arguments to $pkg\::$subname did not match any known signature for multi sub");
141 }
142  
143 sub __compile
144 8 ▶{
Subroutine __compile: called CC=7 SCAR=19.7
145 8 my ($pkg, $subname) = @_;
146
147 8 my @candidates = __gather_candidates($pkg, $subname, [$pkg]);
148 8 my @coderefs = map $_->body, @candidates;
149
150 8 my $slowpath = '';
151 8 ▶ if ($DISPATCH_STYLE{$pkg}{$subname} ne 'fun')
Branch: 1 branch
BranchTrueFalseTotal
if ($Kavorka::Multi::DISPATCH_STYLE{$pkg}{$subname} ne "fun") 5 3 8
152 {
153 5 my $this = [$pkg, $subname];
154 5 ▶ push @{ $INVALIDATION{"$_\::$subname"} ||= [] }, $this for @{ $pkg->mro::get_linear_isa };
Condition100%2/2
$Kavorka::Multi::INVALIDATION{"${_}::$subname"} ||= []
l!l
26
MC/DC100%1/1
$Kavorka::Multi::INVALIDATION{"${_}::$subname"} ||= []
$Kavorka::Multi::INVALIDATION{"${_}::$subname"}
Truth table100%2/2
A
A= $Kavorka::Multi::INVALIDATION{"${_}::$subname"}
Aresult
11
00
155
156 5 $slowpath = sprintf(
157 'if ((ref($_[0]) || $_[0]) ne %s) { unshift @_, [%s, %s]; goto \\&Kavorka::Multi::__dispatch }',
158 B::perlstring($pkg),
159 B::perlstring($pkg),
160 B::perlstring($subname),
161 );
162 }
163
164 my $compiled = join q[] => (
165 map {
166 8 my $sig = $candidates[$_]->signature;
167 17 ▶ $sig && $sig->nobble_checks ? sprintf(
Branch: 2 branches
BranchTrueFalseTotal
$sig && $sig->nobble_checks ? : 17 0 17
$sig ? : 0 0 0
Condition33%1/3
$sig and $sig->nobble_checks
!ll&&!rl&&r
0017
MC/DC0%0/2
$sig and $sig->nobble_checks
$sig $sig->nobble_checks
Truth table33%1/3
A and B
A= $sigB= $sig->nobble_checks
ABresult
0X0
100
111
168 "\@tmp = \@_; if (%s) { unshift \@_, \$Kavorka::Signature::NOBBLE; goto \$coderefs[%d] }\n",
169 $candidates[$_]->signature->inline_check('@tmp'),
170 $_,
171 ) :
172 $sig ? sprintf(
173 "\@tmp = \@_; if (%s) { goto \$coderefs[%d] }\n",
174 $candidates[$_]->signature->inline_check('@tmp'),
175 $_,
176 ) : sprintf('goto \$coderefs[%d];', $_);
177 } 0 .. $#candidates,
178 );
179
180 8 my $error = "Carp::croak(qq/Arguments to $pkg\::$subname did not match any known signature for multi sub/);";
181
182 0 ▶ Sub::Util::set_subname(
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Branch: 34 branches
BranchTrueFalseTotal
if (do { local(@_) = @tmp; eval { do { Carp::croak("Expected 1 parameter") if @_ != 1; my $z = $#_ >= 0 ? $_[0] : Carp::croak("Positional parameter 0 is required"); if ($#_ >= 0) { do { package Type::Tiny; BEGIN {${^WARNING_BITS} = "\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x51\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55"} ref $z eq "SCALAR" or ref $z eq "REF" or _failed_check(28, "ScalarRef", $z, "exception_class", "Error::TypeTiny::Assertion"); $z }; } ; (); 1 } } }) 2 0 2
if @_ != 1 1 1 2
$#_ >= 0 ? : 7 8 15
if ($#_ >= 0) 2 7 9
ref $z eq "SCALAR" or ref $z eq "REF" or _failed_check(28, "ScalarRef", $z, "exception_class", "Error::TypeTiny::Assertion") 9 0 9
if ((ref $_[0] || $_[0]) ne "Example") 3 6 9
if (do { local(@_) = @tmp; eval { do { my $self = @_ ? shift @_ : Carp::croak("Invocant \$self is required"); Carp::croak("Expected 1 parameter") if @_ != 1; my $x = $#_ >= 0 ? $_[0] : Carp::croak("Positional parameter 0 is required"); if ($#_ >= 0) { do { package Type::Tiny; BEGIN {${^WARNING_BITS} = "\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x51\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55"} Ref::Util::XS::is_plain_hashref($x) or _failed_check(27, "HashRef", $x, "exception_class", "Error::TypeTiny::Assertion"); $x }; } ; (); 1 } } }) 7 2 9
@_ ? : 7 1 8
if @_ != 1 2 6 8
$#_ >= 0 ? : 4 2 6
if ($#_ >= 0) 6 0 6
Ref::Util::XS::is_plain_hashref($x) or _failed_check(27, "HashRef", $x, "exception_class", "Error::TypeTiny::Assertion") 1 5 6
if (do { local(@_) = @tmp; eval { do { my $self = @_ ? shift @_ : Carp::croak("Invocant \$self is required"); Carp::croak("Expected 1 parameter") if @_ != 1; my $y = $#_ >= 0 ? $_[0] : Carp::croak("Positional parameter 0 is required"); if ($#_ >= 0) { do { package Type::Tiny; BEGIN {${^WARNING_BITS} = "\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x51\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55"} Ref::Util::XS::is_plain_arrayref($y) or _failed_check(26, "ArrayRef", $y, "exception_class", "Error::TypeTiny::Assertion"); $y }; } ; (); 1 } } }) 5 0 5
@_ ? : 4 1 5
if @_ != 1 3 2 5
$#_ >= 0 ? : 2 3 5
if ($#_ >= 0) 3 2 5
Ref::Util::XS::is_plain_arrayref($y) or _failed_check(26, "ArrayRef", $y, "exception_class", "Error::TypeTiny::Assertion") 3 1 4
if ((ref $_[0] || $_[0]) ne "Example2") 1 3 4
if (do { local(@_) = @tmp; eval { do { my $self = @_ ? shift @_ : Carp::croak("Invocant \$self is required"); Carp::croak("Expected 1 parameter") if @_ != 1; my $z = $#_ >= 0 ? $_[0] : Carp::croak("Positional parameter 0 is required"); if ($#_ >= 0) { do { package Type::Tiny; BEGIN {${^WARNING_BITS} = "\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x51\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55"} ref $z eq "SCALAR" or ref $z eq "REF" or _failed_check(28, "ScalarRef", $z, "exception_class", "Error::TypeTiny::Assertion"); $z }; } ; (); 1 } } }) 4 0 4
@_ ? : 4 0 4
if @_ != 1 1 2 3
$#_ >= 0 ? : 1 1 2
if ($#_ >= 0) 2 0 2
ref $z eq "SCALAR" or ref $z eq "REF" or _failed_check(28, "ScalarRef", $z, "exception_class", "Error::TypeTiny::Assertion") 0 2 2
if (do { local(@_) = @tmp; eval { do { my $self = @_ ? shift @_ : Carp::croak("Invocant \$self is required"); Carp::croak("Expected 1 parameter") if @_ != 1; my $x = $#_ >= 0 ? $_[0] : Carp::croak("Positional parameter 0 is required"); if ($#_ >= 0) { do { package Type::Tiny; BEGIN {${^WARNING_BITS} = "\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x51\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55"} Ref::Util::XS::is_plain_hashref($x) or _failed_check(27, "HashRef", $x, "exception_class", "Error::TypeTiny::Assertion"); $x }; } ; (); 1 } } }) 2 0 2
@_ ? : 2 0 2
if @_ != 1 1 1 2
$#_ >= 0 ? : 1 0 1
if ($#_ >= 0) 1 0 1
Ref::Util::XS::is_plain_hashref($x) or _failed_check(27, "HashRef", $x, "exception_class", "Error::TypeTiny::Assertion") 0 1 1
if (do { local(@_) = @tmp; eval { do { my $self = @_ ? shift @_ : Carp::croak("Invocant \$self is required"); Carp::croak("Expected 1 parameter") if @_ != 1; my $y = $#_ >= 0 ? $_[0] : Carp::croak("Positional parameter 0 is required"); if ($#_ >= 0) { do { package Type::Tiny; BEGIN {${^WARNING_BITS} = "\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x51\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55"} Ref::Util::XS::is_plain_arrayref($y) or _failed_check(26, "ArrayRef", $y, "exception_class", "Error::TypeTiny::Assertion"); $y }; } ; (); 1 } } }) 1 0 1
@_ ? : 1 0 1
if @_ != 1 1 0 1
Condition66%6/9
ref $z eq "SCALAR" or ref $z eq "REF"
l!l&&r!l&&!r
1120
ref $_[0] || $_[0]
l!l&&r!l&&!r
130
ref $_[0] || $_[0]
l!l&&r!l&&!r
102
MC/DC0%0/2
ref $z eq "SCALAR" or ref $z eq "REF"
ref $z eq "SCALAR" ref $z eq "REF"
MC/DC0%0/2
ref $_[0] || $_[0]
ref $_[0] $_[0]
MC/DC50%1/2
ref $_[0] || $_[0]
ref $_[0] $_[0]
Truth table66%2/3
A or B
A= ref $z eq "SCALAR"B= ref $z eq "REF"
ABresult
1X1
011
000
Truth table66%2/3
A || B
A= ref $_[0]B= $_[0]
ABresult
1X1
011
000
Truth table66%2/3
A || B
A= ref $_[0]B= $_[0]
ABresult
1X1
011
000
183 "$pkg\::$subname",
184 eval("package $pkg; sub { $slowpath; my \@tmp; $compiled; $error }"),
185 );
186 }
187  
188 sub __defer_compile
189 16 ▶{
Subroutine __defer_compile: called CC=1 SCAR=0.0
190 16 my ($pkg, $subname) = @_;
191 return Sub::Util::set_subname(
192 22 ▶ "$pkg\::$subname" => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
193 6 ▶ no strict "refs";
Subroutine __ANON__: called CC=1 SCAR=0.0
194 6 ▶ no warnings "redefine";
Subroutine __ANON__: called CC=1 SCAR=0.0
195 9 *{"$pkg\::$subname"} = (my $compiled = __compile($pkg, $subname));
196 9 goto $compiled;
197 },
198 16 );
199 }
200  
201 sub install_sub
202 16 ▶{
Subroutine install_sub: called CC=6 SCAR=17.9
203 15 my $self = shift;
204 15 my ($pkg, $subname) = ($self->qualified_name =~ /^(.+)::(\w+)$/);
205
206 15 ▶ unless ($DISPATCH_TABLE{$pkg}{$subname})
Branch: 1 branch
BranchTrueFalseTotal
unless ($Kavorka::Multi::DISPATCH_TABLE{$pkg}{$subname}) 10 5 15
207 {
208 11 $DISPATCH_TABLE{$pkg}{$subname} = [];
209 11 $DISPATCH_STYLE{$pkg}{$subname} = $self->invocation_style;
210 }
211
212 14 ▶ $DISPATCH_STYLE{$pkg}{$subname} eq $self->invocation_style
Branch: 1 branch
BranchTrueFalseTotal
$Kavorka::Multi::DISPATCH_STYLE{$pkg}{$subname} eq $self->invocation_style or Carp::croak("Two different invocation styles used for $subname") 13 2 15
213 or Carp::croak("Two different invocation styles used for $subname");
214
215 {
216 # A placeholder dispatcher that will replace itself with a more
217 # efficient optimized (compiled) dispatcher.
218 6 ▶ no strict "refs";
Subroutine __ANON__: called CC=1 SCAR=0.0
219 6 ▶ no warnings "redefine";
Subroutine __ANON__: called CC=1 SCAR=0.0
220 14 *{"$pkg\::$subname"} = __defer_compile($pkg, $subname);
221
222 # Invalidate previously optimized dispatchers in subclasses of $pkg
223 16 *{join '::', @$_} = __defer_compile(@$_)
224 14 ▶ for @{ delete($INVALIDATION{"$pkg\::$subname"}) || [] };
Condition100%3/3
delete $Kavorka::Multi::INVALIDATION{"${pkg}::$subname"} || []
l!l
2121
MC/DC100%1/1
delete $Kavorka::Multi::INVALIDATION{"${pkg}::$subname"} || []
delete $Kavorka::Multi::INVALIDATION{"${pkg}::$subname"}
Truth table100%2/2
A
A= delete $Kavorka::Multi::INVALIDATION{"${pkg}::$subname"}
Aresult
11
00
225 }
226
227 22 my $long = $self->qualified_long_name;
228 22 ▶ if (defined $long)
Branch: 1 branch
BranchTrueFalseTotal
if (defined $long) 3 12 15
229 {
230 6 ▶ no strict 'refs';
Subroutine __ANON__: called CC=1 SCAR=0.0
231 10 *$long = $self->body;
232 }
233
234 22 push @{ $DISPATCH_TABLE{$pkg}{$subname} }, $self;
235 }
236  
237 10 1;
« Kavorka/Signature.pm ↑ Summary Kavorka/Sub.pm »