blib/lib/Data/FormValidator/Results.pm

filter: Statementstmt 91.0% 445 / 489 Branchbran 79.3% 219 / 276 Conditioncond 75.6% 87 / 115 MC/DCmcdc 57.5% 38 / 66 Subroutinesub 95.5% 43 / 45 Podpod 0.0% 0 / 20 totaltotal 82.2% 832 / 1011 SCAR 59.1
CC
219
Cov
85%
_process
123.9
meta
30.0
_get_input_as_hash
25.5
CRAP 370.1
« blib/lib/Data/FormValidator/Constraints.pm ↑ Summary blib/lib/Data/FormValidator/ConstraintsFactory.pm »
1 #
2 # Results.pm - Object which contains validation result.
3 #
4 # This file is part of FormValidator.
5 #
6 # Author: Francis J. Lacoste <francis.lacoste@iNsu.COM>
7 # Maintainer: Mark Stosberg <mark@summersault.com>
8 #
9 # Copyright (C) 2000 iNsu Innovations Inc.
10 #
11 # This program is free software; you can redistribute it and/or modify
12 # it under the terms same terms as perl itself.
13 #
14 64 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 package Data::FormValidator::Results;
17 63 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 63 ▶use Symbol;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 63 ▶use Data::FormValidator::Filters ':filters';
Subroutine BEGIN: called CC=1 SCAR=0.0
20 62 ▶use Data::FormValidator::Constraints qw(:validators :matchers);
Subroutine BEGIN: called CC=1 SCAR=0.0
21 use overload
22 62 'bool' => \&_bool_overload_based_on_success,
23 62 ▶ fallback => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
24  
25 59 our $VERSION = 4.85;
26  
27 =pod
28  
29 =head1 NAME
30  
31 Data::FormValidator::Results - results of form input validation.
32  
33 =head1 SYNOPSIS
34  
35 my $results = Data::FormValidator->check(\%input_hash, \%dfv_profile);
36  
37 # Print the name of missing fields
38 if ( $results->has_missing ) {
39 for my $f ( $results->missing ) {
40 print $f, " is missing\n";
41 }
42 }
43  
44 # Print the name of invalid fields
45 if ( $results->has_invalid ) {
46 for my $f ( $results->invalid ) {
47 print $f, " is invalid: ", $results->invalid( $f ), "\n";
48 }
49 }
50  
51 # Print unknown fields
52 if ( $results->has_unknown ) {
53 for my $f ( $results->unknown ) {
54 print $f, " is unknown\n";
55 }
56 }
57  
58 # Print valid fields
59 for my $f ( $results->valid() ) {
60 print $f, " = ", $results->valid( $f ), "\n";
61 }
62  
63 =head1 DESCRIPTION
64  
65 This object is returned by the L<Data::FormValidator> C<check> method.
66 It can be queried for information about the validation results.
67  
68 =cut
69  
70 sub new {
71 127 ▶ my $proto = shift;
Subroutine new: called CC=2 SCAR=7.1
Pod undocumented
72 127 ▶ my $class = ref $proto || $proto;
Condition33%1/3
ref $proto || $proto
l!l&&r!l&&!r
01270
MC/DC0%0/2
ref $proto || $proto
ref $proto $proto
Truth table33%1/3
A || B
A= ref $protoB= $proto
ABresult
1X1
011
000
73 127 my ($profile, $data) = @_;
74  
75 127 my $self = bless {}, $class;
76  
77 127 $self->_process( $profile, $data );
78  
79 122 $self;
80 }
81  
82 sub _process {
83 127 ▶ my ($self, $profile, $data) = @_;
Subroutine _process: called CC=96 SCAR=48.2
84  
85 # Copy data and assumes that all is valid to start with
86  
87 127 my %data = $self->_get_input_as_hash($data);
88 127 my %valid = %data;
89 127 my @missings = ();
90 127 my @unknown = ();
91  
92 # msgs() method will need access to the profile
93 127 $self->{profile} = $profile;
94  
95 127 my %imported_validators;
96  
97 # import valid_* subs from requested packages
98 127 for my $package (_arrayify($profile->{validator_packages})) {
99 6 ▶ if ( !exists $imported_validators{$package} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $imported_validators{$package}) 6 0 6
100 6 local $SIG{__DIE__} = \&confess;
101 6 eval "require $package";
102 6 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 6 6
103 0 die "Couldn't load validator package '$package': $@";
104 }
105  
106 # Perl will die with a nice error message if the package can't be found
107 # No need to go through extra effort here. -mls :)
108 6 my $package_ref = qualify_to_ref("${package}::");
109 my @subs = grep(/^(valid_|match_|filter_)/,
110 6 keys(%{*{$package_ref}}));
111 6 for my $sub (@subs) {
112 # is it a sub? (i.e. make sure it's not a scalar, hash, etc.)
113 53 my $subref = *{qualify_to_ref("${package}::$sub")}{CODE};
114 53 ▶ if (defined $subref) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $subref) 53 0 53
115 53 *{qualify_to_ref($sub)} = $subref;
116 }
117 }
118 6 $imported_validators{$package} = 1;
119 }
120 }
121  
122 # Apply unconditional filters
123 127 for my $filter (_arrayify($profile->{filters})) {
124 7 ▶ if (defined $filter) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $filter) 7 0 7
125 # Qualify symbolic references
126 7 ▶ $filter = (ref $filter eq 'CODE' ? $filter : *{qualify_to_ref("filter_$filter")}{CODE}) ||
Branch: 1 branch
BranchTrueFalseTotal
ref $filter eq 'CODE' ? : 1 6 7
Condition100%2/2
(ref $filter eq 'CODE' ? $filter : *{&qualify_to_ref("filter_$filter");}{'CODE'}) || die("No filter found named: '${filter}'")
l!l
61
MC/DC100%1/1
(ref $filter eq 'CODE' ? $filter : *{&qualify_to_ref("filter_$filter");}{'CODE'}) || die("No filter found named: '${filter}'")
(ref $filter eq 'CODE' ? $filter : *{&qualify_to_ref("filter_$filter");}{'CODE'})
Truth table100%2/2
A
A= (ref $filter eq 'CODE' ? $filter : *{&qualify_to_ref("filter_$filter");}{'CODE'})
Aresult
11
00
127 die "No filter found named: '$filter'";
128 6 for my $field ( keys %valid ) {
129 # apply filter, modifying %valid by reference, skipping undefined values
130 13 _filter_apply(\%valid,$field,$filter);
131 }
132 }
133 }
134  
135 # Apply specific filters
136 126 ▶ while ( my ($field,$filters) = each %{$profile->{field_filters} }) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$$profile{"field_filters"};}) 7 125 132
137 7 for my $filter ( _arrayify($filters)) {
138 7 ▶ if (defined $filter) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $filter) 7 0 7
139 # Qualify symbolic references
140 7 ▶ $filter = (ref $filter eq 'CODE' ? $filter : *{qualify_to_ref("filter_$filter")}{CODE}) ||
Branch: 1 branch
BranchTrueFalseTotal
ref $filter eq 'CODE' ? : 2 5 7
Condition100%2/2
(ref $filter eq 'CODE' ? $filter : *{&qualify_to_ref("filter_$filter");}{'CODE'}) || die("No filter found named '${filter}'")
l!l
61
MC/DC100%1/1
(ref $filter eq 'CODE' ? $filter : *{&qualify_to_ref("filter_$filter");}{'CODE'}) || die("No filter found named '${filter}'")
(ref $filter eq 'CODE' ? $filter : *{&qualify_to_ref("filter_$filter");}{'CODE'})
Truth table100%2/2
A
A= (ref $filter eq 'CODE' ? $filter : *{&qualify_to_ref("filter_$filter");}{'CODE'})
Aresult
11
00
141 die "No filter found named '$filter'";
142  
143 # apply filter, modifying %valid by reference
144 6 _filter_apply(\%valid,$field,$filter);
145 }
146 }
147 }
148  
149 # add in specific filters from the regexp map
150 125 ▶ while ( my ($re,$filters) = each %{$profile->{field_filter_regexp_map} }) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$$profile{"field_filter_regexp_map"};}) 6 124 130
151 6 my $sub = _create_sub_from_RE($re);
152  
153 6 for my $filter ( _arrayify($filters)) {
154 6 ▶ if (defined $filter) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $filter) 6 0 6
155 # Qualify symbolic references
156 6 ▶ $filter = (ref $filter eq 'CODE' ? $filter : *{qualify_to_ref("filter_$filter")}{CODE}) ||
Branch: 1 branch
BranchTrueFalseTotal
ref $filter eq 'CODE' ? : 0 6 6
Condition100%2/2
(ref $filter eq 'CODE' ? $filter : *{&qualify_to_ref("filter_$filter");}{'CODE'}) || die("No filter found named '${filter}'")
l!l
51
MC/DC100%1/1
(ref $filter eq 'CODE' ? $filter : *{&qualify_to_ref("filter_$filter");}{'CODE'}) || die("No filter found named '${filter}'")
(ref $filter eq 'CODE' ? $filter : *{&qualify_to_ref("filter_$filter");}{'CODE'})
Truth table100%2/2
A
A= (ref $filter eq 'CODE' ? $filter : *{&qualify_to_ref("filter_$filter");}{'CODE'})
Aresult
11
00
157 die "No filter found named '$filter'";
158  
159 59 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
160  
161 # find all the keys that match this RE and apply filters to them
162 5 for my $field (grep { $sub->($_) } (keys %valid)) {
163 # apply filter, modifying %valid by reference
164 7 _filter_apply(\%valid,$field,$filter);
165 }
166 }
167 }
168 }
169  
170 # store the filtered data away for later use
171 124 $self->{__FILTERED_DATA} = \%valid;
172  
173 124 my %required = map { $_ => 1 } _arrayify($profile->{required});
174 124 my %optional = map { $_ => 1 } _arrayify($profile->{optional});
175  
176 # loop through and add fields to %required and %optional based on regular expressions
177 124 my $required_re = _create_sub_from_RE($profile->{required_regexp});
178 124 my $optional_re = _create_sub_from_RE($profile->{optional_regexp});
179  
180 124 for my $k (keys %valid) {
181 256 ▶ if ($required_re && $required_re->($k)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($required_re and &$required_re($k)) 1 255 256
Condition100%3/3
$required_re and &$required_re($k)
!ll&&!rl&&r
24961
MC/DC100%2/2
$required_re and &$required_re($k)
$required_re &$required_re($k)
Truth table100%3/3
A and B
A= $required_reB= &$required_re($k)
ABresult
0X0
100
111
182 1 $required{$k} = 1;
183 }
184  
185 256 ▶ if ($optional_re && $optional_re->($k)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($optional_re and &$optional_re($k)) 6 250 256
Condition100%3/3
$optional_re and &$optional_re($k)
!ll&&!rl&&r
24556
MC/DC100%2/2
$optional_re and &$optional_re($k)
$optional_re &$optional_re($k)
Truth table100%3/3
A and B
A= $optional_reB= &$optional_re($k)
ABresult
0X0
100
111
186 6 $optional{$k} = 1;
187 }
188 }
189  
190 # handle "require_some"
191 124 my %require_some;
192 124 ▶ while ( my ( $field, $deps) = each %{$profile->{require_some}} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$$profile{"require_some"};}) 3 124 127
193 3 for my $dep (_arrayify($deps)){
194 11 $require_some{$dep} = 1;
195 }
196 }
197  
198  
199 # Remove all empty fields
200 124 for my $field (keys %valid) {
201 256 ▶ if (ref $valid{$field}) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $valid{$field}) { } 20 236 256
202 20 ▶ if ( ref $valid{$field} eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $valid{$field} eq "ARRAY") 18 2 20
203 18 ▶ for (my $i = 0; $i < scalar @{ $valid{$field} }; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < scalar @{$valid{$field};}) 38 18 56
204 38 ▶ $valid{$field}->[$i] = undef unless (defined $valid{$field}->[$i] and length $valid{$field}->[$i] and $valid{$field}->[$i] !~ /^\x00$/);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $valid{$field}[$i] and length $valid{$field}[$i] and not $valid{$field}[$i] =~ /^\x00$/ 1 34 35
Condition83%5/6
defined $valid{$field}[$i] and length $valid{$field}[$i] and not $valid{$field}[$i] =~ /^\x00$/
!ll&&!rl&&r
4034
defined $valid{$field}[$i] and length $valid{$field}[$i]
!ll&&!rl&&r
3134
MC/DC66%2/3
defined $valid{$field}[$i] and length $valid{$field}[$i] and not $valid{$field}[$i] =~ /^\x00$/
defined $valid{$field}[$i] length $valid{$field}[$i] not $valid{$field}[$i] =~ /^\x00$/
Truth table75%3/4
A and B and not(C)
A= defined $valid{$field}[$i]B= length $valid{$field}[$i]C= not $valid{$field}[$i] =~ /^\x00$/
ABCresult
0XX0
10X0
1100
1111
205 }
206 # If all fields are empty, we delete it.
207 18 ▶ delete $valid{$field} unless grep { defined $_ } @{$valid{$field}};
Branch: 1 branch
BranchTrueFalseTotal
unless grep {defined $_;} @{$valid{$field};} 2 16 18
208  
209 }
210 }
211 else {
212 236 ▶ delete $valid{$field} unless (defined $valid{$field} and length $valid{$field} and $valid{$field} !~ /^\x00$/);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $valid{$field} and length $valid{$field} and not $valid{$field} =~ /^\x00$/ 1 233 234
Condition83%5/6
defined $valid{$field} and length $valid{$field} and not $valid{$field} =~ /^\x00$/
!ll&&!rl&&r
30233
defined $valid{$field} and length $valid{$field}
!ll&&!rl&&r
21233
MC/DC66%2/3
defined $valid{$field} and length $valid{$field} and not $valid{$field} =~ /^\x00$/
defined $valid{$field} length $valid{$field} not $valid{$field} =~ /^\x00$/
Truth table75%3/4
A and B and not(C)
A= defined $valid{$field}B= length $valid{$field}C= not $valid{$field} =~ /^\x00$/
ABCresult
0XX0
10X0
1100
1111
213 }
214 }
215  
216 # Check if the presence of some fields makes other optional fields required.
217 124 ▶ while ( my ( $field, $deps) = each %{$profile->{dependencies}} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$$profile{"dependencies"};}) 15 124 139
218 15 ▶ if (defined $valid{$field}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $valid{$field}) 11 4 15
219 11 ▶ if (ref($deps) eq 'HASH') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $deps eq 'HASH') { } 6 5 11
elsif (ref $deps eq 'CODE') { } 4 1 5
220 6 for my $key (keys %$deps) {
221 # Handle case of a key with a single value given as an arrayref
222 # There is probably a better, more general solution to this problem.
223 12 my $val_to_compare;
224 12 ▶ if ((ref $valid{$field} eq 'ARRAY') and (scalar @{ $valid{$field} } == 1)) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $valid{$field} eq 'ARRAY' and scalar @{$valid{$field};} == 1) { } 2 10 12
Condition66%2/3
ref $valid{$field} eq "ARRAY" and scalar @{$valid{$field};} == 1
!ll&&!rl&&r
1002
MC/DC50%1/2
ref $valid{$field} eq "ARRAY" and scalar @{$valid{$field};} == 1
ref $valid{$field} eq "ARRAY" scalar @{$valid{$field};} == 1
Truth table66%2/3
A and B
A= ref $valid{$field} eq "ARRAY"B= scalar @{$valid{$field};} == 1
ABresult
0X0
100
111
225 2 $val_to_compare = $valid{$field}->[0];
226 }
227 else {
228 10 $val_to_compare = $valid{$field}
229 }
230  
231 12 ▶ if($val_to_compare eq $key){
Branch: 1 branch
BranchTrueFalseTotal
if ($val_to_compare eq $key) 6 6 12
232 6 for my $dep (_arrayify($deps->{$key})){
233 11 $required{$dep} = 1;
234 }
235 }
236 }
237 }
238 elsif (ref $deps eq "CODE") {
239 4 for my $val (_arrayify($valid{$field})) {
240 5 my $returned_deps = $deps->($self, $val);
241  
242 5 for my $dep (_arrayify($returned_deps)) {
243 2 $required{$dep} = 1;
244 }
245 }
246 }
247 else {
248 1 for my $dep (_arrayify($deps)){
249 2 $required{$dep} = 1;
250 }
251 }
252 }
253 }
254  
255 # check dependency groups
256 # the presence of any member makes them all required
257 124 for my $group (values %{ $profile->{dependency_groups} }) {
258 0 my $require_all = 0;
259 0 for my $field (_arrayify($group)) {
260 0 ▶ $require_all = 1 if $valid{$field};
Branch: 1 branch
BranchTrueFalseTotal
if $valid{$field} 0 0 0
261 }
262 0 ▶ if ($require_all) {
Branch: 1 branch
BranchTrueFalseTotal
if ($require_all) 0 0 0
263 0 map { $required{$_} = 1 } _arrayify($group);
264 }
265 }
266  
267 # Find unknown
268 @unknown =
269 124 ▶ grep { not (exists $optional{$_} or exists $required{$_} or exists $require_some{$_} ) } keys %valid;
Condition100%6/6
exists $optional{$_} || exists $required{$_} || exists $require_some{$_}
l!l&&r!l&&!r
202346
exists $optional{$_} or exists $required{$_}
l!l&&r!l&&!r
2018249
MC/DC100%3/3
exists $optional{$_} || exists $required{$_} || exists $require_some{$_}
exists $optional{$_} exists $required{$_} exists $require_some{$_}
Truth table100%4/4
A or B || C
A= exists $optional{$_}B= exists $required{$_}C= exists $require_some{$_}
ABCresult
1XX1
01X1
0011
0000
270 # and remove them from the list
271 124 for my $field ( @unknown ) {
272 46 delete $valid{$field};
273 }
274  
275 # Add defaults from defaults_regexp_map
276 124 my %private_defaults;
277 124 my @all_possible = keys %optional, keys %required, keys %require_some;
278 124 ▶ while ( my ($re,$value) = each %{$profile->{defaults_regexp_map}} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$$profile{"defaults_regexp_map"};}) 1 124 125
279 # We only add defaults for known fields.
280 1 for (@all_possible) {
281 3 ▶ $private_defaults{$_} = $value if m/$re/;
Branch: 1 branch
BranchTrueFalseTotal
if /$re/ 2 1 3
282 }
283 }
284  
285 # Fill defaults
286 my %combined_defaults = (
287 %private_defaults,
288 124 ▶ %{ $profile->{defaults} || {} }
Branch: 1 branch
BranchTrueFalseTotal
$profile->{"defaults"} or {} 123 1 124
289 );
290 124 ▶ while ( my ($field,$value) = each %combined_defaults ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %combined_defaults) 3 124 127
291 3 ▶ unless(exists $valid{$field}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $valid{$field}) 3 0 3
292 3 ▶ if (ref($value) && ref($value) eq "CODE") {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $value and ref $value eq 'CODE') { } 1 2 3
Condition66%2/3
ref $value and ref $value eq "CODE"
!ll&&!rl&&r
201
MC/DC50%1/2
ref $value and ref $value eq "CODE"
ref $value ref $value eq "CODE"
Truth table66%2/3
A and B
A= ref $valueB= ref $value eq "CODE"
ABresult
0X0
100
111
293 1 $valid{$field} = $value->($self);
294 } else {
295 2 $valid{$field} = $value;
296 }
297 }
298 }
299  
300 # Check for required fields
301 124 for my $field ( keys %required ) {
302 212 ▶ push @missings, $field unless exists $valid{$field};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $valid{$field} 29 183 212
303 }
304  
305 # Check for the absence of require_some fields
306 124 ▶ while ( my ( $field, $deps) = each %{$profile->{require_some}} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$$profile{"require_some"};}) 3 124 127
307 3 my $enough_required_fields = 0;
308 3 my @deps = _arrayify($deps);
309 # num fields to require is first element in array if looks like a digit, 1 otherwise.
310 3 ▶ my $num_fields_to_require = ($deps[0] =~ m/^\d+$/) ? $deps[0] : 1;
Branch: 1 branch
BranchTrueFalseTotal
$deps[0] =~ /^\d+$/ ? : 2 1 3
311 3 for my $dep (@deps){
312 11 ▶ $enough_required_fields++ if exists $valid{$dep};
Branch: 1 branch
BranchTrueFalseTotal
if exists $valid{$dep} 3 8 11
313 }
314 3 ▶ push @missings, $field unless ($enough_required_fields >= $num_fields_to_require);
Branch: 1 branch
BranchTrueFalseTotal
unless $enough_required_fields >= $num_fields_to_require 1 2 3
315 }
316  
317 # add in the constraints from the regexp maps
318 # We don't want to modify the profile, so we use a new variable.
319 124 ▶ $profile->{constraints} ||= {};
Condition100%2/2
$profile->{"constraints"} ||= {}
l!l
6856
MC/DC100%1/1
$profile->{"constraints"} ||= {}
$profile->{"constraints"}
Truth table100%2/2
A
A= $profile->{"constraints"}
Aresult
11
00
320 my $private_constraints = {
321 124 %{ $profile->{constraints} },
322 _add_constraints_from_map($profile,'constraint',\%valid),
323 };
324 124 ▶ $profile->{constraint_methods} ||= {};
Condition100%2/2
$profile->{"constraint_methods"} ||= {}
l!l
5767
MC/DC100%1/1
$profile->{"constraint_methods"} ||= {}
$profile->{"constraint_methods"}
Truth table100%2/2
A
A= $profile->{"constraint_methods"}
Aresult
11
00
325 my $private_constraint_methods = {
326 124 %{ $profile->{constraint_methods} },
327 _add_constraints_from_map($profile,'constraint_method',\%valid),
328 };
329  
330 #Decide which fields to untaint
331 124 my ($untaint_all, %untaint_hash);
332 124 ▶ if (defined $profile->{untaint_regexp_map} or defined $profile->{untaint_constraint_fields} ) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $profile->{'untaint_regexp_map'} or defined $profile->{'untaint_constraint_fields'}) { } 3 121 124
elsif (defined $profile->{'untaint_all_constraints'} and $profile->{'untaint_all_constraints'} == 1) { } 10 222 232
Condition66%2/3
defined $profile->{"untaint_regexp_map"} or defined $profile->{"untaint_constraint_fields"}
l!l&&r!l&&!r
03121
MC/DC50%1/2
defined $profile->{"untaint_regexp_map"} or defined $profile->{"untaint_constraint_fields"}
defined $profile->{"untaint_regexp_map"} defined $profile->{"untaint_constraint_fields"}
Truth table66%2/3
A or B
A= defined $profile->{"untaint_regexp_map"}B= defined $profile->{"untaint_constraint_fields"}
ABresult
1X1
011
000
333 # first deal with untaint_constraint_fields
334 3 ▶ if (defined($profile->{untaint_constraint_fields})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $profile->{"untaint_constraint_fields"}) 3 0 3
335 3 ▶ if (ref $profile->{untaint_constraint_fields} eq "ARRAY") {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $profile->{'untaint_constraint_fields'} eq 'ARRAY') { } 3 0 3
elsif ($valid{$profile->{'untaint_constraint_fields'}}) { } 0 0 0
336 3 for my $field (@{$profile->{untaint_constraint_fields}}) {
337 3 $untaint_hash{$field} = 1;
338 }
339 }
340 elsif ($valid{$profile->{untaint_constraint_fields}}) {
341 0 $untaint_hash{$profile->{untaint_constraint_fields}} = 1;
342 }
343 }
344  
345 # now look at untaint_regexp_map
346 3 ▶ if(defined($profile->{untaint_regexp_map})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $profile->{"untaint_regexp_map"}) 0 3 3
347 0 my @untaint_regexes;
348 0 ▶ if(ref $profile->{untaint_regexp_map} eq "ARRAY") {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $profile->{'untaint_regexp_map'} eq 'ARRAY') { } 0 0 0
349 0 @untaint_regexes = @{$profile->{untaint_regexp_map}};
350 }
351 else {
352 0 push(@untaint_regexes, $profile->{untaint_regexp_map});
353 }
354  
355 0 for my $regex (@untaint_regexes) {
356 # look at both constraints and constraint_methods
357 0 for my $field (keys %$private_constraints, keys %$private_constraint_methods) {
358 0 ▶ next if($untaint_hash{$field});
Branch: 1 branch
BranchTrueFalseTotal
if $untaint_hash{$field} 0 0 0
359 0 ▶ $untaint_hash{$field} = 1 if( $field =~ $regex );
Branch: 1 branch
BranchTrueFalseTotal
if $field =~ /$regex/ 0 0 0
360 }
361 }
362 }
363 }
364 ▶ elsif ((defined($profile->{untaint_all_constraints}))
Condition66%2/3
defined $profile->{"untaint_all_constraints"} and $profile->{"untaint_all_constraints"} == 1
!ll&&!rl&&r
111010
MC/DC50%1/2
defined $profile->{"untaint_all_constraints"} and $profile->{"untaint_all_constraints"} == 1
defined $profile->{"untaint_all_constraints"} $profile->{"untaint_all_constraints"} == 1
Truth table66%2/3
A and B
A= defined $profile->{"untaint_all_constraints"}B= $profile->{"untaint_all_constraints"} == 1
ABresult
0X0
100
111
365 && ($profile->{untaint_all_constraints} == 1)) {
366 10 $untaint_all = 1;
367 }
368  
369 124 $self->_check_constraints($private_constraints,\%valid,$untaint_all,\%untaint_hash);
370  
371 122 my $force_method_p = 1;
372 122 $self->_check_constraints($private_constraint_methods,\%valid,$untaint_all,\%untaint_hash, $force_method_p);
373  
374 # add back in missing optional fields from the data hash if we need to
375 122 for my $field ( keys %data ) {
376 250 ▶ if ($profile->{missing_optional_valid} and $optional{$field} and (not exists $valid{$field})) {
Branch: 1 branch
BranchTrueFalseTotal
if ($profile->{"missing_optional_valid"} and $optional{$field} and not exists $valid{$field}) 2 248 250
Condition100%6/6
$profile->{"missing_optional_valid"} and $optional{$field} and not exists $valid{$field}
!ll&&!rl&&r
24532
$profile->{"missing_optional_valid"} and $optional{$field}
!ll&&!rl&&r
23785
MC/DC100%3/3
$profile->{"missing_optional_valid"} and $optional{$field} and not exists $valid{$field}
$profile->{"missing_optional_valid"} $optional{$field} not exists $valid{$field}
Truth table100%4/4
A and B and not(C)
A= $profile->{"missing_optional_valid"}B= $optional{$field}C= not exists $valid{$field}
ABCresult
0XX0
10X0
1100
1111
377 2 $valid{$field} = undef;
378 }
379 }
380  
381 # all invalid fields are removed from valid hash
382 122 for my $field (keys %{ $self->{invalid} }) {
383 71 delete $valid{$field};
384 }
385  
386 122 my ($missing,$invalid);
387  
388 122 ▶ $self->{valid} ||= {};
Condition50%1/2
$self->{"valid"} ||= {}
l!l
0122
MC/DC0%0/1
$self->{"valid"} ||= {}
$self->{"valid"}
Truth table50%1/2
A
A= $self->{"valid"}
Aresult
11
00
389 122 $self->{valid} = { %valid , %{$self->{valid}} };
390 122 $self->{missing} = { map { $_ => 1 } @missings };
391 122 $self->{unknown} = { map { $_ => $data{$_} } @unknown };
392  
393 }
394  
395 =pod
396  
397 =head1 success();
398  
399 This method returns true if there were no invalid or missing fields,
400 else it returns false.
401  
402 As a shorthand, When the $results object is used in boolean context, it is overloaded
403 to use the value of success() instead. That allows creation of a syntax like this one used
404 in C<CGI::Application::Plugin::ValidateRM>:
405  
406 my $results = $self->check_rm('form_display','_form_profile') || return $self->dfv_error_page;
407  
408 =cut
409  
410 sub success {
411 27 ▶ my $self = shift;
Subroutine success: called CC=2 SCAR=6.9
Pod undocumented
412 27 ▶ return !($self->has_invalid || $self->has_missing);
Condition100%3/3
$self->has_invalid || $self->has_missing
l!l&&r!l&&!r
12510
MC/DC100%2/2
$self->has_invalid || $self->has_missing
$self->has_invalid $self->has_missing
Truth table100%3/3
A || B
A= $self->has_invalidB= $self->has_missing
ABresult
1X1
011
000
413 }
414  
415 =head1 valid( [[field] [, value]] );
416  
417 In list context with no arguments, it returns the list of fields which
418 contain valid values:
419  
420 @all_valid_field_names = $r->valid;
421  
422 In a scalar context with no arguments, it returns an hash reference which
423 contains the valid fields as keys and their input as values:
424  
425 $all_valid_href = $r->valid;
426  
427 If called with one argument in scalar context, it returns the value of that
428 C<field> if it contains valid data, C<undef> otherwise. The value will be an
429 array ref if the field had multiple values:
430  
431 $value = $r->valid('field');
432  
433 If called with one argument in list context, it returns the values of C<field>
434 as an array:
435  
436 @values = $r->valid('field');
437  
438 If called with two arguments, it sets C<field> to C<value> and returns C<value>.
439 This form is useful to alter the results from within some constraints.
440 See the L<Data::FormValidator::Constraints> documentation.
441  
442 $new_value = $r->valid('field',$new_value);
443  
444 =cut
445  
446 sub valid {
447 81 ▶ my $self = shift;
Subroutine valid: called CC=5 SCAR=16.1
Pod undocumented
448 81 my $key = shift;
449 81 my $val = shift;
450 81 ▶ $self->{valid}{$key} = $val if defined $val;
Branch: 1 branch
BranchTrueFalseTotal
if defined $val 0 81 81
451  
452 81 ▶ if (defined $key) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $key) 42 39 81
453 42 ▶ return wantarray ? _arrayify($self->{valid}{$key}) : $self->{valid}{$key};
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 2 40 42
454 }
455  
456 # If we got this far, there were no arguments passed.
457 39 ▶ return wantarray ? keys %{ $self->{valid} } : $self->{valid};
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 1 38 39
458 }
459  
460  
461 =pod
462  
463 =head1 has_missing()
464  
465 This method returns true if the results contain missing fields.
466  
467 =cut
468  
469 sub has_missing {
470 52 ▶ return scalar keys %{$_[0]{missing}};
Subroutine has_missing: called CC=1 SCAR=0.0
Pod undocumented
471 }
472  
473 =pod
474  
475 =head1 missing( [field] )
476  
477 In list context it returns the list of fields which are missing.
478 In a scalar context, it returns an array reference to the list of missing fields.
479  
480 If called with an argument, it returns true if that C<field> is missing,
481 undef otherwise.
482  
483 =cut
484  
485 sub missing {
486 44 ▶ return $_[0]{missing}{$_[1]} if (defined $_[1]);
Subroutine missing: called CC=3 SCAR=11.0
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
if defined $_[1] 11 33 44
487  
488 33 ▶ wantarray ? keys %{$_[0]{missing}} : [ keys %{$_[0]{missing}} ];
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 1 32 33
489 }
490  
491  
492 =pod
493  
494 =head1 has_invalid()
495  
496 This method returns true if the results contain fields with invalid
497 data.
498  
499 =cut
500  
501 sub has_invalid {
502 67 ▶ return scalar keys %{$_[0]{invalid}};
Subroutine has_invalid: called CC=1 SCAR=0.0
Pod undocumented
503 }
504  
505 =pod
506  
507 =head1 invalid( [field] )
508  
509 In list context, it returns the list of fields which contains invalid value.
510  
511 In a scalar context, it returns an hash reference which contains the invalid
512 fields as keys, and references to arrays of failed constraints as values.
513  
514 If called with an argument, it returns the reference to an array of failed
515 constraints for C<field>.
516  
517 =cut
518  
519 sub invalid {
520 34 ▶ my $self = shift;
Subroutine invalid: called CC=3 SCAR=11.0
Pod undocumented
521 34 my $field = shift;
522 34 ▶ return $self->{invalid}{$field} if defined $field;
Branch: 1 branch
BranchTrueFalseTotal
if defined $field 13 21 34
523  
524 21 ▶ wantarray ? keys %{$self->{invalid}} : $self->{invalid};
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 2 19 21
525 }
526  
527 =pod
528  
529 =head1 has_unknown()
530  
531 This method returns true if the results contain unknown fields.
532  
533 =cut
534  
535 sub has_unknown {
536 0 ▶ return scalar keys %{$_[0]{unknown}};
Subroutine has_unknown: not called CC=1 SCAR=6.9
Pod undocumented
537  
538 }
539  
540 =pod
541  
542 =head1 unknown( [field] )
543  
544 In list context, it returns the list of fields which are unknown.
545 In a scalar context, it returns an hash reference which contains the unknown
546 fields and their values.
547  
548 If called with an argument, it returns the value of that C<field> if it
549 is unknown, undef otherwise.
550  
551 =cut
552  
553 sub unknown {
554 29 ▶ return (wantarray ? _arrayify($_[0]{unknown}{$_[1]}) : $_[0]{unknown}{$_[1]})
Subroutine unknown: called CC=4 SCAR=13.9
Pod undocumented
Branch: 2 branches
BranchTrueFalseTotal
if defined $_[1] 4 25 29
wantarray ? : 2 2 4
555 if (defined $_[1]);
556  
557 25 ▶ wantarray ? keys %{$_[0]{unknown}} : $_[0]{unknown};
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 25 0 25
558 }
559  
560  
561 =pod
562  
563 =head1 msgs([config parameters])
564  
565 This method returns a hash reference to error messages. The exact format
566 is determined by parameters in the C<msgs> area of the validation profile,
567 described in the L<Data::FormValidator> documentation.
568  
569 B<NOTE:> the C<msgs> parameter in the profile can take a code reference as a
570 value, allowing complete control of how messages are generated. If such a code
571 reference was provided there, it will be called here instead of the usual
572 processing, described below. It will receive as arguments the L<Data::FormValidator::Results>
573 object and a hash reference of control parameters.
574  
575 The hashref passed in should contain the same options that you can define in
576 the validation profile. This allows you to separate the controls for message
577 display from the rest of the profile. While validation profiles may be
578 different for every form, you may wish to format messages the same way across
579 many projects.
580  
581 Controls passed into the <msgs> method will be applied first, followed by ones
582 applied in the profile. This allows you to keep the controls you pass to
583 C<msgs> as "global" and override them in a specific profile if needed.
584  
585 =cut
586  
587 sub msgs {
588 17 ▶ my $self = shift;
Subroutine msgs: called CC=3 SCAR=11.0
Pod undocumented
589 17 ▶ my $msgs = $self->{profile}{msgs} || {};
Condition100%2/2
$self->{'profile'}{'msgs'} || {}
l!l
152
MC/DC100%1/1
$self->{'profile'}{'msgs'} || {}
$self->{'profile'}{'msgs'}
Truth table100%2/2
A
A= $self->{'profile'}{'msgs'}
Aresult
11
00
590 17 ▶ if ((ref $msgs eq 'CODE')) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $msgs eq 'CODE') { } 1 16 17
591 1 return $msgs->($self,@_);
592 } else {
593 16 return $self->_generate_msgs(@_);
594 }
595 }
596  
597  
598 sub _generate_msgs {
599 16 ▶ my $self = shift;
Subroutine _generate_msgs: called CC=16 SCAR=27.8
600 16 ▶ my $controls = shift || {};
Condition100%2/2
shift() || {}
l!l
313
MC/DC100%1/1
shift() || {}
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
601 16 ▶ if (defined $controls and ref $controls ne 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $controls and ref $controls ne "HASH") 0 16 16
Condition33%1/3
defined $controls and ref $controls ne "HASH"
!ll&&!rl&&r
0160
MC/DC0%0/2
defined $controls and ref $controls ne "HASH"
defined $controls ref $controls ne "HASH"
Truth table33%1/3
A and B
A= defined $controlsB= ref $controls ne "HASH"
ABresult
0X0
100
111
602 0 die "$0: parameter passed to msgs must be a hash ref";
603 }
604  
605  
606 # Allow msgs to be called more than one to accumulate error messages
607 16 ▶ $self->{msgs} ||= {};
Condition100%2/2
$self->{"msgs"} ||= {}
l!l
313
MC/DC100%1/1
$self->{"msgs"} ||= {}
$self->{"msgs"}
Truth table100%2/2
A
A= $self->{"msgs"}
Aresult
11
00
608 16 ▶ $self->{profile}{msgs} ||= {};
Condition100%2/2
$self->{"profile"}{"msgs"} ||= {}
l!l
142
MC/DC100%1/1
$self->{"profile"}{"msgs"} ||= {}
$self->{"profile"}{"msgs"}
Truth table100%2/2
A
A= $self->{"profile"}{"msgs"}
Aresult
11
00
609 16 $self->{msgs} = { %{ $self->{msgs} }, %$controls };
610  
611 # Legacy typo support.
612 16 for my $href ($self->{msgs}, $self->{profile}{msgs}) {
613 32 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $href->{"invalid_separator"} and defined $href->{"invalid_seperator"}) 1 31 32
Condition100%3/3
not defined $href->{"invalid_separator"} and defined $href->{"invalid_seperator"}
!ll&&!rl&&r
1301
MC/DC100%2/2
not defined $href->{"invalid_separator"} and defined $href->{"invalid_seperator"}
not defined $href->{"invalid_separator"} defined $href->{"invalid_seperator"}
Truth table100%3/3
not(A) and B
A= not defined $href->{"invalid_separator"}B= defined $href->{"invalid_seperator"}
ABresult
0X0
100
111
614 (not defined $href->{invalid_separator})
615 && (defined $href->{invalid_seperator})
616 ) {
617 1 $href->{invalid_separator} = $href->{invalid_seperator};
618 }
619 }
620  
621 my %profile = (
622 prefix => '',
623 missing => 'Missing',
624 invalid => 'Invalid',
625 invalid_separator => ' ',
626  
627 format => '<span style="color:red;font-weight:bold" class="dfv_errors">* %s</span>',
628 16 %{ $self->{msgs} },
629 16 %{ $self->{profile}{msgs} },
630 );
631  
632  
633 16 my %msgs = ();
634  
635 # Add invalid messages to hash
636 # look at all the constraints, look up their messages (or provide a default)
637 # add field + formatted constraint message to hash
638 16 ▶ if ($self->has_invalid) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_invalid) 9 7 16
639 9 my $invalid = $self->invalid;
640 9 for my $i ( keys %$invalid ) {
641 $msgs{$i} = join $profile{invalid_separator}, map {
642 18 ▶ _error_msg_fmt($profile{format},($profile{constraints}{$_} || $profile{invalid}))
Condition66%2/3
$profile{'constraints'}{$_} || $profile{'invalid'}
l!l&&r!l&&!r
1080
MC/DC0%0/2
$profile{'constraints'}{$_} || $profile{'invalid'}
$profile{'constraints'}{$_} $profile{'invalid'}
Truth table66%2/3
A || B
A= $profile{'constraints'}{$_}B= $profile{'invalid'}
ABresult
1X1
011
000
643 14 } @{ $invalid->{$i} };
644 }
645 }
646  
647 # Add missing messages, if any
648 16 ▶ if ($self->has_missing) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_missing) 6 10 16
649 6 my $missing = $self->missing;
650 6 for my $m (@$missing) {
651 6 $msgs{$m} = _error_msg_fmt($profile{format},$profile{missing});
652 }
653 }
654  
655 16 my $msgs_ref = prefix_hash($profile{prefix},\%msgs);
656  
657 16 ▶ if (! $self->success) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->success) 12 4 16
658 12 ▶ $msgs_ref->{ $profile{any_errors} } = 1 if defined $profile{any_errors};
Branch: 1 branch
BranchTrueFalseTotal
if defined $profile{"any_errors"} 3 9 12
659 }
660  
661 16 return $msgs_ref;
662  
663 }
664  
665 =pod
666  
667 =head1 meta()
668  
669 In a few cases, a constraint may discover meta data that is useful
670 to access later. For example, when using L<Data::FormValidator::Constraints::Upload>, several bits of meta data are discovered about files in the process
671 of validating. These can include "bytes", "width", "height" and "extension".
672 The C<meta()> function is used by constraint methods to set this data. It's
673 also used to access this data. Here are some examples.
674  
675 # return all field names that have meta data
676 my @fields = $results->meta();
677  
678 # To retrieve all meta data for a field:
679 $meta_href = $results->meta('img');
680  
681 # Access a particular piece:
682 $width = $results->meta('img')->{width};
683  
684 Here's how to set some meta data. This is useful to know if you are
685 writing your own complex constraint.
686  
687 $self->meta('img', {
688 width => '50',
689 height => '60',
690 });
691  
692 This function does not currently support multi-valued fields. If it
693 does in the future, the above syntax will still work.
694  
695 =cut
696  
697 sub meta {
698 0 ▶ my $self = shift;
Subroutine meta: not called CC=5 SCAR=34.0
Pod undocumented
699 0 my $field = shift;
700 0 my $data = shift;
701  
702 # initialize if it's the first call
703 0 ▶ $self->{__META} ||= {};
Condition0%0/2
$self->{"__META"} ||= {}
l!l
00
MC/DC0%0/1
$self->{"__META"} ||= {}
$self->{"__META"}
Truth table0%0/2
A
A= $self->{"__META"}
Aresult
11
00
704  
705 0 ▶ if ($data) {
Branch: 1 branch
BranchTrueFalseTotal
if ($data) 0 0 0
706 0 ▶ (ref $data eq 'HASH') or die 'meta: data passed not a hash ref';
Branch: 1 branch
BranchTrueFalseTotal
ref $data eq "HASH" or die "meta: data passed not a hash ref" 0 0 0
707 0 $self->{__META}{$field} = $data;
708 }
709  
710  
711 # If we are passed a field, return data for that field
712 0 ▶ if ($field) {
Branch: 1 branch
BranchTrueFalseTotal
if ($field) { } 0 0 0
713 0 return $self->{__META}{$field};
714 }
715 # Otherwise return a list of all fields that have meta data
716 else {
717 0 return keys %{ $self->{__META} };
718 }
719 }
720  
721 # These are documented in ::Constraints, in the section
722 # on writing your own routines. It was more intuitive
723 # for the user to look there.
724  
725 sub get_input_data {
726 157 ▶ my $self = shift;
Subroutine get_input_data: called CC=2 SCAR=7.9
Pod undocumented
727 157 my %p = @_;
728 157 ▶ if ($p{as_hashref}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($p{'as_hashref'}) { } 0 157 157
729 0 my %hash = $self->_get_input_as_hash( $self->{__INPUT_DATA} );
730 0 return \%hash;
731 }
732 else {
733 157 return $self->{__INPUT_DATA};
734 }
735 }
736  
737 sub get_filtered_data {
738 21 ▶ my $self = shift;
Subroutine get_filtered_data: called CC=1 SCAR=0.0
Pod undocumented
739 21 return $self->{__FILTERED_DATA};
740 }
741  
742 sub get_current_constraint_field {
743 5 ▶ my $self = shift;
Subroutine get_current_constraint_field: called CC=1 SCAR=0.0
Pod undocumented
744 5 return $self->{__CURRENT_CONSTRAINT_FIELD};
745 }
746  
747 sub get_current_constraint_value {
748 36 ▶ my $self = shift;
Subroutine get_current_constraint_value: called CC=1 SCAR=0.0
Pod undocumented
749 36 return $self->{__CURRENT_CONSTRAINT_VALUE};
750 }
751  
752 sub get_current_constraint_name {
753 38 ▶ my $self = shift;
Subroutine get_current_constraint_name: called CC=1 SCAR=0.0
Pod undocumented
754 38 return $self->{__CURRENT_CONSTRAINT_NAME};
755 }
756  
757 sub untainted_constraint_value {
758 16 ▶ my $self = shift;
Subroutine untainted_constraint_value: called CC=3 SCAR=11.0
Pod undocumented
759 16 my $match = shift;
760  
761 16 ▶ return undef unless defined $match;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $match 4 12 16
762 12 ▶ return $self->{__UNTAINT_THIS} ? $match : length $match;
Branch: 1 branch
BranchTrueFalseTotal
$self->{'__UNTAINT_THIS'} ? : 5 7 12
763 }
764  
765 sub set_current_constraint_name {
766 4 ▶ my $self = shift;
Subroutine set_current_constraint_name: called CC=1 SCAR=0.0
Pod undocumented
767 4 my $value = shift;
768 4 $self->{__CURRENT_CONSTRAINT_NAME} = $value;
769 }
770 # same as above
771 sub name_this {
772 35 ▶ my $self = shift;
Subroutine name_this: called CC=1 SCAR=0.0
Pod undocumented
773 35 my $value = shift;
774 35 $self->{__CURRENT_CONSTRAINT_NAME} = $value;
775 }
776  
777 # INPUT: prefix_string, hash reference
778 # Copies the hash and prefixes all keys with prefix_string
779 # OUTPUT: hash reference
780 sub prefix_hash {
781 16 ▶ my ($pre,$href) = @_;
Subroutine prefix_hash: called CC=4 SCAR=14.1
Pod undocumented
782 16 ▶ die "prefix_hash: need two arguments" unless (scalar @_ == 2);
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @_ == 2 0 16 16
783 16 ▶ die "prefix_hash: second argument must be a hash ref" unless (ref $href eq 'HASH');
Branch: 1 branch
BranchTrueFalseTotal
unless ref $href eq "HASH" 0 16 16
784 16 my %out;
785 16 for (keys %$href) {
786 20 $out{$pre.$_} = $href->{$_};
787 }
788 16 return \%out;
789 }
790  
791  
792 # We tolerate two kinds of regular expression formats
793 # First, the preferred format made with "qr", matched using a leading paren
794 # Also, we accept the deprecated format given as strings: 'm/old/'
795 # (which must start with a slash or "m", not a paren)
796 sub _create_sub_from_RE {
797 293 ▶ my $re = shift || return undef;
Subroutine _create_sub_from_RE: called CC=6 SCAR=18.1
Condition100%2/2
shift() || (return undef)
l!l
48245
MC/DC100%1/1
shift() || (return undef)
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
798 48 my $untaint_this = shift;
799 48 my $force_method_p = shift;
800  
801 48 my $sub;
802 # If it's "qr" style
803 48 ▶ if (substr($re,0,1) eq '(') {
Branch: 1 branch
BranchTrueFalseTotal
if (substr($re, 0, 1) eq '(') { } 41 7 48
804 $sub = sub {
805 # With methods, the value is the second argument
806 75 ▶ my $val = $force_method_p ? $_[1] : $_[0];
Subroutine __ANON__: called CC=4 SCAR=13.9
Branch: 1 branch
BranchTrueFalseTotal
$force_method_p ? : 12 63 75
807 75 my ($match) = scalar ($val =~ $re);
808 75 ▶ if ($untaint_this && defined $match) {
Branch: 1 branch
BranchTrueFalseTotal
if ($untaint_this and defined $match) { } 5 70 75
Condition66%2/3
$untaint_this and defined $match
!ll&&!rl&&r
7005
MC/DC50%1/2
$untaint_this and defined $match
$untaint_this defined $match
Truth table66%2/3
A and B
A= $untaint_thisB= defined $match
ABresult
0X0
100
111
809 # pass the value through a RE that matches anything to untaint it.
810 5 my ($untainted) = ($& =~ m/(.*)/s);
811 5 return $untainted;
812 }
813 else {
814 70 return $match;
815 }
816 41 };
817  
818 }
819 else {
820 7 local $SIG{__DIE__} = \&confess;
821 7 ▶ my $return_code = ($untaint_this) ? '; return ($& =~ m/(.*)/s)[0] if defined($`);' : '';
Branch: 1 branch
BranchTrueFalseTotal
$untaint_this ? : 0 7 7
822 # With methods, the value is the second argument
823 7 ▶ if ($force_method_p) {
Branch: 1 branch
BranchTrueFalseTotal
if ($force_method_p) { } 0 7 7
824 0 $sub = eval 'sub { $_[1] =~ '.$re.$return_code. '}';
825 }
826 else {
827 7 $sub = eval 'sub { $_[0] =~ '.$re.$return_code. '}';
828 }
829 7 ▶ die "Error compiling regular expression $re: $@" if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 7 7
830 }
831 48 return $sub;
832 }
833  
834  
835 sub _error_msg_fmt {
836 24 ▶ my ($fmt,$msg) = @_;
Subroutine _error_msg_fmt: called CC=3 SCAR=11.4
837 24 ▶ $fmt ||=
Condition50%1/2
$fmt ||= "<span style=\"color:red;font-weight:bold\" class=\"dfv_errors\">* %s</span>"
l!l
240
MC/DC0%0/1
$fmt ||= "<span style=\"color:red;font-weight:bold\" class=\"dfv_errors\">* %s</span>"
$fmt
Truth table50%1/2
A
A= $fmt
Aresult
11
00
838 '<span style="color:red;font-weight:bold" class="dfv_errors">* %s</span>';
839 24 ▶ ($fmt =~ m/%s/) || die 'format must contain %s';
Branch: 1 branch
BranchTrueFalseTotal
$fmt =~ /%s/ or die "format must contain %s" 0 24 24
840 24 return sprintf $fmt, $msg;
841 }
842  
843  
844  
845 # takes string or array ref as input
846 # returns array
847 sub _arrayify {
848 # if the input is undefined, return an empty list
849 780 ▶ my $val = shift;
Subroutine _arrayify: called CC=5 SCAR=16.1
850 780 ▶ defined $val or return ();
Branch: 1 branch
BranchTrueFalseTotal
defined $val or return () 371 409 780
851  
852 # if it's a reference, return an array unless it points to an empty array. -mls
853 409 ▶ if ( ref $val eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $val eq 'ARRAY') { } 175 234 409
854 175 local $^W = 0; # turn off warnings about undef
855 175 ▶ return grep(defined, @$val) ? @$val : ();
Branch: 1 branch
BranchTrueFalseTotal
grep(defined($_), @$val) ? : 172 3 175
856 }
857 # if it's a string, return an array unless the string is missing or empty. -mls
858 else {
859 234 ▶ return (length $val) ? ($val) : ();
Branch: 1 branch
BranchTrueFalseTotal
length $val ? : 234 0 234
860 }
861 }
862  
863 # apply filter, modifying %valid by reference
864 # We don't bother trying to filter undefined fields.
865 # This prevents warnings from Perl.
866 sub _filter_apply {
867 26 ▶ my ($valid,$field,$filter) = @_;
Subroutine _filter_apply: called CC=6 SCAR=18.2
868 26 ▶ die 'wrong number of arguments passed to _filter_apply' unless (scalar @_ == 3);
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @_ == 3 0 26 26
869 26 ▶ if (ref $valid->{$field} eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $valid->{$field} eq 'ARRAY') { } 7 19 26
870 7 ▶ for (my $i = 0; $i < @{ $valid->{$field} }; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @{$valid->{$field};}) 22 7 29
871 22 ▶ $valid->{$field}->[$i] = $filter->( $valid->{$field}->[$i] ) if defined $valid->{$field}->[$i];
Branch: 1 branch
BranchTrueFalseTotal
if defined $valid->{$field}[$i] 22 0 22
872 }
873 }
874 else {
875 19 ▶ $valid->{$field} = $filter->( $valid->{$field} ) if defined $valid->{$field};
Branch: 1 branch
BranchTrueFalseTotal
if defined $valid->{$field} 19 0 19
876 }
877 }
878  
879 # =head2 _constraint_hash_build()
880 #
881 # $constraint_href = $self->_constraint_hash_build($spec,$untaint_p)
882 #
883 # Input:
884 # - $spec # Any constraint valid in the profile
885 # - $untaint # bool for whether we could try to untaint the field.
886 # - $force_method_p # bool for if it's a method ?
887 #
888 # Output:
889 # - $constraint_hashref
890 # Keys are as follows:
891 # constraint - the constraint as coderef
892 # name - the constraint name, if we know it.
893 # params - 'params', as given in the hashref style of specifying a constraint
894 # is_method - bool for whether this was a 'constraint' or 'constraint_method'
895  
896 sub _constraint_hash_build {
897 159 ▶ my ($self,$constraint_spec,$untaint_this,$force_method_p) = @_;
Subroutine _constraint_hash_build: called CC=20 SCAR=30.2
898 159 ▶ die "_constraint_hash_build received wrong number of arguments" unless (scalar @_ == 4);
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @_ == 4 0 159 159
899  
900 159 my $c = {
901 name => undef,
902 constraint => $constraint_spec,
903 };
904 159 ▶ $c->{name} = $constraint_spec if not ref $constraint_spec;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $constraint_spec 44 115 159
905  
906 # constraints can be passed in directly via hash
907 159 ▶ if (ref $c->{constraint} eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $c->{"constraint"} eq "HASH") 38 121 159
908 38 ▶ $c->{constraint} = ($constraint_spec->{constraint_method} || $constraint_spec->{constraint});
Condition66%2/3
$constraint_spec->{'constraint_method'} || $constraint_spec->{'constraint'}
l!l&&r!l&&!r
5330
MC/DC0%0/2
$constraint_spec->{'constraint_method'} || $constraint_spec->{'constraint'}
$constraint_spec->{'constraint_method'} $constraint_spec->{'constraint'}
Truth table66%2/3
A || B
A= $constraint_spec->{'constraint_method'}B= $constraint_spec->{'constraint'}
ABresult
1X1
011
000
909 38 $c->{name} = $constraint_spec->{name};
910 38 $c->{params} = $constraint_spec->{params};
911 38 ▶ $c->{is_method} = 1 if $constraint_spec->{constraint_method};
Branch: 1 branch
BranchTrueFalseTotal
if $constraint_spec->{"constraint_method"} 5 33 38
912 }
913  
914 # Check for regexp constraint
915 159 ▶ if ((ref $c->{constraint} eq 'Regexp')
Branch: 2 branches
BranchTrueFalseTotal
if (ref $c->{'constraint'} eq 'Regexp' or $c->{'constraint'} =~ m[^\s*(/.+/|m(.).+\2)[cgimosx]*\s*$]) { } 22 137 159
elsif (ref $c->{'constraint'} eq 'CODE') { } 76 61 137
Condition100%3/3
ref $c->{"constraint"} eq "Regexp" or $c->{"constraint"} =~ m[^\s*(/.+/|m(.).+\2)[cgimosx]*\s*$]
l!l&&r!l&&!r
211137
MC/DC100%2/2
ref $c->{"constraint"} eq "Regexp" or $c->{"constraint"} =~ m[^\s*(/.+/|m(.).+\2)[cgimosx]*\s*$]
ref $c->{"constraint"} eq "Regexp" $c->{"constraint"} =~ m[^\s*(/.+/|m(.).+\2)[cgimosx]*\s*$]
Truth table100%3/3
A or B
A= ref $c->{"constraint"} eq "Regexp"B= $c->{"constraint"} =~ m[^\s*(/.+/|m(.).+\2)[cgimosx]*\s*$]
ABresult
1X1
011
000
916 or ( $c->{constraint} =~ m@^\s*(/.+/|m(.).+\2)[cgimosx]*\s*$@ )) {
917 22 $c->{constraint} = _create_sub_from_RE($c->{constraint},$untaint_this,$force_method_p);
918 }
919 # check for code ref
920 elsif (ref $c->{constraint} eq 'CODE') {
921 # do nothing, it's already a code ref
922 }
923 else {
924 # provide a default name for the constraint if we don't have one already
925 61 ▶ if (not $c->{name} and not ref $c->{constraint}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($c->{"name"} or ref $c->{"constraint"}) 16 45 61
Condition66%2/3
$c->{"name"} or ref $c->{"constraint"}
l!l&&r!l&&!r
45016
MC/DC50%1/2
$c->{"name"} or ref $c->{"constraint"}
$c->{"name"} ref $c->{"constraint"}
Truth table66%2/3
A or B
A= $c->{"name"}B= ref $c->{"constraint"}
ABresult
1X1
011
000
926 16 ▶ $c->{name} ||= $c->{constraint};
Condition33%1/3
$c->{"name"} ||= $c->{"constraint"}
l!l&&r!l&&!r
0160
MC/DC0%0/2
$c->{"name"} ||= $c->{"constraint"}
$c->{"name"} $c->{"constraint"}
Truth table33%1/3
A ||= B
A= $c->{"name"}B= $c->{"constraint"}
ABresult
1X1
011
000
927 }
928  
929 #If untaint is turned on call match_* sub directly.
930 61 ▶ if ($untaint_this) {
Branch: 1 branch
BranchTrueFalseTotal
if ($untaint_this) { } 3 58 61
931 3 my $routine = 'match_'.$c->{constraint};
932 3 my $match_sub = *{qualify_to_ref($routine)}{CODE};
933 3 ▶ if ($match_sub) {
Branch: 2 branches
BranchTrueFalseTotal
if ($match_sub) { } 0 3 3
elsif ($c->{'constraint'} =~ /^RE_/) { } 2 1 3
934 0 $c->{constraint} = $match_sub;
935 }
936 # If the constraint name starts with RE_, try looking for it in the Regexp::Common package
937 elsif ($c->{constraint} =~ m/^RE_/) {
938 2 local $SIG{__DIE__} = \&confess;
939 2 $c->{is_method} = 1;
940 2 ▶ $c->{constraint} = eval 'sub { &_create_regexp_common_constraint(@_)}'
Condition50%1/2
eval 'sub { &_create_regexp_common_constraint(@_)}' || die("could not create Regexp::Common constraint: $@")
l!l
20
MC/DC0%0/1
eval 'sub { &_create_regexp_common_constraint(@_)}' || die("could not create Regexp::Common constraint: $@")
eval 'sub { &_create_regexp_common_constraint(@_)}'
Truth table50%1/2
A
A= eval 'sub { &_create_regexp_common_constraint(@_)}'
Aresult
11
00
941 || die "could not create Regexp::Common constraint: $@";
942 } else {
943 1 die "No untainting constraint found named $c->{constraint}";
944 }
945 }
946 else {
947 # try to use match_* first
948 58 my $routine = 'match_'.$c->{constraint};
949 58 ▶ if (defined *{qualify_to_ref($routine)}{CODE}) {
Branch: 3 branches
BranchTrueFalseTotal
if (defined *{&qualify_to_ref($routine);}{'CODE'}) { } 51 7 58
elsif (my $valid_sub = *{&qualify_to_ref('valid_' . $c->{'constraint'});}{'CODE'}) { } 1 6 7
elsif ($c->{'constraint'} =~ /^RE_/) { } 5 1 6
950 51 local $SIG{__DIE__} = \&confess;
951 51 ▶ $c->{constraint} = eval 'sub { no strict qw/refs/; return defined &{"match_'.$c->{constraint}.'"}(@_)}';
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
952 }
953 # match_* doesn't exist; if it is supposed to be from the
954 # validator_package(s) there may be only valid_* defined
955 7 elsif (my $valid_sub = *{qualify_to_ref('valid_'.$c->{constraint})}{CODE}) {
956 1 $c->{constraint} = $valid_sub;
957 }
958 # Load it from Regexp::Common
959 elsif ($c->{constraint} =~ m/^RE_/) {
960 5 local $SIG{__DIE__} = \&confess;
961 5 $c->{is_method} = 1;
962 5 ▶ $c->{constraint} = eval 'sub { return defined &_create_regexp_common_constraint(@_)}' ||
Condition50%1/2
eval 'sub { return defined &_create_regexp_common_constraint(@_)}' || die("could not create Regexp::Common constraint: $@")
l!l
50
MC/DC0%0/1
eval 'sub { return defined &_create_regexp_common_constraint(@_)}' || die("could not create Regexp::Common constraint: $@")
eval 'sub { return defined &_create_regexp_common_constraint(@_)}'
Truth table50%1/2
A
A= eval 'sub { return defined &_create_regexp_common_constraint(@_)}'
Aresult
11
00
963 die "could not create Regexp::Common constraint: $@";
964 }
965 else {
966 1 die "No constraint found named '$c->{name}'";
967 }
968 }
969 }
970  
971 # Save the current constraint name for later
972 157 $self->{__CURRENT_CONSTRAINT_NAME} = $c->{name};
973  
974 157 return $c;
975  
976 }
977  
978 # =head2 _constraint_input_build()
979 #
980 # @params = $self->constraint_input_build($c,$value,$data);
981 #
982 # Build in the input that passed into the constraint.
983 #
984 # =cut
985  
986 sub _constraint_input_build {
987 161 ▶ my ($self,$c,$value,$data) = @_;
Subroutine _constraint_input_build: called CC=6 SCAR=17.9
988 161 ▶ die "_constraint_input_build received wrong number of arguments" unless (scalar @_ == 4);
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @_ == 4 0 161 161
989  
990 161 my @params;
991 161 ▶ if (defined $c->{params}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $c->{'params'}) { } 24 137 161
992 24 for my $fname (_arrayify($c->{params})) {
993 # If the value is passed by reference, we treat it literally
994 44 ▶ push @params, (ref $fname) ? $fname : $data->{$fname}
Branch: 1 branch
BranchTrueFalseTotal
ref $fname ? : 7 37 44
995 }
996 }
997 else {
998 137 push @params, $value;
999 }
1000  
1001 161 ▶ unshift @params, $self if $c->{is_method};
Branch: 1 branch
BranchTrueFalseTotal
if $c->{"is_method"} 77 84 161
1002 161 return @params;
1003 }
1004  
1005 # =head2 _constraint_check_match()
1006 #
1007 # ($value,$failed_href) = $self->_constraint_check_match($c,\@params,$untaint_this);
1008 #
1009 # This is the routine that actually, finally, checks if a constraint passes or fails.
1010 #
1011 # Input:
1012 # - $c, a constraint hash, as returned by C<_constraint_hash_build()>.
1013 # - \@params, params to pass to the constraint, as prepared by C<_constraint_input_build()>.
1014 # - $untaint_this bool if we untaint successful constraints.
1015 #
1016 # Output:
1017 # - $value the value if successful
1018 # - $failed_href a hashref with the following keys:
1019 # - failed bool for failure or not
1020 # - name name of the failed constraint, if known.
1021  
1022 sub _constraint_check_match {
1023 161 ▶ my ($self,$c,$params,$untaint_this) = @_;
Subroutine _constraint_check_match: called CC=5 SCAR=16.1
1024 161 ▶ die "_constraint_check_match received wrong number of arguments" unless (scalar @_ == 4);
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @_ == 4 0 161 161
1025  
1026 # Store whether or not we want untainting in the object so that constraints
1027 # can do the right thing conditionally.
1028 161 $self->{__UNTAINT_THIS} = $untaint_this;
1029  
1030 161 my $match = $c->{constraint}->( @$params );
1031  
1032 # We need to make this distinction when untainting,
1033 # to allow untainting values that are defined but not true,
1034 # such as zero.
1035 161 my $success;
1036 161 ▶ if (defined $match) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $match) 137 24 161
1037 137 ▶ $success = ($untaint_this) ? length $match : $match;
Branch: 1 branch
BranchTrueFalseTotal
$untaint_this ? : 12 125 137
1038 }
1039  
1040 161 ▶ my $failed = 1 unless $success;
Branch: 1 branch
BranchTrueFalseTotal
unless $success 79 82 161
1041 return (
1042 $match,
1043 {
1044 failed => $failed,
1045 name => $self->{__CURRENT_CONSTRAINT_NAME},
1046 },
1047 161 );
1048 }
1049  
1050 # Figure out whether the data is a hash reference of a param-capable object and return it has a hash
1051 sub _get_input_as_hash {
1052 285 ▶ my ($self,$data) = @_;
Subroutine _get_input_as_hash: called CC=9 SCAR=32.4
1053 285 $self->{__INPUT_DATA} = $data;
1054  
1055 285 require Scalar::Util;
1056  
1057 # This checks whether we have an object that supports param
1058 285 ▶ if ( Scalar::Util::blessed($data) && $data->can('param') ) {
Branch: 2 branches
BranchTrueFalseTotal
if (builtin::blessed($data) and $data->can('param')) { } 0 285 285
elsif (ref $data eq 'HASH') { } 285 0 285
Condition33%1/3
builtin::blessed($data) and $data->can("param")
!ll&&!rl&&r
28500
MC/DC0%0/2
builtin::blessed($data) and $data->can("param")
builtin::blessed($data) $data->can("param")
Truth table33%1/3
A and B
A= builtin::blessed($data)B= $data->can("param")
ABresult
0X0
100
111
1059 0 my %return;
1060 0 for my $k ($data->param()){
1061 # we expect param to return an array if there are multiple values
1062 0 my @v;
1063  
1064 # CGI::Simple requires us to call 'upload()' to get upload data,
1065 # while CGI/Apache::Request return it on calling 'param()'.
1066 #
1067 # This seems quirky, but there isn't a way for us to easily check if
1068 # "this field contains a file upload" or not.
1069 0 ▶ if ($data->isa('CGI::Simple')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($data->isa('CGI::Simple')) { } 0 0 0
1070 0 ▶ @v = $data->upload($k) || $data->param($k);
Condition0%0/3
$data->upload($k) || $data->param($k)
l!l&&r!l&&!r
000
MC/DC0%0/2
$data->upload($k) || $data->param($k)
$data->upload($k) $data->param($k)
Truth table0%0/3
A || B
A= $data->upload($k)B= $data->param($k)
ABresult
1X1
011
000
1071 }
1072 else {
1073 # insecure
1074 0 @v = $data->multi_param($k);
1075 }
1076  
1077 # we expect param to return an array if there are multiple values
1078 0 ▶ $return{$k} = scalar(@v)>1 ? \@v : $v[0];
Branch: 1 branch
BranchTrueFalseTotal
scalar @v > 1 ? : 0 0 0
1079 }
1080 0 return %return;
1081 }
1082 # otherwise, it's already a hash reference
1083 elsif (ref $data eq 'HASH') {
1084 # be careful to actually copy array references
1085 285 my %copy = %$data;
1086 285 for (grep { ref $data->{$_} eq 'ARRAY' } keys %$data) {
1087 44 my @array_copy = @{ $data->{$_} };
1088 44 $copy{$_} = \@array_copy;
1089 }
1090  
1091 285 return %copy;
1092 }
1093 else {
1094 0 die "Data::FormValidator->validate() or check() called with invalid input data structure.";
1095 }
1096 }
1097  
1098 # A newer version of this logic now exists in Constraints.pm in the AUTOLOADing section
1099 # This is is used to support the older param passing style. Eg:
1100 #
1101 # {
1102 # constraint => 'RE_foo_bar',
1103 # params => [ \'zoo' ]
1104 # }
1105 #
1106 # Still, it's possible, the two bits of logic could be refactored into one location if you cared
1107 # to do that.
1108  
1109 sub _create_regexp_common_constraint {
1110 # this should work most of the time and is useful for preventing warnings
1111  
1112 # prevent name space clashes
1113 package Data::FormValidator::Constraints::RegexpCommon;
1114  
1115 7 ▶ require Regexp::Common;
Subroutine _create_regexp_common_constraint: called CC=4 SCAR=13.9
1116 7 import Regexp::Common 'RE_ALL';
1117  
1118 7 my $self = shift;
1119 7 my $re_name = $self->get_current_constraint_name;
1120 # deference all input
1121 7 ▶ my @params = map {$_ = $$_ if ref $_ } @_;
Branch: 1 branch
BranchTrueFalseTotal
if ref $_ 4 5 9
1122  
1123 59 ▶ no strict "refs";
Subroutine BEGIN: called CC=1 SCAR=0.0
1124 7 ▶ my $re = &$re_name(-keep=>1,@params) || die 'no matching Regexp::Common routine found';
Condition50%1/2
&$re_name('-keep', 1, @params) || die('no matching Regexp::Common routine found')
l!l
70
MC/DC0%0/1
&$re_name('-keep', 1, @params) || die('no matching Regexp::Common routine found')
&$re_name('-keep', 1, @params)
Truth table50%1/2
A
A= &$re_name('-keep', 1, @params)
Aresult
11
00
1125 7 ▶ return ($self->get_current_constraint_value =~ qr/^$re$/) ? $1 : undef;
Branch: 1 branch
BranchTrueFalseTotal
$self->get_current_constraint_value =~ qr/^$re$/ ? : 3 4 7
1126 }
1127  
1128 # _add_constraints_from_map($profile,'constraint',\%valid);
1129 # Returns:
1130 # - a hash to add to either 'constraints' or 'constraint_methods'
1131  
1132 sub _add_constraints_from_map {
1133 248 ▶ die "_add_constraints_from_map: need 3 arguments" unless (scalar @_ == 3);
Subroutine _add_constraints_from_map: called CC=9 SCAR=22.0
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @_ == 3 0 248 248
1134 248 my ($profile, $name, $valid) = @_;
1135 248 ▶ ($name =~ m/^constraint(_method)?$/) || die "unexpected input.";
Branch: 1 branch
BranchTrueFalseTotal
$name =~ /^constraint(_method)?$/ or die "unexpected input." 0 248 248
1136  
1137 248 my $key_name = $name.'s';
1138 248 my $map_name = $name.'_regexp_map';
1139  
1140 248 my %result = ();
1141 248 for my $re (keys %{ $profile->{$map_name} }) {
1142 17 my $sub = _create_sub_from_RE($re);
1143  
1144 # find all the keys that match this RE and add a constraint for them
1145 17 for my $key (keys %$valid) {
1146 45 ▶ if ($sub->($key)) {
Branch: 1 branch
BranchTrueFalseTotal
if (&$sub($key)) 28 17 45
1147 28 my $cur = $profile->{$key_name}{$key};
1148 28 my $new = $profile->{$map_name}{$re};
1149 # If they already have an arrayref of constraints, add to the list
1150 28 ▶ if (ref $cur eq 'ARRAY') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $cur eq 'ARRAY') { } 2 26 28
elsif ($cur) { } 1 25 26
1151 2 push @{ $result{$key} }, @$cur, $new;
1152 }
1153 # If they have a single constraint defined, create an array ref with with this plus the new one
1154 elsif ($cur) {
1155 1 $result{$key} = [$cur,$new];
1156 }
1157 # otherwise, a new constraint is created with this as the single constraint
1158 else {
1159 25 $result{$key} = $new;
1160 }
1161 28 ▶ warn "$map_name: $key matches\n" if $profile->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $profile->{"debug"} 0 28 28
1162 }
1163 }
1164 }
1165 248 return %result;
1166 }
1167  
1168 sub _bool_overload_based_on_success {
1169 9 ▶ my $results = shift;
Subroutine _bool_overload_based_on_success: called CC=1 SCAR=0.0
1170 9 return $results->success()
1171 }
1172  
1173 # =head2 _check_constraints()
1174 #
1175 # $self->_check_constraints(
1176 # $profile->{constraint_methods},
1177 # \%valid,
1178 # $untaint_all
1179 # \%untaint_hash
1180 # $force_method_p
1181 #);
1182 #
1183 # Input:
1184 # - 'constraints' or 'constraint_methods' hashref
1185 # - hashref of valid data
1186 # - bool to try to untaint everything
1187 # - hashref of things to untaint
1188 # - bool if all constraints should be treated as methods.
1189  
1190 sub _check_constraints {
1191 246 ▶ my ($self,
Subroutine _check_constraints: called CC=18 SCAR=28.9
1192 $constraint_href,
1193 $valid,
1194 $untaint_all,
1195 $untaint_href,
1196 $force_method_p) = @_;
1197  
1198 246 ▶ while ( my ($field,$constraint_list) = each %$constraint_href ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$constraint_href) 149 244 393
1199 149 ▶ next unless exists $valid->{$field};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $valid->{$field} 4 145 149
1200  
1201 145 ▶ my $is_constraint_list = 1 if (ref $constraint_list eq 'ARRAY');
Branch: 1 branch
BranchTrueFalseTotal
if ref $constraint_list eq "ARRAY" 14 131 145
1202 145 ▶ my $untaint_this = ($untaint_all || $untaint_href->{$field} || 0);
Condition100%5/5
$untaint_all || $untaint_href->{$field} || 0
l!l
20125
$untaint_all or $untaint_href->{$field}
l!l&&r!l&&!r
191125
MC/DC100%2/2
$untaint_all || $untaint_href->{$field} || 0
$untaint_all $untaint_href->{$field}
Truth table100%3/3
A or B
A= $untaint_allB= $untaint_href->{$field}
ABresult
1X1
011
000
1203  
1204 145 my @invalid_list;
1205 # used to insure we only bother recording each failed constraint once
1206 my %constraints_seen;
1207 145 for my $constraint_spec (_arrayify($constraint_list)) {
1208  
1209 # set current constraint field for use by get_current_constraint_field
1210 159 $self->{__CURRENT_CONSTRAINT_FIELD} = $field;
1211  
1212 # Initialize the current constraint name to undef, to prevent it
1213 # from being accidently shared
1214 159 $self->{__CURRENT_CONSTRAINT_NAME} = undef;
1215  
1216 159 my $c = $self->_constraint_hash_build($constraint_spec,$untaint_this, $force_method_p);
1217 157 ▶ $c->{is_method} = 1 if $force_method_p;
Branch: 1 branch
BranchTrueFalseTotal
if $force_method_p 66 91 157
1218  
1219 157 ▶ my $is_value_list = 1 if (ref $valid->{$field} eq 'ARRAY');
Branch: 1 branch
BranchTrueFalseTotal
if ref $valid->{$field} eq "ARRAY" 7 150 157
1220 157 my %param_data = ( $self->_get_input_as_hash($self->get_input_data) , %$valid );
1221 157 ▶ if ($is_value_list) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_value_list) { } 7 150 157
1222 7 ▶ for (my $i = 0; $i < scalar @{ $valid->{$field}} ; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < scalar @{$valid->{$field};}) 14 7 21
1223 14 ▶ if( !exists $constraints_seen{\$c} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $constraints_seen{\$c}) 11 3 14
1224  
1225 11 my @params = $self->_constraint_input_build($c,$valid->{$field}->[$i],\%param_data);
1226  
1227 # set current constraint field for use by get_current_constraint_value
1228 11 $self->{__CURRENT_CONSTRAINT_VALUE} = $valid->{$field}->[$i];
1229  
1230 11 my ($match,$failed) = $self->_constraint_check_match($c,\@params,$untaint_this);
1231 11 ▶ if ($failed->{failed}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($failed->{'failed'}) { } 4 7 11
1232 4 push @invalid_list, $failed;
1233 4 $constraints_seen{\$c} = 1;
1234 }
1235 else {
1236 7 ▶ $valid->{$field}->[$i] = $match if $untaint_this;
Branch: 1 branch
BranchTrueFalseTotal
if $untaint_this 0 7 7
1237 }
1238 }
1239 }
1240 }
1241 else {
1242 150 my @params = $self->_constraint_input_build($c,$valid->{$field},\%param_data);
1243  
1244 # set current constraint field for use by get_current_constraint_value
1245 150 $self->{__CURRENT_CONSTRAINT_VALUE} = $valid->{$field};
1246  
1247 150 my ($match,$failed) = $self->_constraint_check_match($c,\@params,$untaint_this);
1248 150 ▶ if ($failed->{failed}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($failed->{'failed'}) { } 75 75 150
1249 75 push @invalid_list, $failed
1250 }
1251 else {
1252 75 ▶ $valid->{$field} = $match if $untaint_this;
Branch: 1 branch
BranchTrueFalseTotal
if $untaint_this 12 63 75
1253 }
1254 }
1255 }
1256  
1257 143 ▶ if (@invalid_list) {
Branch: 1 branch
BranchTrueFalseTotal
if (@invalid_list) 71 72 143
1258 71 my @failed = map { $_->{name} } @invalid_list;
1259 71 push @{ $self->{invalid}{$field} }, @failed;
1260 # the older interface to validate returned things differently
1261 71 ▶ push @{ $self->{validate_invalid} }, $is_constraint_list ? [$field, @failed] : $field;
Branch: 1 branch
BranchTrueFalseTotal
$is_constraint_list ? : 12 59 71
1262 }
1263 }
1264 }
1265  
1266 59 1;
1267  
1268 __END__
« blib/lib/Data/FormValidator/Constraints.pm ↑ Summary blib/lib/Data/FormValidator/ConstraintsFactory.pm »