blib/lib/FormValidator/Lite.pm

filter: Statementstmt 95.3% 145 / 152 Branchbran 75.7% 50 / 66 Conditioncond 46.6% 7 / 15 MC/DCmcdc 33.3% 3 / 9 Subroutinesub 96.5% 28 / 29 Podpod 100% 16 / 16 totaltotal 86.7% 249 / 287 SCAR 39.1
CC
45
Cov
87%
get_error_message
16.8
check
16.0
is_valid
6.0
CRAP 49.8
« blib/lib/FormValidator/Lite/Constraint/Date.pm ↑ Summary blib/lib/FormValidator/Lite/Constraint/Default.pm »
1 package FormValidator::Lite;
2 30 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 30 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 30 ▶use 5.008_001;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 30 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
6 30 ▶use Scalar::Util qw/blessed/;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 30 ▶use FormValidator::Lite::Constraint::Default;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 30 ▶use FormValidator::Lite::Upload;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 use Class::Accessor::Lite 0.05 (
10 30 rw => [qw/query/]
11 30 ▶);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 30 ▶use Class::Load ();
Subroutine BEGIN: called CC=1 SCAR=0.0
13 30 ▶use FormValidator::Lite::Hash;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 30 our $VERSION = '0.38';
16  
17 30 our $Rules;
18 30 our $FileRules;
19  
20 sub import {
21 30 ▶ my ($class, @constraints) = @_;
Subroutine import: called CC=1 SCAR=0.0
22 30 $class->load_constraints(@constraints);
23 }
24  
25 sub new {
26 82 ▶ my ($class, $q) = @_;
Subroutine new: called CC=4 SCAR=13.9
Pod documented
27 82 ▶ Carp::croak("Usage: ${class}->new(\$q)") unless $q;
Branch: 1 branch
BranchTrueFalseTotal
unless $q 0 82 82
28  
29 82 ▶ if (ref $q eq 'HASH') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $q eq 'HASH') { } 3 79 82
elsif (UNIVERSAL::isa($q, 'Hash::MultiValue')) { } 3 76 79
30 3 $q = FormValidator::Lite::Hash->new($q);
31 } elsif (UNIVERSAL::isa($q, 'Hash::MultiValue')) {
32 3 $q = FormValidator::Lite::Hash->new($q->flatten);
33 }
34 82 bless { query => $q, _error => {} }, $class;
35 }
36  
37 sub check {
38 77 ▶ my ($self, @rule_ary) = @_;
Subroutine check: called CC=16 SCAR=27.7
Pod documented
39 77 ▶ Carp::croak("this is an instance method") unless ref $self;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $self 0 77 77
40  
41 77 ▶ while (my ($rule_key, $rules) = splice(@rule_ary, 0, 2)) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = splice(@rule_ary, 0, 2)) 133 77 210
42 133 my ($key, @values) = $self->_extract_values($rule_key);
43 133 for my $value (@values) {
44 144 local $_ = $value;
45 144 for my $rule (@$rules) {
46 167 ▶ my $rule_name = ref($rule) ? $rule->[0] : $rule;
Branch: 1 branch
BranchTrueFalseTotal
ref $rule ? : 65 102 167
47 167 ▶ my $args = ref($rule) ? [ @$rule[ 1 .. scalar(@$rule)-1 ] ] : +[];
Branch: 1 branch
BranchTrueFalseTotal
ref $rule ? : 65 102 167
48  
49 167 ▶ if ($FileRules->{$rule_name}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($FormValidator::Lite::FileRules->{$rule_name}) 9 158 167
50 9 $_ = FormValidator::Lite::Upload->new($self->{query}, $key);
51 }
52 167 my $is_ok = do {
53 167 ▶ if ((not (defined $_ && length $_)) && $rule_name !~ /^(NOT_NULL|NOT_BLANK|REQUIRED)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $_ && length $_ || $rule_name =~ /^(NOT_NULL|NOT_BLANK|REQUIRED)$/) { } 8 159 167
Condition100%6/6
defined $_ && length $_ || $rule_name =~ /^(NOT_NULL|NOT_BLANK|REQUIRED)$/
l!l&&r!l&&!r
146138
defined $_ and length $_
!ll&&!rl&&r
156146
MC/DC100%3/3
defined $_ && length $_ || $rule_name =~ /^(NOT_NULL|NOT_BLANK|REQUIRED)$/
defined $_ length $_ $rule_name =~ /^(NOT_NULL|NOT_BLANK|REQUIRED)$/
Truth table100%5/5
A and B || C
A= defined $_B= length $_C= $rule_name =~ /^(NOT_NULL|NOT_BLANK|REQUIRED)$/
ABCresult
11X1
0X11
1011
0X00
1000
54 8 1;
55 } else {
56 159 ▶ if (my $file_rule = $FileRules->{$rule_name}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $file_rule = $FormValidator::Lite::FileRules->{$rule_name}) { } 9 150 159
57 9 ▶ $file_rule->(@$args) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
&$file_rule(@$args) ? : 4 5 9
58 } else {
59 150 ▶ my $code = $Rules->{$rule_name} or Carp::croak("unknown rule $rule_name");
Branch: 1 branch
BranchTrueFalseTotal
my $code = $FormValidator::Lite::Rules->{$rule_name} or Carp::croak("unknown rule $rule_name") 0 150 150
60 150 ▶ $code->(@$args) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
&$code(@$args) ? : 88 62 150
61 }
62 }
63 };
64 167 ▶ if ($is_ok==0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_ok == 0) 67 100 167
65 67 $self->set_error($key => $rule_name);
66 }
67 }
68 }
69 }
70  
71 77 return $self;
72 }
73  
74 sub _extract_values {
75 133 ▶ my ($self, $key) = @_;
Subroutine _extract_values: called CC=3 SCAR=11.0
76  
77 133 local $CGI::LIST_CONTEXT_WARN = 0;
78 133 my $q = $self->{query};
79 133 my @values;
80 133 ▶ if (ref $key) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $key) { } 15 118 133
81 15 $key = [%$key];
82 15 @values = [ map { $q->param($_) } @{ $key->[1] } ];
83 15 $key = $key->[0];
84 } else {
85 118 ▶ @values = defined $q->param($key) ? $q->param($key) : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $q->param($key) ? : 103 15 118
86 }
87 133 return ($key, @values);
88 }
89  
90 sub is_error {
91 118 ▶ my ($self, $key) = @_;
Subroutine is_error: called CC=2 SCAR=6.9
Pod documented
92 118 ▶ $self->{_error}->{$key} ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$self->{'_error'}{$key} ? : 58 60 118
93 }
94  
95 sub is_valid {
96 0 ▶ my $self = shift;
Subroutine is_valid: not called CC=2 SCAR=17.9
Pod documented
97 0 ▶ !$self->has_error ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
!$self->has_error ? : 0 0 0
98 }
99  
100 sub has_error {
101 43 ▶ my ($self, ) = @_;
Subroutine has_error: called CC=2 SCAR=6.9
Pod documented
102 43 ▶ %{ $self->{_error} } ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
%{$self->{'_error'};} ? : 15 28 43
103 }
104  
105 sub set_error {
106 76 ▶ my ($self, $param, $rule_name) = @_;
Subroutine set_error: called CC=1 SCAR=0.0
Pod documented
107 76 $self->{_error}->{$param}->{$rule_name}++;
108 76 push @{$self->{_error_ary}}, [$param, $rule_name];
109 }
110  
111 sub errors {
112 6 ▶ my ($self) = @_;
Subroutine errors: called CC=1 SCAR=0.0
Pod documented
113 6 $self->{_error};
114 }
115  
116 sub load_constraints {
117 36 ▶ my $class = shift;
Subroutine load_constraints: called CC=3 SCAR=11.0
Pod documented
118 36 for (@_) {
119 10 my $constraint = $_;
120 10 ▶ $constraint = ($constraint =~ s/^\+//) ? $constraint : "FormValidator::Lite::Constraint::${constraint}";
Branch: 1 branch
BranchTrueFalseTotal
$constraint =~ s/^\+// ? : 1 9 10
121 10 Class::Load::load_class($constraint);
122 }
123 }
124  
125 sub load_function_message {
126 1 ▶ my ($self, $lang) = @_;
Subroutine load_function_message: called CC=1 SCAR=0.0
Pod documented
127 1 my $pkg = "FormValidator::Lite::Messages::$lang";
128 1 Class::Load::load_class($pkg);
129  
130 30 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
131 1 $self->{_msg}->{function} = ${"${pkg}::MESSAGES"};
132 }
133  
134 sub set_param_message {
135 1 ▶ my ($self, %args) = @_;
Subroutine set_param_message: called CC=1 SCAR=0.0
Pod documented
136 1 $self->{_msg}->{param} = \%args;
137 }
138  
139 sub set_message_data {
140 3 ▶ my ($self, $msg) = @_;
Subroutine set_message_data: called CC=3 SCAR=11.1
Pod documented
141 3 for my $key (qw/param function/) {
142 6 ▶ Carp::croak("missing key $key") unless $msg->{$key};
Branch: 1 branch
BranchTrueFalseTotal
unless $msg->{$key} 0 6 6
143 }
144 3 $self->{_msg} = $msg;
145 }
146  
147 sub set_message {
148 1 ▶ my ($self, @args) = @_;
Subroutine set_message: called CC=3 SCAR=12.0
Pod documented
149 1 ▶ my %msg = ref $args[0] ? %{$args[0]} : @args;
Branch: 1 branch
BranchTrueFalseTotal
ref $args[0] ? : 0 1 1
150 $self->{_msg}->{message} = +{
151 1 ▶ %{ $self->{_msg}->{message} || +{} },
Branch: 1 branch
BranchTrueFalseTotal
$self->{"_msg"}{"message"} or {} 0 1 1
152 %msg
153 };
154 }
155  
156 sub get_error_messages {
157 5 ▶ my $self = shift;
Subroutine get_error_messages: called CC=5 SCAR=16.2
Pod documented
158 5 ▶ Carp::croak("No messages loaded yet") unless $self->{_msg};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"_msg"} 0 5 5
159  
160 5 my %dup_check;
161 my @messages;
162 5 for my $err (@{$self->{_error_ary}}) {
163 11 my $param = $err->[0];
164 11 my $func = $err->[1];
165  
166 11 ▶ next if exists $dup_check{"$param.$func"};
Branch: 1 branch
BranchTrueFalseTotal
if exists $dup_check{"$param.$func"} 0 11 11
167 11 push @messages, $self->get_error_message( $param, $func );
168 11 $dup_check{"$param.$func"}++;
169 }
170  
171 5 ▶ return wantarray ? @messages : \@messages;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 4 1 5
172 }
173  
174 # $validator->get_error_message('email', 'NOT_NULL');
175 sub get_error_message {
176 16 ▶ my ($self, $param, $function) = @_;
Subroutine get_error_message: called CC=8 SCAR=28.2
Pod documented
177 16 $function = lc($function);
178  
179 16 my $msg = $self->{_msg};
180 16 ▶ Carp::croak("please load message file first") unless $msg;
Branch: 1 branch
BranchTrueFalseTotal
unless $msg 0 16 16
181  
182 16 my $err_message = $msg->{message}->{"${param}.${function}"};
183 16 my $err_param = $msg->{param}->{$param};
184 16 my $err_function = $msg->{function}->{$function};
185
186 16 ▶ if ($err_message) {
Branch: 2 branches
BranchTrueFalseTotal
if ($err_message) { } 5 11 16
elsif ($err_function and $err_param) { } 11 0 11
187 5 return $self->build_message($err_message, $err_param);
188 ▶ } elsif ($err_function && $err_param) {
Condition33%1/3
$err_function and $err_param
!ll&&!rl&&r
0011
MC/DC0%0/2
$err_function and $err_param
$err_function $err_param
Truth table33%1/3
A and B
A= $err_functionB= $err_param
ABresult
0X0
100
111
189 11 return $self->build_message($err_function, $err_param);
190 } else {
191 0 Carp::carp "${param}.${function} is not defined in message file.";
192 0 ▶ if ($msg->{default_tmpl}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($msg->{'default_tmpl'}) { } 0 0 0
193 0 ▶ return $self->build_message($err_function || $msg->{default_tmpl}, $err_function || $param);
Condition0%0/6
$err_function || $msg->{'default_tmpl'}
l!l&&r!l&&!r
000
$err_function || $param
l!l&&r!l&&!r
000
MC/DC0%0/2
$err_function || $msg->{'default_tmpl'}
$err_function $msg->{'default_tmpl'}
MC/DC0%0/2
$err_function || $param
$err_function $param
Truth table0%0/3
A || B
A= $err_functionB= $msg->{'default_tmpl'}
ABresult
1X1
011
000
Truth table0%0/3
A || B
A= $err_functionB= $param
ABresult
1X1
011
000
194 } else {
195 0 return '';
196 }
197 }
198 }
199  
200 sub build_message {
201 17 ▶ my ($self, $tmpl, @args) = @_;
Subroutine build_message: called CC=1 SCAR=0.0
Pod documented
202 17 local $_ = $tmpl;
203 17 s!\[_(\d+)\]!$args[$1-1]!ge;
204 17 $_;
205 }
206  
207 sub get_error_messages_from_param {
208 1 ▶ my ($self, $target_param) = @_;
Subroutine get_error_messages_from_param: called CC=5 SCAR=16.2
Pod documented
209  
210 1 my %dup_check;
211 my @messages;
212 1 for my $err (@{$self->{_error_ary}}) {
213 3 my $param = $err->[0];
214 3 my $func = $err->[1];
215  
216 3 ▶ next if $target_param ne $param;
Branch: 1 branch
BranchTrueFalseTotal
if $target_param ne $param 1 2 3
217 2 ▶ next if exists $dup_check{"$param.$func"};
Branch: 1 branch
BranchTrueFalseTotal
if exists $dup_check{"$param.$func"} 0 2 2
218 2 push @messages, $self->get_error_message( $param, $func );
219 2 $dup_check{"$param.$func"}++;
220 }
221  
222 1 ▶ return wantarray ? @messages : \@messages;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 1 0 1
223 }
224  
225 30 1;
226  
227 __END__
« blib/lib/FormValidator/Lite/Constraint/Date.pm ↑ Summary blib/lib/FormValidator/Lite/Constraint/Default.pm »