Kavorka/Parameter.pm

filter: Statementstmt 94.4% 289 / 306 Branchbran 64.5% 102 / 158 Conditioncond 68.1% 45 / 66 MC/DCmcdc 33.3% 12 / 36 Subroutinesub 85.0% 34 / 40 Podpod 92.8% 13 / 14 totaltotal 79.8% 495 / 620 CC 107 SCAR 30.1
CC
107
Cov
82%
parse
56.6
_injection_extract_value
19.1
sanity_check
14.1
CRAP 20.3
↑ Summary Kavorka/Signature.pm »
1 38 ▶use 5.014;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 38 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 38 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 package Kavorka::Parameter;
6  
7 38 our $AUTHORITY = 'cpan:TOBYINK';
8 38 our $VERSION = '0.039';
9 38 our @CARP_NOT = qw( Kavorka::Signature Kavorka::Sub Kavorka );
10  
11 38 ▶use Carp qw( croak );
Subroutine __ANON__: called CC=1 SCAR=0.0
12 38 ▶use Text::Balanced qw( extract_codeblock extract_bracketed );
Subroutine __ANON__: called CC=1 SCAR=0.0
13 38 ▶use Parse::Keyword {};
Subroutine __ANON__: called CC=1 SCAR=0.0
14 38 ▶use Parse::KeywordX;
Subroutine __ANON__: called CC=1 SCAR=0.0
15  
16 38 ▶use Moo;
Subroutine __ANON__: called CC=1 SCAR=0.0
17 38 ▶use namespace::sweep;
Subroutine __ANON__: called CC=1 SCAR=0.0
18  
19 38 has package => (is => 'ro');
20 38 has type => (is => 'ro');
21 38 has name => (is => 'ro');
22 76 ▶has constraints => (is => 'ro', default => sub { +[] });
Subroutine __ANON__: called CC=1 SCAR=0.0
23 76 ▶has named => (is => 'ro', default => sub { 0 });
Subroutine __ANON__: called CC=1 SCAR=0.0
24 76 ▶has named_names => (is => 'ro', default => sub { +[] });
Subroutine __ANON__: called CC=1 SCAR=0.0
25  
26 38 has position => (is => 'rwp');
27 38 has default => (is => 'ro');
28 38 has default_when => (is => 'ro');
29 38 has ID => (is => 'rwp');
30 0 ▶has traits => (is => 'ro', default => sub { +{} });
Subroutine __ANON__: not called CC=1 SCAR=1.2
31  
32 61 ▶has sigil => (is => 'lazy', builder => sub { substr(shift->name, 0, 1) });
Subroutine __ANON__: called CC=1 SCAR=0.0
Subroutine _build_sigil: called CC=1 SCAR=0.0
33 38 has kind => (is => 'lazy', builder => 1);
34  
35 0 ▶sub readonly { !!shift->traits->{ro} }
Subroutine readonly: not called CC=1 SCAR=6.9
Pod documented
36 0 ▶sub ro { !!shift->traits->{ro} }
Subroutine ro: not called CC=1 SCAR=6.9
Pod documented
37 0 ▶sub rw { !shift->traits->{ro} }
Subroutine rw: not called CC=1 SCAR=6.9
Pod documented
38 6 ▶sub alias { !!shift->traits->{alias} }
Subroutine alias: called CC=1 SCAR=0.0
Pod documented
39 0 ▶sub copy { !shift->traits->{alias} }
Subroutine copy: not called CC=1 SCAR=6.9
Pod documented
40 1993 ▶sub slurpy { !!shift->traits->{slurpy} }
Subroutine slurpy: called CC=1 SCAR=0.0
Pod documented
41 264 ▶sub optional { !!shift->traits->{optional} }
Subroutine optional: called CC=1 SCAR=0.0
Pod documented
42 2365 ▶sub invocant { !!shift->traits->{invocant} }
Subroutine invocant: called CC=1 SCAR=0.0
Pod documented
43 301 ▶sub coerce { !!shift->traits->{coerce} }
Subroutine coerce: called CC=1 SCAR=0.0
Pod documented
44 0 ▶sub locked { !!shift->traits->{locked} }
Subroutine locked: not called CC=1 SCAR=6.9
Pod documented
45  
46 38 our @PARAMS;
47 sub BUILD
48 269 ▶{
Subroutine BUILD: called CC=12 SCAR=25.6
Pod undocumented
49 269 my $self = shift;
50 269 my $id = scalar(@PARAMS);
51 269 $self->_set_ID($id);
52 269 $PARAMS[$id] = $self;
53
54 269 my $traits = $self->traits;
55
56 exists($traits->{rw})
57 and !exists($traits->{ro})
58 269 ▶ and ($traits->{ro} = !$traits->{rw});
Branch: 1 branch
BranchTrueFalseTotal
exists $traits->{"rw"} and not exists $traits->{"ro"} and $traits->{"ro"} = !$traits->{"rw"} 1 268 269
Condition66%2/3
exists $traits->{"rw"} and not exists $traits->{"ro"}
!ll&&!rl&&r
26801
MC/DC50%1/2
exists $traits->{"rw"} and not exists $traits->{"ro"}
exists $traits->{"rw"} not exists $traits->{"ro"}
Truth table66%2/3
A and not(B)
A= exists $traits->{"rw"}B= not exists $traits->{"ro"}
ABresult
0X0
100
111
59
60 exists($traits->{ro})
61 and !exists($traits->{rw})
62 269 ▶ and ($traits->{rw} = !$traits->{ro});
Branch: 1 branch
BranchTrueFalseTotal
exists $traits->{"ro"} and not exists $traits->{"rw"} and $traits->{"rw"} = !$traits->{"ro"} 1 268 269
Condition100%3/3
exists $traits->{"ro"} and not exists $traits->{"rw"}
!ll&&!rl&&r
26711
MC/DC100%2/2
exists $traits->{"ro"} and not exists $traits->{"rw"}
exists $traits->{"ro"} not exists $traits->{"rw"}
Truth table100%3/3
A and not(B)
A= exists $traits->{"ro"}B= not exists $traits->{"rw"}
ABresult
0X0
100
111
63
64 exists($traits->{copy})
65 and !exists($traits->{alias})
66 269 ▶ and ($traits->{alias} = !$traits->{copy});
Branch: 1 branch
BranchTrueFalseTotal
exists $traits->{"copy"} and not exists $traits->{"alias"} and $traits->{"alias"} = !$traits->{"copy"} 0 269 269
Condition33%1/3
exists $traits->{"copy"} and not exists $traits->{"alias"}
!ll&&!rl&&r
26900
MC/DC0%0/2
exists $traits->{"copy"} and not exists $traits->{"alias"}
exists $traits->{"copy"} not exists $traits->{"alias"}
Truth table33%1/3
A and not(B)
A= exists $traits->{"copy"}B= not exists $traits->{"alias"}
ABresult
0X0
100
111
67
68 exists($traits->{alias})
69 and !exists($traits->{copy})
70 269 ▶ and ($traits->{copy} = !$traits->{alias});
Branch: 1 branch
BranchTrueFalseTotal
exists $traits->{"alias"} and not exists $traits->{"copy"} and $traits->{"copy"} = !$traits->{"alias"} 0 269 269
Condition33%1/3
exists $traits->{"alias"} and not exists $traits->{"copy"}
!ll&&!rl&&r
26900
MC/DC0%0/2
exists $traits->{"alias"} and not exists $traits->{"copy"}
exists $traits->{"alias"} not exists $traits->{"copy"}
Truth table33%1/3
A and not(B)
A= exists $traits->{"alias"}B= not exists $traits->{"copy"}
ABresult
0X0
100
111
71
72 269 ▶ $traits->{$_} || delete($traits->{$_}) for keys %$traits;
Branch: 1 branch
BranchTrueFalseTotal
$traits->{$_} or delete $traits->{$_} 160 353 513
73
74 # traits handled natively
75 269 state $native_traits = {
76 coerce => 1,
77 copy => 1,
78 invocant => 1,
79 rw => 1,
80 slurpy => 1,
81 };
82
83 my @custom_traits =
84 map "Kavorka::TraitFor::Parameter::$_",
85 269 grep !exists($native_traits->{$_}),
86 keys %$traits;
87
88 269 ▶ 'Moo::Role'->apply_roles_to_object($self, @custom_traits) if @custom_traits;
Branch: 1 branch
BranchTrueFalseTotal
if @custom_traits 42 227 269
89 }
90  
91 sub _build_kind
92 267 ▶{
Subroutine _build_kind: called CC=3 SCAR=11.0
93 267 my $self = shift;
94 267 local $_ = $self->name;
95 267 ▶ /::/ ? 'global' : /\A[\$\@\%](?:\W|_\z)/ ? 'magic' : 'my';
Branch: 2 branches
BranchTrueFalseTotal
/::/u ? : 1 266 267
/\A[\$\@\%](?:\W|_\z)/u ? : 9 257 266
96 }
97  
98 38 my $variable_re = qr{ [\$\%\@] (?: \{\^[A-Z]+\} | \w* ) }x;
99  
100 sub parse
101 193 ▶{
Subroutine parse: called CC=34 SCAR=40.4
Pod documented
102 193 state $deparse = do { require B::Deparse; 'B::Deparse'->new };
103
104 193 my $class = shift;
105 193 my %args = @_;
106
107 193 lex_read_space;
108
109 193 my %traits = (
110 invocant => 0,
111 _optional => 1,
112 );
113
114 193 ▶ if (lex_peek(6) eq 'slurpy')
Branch: 1 branch
BranchTrueFalseTotal
if (lex_peek(6) eq "slurpy") 0 0 0
115 {
116 13 lex_read(6);
117 13 lex_read_space;
118 13 $traits{slurpy} = 1;
119 }
120
121 193 my $type;
122 193 my $peek = lex_peek(1000);
123 193 ▶ if ($peek =~ /\A[^\W0-9]/ and not $peek =~ /\A(my|our)\b/)
Branch: 2 branches
BranchTrueFalseTotal
if ($peek =~ /\A[^\W0-9]/u and not $peek =~ /\A(my|our)\b/u) { } 60 133 193
elsif ($peek =~ /\A\(/u) { } 9 124 133
Condition100%3/3
$peek =~ /\A[^\W0-9]/u and not $peek =~ /\A(my|our)\b/u
!ll&&!rl&&r
131260
MC/DC100%2/2
$peek =~ /\A[^\W0-9]/u and not $peek =~ /\A(my|our)\b/u
$peek =~ /\A[^\W0-9]/u not $peek =~ /\A(my|our)\b/u
Truth table100%3/3
A and not(B)
A= $peek =~ /\A[^\W0-9]/uB= not $peek =~ /\A(my|our)\b/u
ABresult
0X0
100
111
124 {
125 60 my $reg = do {
126 60 require Type::Registry;
127 60 require Type::Utils;
128 60 my $tmp = 'Type::Registry::DWIM'->new;
129 60 $tmp->{'~~chained'} = $args{package};
130 60 $tmp->{'~~assume'} = 'make_class_type';
131 60 $tmp;
132 };
133
134 60 require Type::Parser;
135 60 ($type, my($remaining)) = Type::Parser::extract_type($peek, $reg);
136 60 my $len = length($peek) - length($remaining);
137 60 lex_read($len);
138 60 lex_read_space;
139 }
140 elsif ($peek =~ /\A\(/)
141 {
142 9 lex_read(1);
143 9 lex_read_space;
144 9 ▶ my $expr = parse_listexpr
Branch: 1 branch
BranchTrueFalseTotal
my $expr = parse_listexpr() or croak("Could not parse type constraint expression as listexpr") 0 0 0
145 or croak('Could not parse type constraint expression as listexpr');
146 9 lex_read_space;
147 9 ▶ lex_peek eq ')'
Branch: 1 branch
BranchTrueFalseTotal
lex_peek() eq ")" or croak("Expected ')' after type constraint expression") 0 0 0
148 or croak("Expected ')' after type constraint expression");
149 9 lex_read(1);
150 9 lex_read_space;
151
152 9 require Types::TypeTiny;
153 9 $type = Types::TypeTiny::to_TypeTiny( scalar $expr->() );
154 9 ▶ $type->isa('Type::Tiny')
Branch: 1 branch
BranchTrueFalseTotal
$type->isa("Type::Tiny") or croak("Type constraint expression did not return a blessed type constraint object") 9 0 9
155 or croak("Type constraint expression did not return a blessed type constraint object");
156 }
157
158 193 my ($named, $parens, $varname, $varkind, @paramname) = (0, 0);
159
160 # :foo( ... )
161 193 ▶ if (lex_peek(2) =~ /\A\:\w/)
Branch: 1 branch
BranchTrueFalseTotal
if (lex_peek(2) =~ /\A\:\w/u) 0 0 0
162 {
163 5 $named = 2;
164 5 $traits{_optional} = 1;
165 5 ▶ while (lex_peek(2) =~ /\A\:\w/)
Branch: 1 branch
BranchTrueFalseTotal
if (lex_peek(2) =~ /\A\:\w/u) 0 0 0
166 {
167 13 lex_read(1);
168 13 push @paramname, parse_name('named parameter name', 0);
169 13 ▶ if (lex_peek eq '(')
Branch: 1 branch
BranchTrueFalseTotal
if (lex_peek() eq "(") 0 0 0
170 {
171 7 lex_read(1);
172 7 $parens++;
173 }
174 13 lex_read_space;
175 }
176 }
177
178 # Allow colon before "my"/"our" - just shift it to the correct position
179 193 my $saw_colon;
180 193 ▶ if (lex_peek eq ':')
Branch: 1 branch
BranchTrueFalseTotal
if (lex_peek() eq ":") 0 0 0
181 {
182 16 $saw_colon++;
183 16 lex_read(1);
184 16 lex_read_space;
185 }
186
187 193 ▶ if (lex_peek eq '\\')
Branch: 1 branch
BranchTrueFalseTotal
if (lex_peek() eq "\\") 0 0 0
188 {
189 0 $traits{ref_alias} = 1;
190 0 lex_read(1);
191 0 lex_read_space;
192 }
193
194 193 ▶ if (lex_peek(3) =~ /\A(my|our)/)
Branch: 1 branch
BranchTrueFalseTotal
if (lex_peek(3) =~ /\A(my|our)/u) 0 0 0
195 {
196 2 $varkind = $1;
197 2 lex_read(length $varkind);
198 2 lex_read_space;
199 }
200
201 193 ▶ if (lex_peek eq '\\')
Branch: 1 branch
BranchTrueFalseTotal
if (lex_peek() eq "\\") 0 0 0
202 {
203 0 ▶ croak("cannot be a double-ref-alias") if $traits{ref_alias}++;
Branch: 1 branch
BranchTrueFalseTotal
if $traits{"ref_alias"}++ 0 0 0
204 0 lex_read(1);
205 0 lex_read_space;
206 }
207
208 193 ▶ lex_stuff(':') if $saw_colon; # re-insert colon
Branch: 1 branch
BranchTrueFalseTotal
if $saw_colon 16 177 193
209 193 $peek = lex_peek;
210
211 # :$foo
212 193 ▶ if ($peek eq ':')
Branch: 2 branches
BranchTrueFalseTotal
if ($peek eq ':') { } 16 177 193
elsif ($peek eq '$' or $peek eq '@' or $peek eq '%') { } 177 0 177
213 {
214 16 lex_read(1);
215 16 lex_read_space;
216 16 $varname = parse_variable;
217 16 $named = 1;
218 16 $traits{_optional} = 1;
219 16 push @paramname, substr($varname, 1);
220 16 lex_read_space;
221 }
222 # $foo
223 ▶ elsif ($peek eq '$' or $peek eq '@' or $peek eq '%')
Condition83%5/6
$peek eq "\$" or $peek eq "\@" or $peek eq "%"
l!l&&r!l&&!r
167100
$peek eq "\$" or $peek eq "\@"
l!l&&r!l&&!r
1551210
MC/DC0%0/3
$peek eq "\$" or $peek eq "\@" or $peek eq "%"
$peek eq "\$" $peek eq "\@" $peek eq "%"
Truth table75%3/4
A or B or C
A= $peek eq "\$"B= $peek eq "\@"C= $peek eq "%"
ABCresult
1XX1
01X1
0011
0000
224 {
225 177 $varname = parse_variable(1);
226 177 ▶ $traits{_optional} = 0 unless @paramname;
Branch: 1 branch
BranchTrueFalseTotal
unless @paramname 174 3 177
227 177 lex_read_space;
228 }
229
230 193 undef($peek);
231
232 193 for (1 .. $parens)
233 {
234 7 ▶ lex_peek(1) eq ')'
Branch: 1 branch
BranchTrueFalseTotal
lex_peek(1) eq ')' ? : 0 0 0
235 ? lex_read(1)
236 : croak("Expected close parentheses after named parameter name");
237 7 lex_read_space;
238 }
239
240 193 ▶ if (lex_peek eq '!')
Branch: 2 branches
BranchTrueFalseTotal
if (lex_peek() eq '!') { } 0 0 0
elsif (lex_peek() eq '?') { } 0 0 0
241 {
242 1 $traits{optional} = 0;
243 1 lex_read(1);
244 1 lex_read_space;
245 }
246 elsif (lex_peek eq '?')
247 {
248 4 $traits{optional} = 1;
249 4 lex_read(1);
250 4 lex_read_space;
251 }
252
253 193 my (@constraints, $default, $default_when);
254
255 193 ▶ while (lex_peek(5) eq 'where')
Branch: 1 branch
BranchTrueFalseTotal
if (lex_peek(5) eq "where") 0 0 0
256 {
257 4 lex_read(5);
258 4 lex_read_space;
259 4 push @constraints, parse_block_or_match;
260 4 lex_read_space;
261 }
262
263 193 ▶ while (lex_peek(5) =~ m{ \A (is|does|but) \s }xsm)
Branch: 1 branch
BranchTrueFalseTotal
if (lex_peek(5) =~ / \A (is|does|but) \s /msux) 0 0 0
264 {
265 14 lex_read(length($1));
266 14 lex_read_space;
267 14 my ($name, undef, $args) = parse_trait;
268 14 $traits{$name} = $args;
269 14 lex_read_space;
270 }
271
272 193 ▶ if (lex_peek(5) =~ m{ \A ( (?: [/]{2} | [|]{2} )?= ) }x)
Branch: 1 branch
BranchTrueFalseTotal
if (lex_peek(5) =~ m[ \A ( (?: [/]{2} | [|]{2} )?= ) ]ux) 0 0 0
273 {
274 13 $default_when = $1;
275 13 lex_read(length($1));
276 13 lex_read_space;
277 $default = lex_peek(5) =~ m{ \A (?: when\b | [,)] ) }x
278 3 ▶ ? sub { (); }
Subroutine __ANON__: called CC=1 SCAR=0.0
279 13 ▶ : parse_arithexpr;
Branch: 1 branch
BranchTrueFalseTotal
lex_peek(5) =~ / \A (?: when\b | [,)] ) /ux ? : 0 0 0
280 13 lex_read_space;
281 13 $traits{_optional} = 1;
282 }
283
284 193 ▶ $traits{optional} //= $traits{_optional};
Condition66%2/3
$traits{"optional"} //= $traits{"_optional"}
l!l&&r!l&&!r
51880
MC/DC0%0/2
$traits{"optional"} //= $traits{"_optional"}
$traits{"optional"} $traits{"_optional"}
Truth table66%2/3
A //= B
A= $traits{"optional"}B= $traits{"_optional"}
ABresult
1X1
011
000
285 193 delete($traits{_optional});
286
287 $traits{slurpy} = 1
288 if defined($varname)
289 && !$traits{ref_alias}
290 193 ▶ && $varname =~ /\A[\@\%]/;
Branch: 1 branch
BranchTrueFalseTotal
if defined $varname and not $traits{"ref_alias"} and $varname =~ /\A[\@\%]/u 22 171 193
Condition50%3/6
defined $varname and not $traits{"ref_alias"} and $varname =~ /\A[\@\%]/u
!ll&&!rl&&r
017122
defined $varname and not $traits{"ref_alias"}
!ll&&!rl&&r
00193
MC/DC33%1/3
defined $varname and not $traits{"ref_alias"} and $varname =~ /\A[\@\%]/u
defined $varname not $traits{"ref_alias"} $varname =~ /\A[\@\%]/u
Truth table50%2/4
A and not(B) and C
A= defined $varnameB= not $traits{"ref_alias"}C= $varname =~ /\A[\@\%]/u
ABCresult
0XX0
10X0
1100
1111
291
292 193 return $class->new(
293 %args,
294 type => $type,
295 name => $varname,
296 constraints => \@constraints,
297 named => !!$named,
298 named_names => \@paramname,
299 default => $default,
300 default_when => $default_when,
301 traits => \%traits,
302 ((kind => $varkind) x!!(defined $varkind)),
303 );
304 }
305  
306 sub sanity_check
307 193 ▶{
Subroutine sanity_check: called CC=12 SCAR=26.4
Pod documented
308 193 my $self = shift;
309
310 193 my $traits = $self->traits;
311 193 my $name = $self->name;
312
313 193 ▶ if ($self->named)
Branch: 1 branch
BranchTrueFalseTotal
if ($self->named) 19 174 193
314 {
315 length($_) || croak("Bad name for parameter $name")
316 19 ▶ for @{ $self->named_names or die };
Branch: 1 branch
BranchTrueFalseTotal
length $_ or croak("Bad name for parameter $name") 29 0 29
Condition50%1/2
$self->named_names || die()
l!l
190
MC/DC0%0/1
$self->named_names || die()
$self->named_names
Truth table50%1/2
A
A= $self->named_names
Aresult
11
00
317
318 19 ▶ croak("Bad parameter $name") if $self->invocant;
Branch: 1 branch
BranchTrueFalseTotal
if $self->invocant 0 19 19
319 19 ▶ croak("Bad parameter $name") if $self->slurpy;
Branch: 1 branch
BranchTrueFalseTotal
if $self->slurpy 0 19 19
320 }
321
322 193 ▶ if ($self->kind eq 'my')
Branch: 1 branch
BranchTrueFalseTotal
if ($self->kind eq 'my') { } 182 11 193
323 {
324 182 ▶ croak("Bad name for lexical variable: $name") if $name =~ /(::|\^)/;
Branch: 1 branch
BranchTrueFalseTotal
if $name =~ /(::|\^)/u 0 182 182
325 }
326 else
327 {
328 11 ▶ croak("Bad name for package variable: $name") if length($name) < 2;
Branch: 1 branch
BranchTrueFalseTotal
if length $name < 2 0 11 11
329 }
330
331 193 ▶ croak("Bad parameter $name") if $self->invocant && $self->slurpy;
Branch: 1 branch
BranchTrueFalseTotal
if $self->invocant and $self->slurpy 0 193 193
Condition66%2/3
$self->invocant and $self->slurpy
!ll&&!rl&&r
18670
MC/DC0%0/2
$self->invocant and $self->slurpy
$self->invocant $self->slurpy
Truth table66%2/3
A and B
A= $self->invocantB= $self->slurpy
ABresult
0X0
100
111
332 }
333  
334 sub injection
335 301 ▶{
Subroutine injection: called CC=3 SCAR=11.0
Pod documented
336 301 my $self = shift;
337 301 my ($sig) = @_;
338
339 301 my $var = $self->name;
340 301 my $is_dummy = 0;
341 301 ▶ if (length($var) == 1)
Branch: 1 branch
BranchTrueFalseTotal
if (length $var == 1) 8 293 301
342 {
343 8 $var .= 'tmp';
344 8 $is_dummy = 1;
345 }
346
347 301 my ($val, $condition) = $self->_injection_extract_and_coerce_value($sig);
348
349 301 my $code = $self->_injection_assignment($sig, $var, $val)
350 . $self->_injection_conditional_type_check($sig, $condition, $var);
351
352 301 ▶ $is_dummy ? "{ $code }" : $code;
Branch: 1 branch
BranchTrueFalseTotal
$is_dummy ? : 8 293 301
353 }
354  
355 sub _injection_assignment
356 301 ▶{
Subroutine _injection_assignment: called CC=3 SCAR=11.0
357 301 my $self = shift;
358 301 my ($sig, $var, $val) = @_;
359 301 my $kind = $self->kind;
360
361 301 ▶ sprintf(
Branch: 2 branches
BranchTrueFalseTotal
$kind eq 'our' ? : 1 300 301
$kind eq 'my' ? : 290 10 300
362 '%s %s = %s;',
363 (
364 $kind eq 'our' ? "our $var; local" :
365 $kind eq 'my' ? 'my' :
366 'local'
367 ),
368 $var,
369 $val,
370 );
371 }
372  
373 sub _injection_conditional_type_check
374 301 ▶{
Subroutine _injection_conditional_type_check: called CC=6 SCAR=17.9
375 301 my $self = shift;
376 301 my ($sig, $condition, $var) = @_;
377
378 301 my $sigil = $self->sigil;
379 301 ▶ my $type =
Branch: 3 branches
BranchTrueFalseTotal
$sigil eq '@' ? : 12 289 301
$sigil eq '%' ? : 10 279 289
$condition eq '1' ? : 122 157 279
380 ($sigil eq '@') ? sprintf('for (%s) { %s }', $var, $self->_injection_type_check('$_')) :
381 ($sigil eq '%') ? sprintf('for (values %s) { %s }', $var, $self->_injection_type_check('$_')) :
382 ($condition eq '1') ? sprintf('%s;', $self->_injection_type_check($var)) :
383 sprintf('if (%s) { %s }', $condition, $self->_injection_type_check($var));
384
385 301 ▶ return '' if $type =~ /\{ \}\z/;
Branch: 1 branch
BranchTrueFalseTotal
if $type =~ /\{ \}\z/u 106 195 301
386
387 195 ▶ return sprintf(
Branch: 1 branch
BranchTrueFalseTotal
if $sig->nobble_checks 25 170 195
388 'unless ($____nobble_checks) { %s };',
389 $type,
390 ) if $sig->nobble_checks;
391
392 170 return $type;
393 }
394  
395 sub _injection_extract_and_coerce_value
396 301 ▶{
Subroutine _injection_extract_and_coerce_value: called CC=10 SCAR=26.4
397 301 my $self = shift;
398 301 my ($sig) = @_;
399
400 301 ▶ $self->coerce
Branch: 1 branch
BranchTrueFalseTotal
$self->coerce or return $self->_injection_extract_value(@_) 6 295 301
401 or return $self->_injection_extract_value(@_);
402
403 6 ▶ my $type = $self->type
Branch: 1 branch
BranchTrueFalseTotal
my $type = $self->type or croak("Parameter ${\$self->name;} cannot coerce without a type constraint") 6 0 6
404 0 or croak("Parameter ${\ $self->name } cannot coerce without a type constraint");
405 6 ▶ $type->has_coercion
Branch: 1 branch
BranchTrueFalseTotal
$type->has_coercion or croak("Parameter ${\$self->name;} cannot coerce because type constraint has no coercions defined") 6 0 6
406 0 or croak("Parameter ${\ $self->name } cannot coerce because type constraint has no coercions defined");
407
408 6 my ($val, $condition) = $self->_injection_extract_value(@_);
409
410 6 ▶ my $coerce_variable = sub {
Subroutine __ANON__: called CC=2 SCAR=7.6
411 6 my $variable = shift;
412 6 ▶ if ($type->coercion->can_be_inlined)
Branch: 1 branch
BranchTrueFalseTotal
if ($type->coercion->can_be_inlined) { } 0 6 6
413 {
414 0 $type->coercion->inline_coercion($variable),
415 }
416 else
417 {
418 6 sprintf(
419 '$%s::PARAMS[%d]->{type}->coerce(%s)',
420 __PACKAGE__,
421 $self->ID,
422 $variable,
423 );
424 }
425 6 };
426
427 6 my $sigil = $self->sigil;
428
429 6 ▶ if ($sigil eq '@')
Branch: 4 branches
BranchTrueFalseTotal
if ($sigil eq '@') { } 2 4 6
elsif ($sigil eq '%') { } 0 4 4
elsif ($sigil eq '$' and $type->coercion->can_be_inlined) { } 0 4 4
elsif ($sigil eq '$') { } 4 0 4
430 {
431 2 $val = sprintf(
432 '(map { %s } %s)',
433 $coerce_variable->('$_'),
434 $val,
435 );
436 }
437
438 elsif ($sigil eq '%')
439 {
440 0 $val = sprintf(
441 'do { my %%tmp = %s; for (values %%tmp) { %s }; %%tmp }',
442 $val,
443 $coerce_variable->('$_'),
444 );
445 }
446
447 ▶ elsif ($sigil eq '$' and $type->coercion->can_be_inlined)
Condition33%1/3
$sigil eq "\$" and $type->coercion->can_be_inlined
!ll&&!rl&&r
040
MC/DC0%0/2
$sigil eq "\$" and $type->coercion->can_be_inlined
$sigil eq "\$" $type->coercion->can_be_inlined
Truth table33%1/3
A and B
A= $sigil eq "\$"B= $type->coercion->can_be_inlined
ABresult
0X0
100
111
448 {
449 0 $val = sprintf(
450 'do { my $tmp = %s; %s}',
451 $val,
452 $coerce_variable->('$tmp'),
453 );
454 }
455
456 elsif ($sigil eq '$')
457 {
458 4 $val = $coerce_variable->($val);
459 }
460
461 6 ▶ wantarray ? ($val, $condition) : $val;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 6 0 6
462 }
463  
464 sub _injection_default_value
465 602 ▶{
Subroutine _injection_default_value: called CC=5 SCAR=16.1
466 602 my $self = shift;
467 602 my ($fallback) = @_;
468
469 602 ▶ return sprintf('$%s::PARAMS[%d]{default}->()', __PACKAGE__, $self->ID) if $self->default;
Branch: 1 branch
BranchTrueFalseTotal
if $self->default 26 576 602
470 576 ▶ return $fallback if defined $fallback;
Branch: 1 branch
BranchTrueFalseTotal
if defined $fallback 345 231 576
471
472 231 ▶ return sprintf(
Branch: 1 branch
BranchTrueFalseTotal
if $self->named 1 230 231
473 'Carp::croak(sprintf q/Named parameter `%%s` is required/, %s)',
474 B::perlstring($self->named_names->[0]),
475 ) if $self->named;
476
477 230 ▶ return sprintf(
Branch: 1 branch
BranchTrueFalseTotal
if $self->invocant 96 134 230
478 'Carp::croak(q/Invocant %s is required/)',
479 $self->name,
480 ) if $self->invocant;
481
482 134 return sprintf(
483 'Carp::croak(q/Positional parameter %d is required/)',
484 $self->position,
485 );
486 }
487  
488 sub _injection_extract_value
489 301 ▶{
Subroutine _injection_extract_value: called CC=19 SCAR=29.5
490 301 my $self = shift;
491 301 my ($sig) = @_;
492
493 301 my $condition;
494 my $val;
495 301 my $slurpy_style = '';
496
497 301 ▶ if ($self->slurpy)
Branch: 3 branches
BranchTrueFalseTotal
if ($self->slurpy) { } 35 266 301
elsif ($self->named) { } 19 247 266
elsif ($self->invocant) { } 96 151 247
498 {
499 35 ▶ if ($self->sigil eq '%'
Branch: 1 branch
BranchTrueFalseTotal
if ($self->sigil eq '%' or $self->sigil eq '$' and $self->type and do { require Types::Standard; $self->type->is_a_type_of(Types::Standard::HashRef()) }) { } 13 22 35
Condition88%8/9
$self->sigil eq "%" or $self->sigil eq "\$" and $self->type and do { require Types::Standard; $self->type->is_a_type_of(Types::Standard::HashRef()) }
l!l&&r!l&&!r
10322
$self->sigil eq "\$" and $self->type and do { require Types::Standard; $self->type->is_a_type_of(Types::Standard::HashRef()) }
!ll&&!rl&&r
12103
$self->sigil eq "\$" and $self->type
!ll&&!rl&&r
12013
MC/DC75%3/4
$self->sigil eq "%" or $self->sigil eq "\$" and $self->type and do { require Types::Standard; $self->type->is_a_type_of(Types::Standard::HashRef()) }
$self->sigil eq "%" $self->sigil eq "\$" $self->type do { require Types::Standard; $self->type->is_a_type_of(Types::Standard::HashRef()) }
Truth table80%4/5
A or B and C and D
A= $self->sigil eq "%"B= $self->sigil eq "\$"C= $self->typeD= do { require Types::Standard; $self->type->is_a_type_of(Types::Standard::HashRef()) }
ABCDresult
1XXX1
01111
00XX0
010X0
01100
500 or ($self->sigil eq '$'
501 and $self->type
502 13 and do { require Types::Standard; $self->type->is_a_type_of(Types::Standard::HashRef()) }))
503 {
504 13 ▶ my @names = map(@{$_->named ? $_->named_names : []}, @{$sig->params});
Branch: 1 branch
BranchTrueFalseTotal
$_->named ? : 10 23 33
505 13 ▶ if (@names)
Branch: 1 branch
BranchTrueFalseTotal
if (@names) { } 6 7 13
506 {
507 6 ▶ croak("Cannot alias slurpy hash for a function with named parameters")
Branch: 1 branch
BranchTrueFalseTotal
if $self->alias 0 6 6
508 if $self->alias;
509
510 6 ▶ my $delete = $_->name eq '%_' ? '' : sprintf(
Branch: 1 branch
BranchTrueFalseTotal
$_->name eq '%_' ? : 1 5 6
511 'delete $tmp{$_} for (%s);',
512 join(q[,], map B::perlstring($_), @names),
513 );
514 6 my $ix = 1 + $sig->last_position;
515 6 $val = sprintf(
516 'do { use warnings FATAL => qw(all); my %%tmp = ($#_==%d && ref($_[%d]) eq q(HASH)) ? %%{$_[%d]} : @_[ %d .. $#_ ]; %s %%tmp ? %%tmp : (%s) }',
517 ($ix) x 4,
518 $delete,
519 $self->_injection_default_value('()'),
520 );
521 }
522 else
523 {
524 7 $val = sprintf(
525 'do { use warnings FATAL => qw(all); my %%tmp = @_[ %d .. $#_ ]; %%tmp ? @_[ %d .. $#_ ] : (%s) }',
526 $sig->last_position + 1,
527 $sig->last_position + 1,
528 $self->_injection_default_value('()'),
529 );
530 }
531 13 $condition = 1;
532 13 $slurpy_style = '%';
533 }
534 else
535 {
536 22 ▶ croak("Cannot have a slurpy array for a function with named parameters")
Branch: 1 branch
BranchTrueFalseTotal
if $sig->has_named 0 22 22
537 if $sig->has_named;
538 22 $val = sprintf(
539 '($#_ >= %d) ? @_[ %d .. $#_ ] : (%s)',
540 $sig->last_position + 1,
541 $sig->last_position + 1,
542 $self->_injection_default_value('()'),
543 );
544 22 $condition = 1;
545 22 $slurpy_style = '@';
546 }
547
548 35 ▶ if ($self->sigil eq '$')
Branch: 1 branch
BranchTrueFalseTotal
if ($self->sigil eq "\$") 13 22 35
549 {
550 13 ▶ $val = $slurpy_style eq '%' ? "+{ $val }" : "[ $val ]";
Branch: 1 branch
BranchTrueFalseTotal
$slurpy_style eq '%' ? : 3 10 13
551 13 $slurpy_style = '$';
552 }
553 }
554 elsif ($self->named)
555 {
556 38 ▶ no warnings 'uninitialized';
Subroutine __ANON__: called CC=1 SCAR=0.0
557 my $when = +{
558 '//=' => 'defined',
559 '||=' => '!!',
560 '=' => 'exists',
561 19 ▶ }->{ $self->default_when } || 'exists';
Condition50%1/2
{'//=', 'defined', '||=', '!!', '=', 'exists'}->{$self->default_when} || 'exists'
l!l
019
MC/DC0%0/1
{'//=', 'defined', '||=', '!!', '=', 'exists'}->{$self->default_when} || 'exists'
{'//=', 'defined', '||=', '!!', '=', 'exists'}->{$self->default_when}
Truth table50%1/2
A
A= {'//=', 'defined', '||=', '!!', '=', 'exists'}->{$self->default_when}
Aresult
11
00
562
563 $val = join '', map(
564 sprintf('%s($_{%s}) ? $_{%s} : ', $when, $_, $_),
565 19 map B::perlstring($_), @{$self->named_names}
566 ), $self->_injection_default_value();
567
568 $condition = join ' or ', map(
569 sprintf('%s($_{%s})', $when, $_),
570 19 map B::perlstring($_), @{$self->named_names}
571 );
572 }
573 elsif ($self->invocant)
574 {
575 96 $val = sprintf('@_ ? shift(@_) : %s', $self->_injection_default_value());
576 96 $condition = 1;
577 }
578 else
579 {
580 38 ▶ no warnings 'uninitialized';
Subroutine __ANON__: called CC=1 SCAR=0.0
581 my $when = +{
582 '//=' => 'defined($_[%d])',
583 '||=' => '!!($_[%d])',
584 '=' => '($#_ >= %d)',
585 151 ▶ }->{ $self->default_when } || '($#_ >= %d)';
Condition100%2/2
{'//=', 'defined($_[%d])', '||=', '!!($_[%d])', '=', '($#_ >= %d)'}->{$self->default_when} || '($#_ >= %d)'
l!l
13138
MC/DC100%1/1
{'//=', 'defined($_[%d])', '||=', '!!($_[%d])', '=', '($#_ >= %d)'}->{$self->default_when} || '($#_ >= %d)'
{'//=', 'defined($_[%d])', '||=', '!!($_[%d])', '=', '($#_ >= %d)'}->{$self->default_when}
Truth table100%2/2
A
A= {'//=', 'defined($_[%d])', '||=', '!!($_[%d])', '=', '($#_ >= %d)'}->{$self->default_when}
Aresult
11
00
586
587 151 my $pos = $self->position;
588 151 $val = sprintf($when.' ? $_[%d] : %s', $pos, $pos, $self->_injection_default_value());
589 151 $condition = sprintf($when, $self->position);
590 }
591
592 301 ▶ $condition = 1 if $self->_injection_default_value('@@') ne '@@';
Branch: 1 branch
BranchTrueFalseTotal
if $self->_injection_default_value("\@\@") ne "\@\@" 13 288 301
593
594 301 ▶ wantarray ? ($val, $condition) : $val;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 301 0 301
595 }
596  
597 sub _injection_type_check
598 301 ▶{
Subroutine _injection_type_check: called CC=9 SCAR=22.3
599 301 my $self = shift;
600 301 my ($var) = @_;
601
602 301 my $check = '';
603 301 ▶ if ( my $type = $self->type )
Branch: 1 branch
BranchTrueFalseTotal
if (my $type = $self->type) 88 213 301
604 {
605 my $can_xs =
606 $INC{'Mouse/Util.pm'}
607 && Mouse::Util::MOUSE_XS()
608 88 ▶ && ($type->{_is_core} or $type->is_parameterized && $type->parent->{_is_core});
Condition58%7/12
$INC{'Mouse/Util.pm'} && Mouse::Util::MOUSE_XS() && ($type->{'_is_core'} || $type->is_parameterized && $type->parent->{'_is_core'})
!ll&&!rl&&r
8404
$INC{"Mouse/Util.pm"} and Mouse::Util::MOUSE_XS()
!ll&&!rl&&r
8440
$type->{"_is_core"} or $type->is_parameterized and $type->parent->{"_is_core"}
l!l&&r!l&&!r
310
$type->is_parameterized and $type->parent->{"_is_core"}
!ll&&!rl&&r
001
MC/DC0%0/5
$INC{'Mouse/Util.pm'} && Mouse::Util::MOUSE_XS() && ($type->{'_is_core'} || $type->is_parameterized && $type->parent->{'_is_core'})
$INC{"Mouse/Util.pm"} Mouse::Util::MOUSE_XS() $type->{"_is_core"} $type->is_parameterized $type->parent->{"_is_core"}
Truth table16%1/6
A and B && C or D and E
A= $INC{"Mouse/Util.pm"}B= Mouse::Util::MOUSE_XS()C= $type->{"_is_core"}D= $type->is_parameterizedE= $type->parent->{"_is_core"}
ABCDEresult
0XXXX0
10XXX0
1100X0
110100
111XX1
110111
609
610 88 ▶ if (!$can_xs and $type->can_be_inlined)
Branch: 1 branch
BranchTrueFalseTotal
if (not $can_xs and $type->can_be_inlined) { } 78 10 88
Condition100%3/3
not $can_xs and $type->can_be_inlined
!ll&&!rl&&r
4678
MC/DC100%2/2
not $can_xs and $type->can_be_inlined
not $can_xs $type->can_be_inlined
Truth table100%3/3
not(A) and B
A= not $can_xsB= $type->can_be_inlined
ABresult
0X0
100
111
611 {
612 78 $check .= sprintf(
613 '%s;',
614 $type->inline_assert($var),
615 );
616 }
617 else
618 {
619 10 $check .= sprintf(
620 '$%s::PARAMS[%d]->{type}->assert_valid(%s);',
621 __PACKAGE__,
622 $self->ID,
623 $var,
624 );
625 }
626 }
627
628 301 for my $i (0 .. $#{$self->constraints})
629 {
630 4 $check .= sprintf(
631 'do { local $_ = %s; $%s::PARAMS[%d]->{constraints}[%d]->() } or Carp::croak(sprintf("%%s failed value constraint", %s));',
632 $var,
633 __PACKAGE__,
634 $self->ID,
635 $i,
636 B::perlstring($var),
637 );
638 }
639
640 301 return $check;
641 }
642  
643 38 1;
644  
645  
646 __END__
↑ Summary Kavorka/Signature.pm »