blib/lib/Validate/Tiny.pm

filter: Statementstmt 98.8% 168 / 170 Branchbran 95.6% 111 / 116 Conditioncond 80.2% 61 / 76 MC/DCmcdc 65.2% 30 / 46 Subroutinesub 97.3% 36 / 37 Podpod 100% 13 / 13 totaltotal 91.4% 419 / 458 SCAR 45.9
CC
96
Cov
94%
validate
15.0
AUTOLOAD
9.1
_run_code
7.0
CRAP 98.1
↑ Summary
1 package Validate::Tiny;
2  
3 21 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 21 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 21 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 21 ▶use Exporter;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 21 ▶use List::MoreUtils 'natatime';
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 21 our @ISA = qw/Exporter/;
11 21 our @EXPORT_OK = qw/
12 validate
13 filter
14 is_required
15 is_required_if
16 is_equal
17 is_long_between
18 is_long_at_least
19 is_long_at_most
20 is_a
21 is_like
22 is_in
23 /;
24  
25 21 our %EXPORT_TAGS = (
26 'all' => \@EXPORT_OK
27 );
28  
29 21 our $VERSION = '1.501';
30  
31 our %FILTERS = (
32 16 ▶ trim => sub { $_[0] // return; $_[0] =~ s/^\s+//; $_[0] =~ s/\s+$//; $_[0] },
Subroutine __ANON__: called CC=2 SCAR=6.9
Condition100%2/2
$_[0] // (return)
l!l
142
MC/DC100%1/1
$_[0] // (return)
$_[0]
Truth table100%2/2
A
A= $_[0]
Aresult
11
00
33 10 ▶ strip => sub { $_[0] // return; $_[0] =~ s/(\s){2,}/$1/g; $_[0] },
Subroutine __ANON__: called CC=2 SCAR=6.9
Condition100%2/2
$_[0] // (return)
l!l
91
MC/DC100%1/1
$_[0] // (return)
$_[0]
Truth table100%2/2
A
A= $_[0]
Aresult
11
00
34 4 ▶ lc => sub { $_[0] // return; lc $_[0] },
Subroutine __ANON__: called CC=2 SCAR=6.9
Condition100%2/2
$_[0] // (return)
l!l
31
MC/DC100%1/1
$_[0] // (return)
$_[0]
Truth table100%2/2
A
A= $_[0]
Aresult
11
00
35 14 ▶ uc => sub { $_[0] // return; uc $_[0] },
Subroutine __ANON__: called CC=2 SCAR=7.2
Condition50%1/2
$_[0] // (return)
l!l
140
MC/DC0%0/1
$_[0] // (return)
$_[0]
Truth table50%1/2
A
A= $_[0]
Aresult
11
00
36 1 ▶ ucfirst => sub { $_[0] // return; ucfirst $_[0] },
Subroutine __ANON__: called CC=2 SCAR=7.2
Condition50%1/2
$_[0] // (return)
l!l
10
MC/DC0%0/1
$_[0] // (return)
$_[0]
Truth table50%1/2
A
A= $_[0]
Aresult
11
00
37 21 );
38  
39 sub validate {
40 130 ▶ my ( $input, $rules ) = @_;
Subroutine validate: called CC=15 SCAR=27.1
Pod documented
41 130 my $error = {};
42  
43 # Sanity check
44 #
45 130 ▶ die 'You must define a fields array' unless defined $rules->{fields};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $rules->{"fields"} 1 129 130
46  
47 129 for (qw/filters checks/) {
48 256 ▶ next unless exists $rules->{$_};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $rules->{$_} 128 128 256
49 128 ▶ if ( ref( $rules->{$_} ) ne 'ARRAY' || @{ $rules->{$_} } % 2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $rules->{$_} ne "ARRAY" or @{$rules->{$_};} % 2) 4 124 128
Condition100%3/3
ref $rules->{$_} ne "ARRAY" or @{$rules->{$_};} % 2
l!l&&r!l&&!r
22124
MC/DC100%2/2
ref $rules->{$_} ne "ARRAY" or @{$rules->{$_};} % 2
ref $rules->{$_} ne "ARRAY" @{$rules->{$_};} % 2
Truth table100%3/3
A or B
A= ref $rules->{$_} ne "ARRAY"B= @{$rules->{$_};} % 2
ABresult
1X1
011
000
50 4 die "$_ must be an array with an even number of elements";
51 }
52 }
53  
54 125 for ( keys %$rules ) {
55 250 ▶ /^(fields|filters|checks)$/ or die "Unknown key $_";
Branch: 1 branch
BranchTrueFalseTotal
/^(fields|filters|checks)$/ or die "Unknown key $_" 1 249 250
56 }
57  
58 124 my $param = {};
59 124 ▶ my @fields = @{ $rules->{fields} } ? @{ $rules->{fields} } : keys(%$input);
Branch: 1 branch
BranchTrueFalseTotal
@{$rules->{'fields'};} ? : 121 2 123
60  
61 # Add existing, filtered input to $param
62 #
63 123 for my $key ( @fields ) {
64 218 ▶ exists $input->{$key} and ($param->{$key} = _process( $rules->{filters}, $input, $key ));
Branch: 1 branch
BranchTrueFalseTotal
exists $input->{$key} and $param->{$key} = _process($rules->{"filters"}, $input, $key) 152 66 218
65 }
66  
67 # Process all checks for $param
68 #
69 122 for my $key ( @fields ) {
70 217 my $err = _process( $rules->{checks}, $param, $key, 1 );
71 216 ▶ $error->{$key} ||= $err if $err;
Branch: 1 branch
BranchTrueFalseTotal
if $err 47 169 216
Condition33%1/3
$error->{$key} ||= $err
l!l&&r!l&&!r
0470
MC/DC0%0/2
$error->{$key} ||= $err
$error->{$key} $err
Truth table33%1/3
A ||= B
A= $error->{$key}B= $err
ABresult
1X1
011
000
72 }
73  
74 return {
75 121 ▶ success => keys %$error ? 0 : 1,
Branch: 1 branch
BranchTrueFalseTotal
keys %$error ? : 44 77 121
76 error => $error,
77 data => $param
78 };
79  
80 }
81  
82 sub _run_code {
83 179 ▶ my ( $code, $value, $param, $key ) = @_;
Subroutine _run_code: called CC=7 SCAR=19.5
84 179 my $result = $value;
85 179 my $ref = ref $code;
86 179 ▶ if ( $ref eq 'CODE' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($ref eq 'CODE') { } 151 28 179
elsif ($ref eq 'ARRAY') { } 26 2 28
87 151 $result = $code->( $value, $param, $key );
88 151 ▶ $value = $result unless defined $param;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $param 56 95 151
89 }
90 elsif ( $ref eq 'ARRAY' ) {
91 26 for (@$code) {
92 39 $result = _run_code( $_, $value, $param, $key );
93 39 ▶ if ( defined $param ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $param) { } 19 20 39
94 19 ▶ last if $result;
Branch: 1 branch
BranchTrueFalseTotal
if $result 9 10 19
95 }
96 else {
97 20 $value = $result;
98 }
99 }
100 }
101 else {
102 2 die 'Filters and checks must be either sub{} or []';
103 }
104  
105 177 return $result;
106 }
107  
108 sub _process {
109 369 ▶ my ( $pairs, $param, $key, $check ) = @_;
Subroutine _process: called CC=7 SCAR=19.5
110 369 my $value = $param->{$key};
111 369 my $iterator = natatime(2, @$pairs);
112 369 ▶ while ( my ( $match, $code ) = $iterator->() ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = &$iterator()) 238 320 558
113 238 ▶ if ( _match($key, $match) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (_match($key, $match)) 140 98 238
114 140 ▶ my $temp = _run_code( $code, $value, $check ? ($param, $key) : undef );
Branch: 1 branch
BranchTrueFalseTotal
$check ? : 92 48 140
115 138 ▶ if ( $check ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($check) { } 91 47 138
116 91 ▶ return $temp if $temp
Branch: 1 branch
BranchTrueFalseTotal
if $temp 47 44 91
117 }
118 else {
119 47 $value = $temp;
120 }
121 }
122 }
123 320 ▶ return $check ? undef : $value;
Branch: 1 branch
BranchTrueFalseTotal
$check ? : 169 151 320
124 }
125  
126 sub _match {
127 255 ▶ my ( $a, $b ) = @_;
Subroutine _match: called CC=4 SCAR=13.9
128 255 ▶ if ( !ref($b) ) {
Branch: 3 branches
BranchTrueFalseTotal
if (not ref $b) { } 220 35 255
elsif (ref $b eq 'ARRAY') { } 22 13 35
elsif (ref $b eq 'Regexp') { } 12 1 13
129 220 return $a eq $b;
130 }
131 elsif ( ref($b) eq 'ARRAY' ) {
132 22 return grep { $a eq $_ } @$b;
133 }
134 elsif ( ref($b) eq 'Regexp' ) {
135 12 return $a =~ $b;
136 }
137 else {
138 1 return 0;
139 }
140 }
141  
142 sub filter {
143 22 ▶ my @result = ();
Subroutine filter: called CC=4 SCAR=13.9
Pod documented
144 22 for (@_) {
145 26 ▶ if ( exists $FILTERS{$_} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $FILTERS{$_}) { } 25 1 26
146 25 push @result, $FILTERS{$_};
147 }
148 else {
149 1 die "Invalid filter: $_";
150 }
151 }
152 21 ▶ return @result == 1 ? $result[0] : \@result;
Branch: 1 branch
BranchTrueFalseTotal
@result == 1 ? : 17 4 21
153 }
154  
155 sub is_required {
156 10 ▶ my $err_msg = shift || 'Required';
Subroutine is_required: called CC=2 SCAR=6.9
Pod documented
Condition100%2/2
shift() || 'Required'
l!l
19
MC/DC100%1/1
shift() || 'Required'
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
157 37 ▶ return sub { defined $_[0] && $_[0] ne '' ? undef : $err_msg }
Subroutine __ANON__: called CC=3 SCAR=11.0
Branch: 1 branch
BranchTrueFalseTotal
defined $_[0] && $_[0] ne '' ? : 17 20 37
Condition100%3/3
defined $_[0] and $_[0] ne ""
!ll&&!rl&&r
61417
MC/DC100%2/2
defined $_[0] and $_[0] ne ""
defined $_[0] $_[0] ne ""
Truth table100%3/3
A and B
A= defined $_[0]B= $_[0] ne ""
ABresult
0X0
100
111
158 10 }
159  
160 sub is_required_if {
161 3 ▶ my ( $condition, $err_msg ) = @_;
Subroutine is_required_if: called CC=5 SCAR=16.4
Pod documented
162 3 ▶ $condition = 0 unless defined $condition;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $condition 0 3 3
163 3 ▶ $err_msg ||= 'Required';
Condition100%2/2
$err_msg ||= "Required"
l!l
12
MC/DC100%1/1
$err_msg ||= "Required"
$err_msg
Truth table100%2/2
A
A= $err_msg
Aresult
11
00
164 3 ▶ if ( ref($condition) && ref($condition) ne 'CODE' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $condition and ref $condition ne "CODE") 0 3 3
Condition66%2/3
ref $condition and ref $condition ne "CODE"
!ll&&!rl&&r
210
MC/DC0%0/2
ref $condition and ref $condition ne "CODE"
ref $condition ref $condition ne "CODE"
Truth table66%2/3
A and B
A= ref $conditionB= ref $condition ne "CODE"
ABresult
0X0
100
111
165 0 croak "is_required_if condition must be CODE or SCALAR";
166 }
167 return sub {
168 7 ▶ my ( $value, $params ) = @_;
Subroutine __ANON__: called CC=5 SCAR=16.1
169 7 ▶ my $required =
Branch: 1 branch
BranchTrueFalseTotal
ref $condition eq 'CODE' ? : 3 4 7
170 ref($condition) eq 'CODE'
171 ? $condition->($params)
172 : $condition;
173 7 ▶ return unless $required;
Branch: 1 branch
BranchTrueFalseTotal
unless $required 3 4 7
174 4 ▶ return defined $value && $value ne '' ? undef : $err_msg;
Branch: 1 branch
BranchTrueFalseTotal
defined $value && $value ne '' ? : 2 2 4
Condition66%2/3
defined $value and $value ne ""
!ll&&!rl&&r
022
MC/DC50%1/2
defined $value and $value ne ""
defined $value $value ne ""
Truth table66%2/3
A and B
A= defined $valueB= $value ne ""
ABresult
0X0
100
111
175 3 };
176 }
177  
178 sub is_equal {
179 3 ▶ my ( $other, $err_msg ) = @_;
Subroutine is_equal: called CC=2 SCAR=7.0
Pod documented
180 3 ▶ $err_msg ||= 'Invalid value';
Condition100%2/2
$err_msg ||= "Invalid value"
l!l
12
MC/DC100%1/1
$err_msg ||= "Invalid value"
$err_msg
Truth table100%2/2
A
A= $err_msg
Aresult
11
00
181 return sub {
182 4 ▶ return undef if !defined($_[0]) || $_[0] eq '';
Subroutine __ANON__: called CC=5 SCAR=16.3
Branch: 1 branch
BranchTrueFalseTotal
if not defined $_[0] or $_[0] eq "" 1 3 4
Condition66%2/3
not defined $_[0] or $_[0] eq ""
l!l&&r!l&&!r
013
MC/DC50%1/2
not defined $_[0] or $_[0] eq ""
not defined $_[0] $_[0] eq ""
Truth table66%2/3
not(A) or B
A= not defined $_[0]B= $_[0] eq ""
ABresult
1X1
011
000
183 3 ▶ return defined $_[1]->{$other} && $_[0] eq $_[1]->{$other}
Branch: 1 branch
BranchTrueFalseTotal
defined $_[1]{$other} && $_[0] eq $_[1]{$other} ? : 1 2 3
Condition66%2/3
defined $_[1]{$other} and $_[0] eq $_[1]{$other}
!ll&&!rl&&r
021
MC/DC50%1/2
defined $_[1]{$other} and $_[0] eq $_[1]{$other}
defined $_[1]{$other} $_[0] eq $_[1]{$other}
Truth table66%2/3
A and B
A= defined $_[1]{$other}B= $_[0] eq $_[1]{$other}
ABresult
0X0
100
111
184 ? undef
185 : $err_msg;
186 3 };
187 }
188  
189 sub is_long_between {
190 2 ▶ my ( $min, $max, $err_msg ) = @_;
Subroutine is_long_between: called CC=2 SCAR=6.9
Pod documented
191 2 ▶ $err_msg ||= "Must be between $min and $max symbols";
Condition100%2/2
$err_msg ||= "Must be between $min and $max symbols"
l!l
11
MC/DC100%1/1
$err_msg ||= "Must be between $min and $max symbols"
$err_msg
Truth table100%2/2
A
A= $err_msg
Aresult
11
00
192 return sub {
193 6 ▶ return undef if !defined($_[0]) || $_[0] eq '';
Subroutine __ANON__: called CC=5 SCAR=16.1
Branch: 1 branch
BranchTrueFalseTotal
if not defined $_[0] or $_[0] eq "" 1 5 6
Condition66%2/3
not defined $_[0] or $_[0] eq ""
l!l&&r!l&&!r
015
MC/DC50%1/2
not defined $_[0] or $_[0] eq ""
not defined $_[0] $_[0] eq ""
Truth table66%2/3
not(A) or B
A= not defined $_[0]B= $_[0] eq ""
ABresult
1X1
011
000
194 5 ▶ length( $_[0] ) >= $min && length( $_[0] ) <= $max
Branch: 1 branch
BranchTrueFalseTotal
length $_[0] >= $min && length $_[0] <= $max ? : 2 3 5
Condition100%3/3
length $_[0] >= $min and length $_[0] <= $max
!ll&&!rl&&r
122
MC/DC100%2/2
length $_[0] >= $min and length $_[0] <= $max
length $_[0] >= $min length $_[0] <= $max
Truth table100%3/3
A and B
A= length $_[0] >= $minB= length $_[0] <= $max
ABresult
0X0
100
111
195 ? undef
196 : $err_msg;
197 2 };
198 }
199  
200 sub is_long_at_least {
201 2 ▶ my ( $length, $err_msg ) = @_;
Subroutine is_long_at_least: called CC=2 SCAR=6.9
Pod documented
202 2 ▶ $err_msg ||= "Must be at least $length symbols";
Condition100%2/2
$err_msg ||= "Must be at least $length symbols"
l!l
11
MC/DC100%1/1
$err_msg ||= "Must be at least $length symbols"
$err_msg
Truth table100%2/2
A
A= $err_msg
Aresult
11
00
203 return sub {
204 4 ▶ return undef if !defined($_[0]) || $_[0] eq '';
Subroutine __ANON__: called CC=4 SCAR=13.9
Branch: 1 branch
BranchTrueFalseTotal
if not defined $_[0] or $_[0] eq "" 1 3 4
Condition66%2/3
not defined $_[0] or $_[0] eq ""
l!l&&r!l&&!r
013
MC/DC50%1/2
not defined $_[0] or $_[0] eq ""
not defined $_[0] $_[0] eq ""
Truth table66%2/3
not(A) or B
A= not defined $_[0]B= $_[0] eq ""
ABresult
1X1
011
000
205 3 ▶ length( $_[0] ) >= $length ? undef : $err_msg;
Branch: 1 branch
BranchTrueFalseTotal
length $_[0] >= $length ? : 1 2 3
206 2 };
207 }
208  
209 sub is_long_at_most {
210 2 ▶ my ( $length, $err_msg ) = @_;
Subroutine is_long_at_most: called CC=2 SCAR=6.9
Pod documented
211 2 ▶ $err_msg ||= "Must be at the most $length symbols";
Condition100%2/2
$err_msg ||= "Must be at the most $length symbols"
l!l
11
MC/DC100%1/1
$err_msg ||= "Must be at the most $length symbols"
$err_msg
Truth table100%2/2
A
A= $err_msg
Aresult
11
00
212 return sub {
213 5 ▶ return undef if !defined($_[0]) || $_[0] eq '';
Subroutine __ANON__: called CC=4 SCAR=13.9
Branch: 1 branch
BranchTrueFalseTotal
if not defined $_[0] or $_[0] eq "" 1 4 5
Condition66%2/3
not defined $_[0] or $_[0] eq ""
l!l&&r!l&&!r
014
MC/DC50%1/2
not defined $_[0] or $_[0] eq ""
not defined $_[0] $_[0] eq ""
Truth table66%2/3
not(A) or B
A= not defined $_[0]B= $_[0] eq ""
ABresult
1X1
011
000
214 4 ▶ length( $_[0] ) <= $length ? undef : $err_msg;
Branch: 1 branch
BranchTrueFalseTotal
length $_[0] <= $length ? : 2 2 4
215 2 };
216 }
217  
218 sub is_a {
219 2 ▶ my ( $class, $err_msg ) = @_;
Subroutine is_a: called CC=2 SCAR=6.9
Pod documented
220 2 ▶ $err_msg ||= "Invalid value";
Condition100%2/2
$err_msg ||= "Invalid value"
l!l
11
MC/DC100%1/1
$err_msg ||= "Invalid value"
$err_msg
Truth table100%2/2
A
A= $err_msg
Aresult
11
00
221 return sub {
222 5 ▶ return undef if !defined($_[0]) || $_[0] eq '';
Subroutine __ANON__: called CC=4 SCAR=13.9
Branch: 1 branch
BranchTrueFalseTotal
if not defined $_[0] or $_[0] eq "" 1 4 5
Condition66%2/3
not defined $_[0] or $_[0] eq ""
l!l&&r!l&&!r
014
MC/DC50%1/2
not defined $_[0] or $_[0] eq ""
not defined $_[0] $_[0] eq ""
Truth table66%2/3
not(A) or B
A= not defined $_[0]B= $_[0] eq ""
ABresult
1X1
011
000
223 4 ▶ ref($_[0]) eq $class ? undef : $err_msg;
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] eq $class ? : 1 3 4
224 }
225 2 }
226  
227 sub is_like {
228 3 ▶ my ( $regexp, $err_msg ) = @_;
Subroutine is_like: called CC=3 SCAR=11.0
Pod documented
229 3 ▶ $err_msg ||= "Invalid value";
Condition100%2/2
$err_msg ||= "Invalid value"
l!l
12
MC/DC100%1/1
$err_msg ||= "Invalid value"
$err_msg
Truth table100%2/2
A
A= $err_msg
Aresult
11
00
230 3 ▶ croak 'Regexp expected' unless ref($regexp) eq 'Regexp';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $regexp eq "Regexp" 1 2 3
231 return sub {
232 5 ▶ return undef if !defined($_[0]) || $_[0] eq '';
Subroutine __ANON__: called CC=4 SCAR=13.9
Branch: 1 branch
BranchTrueFalseTotal
if not defined $_[0] or $_[0] eq "" 1 4 5
Condition66%2/3
not defined $_[0] or $_[0] eq ""
l!l&&r!l&&!r
014
MC/DC50%1/2
not defined $_[0] or $_[0] eq ""
not defined $_[0] $_[0] eq ""
Truth table66%2/3
not(A) or B
A= not defined $_[0]B= $_[0] eq ""
ABresult
1X1
011
000
233 4 ▶ $_[0] =~ $regexp ? undef : $err_msg;
Branch: 1 branch
BranchTrueFalseTotal
$_[0] =~ /$regexp/ ? : 1 3 4
234 2 };
235 }
236  
237 sub is_in {
238 3 ▶ my ( $arrayref, $err_msg ) = @_;
Subroutine is_in: called CC=3 SCAR=11.0
Pod documented
239 3 ▶ $err_msg ||= "Invalid value";
Condition100%2/2
$err_msg ||= "Invalid value"
l!l
12
MC/DC100%1/1
$err_msg ||= "Invalid value"
$err_msg
Truth table100%2/2
A
A= $err_msg
Aresult
11
00
240 3 ▶ croak 'ArrayRef expected' unless ref($arrayref) eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $arrayref eq "ARRAY" 1 2 3
241 return sub {
242 5 ▶ return undef if !defined($_[0]) || $_[0] eq '';
Subroutine __ANON__: called CC=4 SCAR=13.9
Branch: 1 branch
BranchTrueFalseTotal
if not defined $_[0] or $_[0] eq "" 1 4 5
Condition66%2/3
not defined $_[0] or $_[0] eq ""
l!l&&r!l&&!r
014
MC/DC50%1/2
not defined $_[0] or $_[0] eq ""
not defined $_[0] $_[0] eq ""
Truth table66%2/3
not(A) or B
A= not defined $_[0]B= $_[0] eq ""
ABresult
1X1
011
000
243 4 ▶ _match( $_[0], $arrayref ) ? undef : $err_msg;
Branch: 1 branch
BranchTrueFalseTotal
_match($_[0], $arrayref) ? : 1 3 4
244 }
245 2 }
246  
247 sub new {
248 6 ▶ my ( $class, %args ) = @_;
Subroutine new: called CC=5 SCAR=16.3
Pod documented
249 6 my $filters = $args{filters};
250 6 ▶ if ( defined $filters && ref $filters eq 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $filters and ref $filters eq "HASH") 1 5 6
Condition66%2/3
defined $filters and ref $filters eq "HASH"
!ll&&!rl&&r
501
MC/DC50%1/2
defined $filters and ref $filters eq "HASH"
defined $filters ref $filters eq "HASH"
Truth table66%2/3
A and B
A= defined $filtersB= ref $filters eq "HASH"
ABresult
0X0
100
111
251 1 for my $key ( keys %$filters ) {
252 1 ▶ $FILTERS{$key} = $filters->{$key} if ref $filters->{$key} eq 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
if ref $filters->{$key} eq "CODE" 1 0 1
253 }
254 }
255 6 bless \%args, $class;
256 }
257  
258 sub check {
259 6 ▶ my ( $self, $input, $rules, %args ) = @_;
Subroutine check: called CC=4 SCAR=13.9
Pod documented
260 6 ▶ $self = $self->new( %args ) unless ref $self;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $self 5 1 6
261  
262 6 ▶ if ( ref $input ne 'HASH' || ref $rules ne 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $input ne "HASH" or ref $rules ne "HASH") 2 4 6
Condition100%3/3
ref $input ne "HASH" or ref $rules ne "HASH"
l!l&&r!l&&!r
114
MC/DC100%2/2
ref $input ne "HASH" or ref $rules ne "HASH"
ref $input ne "HASH" ref $rules ne "HASH"
Truth table100%3/3
A or B
A= ref $input ne "HASH"B= ref $rules ne "HASH"
ABresult
1X1
011
000
263 2 confess("Parameters and rules HASH refs are needed");
264 }
265  
266 4 $self->{input} = $input;
267 4 $self->{rules} = $rules;
268 4 $self->{result} = validate( $input, $rules );
269  
270 4 return $self;
271 }
272  
273 sub AUTOLOAD {
274 27 ▶ my $self = shift;
Subroutine AUTOLOAD: called CC=9 SCAR=22.0
275 27 our $AUTOLOAD;
276 27 ▶ my $sub = $AUTOLOAD =~ /::(\w+)$/ ? $1 : undef;
Branch: 1 branch
BranchTrueFalseTotal
$Validate::Tiny::AUTOLOAD =~ /::(\w+)$/ ? : 27 0 27
277 27 ▶ if ( $sub =~ /(params|rules)/ ) {
Branch: 4 branches
BranchTrueFalseTotal
if ($sub =~ /(params|rules)/) { } 0 27 27
elsif ($sub =~ /(data|error)/) { } 21 6 27
elsif ($sub eq 'success') { } 3 3 6
elsif ($sub eq 'to_hash') { } 2 1 3
278 0 return $self->{$sub};
279 }
280 elsif ( $sub =~ /(data|error)/ ) {
281 21 ▶ if ( my $field = shift ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $field = shift()) { } 8 13 21
282 8 my $fields = $self->{rules}->{fields};
283 8 ▶ if ( scalar(@$fields) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @$fields) 6 2 8
284 6 ▶ croak("Undefined field $sub($field)")
Branch: 1 branch
BranchTrueFalseTotal
unless _match($field, $fields) 3 3 6
285 unless _match( $field, $fields );
286 }
287 5 return $self->{result}->{$sub}->{ $field };
288 }
289 else {
290 13 return {%{$self->{result}->{$sub}}};
291 }
292 }
293 elsif ( $sub eq 'success' ) {
294 return $self->{result}->{success}
295 3 }
296 elsif ( $sub eq 'to_hash' ) {
297 2 return {%{$self->{result}}}
298 }
299 else {
300 1 confess "Undefined method $AUTOLOAD";
301 }
302 }
303  
304 ▶sub DESTROY {}
Subroutine DESTROY: not called CC=1 SCAR=0.0
305  
306 21 1;
307  
308 __END__
↑ Summary