ValidationCompiler/Compiler.pm

filter: Statementstmt 93.5% 420 / 449 Branchbran 81.9% 136 / 166 Conditioncond 88.2% 45 / 51 MC/DCmcdc 75.0% 24 / 32 Subroutinesub 95.1% 59 / 62 Podpod 0.0% 0 / 10 totaltotal 88.8% 684 / 770 SCAR 48.3
CC
113
Cov
90%
_add_moose_check
20.0
new
19.0
_types
16.1
CRAP 124.9
↑ Summary ValidationCompiler.pm »
1 package Params::ValidationCompiler::Compiler;
2  
3 17 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 17 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 17 our $VERSION = '0.30';
7  
8 17 ▶use Carp qw( croak );
Subroutine BEGIN: called CC=1 SCAR=0.0
9 17 ▶use Eval::Closure qw( eval_closure );
Subroutine BEGIN: called CC=1 SCAR=0.0
10 17 ▶use List::Util 1.29 qw( pairkeys pairvalues );
Subroutine BEGIN: called CC=1 SCAR=0.0
11 17 ▶use Params::ValidationCompiler::Exceptions;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 17 ▶use Scalar::Util qw( blessed looks_like_number reftype );
Subroutine BEGIN: called CC=1 SCAR=0.0
13 17 ▶use overload ();
Subroutine BEGIN: called CC=1 SCAR=0.0
14 17 ▶use B qw( perlstring );
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 17 our @CARP_NOT = ( 'Params::ValidationCompiler', __PACKAGE__ );
17  
18 BEGIN {
19 ## no critic (Variables::RequireInitializationForLocalVars)
20 17 ▶ local $@;
Subroutine BEGIN: called CC=2 SCAR=6.9
21 17 my $has_sub_util = eval {
22 17 require Sub::Util;
23 16 Sub::Util->VERSION(1.40);
24 16 Sub::Util->import('set_subname');
25 16 1;
26 };
27  
28 2 ▶ sub HAS_SUB_UTIL () {$has_sub_util}
Subroutine HAS_SUB_UTIL: called CC=1 SCAR=0.0
Pod undocumented
29  
30 17 ▶ unless ($has_sub_util) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($has_sub_util) 1 16 17
31 *set_subname = sub {
32 1 ▶ croak
Subroutine __ANON__: called CC=1 SCAR=0.0
33 'Cannot name a generated validation subroutine. Please install Sub::Util.';
34 1 };
35 }
36  
37 17 my $has_cxsa = eval {
38 17 require Class::XSAccessor;
39 17 1;
40 };
41  
42 5 ▶ sub HAS_CXSA {$has_cxsa}
Subroutine HAS_CXSA: called CC=1 SCAR=0.0
Pod undocumented
43 }
44  
45 my %known
46 17 = map { $_ => 1 }
47 qw( debug name name_is_optional named_to_list params return_object slurpy );
48  
49 # I'd rather use Moo here but I want to make things relatively high on the
50 # CPAN river like DateTime use this distro, so reducing deps is important.
51 sub new {
52 58 ▶ my $class = shift;
Subroutine new: called CC=19 SCAR=29.5
Pod undocumented
53 58 my %p = @_;
54  
55 58 ▶ unless ( exists $p{params} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $p{"params"}) 1 57 58
56 1 croak
57 q{You must provide a "params" parameter when creating a parameter validator};
58 }
59  
60 57 ▶ if ( ref $p{params} eq 'HASH' ) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $p{'params'} eq 'HASH') { } 36 21 57
elsif (ref $p{'params'} eq 'ARRAY') { } 17 4 21
61 croak q{The "params" hashref must contain at least one key-value pair}
62 36 ▶ unless %{ $p{params} };
Branch: 1 branch
BranchTrueFalseTotal
unless %{$p{"params"};} 0 36 36
63  
64 croak
65 q{"named_to_list" must be used with arrayref params containing key-value pairs}
66 36 ▶ if $p{named_to_list};
Branch: 1 branch
BranchTrueFalseTotal
if $p{"named_to_list"} 0 36 36
67  
68 36 $class->_validate_param_spec($_) for values %{ $p{params} };
69 }
70 elsif ( ref $p{params} eq 'ARRAY' ) {
71 croak q{The "params" arrayref must contain at least one element}
72 17 ▶ unless @{ $p{params} };
Branch: 1 branch
BranchTrueFalseTotal
unless @{$p{"params"};} 0 17 17
73  
74 croak q{You can only use "return_object" with named params}
75 17 ▶ if $p{return_object};
Branch: 1 branch
BranchTrueFalseTotal
if $p{"return_object"} 1 16 17
76  
77 my @specs
78 = $p{named_to_list}
79 4 ? pairvalues @{ $p{params} }
80 16 ▶ : @{ $p{params} };
Branch: 1 branch
BranchTrueFalseTotal
$p{'named_to_list'} ? : 4 12 16
81  
82 16 $class->_validate_param_spec($_) for @specs;
83 }
84 else {
85 4 my $type = _describe( $p{params} );
86 4 croak
87 qq{The "params" parameter when creating a parameter validator must be a hashref or arrayref, you passed $type};
88 }
89  
90 48 ▶ if ( $p{named_to_list} && $p{slurpy} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($p{"named_to_list"} and $p{"slurpy"}) 1 47 48
Condition100%3/3
$p{"named_to_list"} and $p{"slurpy"}
!ll&&!rl&&r
4521
MC/DC100%2/2
$p{"named_to_list"} and $p{"slurpy"}
$p{"named_to_list"} $p{"slurpy"}
Truth table100%3/3
A and B
A= $p{"named_to_list"}B= $p{"slurpy"}
ABresult
0X0
100
111
91 1 croak q{You cannot use "named_to_list" and "slurpy" together};
92 }
93  
94 47 ▶ if ( exists $p{name} && ( !defined $p{name} || ref $p{name} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $p{"name"} and !defined($p{"name"}) || ref $p{"name"}) 3 44 47
Condition100%6/6
exists $p{"name"} and !defined($p{"name"}) || ref $p{"name"}
!ll&&!rl&&r
4043
not defined $p{"name"} or ref $p{"name"}
l!l&&r!l&&!r
124
MC/DC66%2/3
exists $p{"name"} and !defined($p{"name"}) || ref $p{"name"}
exists $p{"name"} not defined $p{"name"} ref $p{"name"}
Truth table75%3/4
A and not(B) or C
A= exists $p{"name"}B= not defined $p{"name"}C= ref $p{"name"}
ABCresult
0XX0
1000
11X1
1011
95 3 my $type = _describe( $p{name} );
96 3 croak
97 qq{The "name" parameter when creating a parameter validator must be a scalar, you passed $type};
98 }
99  
100 44 ▶ if ( $p{return_object} && $p{slurpy} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($p{"return_object"} and $p{"slurpy"}) 1 43 44
Condition100%3/3
$p{"return_object"} and $p{"slurpy"}
!ll&&!rl&&r
3941
MC/DC100%2/2
$p{"return_object"} and $p{"slurpy"}
$p{"return_object"} $p{"slurpy"}
Truth table100%3/3
A and B
A= $p{"return_object"}B= $p{"slurpy"}
ABresult
0X0
100
111
101 1 croak q{You cannot use "return_object" and "slurpy" together};
102 }
103  
104 43 my @unknown = sort grep { !$known{$_} } keys %p;
105 43 ▶ if (@unknown) {
Branch: 1 branch
BranchTrueFalseTotal
if (@unknown) 1 42 43
106 1 croak
107 "You passed unknown parameters when creating a parameter validator: [@unknown]";
108 }
109  
110 42 my $self = bless \%p, $class;
111  
112 42 $self->{_source} = [];
113 42 $self->{_env} = {};
114  
115 42 return $self;
116 }
117  
118 sub _describe {
119 7 ▶ my $thing = shift;
Subroutine _describe: called CC=6 SCAR=17.9
120  
121 7 ▶ if ( !defined $thing ) {
Branch: 3 branches
BranchTrueFalseTotal
if (not defined $thing) { } 2 5 7
elsif (my $class = builtin::blessed($thing)) { } 2 3 5
elsif (ref $thing) { } 2 1 3
122 2 return 'an undef';
123 }
124 elsif ( my $class = blessed $thing ) {
125 2 ▶ my $article = $class =~ /^[aeiou]/i ? 'an' : 'a';
Branch: 1 branch
BranchTrueFalseTotal
$class =~ /^[aeiou]/i ? : 0 2 2
126 2 return "$article $class object";
127 }
128 elsif ( ref $thing ) {
129 2 my $ref = lc ref $thing;
130 2 ▶ my $article = $ref =~ /^[aeiou]/i ? 'an' : 'a';
Branch: 1 branch
BranchTrueFalseTotal
$ref =~ /^[aeiou]/i ? : 1 1 2
131 2 return "$article $ref" . 'ref';
132 }
133  
134 1 return 'a scalar';
135 }
136  
137 {
138 17 my %known_keys = (
139 default => 1,
140 getter => 1,
141 optional => 1,
142 predicate => 1,
143 type => 1,
144 );
145  
146 sub _validate_param_spec {
147 73 ▶ shift;
Subroutine _validate_param_spec: called CC=4 SCAR=13.9
148 73 my $spec = shift;
149  
150 73 my $ref = ref $spec;
151 73 ▶ return unless $ref;
Branch: 1 branch
BranchTrueFalseTotal
unless $ref 24 49 73
152  
153 49 ▶ croak
Branch: 1 branch
BranchTrueFalseTotal
unless $ref eq "HASH" 1 48 49
154 "Specifications must be a scalar or hashref, but received a $ref"
155 unless $ref eq 'HASH';
156  
157 48 my @unknown = sort grep { !$known_keys{$_} } keys %{$spec};
158 48 ▶ if (@unknown) {
Branch: 1 branch
BranchTrueFalseTotal
if (@unknown) 3 45 48
159 3 croak "Specification contains unknown keys: [@unknown]";
160 }
161 }
162 }
163  
164 4 ▶sub name { $_[0]->{name} }
Subroutine name: called CC=1 SCAR=0.0
Pod undocumented
165 39 ▶sub _has_name { exists $_[0]->{name} }
Subroutine _has_name: called CC=1 SCAR=0.0
166  
167 4 ▶sub _name_is_optional { $_[0]->{name_is_optional} }
Subroutine _name_is_optional: called CC=1 SCAR=0.0
168  
169 # I have no idea why critic thinks _caller isn't used.
170  
171 ## no critic (Subroutines::ProhibitUnusedPrivateSubroutines)
172 0 ▶sub _caller { $_[0]->{caller} }
Subroutine _caller: not called CC=1 SCAR=6.9
173 ## use critic
174 4 ▶sub _has_caller { exists $_[0]->{caller} }
Subroutine _has_caller: called CC=1 SCAR=0.0
175  
176 130 ▶sub params { $_[0]->{params} }
Subroutine params: called CC=1 SCAR=0.0
Pod undocumented
177  
178 65 ▶sub slurpy { $_[0]->{slurpy} }
Subroutine slurpy: called CC=1 SCAR=0.0
Pod undocumented
179  
180 279 ▶sub _source { $_[0]->{_source} }
Subroutine _source: called CC=1 SCAR=0.0
181  
182 129 ▶sub _env { $_[0]->{_env} }
Subroutine _env: called CC=1 SCAR=0.0
183  
184 22 ▶sub named_to_list { $_[0]->{named_to_list} }
Subroutine named_to_list: called CC=1 SCAR=0.0
Pod undocumented
185  
186 29 ▶sub return_object { $_[0]->{return_object} }
Subroutine return_object: called CC=1 SCAR=0.0
Pod undocumented
187  
188 sub _inlineable_name {
189 return defined $_[0]->{name}
190 ? $_[0]->{name}
191 105 ▶ : 'an un-named validation subroutine';
Subroutine _inlineable_name: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
defined $_[0]{'name'} ? : 11 94 105
192 }
193  
194 sub _any_type_has_coercion {
195 9 ▶ my $self = shift;
Subroutine _any_type_has_coercion: called CC=7 SCAR=21.5
196  
197 9 ▶ return $self->{_has_coercion} if exists $self->{_has_coercion};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"_has_coercion"} 0 9 9
198  
199 9 for my $type ( $self->_types ) {
200  
201 # Specio
202 5 ▶ if ( $type->can('has_coercions') && $type->has_coercions ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($type->can('has_coercions') and $type->has_coercions) { } 1 4 5
elsif ($type->can('has_coercion') and $type->has_coercion) { } 0 4 4
Condition66%2/3
$type->can("has_coercions") and $type->has_coercions
!ll&&!rl&&r
041
MC/DC50%1/2
$type->can("has_coercions") and $type->has_coercions
$type->can("has_coercions") $type->has_coercions
Truth table66%2/3
A and B
A= $type->can("has_coercions")B= $type->has_coercions
ABresult
0X0
100
111
203 1 return $self->{_has_coercion} = 1;
204 }
205  
206 # Moose and Type::Tiny
207 ▶ elsif ( $type->can('has_coercion') && $type->has_coercion ) {
Condition33%1/3
$type->can("has_coercion") and $type->has_coercion
!ll&&!rl&&r
040
MC/DC0%0/2
$type->can("has_coercion") and $type->has_coercion
$type->can("has_coercion") $type->has_coercion
Truth table33%1/3
A and B
A= $type->can("has_coercion")B= $type->has_coercion
ABresult
0X0
100
111
208 0 return $self->{_has_coercion} = 1;
209 }
210 }
211  
212 8 return $self->{_has_coercion} = 0;
213 }
214  
215 sub _types {
216 9 ▶ my $self = shift;
Subroutine _types: called CC=9 SCAR=27.8
217  
218 9 my @types;
219 9 ▶ if ( ref $self->params eq 'HASH' ) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $self->params eq 'HASH') { } 0 9 9
elsif (ref $self->params eq 'ARRAY') { } 9 0 9
220 0 ▶ @types = map { $_->{type} || () }
Branch: 1 branch
BranchTrueFalseTotal
$_->{"type"} or () 0 0 0
221 0 grep { ref $_ } values %{ $self->params };
222 }
223 elsif ( ref $self->params eq 'ARRAY' ) {
224 9 ▶ if ( $self->named_to_list ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->named_to_list) { } 0 9 9
225 0 my %p = @{ $self->params };
226 0 ▶ @types = map { $_->{type} || () } grep { ref $_ } values %p;
Branch: 1 branch
BranchTrueFalseTotal
$_->{"type"} or () 0 0 0
227 }
228 else {
229 @types
230 9 ▶ = map { $_->{type} || () } grep { ref $_ } @{ $self->params };
Branch: 1 branch
BranchTrueFalseTotal
$_->{"type"} or () 11 4 15
231 }
232 }
233  
234 9 ▶ push @types, $self->slurpy if $self->slurpy && ref $self->slurpy;
Branch: 1 branch
BranchTrueFalseTotal
if $self->slurpy and ref $self->slurpy 1 8 9
Condition100%3/3
$self->slurpy and ref $self->slurpy
!ll&&!rl&&r
711
MC/DC100%2/2
$self->slurpy and ref $self->slurpy
$self->slurpy ref $self->slurpy
Truth table100%3/3
A and B
A= $self->slurpyB= ref $self->slurpy
ABresult
0X0
100
111
235  
236 9 return @types;
237 }
238  
239 sub subref {
240 41 ▶ my $self = shift;
Subroutine subref: called CC=6 SCAR=18.0
Pod undocumented
241  
242 41 $self->_compile;
243  
244 39 ▶ local $ENV{EVAL_CLOSURE_PRINT_SOURCE} = 1 if $self->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug"} 0 39 39
245 my $sub = eval_closure(
246 39 source => 'sub { ' . ( join "\n", @{ $self->_source } ) . ' };',
247 environment => $self->_env,
248 );
249  
250 39 ▶ if ( $self->_has_name ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_has_name) 4 35 39
251 4 ▶ my $caller = $self->_has_caller ? $self->_caller : caller(1);
Branch: 1 branch
BranchTrueFalseTotal
$self->_has_caller ? : 0 4 4
252 4 my $name = join '::', $caller, $self->name;
253  
254 4 ▶ return $sub if $self->_name_is_optional && !HAS_SUB_UTIL;
Branch: 1 branch
BranchTrueFalseTotal
if $self->_name_is_optional and not &HAS_SUB_UTIL() 1 3 4
Condition100%3/3
$self->_name_is_optional and not &HAS_SUB_UTIL()
!ll&&!rl&&r
211
MC/DC100%2/2
$self->_name_is_optional and not &HAS_SUB_UTIL()
$self->_name_is_optional not &HAS_SUB_UTIL()
Truth table100%3/3
A and not(B)
A= $self->_name_is_optionalB= not &HAS_SUB_UTIL()
ABresult
0X0
100
111
255 3 set_subname( $name, $sub );
256 }
257  
258 37 return $sub;
259 }
260  
261 sub source {
262 1 ▶ my $self = shift;
Subroutine source: called CC=1 SCAR=0.0
Pod undocumented
263  
264 1 $self->_compile;
265 return (
266 1 ( join "\n", @{ $self->_source } ),
267 $self->_env,
268 );
269 }
270  
271 sub _compile {
272 42 ▶ my $self = shift;
Subroutine _compile: called CC=4 SCAR=13.9
273  
274 42 ▶ if ( ref $self->params eq 'HASH' ) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $self->params eq 'HASH') { } 29 13 42
elsif (ref $self->params eq 'ARRAY') { } 13 0 13
275 29 $self->_compile_named_args_check;
276 }
277 elsif ( ref $self->params eq 'ARRAY' ) {
278 13 ▶ if ( $self->named_to_list ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->named_to_list) { } 2 11 13
279 2 $self->_compile_named_args_list_check;
280 }
281 else {
282 11 $self->_compile_positional_args_check;
283 }
284 }
285 }
286  
287 sub _compile_named_args_check {
288 29 ▶ my $self = shift;
Subroutine _compile_named_args_check: called CC=2 SCAR=6.9
289  
290 29 $self->_compile_named_args_check_body( $self->params );
291  
292 29 ▶ if ( $self->return_object ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->return_object) { } 4 25 29
293 4 push @{ $self->_source }, $self->_add_return_named_args_object;
294 }
295 else {
296 25 push @{ $self->_source }, 'return %args;';
297 }
298  
299 29 return;
300 }
301  
302 {
303 17 my $class_id = 0;
304  
305 sub _add_return_named_args_object {
306 4 ▶ my $self = shift;
Subroutine _add_return_named_args_object: called CC=9 SCAR=22.0
307  
308 4 my $params = $self->params;
309 4 my %getters;
310 my %predicates;
311 4 for my $p ( keys %{$params} ) {
312 $getters{
313 ref $params->{$p} && exists $params->{$p}{getter}
314 ? $params->{$p}{getter}
315 8 ▶ : $p
Branch: 1 branch
BranchTrueFalseTotal
ref $params->{$p} && exists $params->{$p}{'getter'} ? : 2 6 8
Condition100%3/3
ref $params->{$p} and exists $params->{$p}{"getter"}
!ll&&!rl&&r
242
MC/DC100%2/2
ref $params->{$p} and exists $params->{$p}{"getter"}
ref $params->{$p} exists $params->{$p}{"getter"}
Truth table100%3/3
A and B
A= ref $params->{$p}B= exists $params->{$p}{"getter"}
ABresult
0X0
100
111
316 } = $p;
317 $predicates{ $params->{$p}{predicate} } = $p
318 8 ▶ if ref $params->{$p} && exists $params->{$p}{predicate};
Branch: 1 branch
BranchTrueFalseTotal
if ref $params->{$p} and exists $params->{$p}{"predicate"} 2 6 8
Condition100%3/3
ref $params->{$p} and exists $params->{$p}{"predicate"}
!ll&&!rl&&r
242
MC/DC100%2/2
ref $params->{$p} and exists $params->{$p}{"predicate"}
ref $params->{$p} exists $params->{$p}{"predicate"}
Truth table100%3/3
A and B
A= ref $params->{$p}B= exists $params->{$p}{"predicate"}
ABresult
0X0
100
111
319 }
320  
321 4 ▶ my $use_cxsa = HAS_CXSA && !$ENV{TEST_NAMED_ARGS_OBJECT_WITHOUT_CXSA};
Condition66%2/3
HAS_CXSA() && !$ENV{'TEST_NAMED_ARGS_OBJECT_WITHOUT_CXSA'}
!ll&&!rl&&r
022
MC/DC50%1/2
HAS_CXSA() && !$ENV{'TEST_NAMED_ARGS_OBJECT_WITHOUT_CXSA'}
HAS_CXSA() !$ENV{'TEST_NAMED_ARGS_OBJECT_WITHOUT_CXSA'}
Truth table66%2/3
A && not(B)
A= HAS_CXSA()B= !$ENV{'TEST_NAMED_ARGS_OBJECT_WITHOUT_CXSA'}
ABresult
0X0
100
111
322 4 ▶ my $class = sprintf(
Branch: 1 branch
BranchTrueFalseTotal
$use_cxsa ? : 2 2 4
323 '%s::OO::Args%d::%s',
324 __PACKAGE__,
325 $class_id++,
326 $use_cxsa ? 'XS' : 'PP',
327 );
328  
329 4 ▶ if ($use_cxsa) {
Branch: 1 branch
BranchTrueFalseTotal
if ($use_cxsa) { } 2 2 4
330 2 $self->_create_cxsa_return_class(
331 $class,
332 \%getters,
333 \%predicates,
334 );
335 }
336 else {
337 2 $self->_create_pp_return_class( $class, \%getters, \%predicates );
338 }
339  
340 4 return sprintf( 'bless \%%args, %s', perlstring($class) );
341 }
342 }
343  
344 17 sub _create_cxsa_return_class {
345 2 ▶ my $self = shift;
Subroutine _create_cxsa_return_class: called CC=1 SCAR=0.0
346 2 my $class = shift;
347 2 my $getters = shift;
348 2 my $predicates = shift;
349  
350 2 Class::XSAccessor->import(
351 redefine => 1,
352 class => $class,
353 getters => $getters,
354 exists_predicates => $predicates,
355 );
356  
357 2 return;
358 }
359  
360 sub _create_pp_return_class {
361 2 ▶ my $self = shift;
Subroutine _create_pp_return_class: called CC=4 SCAR=13.9
362 2 my $class = shift;
363 2 my $getters = shift;
364 2 my $predicates = shift;
365  
366 2 my @source = sprintf( 'package %s;', $class );
367 2 for my $sub ( keys %{$getters} ) {
368 push @source,
369 sprintf(
370 'sub %s { return $_[0]->{%s} }', $sub,
371 4 perlstring( $getters->{$sub} )
372 );
373 }
374 2 for my $sub ( keys %{$predicates} ) {
375 push @source,
376 sprintf(
377 'sub %s { return exists $_[0]->{%s} }', $sub,
378 1 perlstring( $predicates->{$sub} )
379 );
380 }
381 2 push @source, q{1;};
382 ## no critic (BuiltinFunctions::ProhibitStringyEval, ErrorHandling::RequireCheckingReturnValueOfEval)
383 2 ▶ eval join q{}, @source
Subroutine bar: called CC=1 SCAR=0.2
Subroutine bar: not called CC=1 SCAR=0.2
Subroutine foo: called CC=1 SCAR=0.2
Subroutine get_foo: called CC=1 SCAR=0.2
Subroutine has_bar: called CC=1 SCAR=0.2
Branch: 1 branch
BranchTrueFalseTotal
eval join("", @source) or die $@ 0 2 2
384 or die $@;
385  
386 2 return;
387 }
388  
389 sub _compile_named_args_list_check {
390 2 ▶ my $self = shift;
Subroutine _compile_named_args_list_check: called CC=2 SCAR=6.9
391  
392 2 $self->_compile_named_args_check_body( { @{ $self->params } } );
393  
394 2 my @keys = map { perlstring($_) } pairkeys @{ $self->params };
395  
396 # If we don't handle the one-key case specially we end up getting a
397 # warning like "Scalar value @args{"bar"} better written as $args{"bar"}
398 # at ..."
399 2 ▶ if ( @keys == 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@keys == 1) { } 1 1 2
400 1 push @{ $self->_source }, "return \$args{$keys[0]};";
401 }
402 else {
403 1 my $keys_str = join q{, }, @keys;
404 1 push @{ $self->_source }, "return \@args{$keys_str};";
405 }
406  
407 2 return;
408 }
409  
410 sub _compile_named_args_check_body {
411 31 ▶ my $self = shift;
Subroutine _compile_named_args_check_body: called CC=11 SCAR=24.0
412 31 my $params = shift;
413  
414 31 push @{ $self->_source }, $self->_set_named_args_hash;
415  
416 31 for my $name ( sort keys %{$params} ) {
417 44 my $spec = $params->{$name};
418 44 ▶ $spec = { optional => !$spec } unless ref $spec;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $spec 16 28 44
419  
420 44 my $qname = perlstring($name);
421 44 my $access = "\$args{$qname}";
422  
423 # We check exists $spec->{optional} so as not to blow up on a
424 # restricted hash.
425 $self->_add_check_for_required_named_param( $access, $name )
426 unless ( exists $spec->{optional} && $spec->{optional} )
427 44 ▶ || exists $spec->{default};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $spec->{"optional"} and $spec->{"optional"} or exists $spec->{"default"} 29 15 44
Condition100%6/6
exists $spec->{"optional"} and $spec->{"optional"} or exists $spec->{"default"}
l!l&&r!l&&!r
11429
exists $spec->{"optional"} and $spec->{"optional"}
!ll&&!rl&&r
191411
MC/DC100%3/3
exists $spec->{"optional"} and $spec->{"optional"} or exists $spec->{"default"}
exists $spec->{"optional"} $spec->{"optional"} exists $spec->{"default"}
Truth table80%4/5
A and B or C
A= exists $spec->{"optional"}B= $spec->{"optional"}C= exists $spec->{"default"}
ABCresult
11X1
0X11
1011
0X00
1000
428  
429 $self->_add_named_default_assignment(
430 $access,
431 $name,
432 $spec->{default}
433 44 ▶ ) if exists $spec->{default};
Branch: 1 branch
BranchTrueFalseTotal
if exists $spec->{"default"} 4 40 44
434  
435 # Same issue with restricted hashes here.
436 $self->_add_type_check( $access, $name, $spec )
437 44 ▶ if exists $spec->{type} && $spec->{type};
Branch: 1 branch
BranchTrueFalseTotal
if exists $spec->{"type"} and $spec->{"type"} 18 26 44
Condition66%2/3
exists $spec->{"type"} and $spec->{"type"}
!ll&&!rl&&r
26018
MC/DC50%1/2
exists $spec->{"type"} and $spec->{"type"}
exists $spec->{"type"} $spec->{"type"}
Truth table66%2/3
A and B
A= exists $spec->{"type"}B= $spec->{"type"}
ABresult
0X0
100
111
438 }
439  
440 31 ▶ if ( $self->slurpy ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->slurpy) { } 2 29 31
441 2 ▶ $self->_add_check_for_extra_hash_param_types( $self->slurpy, $params )
Branch: 1 branch
BranchTrueFalseTotal
if ref $self->slurpy 1 1 2
442 if ref $self->slurpy;
443 }
444 else {
445 29 $self->_add_check_for_extra_hash_params($params);
446 }
447  
448 31 return;
449 }
450  
451 sub _set_named_args_hash {
452 31 ▶ my $self = shift;
Subroutine _set_named_args_hash: called CC=1 SCAR=0.0
453  
454 31 push @{ $self->_source },
455 sprintf( <<'EOF', ( $self->_inlineable_name ) x 4 );
456 my %%args;
457 if ( @_ %% 2 == 0 ) {
458 %%args = @_;
459 }
460 elsif ( @_ == 1 ) {
461 if ( ref $_[0] ) {
462 if ( Scalar::Util::blessed( $_[0] ) ) {
463 if ( overload::Overloaded( $_[0] )
464 && defined overload::Method( $_[0], '%%{}' ) ) {
465  
466 %%args = %%{ $_[0] };
467 }
468 else {
469 Params::ValidationCompiler::Exception::BadArguments->throw(
470 message =>
471 'Expected a hash or hash reference but a single object argument was passed to %s',
472 show_trace => 1,
473 );
474 }
475 }
476 elsif ( ref $_[0] eq 'HASH' ) {
477 %%args = %%{ $_[0] };
478 }
479 else {
480 Params::ValidationCompiler::Exception::BadArguments->throw(
481 message =>
482 'Expected a hash or hash reference but a single '
483 . ( ref $_[0] )
484 . ' reference argument was passed to %s',
485 show_trace => 1,
486 );
487 }
488 }
489 else {
490 Params::ValidationCompiler::Exception::BadArguments->throw(
491 message =>
492 'Expected a hash or hash reference but a single non-reference argument was passed to %s',
493 show_trace => 1,
494 );
495 }
496 }
497 else {
498 Params::ValidationCompiler::Exception::BadArguments->throw(
499 message =>
500 'Expected a hash or hash reference but an odd number of arguments was passed to %s',
501 show_trace => 1,
502 );
503 }
504 EOF
505  
506 31 return;
507 }
508  
509 sub _add_check_for_required_named_param {
510 29 ▶ my $self = shift;
Subroutine _add_check_for_required_named_param: called CC=1 SCAR=0.0
511 29 my $access = shift;
512 29 my $name = shift;
513  
514 29 my $qname = perlstring($name);
515 29 push @{ $self->_source },
516 sprintf( <<'EOF', $access, $qname, $self->_inlineable_name, $qname );
517 exists %s
518 or Params::ValidationCompiler::Exception::Named::Required->throw(
519 message => %s . ' is a required parameter for %s',
520 parameter => %s,
521 show_trace => 1,
522 );
523 EOF
524  
525 29 return;
526 }
527  
528 sub _add_check_for_extra_hash_param_types {
529 1 ▶ my $self = shift;
Subroutine _add_check_for_extra_hash_param_types: called CC=1 SCAR=0.0
530 1 my $type = shift;
531 1 my $params = shift;
532  
533 $self->_env->{'%known'}
534 1 = { map { $_ => 1 } keys %{$params} };
535  
536 # We need to set the name argument to something that won't conflict with
537 # names someone would actually use for a parameter.
538 1 my $check = join q{}, $self->_type_check(
539 '$args{$key}',
540 '__PCC extra parameters__',
541 $type,
542 );
543 1 push @{ $self->_source }, sprintf( <<'EOF', $check );
544 for my $key ( grep { !$known{$_} } keys %%args ) {
545 %s;
546 }
547 EOF
548  
549 1 return;
550 }
551  
552 sub _add_check_for_extra_hash_params {
553 29 ▶ my $self = shift;
Subroutine _add_check_for_extra_hash_params: called CC=1 SCAR=0.0
554 29 my $params = shift;
555  
556 $self->_env->{'%known'}
557 29 = { map { $_ => 1 } keys %{$params} };
558 29 push @{ $self->_source }, sprintf( <<'EOF', $self->_inlineable_name );
559 my @extra = grep { !$known{$_} } keys %%args;
560 if (@extra) {
561 my $u = join ', ', sort @extra;
562 Params::ValidationCompiler::Exception::Named::Extra->throw(
563 message => "Found extra parameters passed to %s: [$u]",
564 parameters => \@extra,
565 show_trace => 1,
566 );
567 }
568 EOF
569  
570 29 return;
571 }
572  
573 sub _compile_positional_args_check {
574 11 ▶ my $self = shift;
Subroutine _compile_positional_args_check: called CC=12 SCAR=24.9
575  
576 11 my @specs = $self->_munge_and_check_positional_params;
577  
578 9 my $first_optional_idx = -1;
579 9 for my $i ( 0 .. $#specs ) {
580 15 ▶ next unless $specs[$i]{optional} || exists $specs[$i]{default};
Branch: 1 branch
BranchTrueFalseTotal
unless $specs[$i]{"optional"} or exists $specs[$i]{"default"} 9 6 15
Condition100%3/3
$specs[$i]{"optional"} or exists $specs[$i]{"default"}
l!l&&r!l&&!r
249
MC/DC100%2/2
$specs[$i]{"optional"} or exists $specs[$i]{"default"}
$specs[$i]{"optional"} exists $specs[$i]{"default"}
Truth table100%3/3
A or B
A= $specs[$i]{"optional"}B= exists $specs[$i]{"default"}
ABresult
1X1
011
000
581 6 $first_optional_idx = $i;
582 6 last;
583 }
584  
585 # If optional params start anywhere after the first parameter spec then we
586 # must require at least one param. If there are no optional params then
587 # they're all required.
588 $self->_add_check_for_required_positional_params(
589 9 ▶ $first_optional_idx == -1
Branch: 2 branches
BranchTrueFalseTotal
if $first_optional_idx != 0 9 0 9
$first_optional_idx == -1 ? : 3 6 9
590 ? ( scalar @specs )
591 : $first_optional_idx
592 ) if $first_optional_idx != 0;
593  
594 9 ▶ $self->_add_check_for_extra_positional_params( scalar @specs )
Branch: 1 branch
BranchTrueFalseTotal
unless $self->slurpy 7 2 9
595 unless $self->slurpy;
596  
597 9 my $access_var = '$_';
598 9 my $return_var = '@_';
599 9 ▶ if ( $self->_any_type_has_coercion ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_any_type_has_coercion) 1 8 9
600 1 push @{ $self->_source }, 'my @copy = @_;';
601 1 $access_var = '$copy';
602 1 $return_var = '@copy';
603 }
604  
605 9 for my $i ( 0 .. $#specs ) {
606 15 my $spec = $specs[$i];
607  
608 15 my $name = "Parameter $i";
609 15 my $access = sprintf( '%s[%i]', $access_var, $i );
610  
611 $self->_add_positional_default_assignment(
612 $i,
613 $access,
614 $name,
615 $spec->{default}
616 15 ▶ ) if exists $spec->{default};
Branch: 1 branch
BranchTrueFalseTotal
if exists $spec->{"default"} 4 11 15
617  
618 $self->_add_type_check( $access, $name, $spec )
619 15 ▶ if $spec->{type};
Branch: 1 branch
BranchTrueFalseTotal
if $spec->{"type"} 4 11 15
620 }
621  
622 9 ▶ if ( ref $self->slurpy ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self->slurpy) 1 8 9
623 1 $self->_add_check_for_extra_positional_param_types(
624 scalar @specs,
625 $self->slurpy,
626 $access_var,
627 );
628 }
629  
630 9 push @{ $self->_source }, sprintf( 'return %s;', $return_var );
631  
632 9 return;
633 }
634  
635 sub _munge_and_check_positional_params {
636 11 ▶ my $self = shift;
Subroutine _munge_and_check_positional_params: called CC=6 SCAR=17.9
637  
638 11 my @specs;
639 11 my $in_optional = 0;
640  
641 11 for my $spec ( @{ $self->params } ) {
642 19 ▶ $spec = ref $spec ? $spec : { optional => !$spec };
Branch: 1 branch
BranchTrueFalseTotal
ref $spec ? : 12 7 19
643 19 ▶ if ( $spec->{optional} || exists $spec->{default} ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($spec->{'optional'} or exists $spec->{'default'}) { } 8 11 19
elsif ($in_optional) { } 2 9 11
Condition100%3/3
$spec->{"optional"} or exists $spec->{"default"}
l!l&&r!l&&!r
3511
MC/DC100%2/2
$spec->{"optional"} or exists $spec->{"default"}
$spec->{"optional"} exists $spec->{"default"}
Truth table100%3/3
A or B
A= $spec->{"optional"}B= exists $spec->{"default"}
ABresult
1X1
011
000
644 8 $in_optional = 1;
645 }
646 elsif ($in_optional) {
647 2 croak
648 'Parameter list contains an optional parameter followed by a required parameter.';
649 }
650  
651 17 push @specs, $spec;
652 }
653  
654 9 return @specs;
655 }
656  
657 sub _add_check_for_required_positional_params {
658 9 ▶ my $self = shift;
Subroutine _add_check_for_required_positional_params: called CC=1 SCAR=0.0
659 9 my $min = shift;
660  
661 9 push @{ $self->_source },
662 sprintf( <<'EOF', ($min) x 2, $self->_inlineable_name, $min );
663 if ( @_ < %d ) {
664 my $got = scalar @_;
665 my $got_n = @_ == 1 ? 'parameter' : 'parameters';
666 Params::ValidationCompiler::Exception::Positional::Required->throw(
667 message => "Got $got $got_n but expected at least %d for %s",
668 minimum => %d,
669 got => scalar @_,
670 show_trace => 1,
671 );
672 }
673 EOF
674  
675 9 return;
676 }
677  
678 sub _add_check_for_extra_positional_param_types {
679 1 ▶ my $self = shift;
Subroutine _add_check_for_extra_positional_param_types: called CC=1 SCAR=0.0
680 1 my $max = shift;
681 1 my $type = shift;
682 1 my $access_var = shift;
683  
684 # We need to set the name argument to something that won't conflict with
685 # names someone would actually use for a parameter.
686 1 my $check = join q{}, $self->_type_check(
687 sprintf( '%s[$i]', $access_var ),
688 '__PCC extra parameters__',
689 $type,
690 );
691 1 push @{ $self->_source }, sprintf( <<'EOF', $max, $max, $check );
692 if ( @_ > %d ) {
693 for my $i ( %d .. $#_ ) {
694 %s;
695 }
696 }
697 EOF
698  
699 1 return;
700 }
701  
702 sub _add_check_for_extra_positional_params {
703 7 ▶ my $self = shift;
Subroutine _add_check_for_extra_positional_params: called CC=1 SCAR=0.0
704 7 my $max = shift;
705  
706 7 push @{ $self->_source },
707 sprintf( <<'EOF', ($max) x 2, $self->_inlineable_name, $max );
708 if ( @_ > %d ) {
709 my $extra = @_ - %d;
710 my $extra_n = $extra == 1 ? 'parameter' : 'parameters';
711 Params::ValidationCompiler::Exception::Positional::Extra->throw(
712 message => "Got $extra extra $extra_n for %s",
713 maximum => %d,
714 got => scalar @_,
715 show_trace => 1,
716 );
717 }
718 EOF
719  
720 7 return;
721 }
722  
723 sub _add_positional_default_assignment {
724 4 ▶ my $self = shift;
Subroutine _add_positional_default_assignment: called CC=1 SCAR=0.0
725 4 my $position = shift;
726 4 my $access = shift;
727 4 my $name = shift;
728 4 my $default = shift;
729  
730 4 push @{ $self->_source }, "if ( \$#_ < $position ) {";
731 4 $self->_add_shared_default_assignment( $access, $name, $default );
732 4 push @{ $self->_source }, '}';
733  
734 4 return;
735 }
736  
737 sub _add_named_default_assignment {
738 4 ▶ my $self = shift;
Subroutine _add_named_default_assignment: called CC=1 SCAR=0.0
739 4 my $access = shift;
740 4 my $name = shift;
741 4 my $default = shift;
742  
743 4 my $qname = perlstring($name);
744 4 push @{ $self->_source }, "unless ( exists \$args{$qname} ) {";
745 4 $self->_add_shared_default_assignment( $access, $name, $default );
746 4 push @{ $self->_source }, '}';
747  
748 4 return;
749 }
750  
751 sub _add_shared_default_assignment {
752 8 ▶ my $self = shift;
Subroutine _add_shared_default_assignment: called CC=6 SCAR=17.9
753 8 my $access = shift;
754 8 my $name = shift;
755 8 my $default = shift;
756  
757 8 my $qname = perlstring($name);
758  
759 8 ▶ croak 'Default must be either a plain scalar or a subroutine reference'
Branch: 1 branch
BranchTrueFalseTotal
if ref $default and builtin::reftype($default) ne "CODE" 0 8 8
Condition66%2/3
ref $default and builtin::reftype($default) ne "CODE"
!ll&&!rl&&r
530
MC/DC0%0/2
ref $default and builtin::reftype($default) ne "CODE"
ref $default builtin::reftype($default) ne "CODE"
Truth table66%2/3
A and B
A= ref $defaultB= builtin::reftype($default) ne "CODE"
ABresult
0X0
100
111
760 if ref $default && reftype($default) ne 'CODE';
761  
762 8 ▶ if ( ref $default ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $default) { } 3 5 8
763 3 push @{ $self->_source }, "$access = \$defaults{$qname}->();";
764 3 $self->_env->{'%defaults'}{$name} = $default;
765 }
766 else {
767 5 ▶ if ( defined $default ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $default) { } 4 1 5
768 4 ▶ if ( looks_like_number($default) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (&looks_like_number($default)) { } 3 1 4
769 3 push @{ $self->_source }, "$access = $default;";
770 }
771 else {
772 1 push @{ $self->_source },
773 "$access = " . perlstring($default) . ';';
774 }
775 }
776 else {
777 1 push @{ $self->_source }, "$access = undef;";
778 }
779 }
780  
781 8 return;
782 }
783  
784 sub _add_type_check {
785 22 ▶ my $self = shift;
Subroutine _add_type_check: called CC=4 SCAR=13.9
786 22 my $access = shift;
787 22 my $name = shift;
788 22 my $spec = shift;
789  
790 22 my $type = $spec->{type};
791 22 ▶ croak "Passed a type that is not an object for $name: $type"
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($type) 0 22 22
792 unless blessed $type;
793  
794 7 push @{ $self->_source }, sprintf( 'if ( exists %s ) {', $access )
795 22 ▶ if $spec->{optional};
Branch: 1 branch
BranchTrueFalseTotal
if $spec->{"optional"} 7 15 22
796  
797 22 push @{ $self->_source },
798 22 $self->_type_check( $access, $name, $spec->{type} );
799  
800 7 push @{ $self->_source }, '}'
801 22 ▶ if $spec->{optional};
Branch: 1 branch
BranchTrueFalseTotal
if $spec->{"optional"} 7 15 22
802  
803 22 return;
804 }
805  
806 sub _type_check {
807 24 ▶ my $self = shift;
Subroutine _type_check: called CC=4 SCAR=14.6
808 24 my $access = shift;
809 24 my $name = shift;
810 24 my $type = shift;
811  
812 # Specio
813 24 ▶ return $type->can('can_inline_coercion_and_check')
Branch: 3 branches
BranchTrueFalseTotal
$type->can('can_inline_coercion_and_check') ? : 18 6 24
$type->can('inline_assert') ? : 6 0 6
$type->can('can_be_inlined') ? : 0 0 0
814 ? $self->_add_specio_check( $access, $name, $type )
815  
816 # Type::Tiny
817 : $type->can('inline_assert')
818 ? $self->_add_type_tiny_check( $access, $name, $type )
819  
820 # Moose
821 : $type->can('can_be_inlined')
822 ? $self->_add_moose_check( $access, $name, $type )
823 : croak 'Unknown type object ' . ref $type;
824 }
825  
826 # From reading through the Type::Tiny source, I can't see any cases where a
827 # Type::Tiny type or coercion needs to provide any environment variables to
828 # compile with.
829 sub _add_type_tiny_check {
830 6 ▶ my $self = shift;
Subroutine _add_type_tiny_check: called CC=4 SCAR=13.9
831 6 my $access = shift;
832 6 my $name = shift;
833 6 my $type = shift;
834  
835 6 my $qname = perlstring($name);
836  
837 6 my @source;
838 6 ▶ if ( $type->has_coercion ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type->has_coercion) 4 2 6
839 4 my $coercion = $type->coercion;
840 4 ▶ if ( $coercion->can_be_inlined ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($coercion->can_be_inlined) { } 1 3 4
841 1 push @source,
842 "$access = " . $coercion->inline_coercion($access) . ';';
843 }
844 else {
845 3 $self->_env->{'%tt_coercions'}{$name}
846 = $coercion->compiled_coercion;
847 3 push @source,
848 sprintf(
849 '%s = $tt_coercions{%s}->( %s );',
850 $access, $qname, $access,
851 );
852 }
853 }
854  
855 6 ▶ if ( $type->can_be_inlined ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type->can_be_inlined) { } 3 3 6
856 3 push @source,
857 $type->inline_assert($access);
858 }
859 else {
860 3 push @source,
861 sprintf(
862 '$types{%s}->assert_valid( %s );',
863 $qname, $access,
864 );
865 3 $self->_env->{'%types'}{$name} = $type;
866 }
867  
868 6 return @source;
869 }
870  
871 sub _add_specio_check {
872 18 ▶ my $self = shift;
Subroutine _add_specio_check: called CC=5 SCAR=16.1
873 18 my $access = shift;
874 18 my $name = shift;
875 18 my $type = shift;
876  
877 18 my $qname = perlstring($name);
878  
879 18 my @source;
880  
881 18 ▶ if ( $type->can_inline_coercion_and_check ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type->can_inline_coercion_and_check) { } 13 5 18
882 13 ▶ if ( $type->has_coercions ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type->has_coercions) { } 1 12 13
883 1 my ( $source, $env ) = $type->inline_coercion_and_check($access);
884 1 push @source, sprintf( '%s = %s;', $access, $source );
885 1 $self->_add_to_environment(
886 sprintf(
887 'The inline_coercion_and_check for %s ',
888 $type->_description
889 ),
890 $env,
891 );
892 }
893 else {
894 12 my ( $source, $env ) = $type->inline_assert($access);
895 12 push @source, $source . ';';
896 12 $self->_add_to_environment(
897 sprintf(
898 'The inline_assert for %s ',
899 $type->_description
900 ),
901 $env,
902 );
903 }
904 }
905 else {
906 5 my @coercions = $type->coercions;
907 5 $self->_env->{'%specio_coercions'}{$name} = \@coercions;
908 5 for my $i ( 0 .. $#coercions ) {
909 4 my $c = $coercions[$i];
910 4 ▶ if ( $c->can_be_inlined ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c->can_be_inlined) { } 1 3 4
911 1 push @source,
912 sprintf(
913 '%s = %s if %s;',
914 $access,
915 $c->inline_coercion($access),
916 $c->from->inline_check($access)
917 );
918 1 $self->_add_to_environment(
919 sprintf(
920 'The inline_coercion for %s ',
921 $c->_description
922 ),
923  
924 # This should really be public in Specio
925 $c->_inline_environment,
926 );
927 }
928 else {
929 3 push @source,
930 sprintf(
931 '%s = $specio_coercions{%s}[%s]->coerce(%s) if $specio_coercions{%s}[%s]->from->value_is_valid(%s);',
932 $access,
933 $qname,
934 $i,
935 $access,
936 $qname,
937 $i,
938 $access
939 );
940 }
941 }
942  
943 5 push @source,
944 sprintf(
945 '$types{%s}->validate_or_die(%s);',
946 $qname, $access,
947 );
948  
949 5 $self->_env->{'%types'}{$name} = $type;
950 }
951  
952 18 return @source;
953 }
954  
955 sub _add_moose_check {
956 0 ▶ my $self = shift;
Subroutine _add_moose_check: not called CC=4 SCAR=30.0
957 0 my $access = shift;
958 0 my $name = shift;
959 0 my $type = shift;
960  
961 0 my $qname = perlstring($name);
962  
963 0 my @source;
964  
965 0 ▶ if ( $type->has_coercion ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type->has_coercion) 0 0 0
966 0 $self->_env->{'%moose_coercions'}{$name} = $type->coercion;
967 0 push @source,
968 sprintf(
969 '%s = $moose_coercions{%s}->coerce( %s );',
970 $access, $qname, $access,
971 );
972 }
973  
974 0 $self->_env->{'%types'}{$name} = $type;
975  
976 0 my $code = <<'EOF';
977 if ( !%s ) {
978 my $type = $types{%s};
979 my $param = %s;
980 my $value = %s;
981 my $msg = $param . q{ failed with: } . $type->get_message($value);
982 die
983 Params::ValidationCompiler::Exception::ValidationFailedForMooseTypeConstraint
984 ->new(
985 message => $msg,
986 parameter => $param,
987 value => $value,
988 type => $type,
989 );
990 }
991 EOF
992  
993 0 ▶ my $check
Branch: 1 branch
BranchTrueFalseTotal
$type->can_be_inlined ? : 0 0 0
994 = $type->can_be_inlined
995 ? $type->_inline_check($access)
996 : sprintf( '$types{%s}->check( %s )', $qname, $access );
997  
998 0 push @source, sprintf(
999 $code,
1000 $check,
1001 $qname,
1002 $qname,
1003 $access,
1004 );
1005  
1006 0 ▶ if ( $type->can_be_inlined ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type->can_be_inlined) 0 0 0
1007 0 $self->_add_to_environment(
1008 sprintf( 'The %s type', $type->name ),
1009 $type->inline_environment,
1010 );
1011 }
1012  
1013 0 return @source;
1014 }
1015  
1016 sub _add_to_environment {
1017 14 ▶ my $self = shift;
Subroutine _add_to_environment: called CC=3 SCAR=11.2
1018 14 my $what = shift;
1019 14 my $new_env = shift;
1020  
1021 14 my $env = $self->_env;
1022 14 for my $key ( keys %{$new_env} ) {
1023 26 ▶ if ( exists $env->{$key} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $env->{$key}) 0 26 26
1024 0 croak sprintf(
1025 '%s has an inline environment variable named %s'
1026 . ' that conflicts with a variable already in the environment',
1027 $what, $key
1028 );
1029 }
1030 26 $self->_env->{$key} = $new_env->{$key};
1031 }
1032 }
1033  
1034 17 1;
1035  
1036 # ABSTRACT: Object that implements the check subroutine compilation
1037  
1038 __END__
1039  
1040 =pod
1041  
1042 =encoding UTF-8
1043  
1044 =head1 NAME
1045  
1046 Params::ValidationCompiler::Compiler - Object that implements the check subroutine compilation
1047  
1048 =head1 VERSION
1049  
1050 version 0.30
1051  
1052 =for Pod::Coverage .*
1053  
1054 =head1 SUPPORT
1055  
1056 Bugs may be submitted at L<https://github.com/houseabsolute/Params-ValidationCompiler/issues>.
1057  
1058 I am also usually active on IRC as 'autarch' on C<irc://irc.perl.org>.
1059  
1060 =head1 SOURCE
1061  
1062 The source code repository for Params-ValidationCompiler can be found at L<https://github.com/houseabsolute/Params-ValidationCompiler>.
1063  
1064 =head1 AUTHOR
1065  
1066 Dave Rolsky <autarch@urth.org>
1067  
1068 =head1 COPYRIGHT AND LICENSE
1069  
1070 This software is Copyright (c) 2016 - 2018 by Dave Rolsky.
1071  
1072 This is free software, licensed under:
1073  
1074 The Artistic License 2.0 (GPL Compatible)
1075  
1076 The full text of the license can be found in the
1077 F<LICENSE> file included with this distribution.
1078  
1079 =cut
↑ Summary ValidationCompiler.pm »