Validate.pm

filter: Statementstmt 100% 131 / 131 Branchbran 85.4% 53 / 62 Conditioncond 65.9% 29 / 44 MC/DCmcdc 37.5% 9 / 24 Subroutinesub 100% 20 / 20 Podpod 100% 3 / 3 totaltotal 86.2% 245 / 284 CC 52 SCAR 22.7
CC
52
Cov
90%
_convert_to_param_validate_spec
16.0
validated_list
13.2
validated_hash
13.1
CRAP 9.7
↑ Summary Validate/Exception/ValidationFailedForTypeConstraint.pm »
1 ## no critic (Moose::RequireCleanNamespace, Moose::RequireMakeImmutable)
2 package MooseX::Params::Validate;
3  
4 15 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 15 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 15 our $VERSION = '0.21';
8  
9 15 ▶use Carp 'confess';
Subroutine BEGIN: called CC=1 SCAR=0.0
10 15 ▶use Devel::Caller 'caller_cv';
Subroutine BEGIN: called CC=1 SCAR=0.0
11 15 ▶use Scalar::Util 'blessed', 'refaddr', 'reftype';
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 15 ▶use Moose 2.1200 ();
Subroutine BEGIN: called CC=1 SCAR=0.0
14 15 ▶use Moose::Util qw( throw_exception );
Subroutine BEGIN: called CC=1 SCAR=0.0
15 use Moose::Util::TypeConstraints
16 15 ▶ qw( find_type_constraint class_type role_type );
Subroutine BEGIN: called CC=1 SCAR=0.0
17 15 ▶use MooseX::Params::Validate::Exception::ValidationFailedForTypeConstraint;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 15 ▶use Params::Validate 1.15 ();
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 15 ▶use Sub::Exporter -setup => {
Subroutine BEGIN: called CC=1 SCAR=0.0
21 exports => [
22 qw( validated_hash validated_list pos_validated_list validate validatep )
23 ],
24 groups => {
25 default => [qw( validated_hash validated_list pos_validated_list )],
26 deprecated => [qw( validate validatep )],
27 },
28 15 };
29  
30 15 my %CACHED_SPECS;
31  
32 63 ▶sub validated_hash {
Subroutine validated_hash: called CC=13 SCAR=25.7
Pod documented
33 63 my ( $args, %spec ) = @_;
34  
35 63 my $cache_key = _cache_key( \%spec );
36  
37 63 my $allow_extra = delete $spec{MX_PARAMS_VALIDATE_ALLOW_EXTRA};
38  
39 63 ▶ if ( exists $CACHED_SPECS{$cache_key} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $CACHED_SPECS{$cache_key}) { } 44 19 63
40 44 ▶ ( ref $CACHED_SPECS{$cache_key} eq 'HASH' )
Branch: 1 branch
BranchTrueFalseTotal
ref $CACHED_SPECS{$cache_key} eq "HASH" or confess("I was expecting a HASH-ref in the cached $cache_key parameter" . ' spec, you are doing something funky, stop it!') 44 0 44
41 || confess
42 "I was expecting a HASH-ref in the cached $cache_key parameter"
43 . ' spec, you are doing something funky, stop it!';
44 44 %spec = %{ $CACHED_SPECS{$cache_key} };
45 }
46 else {
47 19 ▶ my $should_cache = delete $spec{MX_PARAMS_VALIDATE_NO_CACHE} ? 0 : 1;
Branch: 1 branch
BranchTrueFalseTotal
delete $spec{'MX_PARAMS_VALIDATE_NO_CACHE'} ? : 3 16 19
48  
49 $spec{$_} = _convert_to_param_validate_spec(
50 $spec{$_},
51 qq{The '$_' parameter},
52 19 ) foreach keys %spec;
53  
54 19 ▶ $CACHED_SPECS{$cache_key} = \%spec
Branch: 1 branch
BranchTrueFalseTotal
if $should_cache 16 3 19
55 if $should_cache;
56 }
57  
58 63 my $instance;
59 63 ▶ $instance = shift @$args if blessed $args->[0];
Branch: 1 branch
BranchTrueFalseTotal
if builtin::blessed($args->[0]) 4 59 63
60  
61 my %args
62 = @$args == 1
63 && ref $args->[0]
64 63 ▶ && reftype( $args->[0] ) eq 'HASH' ? %{ $args->[0] } : @$args;
Branch: 1 branch
BranchTrueFalseTotal
@$args == 1 && ref $args->[0] && builtin::reftype($args->[0]) eq 'HASH' ? : 1 62 63
Condition66%4/6
@$args == 1 and ref $args->[0] and builtin::reftype($args->[0]) eq "HASH"
!ll&&!rl&&r
6201
@$args == 1 and ref $args->[0]
!ll&&!rl&&r
6201
MC/DC33%1/3
@$args == 1 and ref $args->[0] and builtin::reftype($args->[0]) eq "HASH"
@$args == 1 ref $args->[0] builtin::reftype($args->[0]) eq "HASH"
Truth table50%2/4
A and B and C
A= @$args == 1B= ref $args->[0]C= builtin::reftype($args->[0]) eq "HASH"
ABCresult
0XX0
10X0
1100
1111
65  
66 $args{$_} = $spec{$_}{constraint}->coerce( $args{$_} )
67 63 ▶ for grep { $spec{$_}{coerce} && exists $args{$_} } keys %spec;
Condition100%3/3
$spec{$_}{'coerce'} && exists $args{$_}
!ll&&!rl&&r
13614
MC/DC100%2/2
$spec{$_}{'coerce'} && exists $args{$_}
$spec{$_}{'coerce'} exists $args{$_}
Truth table100%3/3
A && B
A= $spec{$_}{'coerce'}B= exists $args{$_}
ABresult
0X0
100
111
68  
69 63 %args = Params::Validate::validate_with(
70 params => \%args,
71 spec => \%spec,
72 allow_extra => $allow_extra,
73 called => _caller_name(),
74 );
75  
76 31 ▶ return ( ( defined $instance ? $instance : () ), %args );
Branch: 1 branch
BranchTrueFalseTotal
defined $instance ? : 4 27 31
77 }
78  
79 15 *validate = \&validated_hash;
80  
81 36 ▶sub validated_list {
Subroutine validated_list: called CC=13 SCAR=25.8
Pod documented
82 36 my ( $args, @spec ) = @_;
83  
84 36 my %spec = @spec;
85  
86 36 my $cache_key = _cache_key( \%spec );
87  
88 36 my $allow_extra = delete $spec{MX_PARAMS_VALIDATE_ALLOW_EXTRA};
89  
90 36 my @ordered_spec;
91 36 ▶ if ( exists $CACHED_SPECS{$cache_key} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $CACHED_SPECS{$cache_key}) { } 28 8 36
92 28 ▶ ( ref $CACHED_SPECS{$cache_key} eq 'ARRAY' )
Branch: 1 branch
BranchTrueFalseTotal
ref $CACHED_SPECS{$cache_key} eq "ARRAY" or confess("I was expecting a ARRAY-ref in the cached $cache_key parameter" . ' spec, you are doing something funky, stop it!') 28 0 28
93 || confess
94 "I was expecting a ARRAY-ref in the cached $cache_key parameter"
95 . ' spec, you are doing something funky, stop it!';
96 28 %spec = %{ $CACHED_SPECS{$cache_key}->[0] };
97 28 @ordered_spec = @{ $CACHED_SPECS{$cache_key}->[1] };
98 }
99 else {
100 8 ▶ my $should_cache = delete $spec{MX_PARAMS_VALIDATE_NO_CACHE} ? 0 : 1;
Branch: 1 branch
BranchTrueFalseTotal
delete $spec{'MX_PARAMS_VALIDATE_NO_CACHE'} ? : 0 8 8
101  
102 8 @ordered_spec = grep { exists $spec{$_} } @spec;
103  
104 $spec{$_} = _convert_to_param_validate_spec(
105 $spec{$_},
106 qq{The '$_' parameter},
107 8 ) foreach keys %spec;
108  
109 8 ▶ $CACHED_SPECS{$cache_key} = [ \%spec, \@ordered_spec ]
Branch: 1 branch
BranchTrueFalseTotal
if $should_cache 8 0 8
110 if $should_cache;
111 }
112  
113 36 my $instance;
114 36 ▶ $instance = shift @$args if blessed $args->[0];
Branch: 1 branch
BranchTrueFalseTotal
if builtin::blessed($args->[0]) 3 33 36
115  
116 my %args
117 = @$args == 1
118 && ref $args->[0]
119 36 ▶ && reftype( $args->[0] ) eq 'HASH' ? %{ $args->[0] } : @$args;
Branch: 1 branch
BranchTrueFalseTotal
@$args == 1 && ref $args->[0] && builtin::reftype($args->[0]) eq 'HASH' ? : 1 35 36
Condition66%4/6
@$args == 1 and ref $args->[0] and builtin::reftype($args->[0]) eq "HASH"
!ll&&!rl&&r
3501
@$args == 1 and ref $args->[0]
!ll&&!rl&&r
3501
MC/DC33%1/3
@$args == 1 and ref $args->[0] and builtin::reftype($args->[0]) eq "HASH"
@$args == 1 ref $args->[0] builtin::reftype($args->[0]) eq "HASH"
Truth table50%2/4
A and B and C
A= @$args == 1B= ref $args->[0]C= builtin::reftype($args->[0]) eq "HASH"
ABCresult
0XX0
10X0
1100
1111
120  
121 $args{$_} = $spec{$_}{constraint}->coerce( $args{$_} )
122 36 ▶ for grep { $spec{$_}{coerce} && exists $args{$_} } keys %spec;
Condition100%3/3
$spec{$_}{'coerce'} && exists $args{$_}
!ll&&!rl&&r
8114
MC/DC100%2/2
$spec{$_}{'coerce'} && exists $args{$_}
$spec{$_}{'coerce'} exists $args{$_}
Truth table100%3/3
A && B
A= $spec{$_}{'coerce'}B= exists $args{$_}
ABresult
0X0
100
111
123  
124 36 %args = Params::Validate::validate_with(
125 params => \%args,
126 spec => \%spec,
127 allow_extra => $allow_extra,
128 called => _caller_name(),
129 );
130  
131 return (
132 ( defined $instance ? $instance : () ),
133 14 ▶ @args{@ordered_spec}
Branch: 1 branch
BranchTrueFalseTotal
defined $instance ? : 3 11 14
134 );
135 }
136  
137 15 *validatep = \&validated_list;
138  
139 34 ▶sub pos_validated_list {
Subroutine pos_validated_list: called CC=8 SCAR=20.9
Pod documented
140 34 my $args = shift;
141  
142 34 my @spec;
143 34 ▶ push @spec, shift while ref $_[0];
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] and do { push @spec, shift() } 102 34 136
144  
145 34 my %extra = @_;
146  
147 34 my $cache_key = _cache_key( \%extra );
148  
149 34 my $allow_extra = delete $extra{MX_PARAMS_VALIDATE_ALLOW_EXTRA};
150  
151 34 my @pv_spec;
152 34 ▶ if ( exists $CACHED_SPECS{$cache_key} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $CACHED_SPECS{$cache_key}) { } 31 3 34
153 31 ▶ ( ref $CACHED_SPECS{$cache_key} eq 'ARRAY' )
Branch: 1 branch
BranchTrueFalseTotal
ref $CACHED_SPECS{$cache_key} eq "ARRAY" or confess("I was expecting an ARRAY-ref in the cached $cache_key parameter" . ' spec, you are doing something funky, stop it!') 31 0 31
154 || confess
155 "I was expecting an ARRAY-ref in the cached $cache_key parameter"
156 . ' spec, you are doing something funky, stop it!';
157 31 @pv_spec = @{ $CACHED_SPECS{$cache_key} };
158 }
159 else {
160 3 ▶ my $should_cache = exists $extra{MX_PARAMS_VALIDATE_NO_CACHE} ? 0 : 1;
Branch: 1 branch
BranchTrueFalseTotal
exists $extra{'MX_PARAMS_VALIDATE_NO_CACHE'} ? : 0 3 3
161  
162 @pv_spec = map {
163 3 _convert_to_param_validate_spec(
164 $spec[$_],
165 'Parameter #' . ( $_ + 1 )
166 )
167 } 0 .. $#spec;
168  
169 3 ▶ $CACHED_SPECS{$cache_key} = \@pv_spec
Branch: 1 branch
BranchTrueFalseTotal
if $should_cache 3 0 3
170 if $should_cache;
171 }
172  
173 34 my @args = @$args;
174  
175 $args[$_] = $pv_spec[$_]{constraint}->coerce( $args[$_] )
176 34 ▶ for grep { $pv_spec[$_] && $pv_spec[$_]{coerce} } 0 .. $#args;
Condition66%2/3
$pv_spec[$_] && $pv_spec[$_]{'coerce'}
!ll&&!rl&&r
0633
MC/DC50%1/2
$pv_spec[$_] && $pv_spec[$_]{'coerce'}
$pv_spec[$_] $pv_spec[$_]{'coerce'}
Truth table66%2/3
A && B
A= $pv_spec[$_]B= $pv_spec[$_]{'coerce'}
ABresult
0X0
100
111
177  
178 34 @args = Params::Validate::validate_with(
179 params => \@args,
180 spec => \@pv_spec,
181 allow_extra => $allow_extra,
182 called => _caller_name(),
183 );
184  
185 10 return @args;
186 }
187  
188 133 ▶sub _cache_key {
Subroutine _cache_key: called CC=2 SCAR=6.9
189 133 my $spec = shift;
190  
191 133 ▶ if ( exists $spec->{MX_PARAMS_VALIDATE_CACHE_KEY} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $spec->{'MX_PARAMS_VALIDATE_CACHE_KEY'}) { } 5 128 133
192 5 return delete $spec->{MX_PARAMS_VALIDATE_CACHE_KEY};
193 }
194 else {
195 128 return refaddr( caller_cv(2) );
196 }
197 }
198  
199 57 ▶sub _convert_to_param_validate_spec {
Subroutine _convert_to_param_validate_spec: called CC=15 SCAR=27.7
200 57 my $spec = shift;
201 57 my $id = shift;
202  
203 57 my %pv_spec;
204  
205 $pv_spec{optional} = $spec->{optional}
206 57 ▶ if exists $spec->{optional};
Branch: 1 branch
BranchTrueFalseTotal
if exists $spec->{"optional"} 26 31 57
207  
208 $pv_spec{default} = $spec->{default}
209 57 ▶ if exists $spec->{default};
Branch: 1 branch
BranchTrueFalseTotal
if exists $spec->{"default"} 3 54 57
210  
211 $pv_spec{coerce} = $spec->{coerce}
212 57 ▶ if exists $spec->{coerce};
Branch: 1 branch
BranchTrueFalseTotal
if exists $spec->{"coerce"} 15 42 57
213  
214 $pv_spec{depends} = $spec->{depends}
215 57 ▶ if exists $spec->{depends};
Branch: 1 branch
BranchTrueFalseTotal
if exists $spec->{"depends"} 2 55 57
216  
217 57 my $constraint;
218 57 ▶ if ( defined $spec->{isa} ) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $spec->{'isa'}) { } 50 7 57
elsif (defined $spec->{'does'}) { } 7 0 7
219 $constraint = _is_tc( $spec->{isa} )
220 || Moose::Util::TypeConstraints::find_or_parse_type_constraint(
221 $spec->{isa} )
222 50 ▶ || class_type( $spec->{isa} );
Condition50%3/6
_is_tc($spec->{'isa'}) || Moose::Util::TypeConstraints::find_or_parse_type_constraint($spec->{'isa'}) || class_type($spec->{'isa'})
l!l&&r!l&&!r
5000
_is_tc($spec->{"isa"}) or Moose::Util::TypeConstraints::find_or_parse_type_constraint($spec->{"isa"})
l!l&&r!l&&!r
3470
MC/DC0%0/3
_is_tc($spec->{'isa'}) || Moose::Util::TypeConstraints::find_or_parse_type_constraint($spec->{'isa'}) || class_type($spec->{'isa'})
_is_tc($spec->{"isa"}) Moose::Util::TypeConstraints::find_or_parse_type_constraint($spec->{"isa"}) class_type($spec->{'isa'})
Truth table50%2/4
A or B || C
A= _is_tc($spec->{"isa"})B= Moose::Util::TypeConstraints::find_or_parse_type_constraint($spec->{"isa"})C= class_type($spec->{'isa'})
ABCresult
1XX1
01X1
0011
0000
223 }
224 elsif ( defined $spec->{does} ) {
225 $constraint
226 = _is_tc( $spec->{does} )
227 || find_type_constraint( $spec->{does} )
228 7 ▶ || role_type( $spec->{does} );
Condition50%3/6
_is_tc($spec->{'does'}) || find_type_constraint($spec->{'does'}) || &role_type($spec->{'does'})
l!l&&r!l&&!r
700
_is_tc($spec->{"does"}) or find_type_constraint($spec->{"does"})
l!l&&r!l&&!r
430
MC/DC0%0/3
_is_tc($spec->{'does'}) || find_type_constraint($spec->{'does'}) || &role_type($spec->{'does'})
_is_tc($spec->{"does"}) find_type_constraint($spec->{"does"}) &role_type($spec->{'does'})
Truth table50%2/4
A or B || C
A= _is_tc($spec->{"does"})B= find_type_constraint($spec->{"does"})C= &role_type($spec->{'does'})
ABCresult
1XX1
01X1
0011
0000
229 }
230  
231 $pv_spec{callbacks} = $spec->{callbacks}
232 57 ▶ if exists $spec->{callbacks};
Branch: 1 branch
BranchTrueFalseTotal
if exists $spec->{"callbacks"} 1 56 57
233  
234 57 ▶ if ($constraint) {
Branch: 1 branch
BranchTrueFalseTotal
if ($constraint) 57 0 57
235 57 $pv_spec{constraint} = $constraint;
236  
237 195 ▶ my $cb = sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
238 195 ▶ return 1 if $constraint->check( $_[0] );
Branch: 1 branch
BranchTrueFalseTotal
if $constraint->check($_[0]) 127 68 195
239 68 die
240 MooseX::Params::Validate::Exception::ValidationFailedForTypeConstraint
241 ->new(
242 parameter => $id,
243 type => $constraint,
244 value => $_[0],
245 );
246 57 };
247  
248 $pv_spec{callbacks}
249 57 { 'checking type constraint for ' . $constraint->name } = $cb;
250 }
251  
252 delete $pv_spec{coerce}
253 57 ▶ unless $pv_spec{constraint} && $pv_spec{constraint}->has_coercion;
Branch: 1 branch
BranchTrueFalseTotal
unless $pv_spec{"constraint"} and $pv_spec{"constraint"}->has_coercion 47 10 57
Condition66%2/3
$pv_spec{"constraint"} and $pv_spec{"constraint"}->has_coercion
!ll&&!rl&&r
04710
MC/DC50%1/2
$pv_spec{"constraint"} and $pv_spec{"constraint"}->has_coercion
$pv_spec{"constraint"} $pv_spec{"constraint"}->has_coercion
Truth table66%2/3
A and B
A= $pv_spec{"constraint"}B= $pv_spec{"constraint"}->has_coercion
ABresult
0X0
100
111
254  
255 57 return \%pv_spec;
256 }
257  
258 57 ▶sub _is_tc {
Subroutine _is_tc: called CC=4 SCAR=14.2
259 57 my $maybe_tc = shift;
260  
261 57 ▶ return $maybe_tc
Branch: 1 branch
BranchTrueFalseTotal
if defined $maybe_tc and builtin::blessed($maybe_tc) and $maybe_tc->isa("Moose::Meta::TypeConstraint") 7 50 57
Condition66%4/6
defined $maybe_tc and builtin::blessed($maybe_tc) and $maybe_tc->isa("Moose::Meta::TypeConstraint")
!ll&&!rl&&r
5007
defined $maybe_tc and builtin::blessed($maybe_tc)
!ll&&!rl&&r
0507
MC/DC33%1/3
defined $maybe_tc and builtin::blessed($maybe_tc) and $maybe_tc->isa("Moose::Meta::TypeConstraint")
defined $maybe_tc builtin::blessed($maybe_tc) $maybe_tc->isa("Moose::Meta::TypeConstraint")
Truth table50%2/4
A and B and C
A= defined $maybe_tcB= builtin::blessed($maybe_tc)C= $maybe_tc->isa("Moose::Meta::TypeConstraint")
ABCresult
0XX0
10X0
1100
1111
262 if defined $maybe_tc
263 && blessed $maybe_tc
264 && $maybe_tc->isa('Moose::Meta::TypeConstraint');
265 }
266  
267 133 ▶sub _caller_name {
Subroutine _caller_name: called CC=2 SCAR=7.2
268 133 ▶ my $depth = shift || 0;
Condition50%1/2
shift() || 0
l!l
0133
MC/DC0%0/1
shift() || 0
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
269  
270 133 return ( caller( 2 + $depth ) )[3];
271 }
272  
273 15 ▶no Moose::Util::TypeConstraints;
Subroutine BEGIN: called CC=1 SCAR=0.0
274  
275 15 1;
276  
277 # ABSTRACT: an extension of Params::Validate using Moose's types
278  
279 __END__
↑ Summary Validate/Exception/ValidationFailedForTypeConstraint.pm »