ZofCMS/Plugin/FormChecker.pm

filter: Statementstmt 6.6% 8 / 120 Branchbran 0.0% 0 / 92 Conditioncond 0.0% 0 / 32 MC/DCmcdc 0.0% 0 / 19 Subroutinesub 16.6% 2 / 12 Podpod 0.0% 0 / 6 totaltotal 3.5% 10 / 281 CC 61 SCAR 62.1
CC
61
Cov
3%
_rule_ok
812.0
process
552.0
_error
30.0
CRAP 496.1
« ZofCMS/Plugin/HTMLMailer.pm ↑ Summary ZofCMS/Plugin/FileUpload.pm »
1 package App::ZofCMS::Plugin::FormChecker;
2  
3 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 our $VERSION = '1.001010'; # VERSION
7  
8 0 ▶sub new { bless {}, shift }
Subroutine new: not called CC=1 SCAR=6.9
Pod undocumented
9  
10 0 ▶sub process {
Subroutine process: not called CC=23 SCAR=63.1
Pod undocumented
11 0 my ( $self, $template, $query, $config ) = @_;
12  
13 return
14 unless $template->{plug_form_checker}
15 0 ▶ or $config->conf->{plug_form_checker};
Branch: 1 branch
BranchTrueFalseTotal
unless $template->{"plug_form_checker"} or $config->conf->{"plug_form_checker"} 0 0 0
Condition0%0/3
$template->{"plug_form_checker"} or $config->conf->{"plug_form_checker"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$template->{"plug_form_checker"} or $config->conf->{"plug_form_checker"}
$template->{"plug_form_checker"} $config->conf->{"plug_form_checker"}
Truth table0%0/3
A or B
A= $template->{"plug_form_checker"}B= $config->conf->{"plug_form_checker"}
ABresult
1X1
011
000
16  
17 my %conf = (
18 trigger => 'plug_form_checker',
19 ok_key => 'd',
20 no_fill => 0,
21 all_errors => 0,
22 fill_prefix => 'plug_form_q_',
23 0 ▶ %{ delete $template->{plug_form_checker} || {} },
Condition0%0/2
delete $template->{'plug_form_checker'} || {}
l!l
00
MC/DC0%0/1
delete $template->{'plug_form_checker'} || {}
delete $template->{'plug_form_checker'}
Truth table0%0/2
A
A= delete $template->{'plug_form_checker'}
Aresult
11
00
24 0 ▶ %{ delete $config->conf->{plug_form_checker} || {} },
Condition0%0/2
delete $config->conf->{'plug_form_checker'} || {}
l!l
00
MC/DC0%0/1
delete $config->conf->{'plug_form_checker'} || {}
delete $config->conf->{'plug_form_checker'}
Truth table0%0/2
A
A= delete $config->conf->{'plug_form_checker'}
Aresult
11
00
25 );
26  
27 return
28 0 ▶ unless $query->{ $conf{trigger} };
Branch: 1 branch
BranchTrueFalseTotal
unless $$query{$conf{"trigger"}} 0 0 0
29  
30 0 $self->template( $template );
31 0 $self->query( $query );
32 0 $self->config( $config );
33 0 $self->plug_conf( \%conf );
34  
35 0 ▶ if ( ref $conf{rules} eq 'CODE' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $conf{"rules"} eq "CODE") 0 0 0
36 0 ▶ $conf{rules} = $conf{rules}->( $template, $query, $config )
Branch: 1 branch
BranchTrueFalseTotal
$conf{"rules"} = $conf{"rules"}->($template, $query, $config) or return 0 0 0
37 or return;
38 }
39  
40 0 keys %{ $conf{rules} };
41 0 ▶ while ( my ( $param, $rule ) = each %{ $conf{rules} } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$conf{"rules"};}) 0 0 0
42 0 ▶ if ( not ref $rule ) {
Branch: 5 branches
BranchTrueFalseTotal
if (not ref $rule) { } 0 0 0
elsif (ref $rule eq 'CODE') { } 0 0 0
elsif (ref $rule eq 'Regexp') { } 0 0 0
elsif (ref $rule eq 'ARRAY') { } 0 0 0
elsif (ref $rule eq 'SCALAR') { } 0 0 0
43 0 $rule = { $rule => 1 };
44 }
45 elsif ( ref $rule eq 'CODE' ) {
46 0 $rule = { code => $rule };
47 }
48 elsif ( ref $rule eq 'Regexp' ) {
49 0 $rule = { must_match => $rule };
50 }
51 elsif ( ref $rule eq 'ARRAY' ) {
52 0 $rule = { map +( $_ => 1 ), @$rule };
53 }
54 elsif ( ref $rule eq 'SCALAR' ) {
55 0 $rule = { param => $$rule };
56 }
57  
58 0 $self->_rule_ok( $param, $rule, $query->{ $param }, $query );
59 }
60  
61 0 ▶ unless ( $conf{no_fill} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($conf{"no_fill"}) 0 0 0
62 0 my @rule_params = keys %{ $conf{rules} };
63 0 my @select_params;
64 0 ▶ while ( my ( $key, $value ) = each %{ $conf{rules} } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$conf{"rules"};}) 0 0 0
65 0 ▶ if ( ref $value eq 'HASH' and $value->{select} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $value eq "HASH" and $value->{"select"}) 0 0 0
Condition0%0/3
ref $value eq "HASH" and $value->{"select"}
!ll&&!rl&&r
000
MC/DC0%0/2
ref $value eq "HASH" and $value->{"select"}
ref $value eq "HASH" $value->{"select"}
Truth table0%0/3
A and B
A= ref $value eq "HASH"B= $value->{"select"}
ABresult
0X0
100
111
66 0 push @select_params, $key;
67 }
68 }
69 0 my @template_keys = map "$conf{fill_prefix}$_", @rule_params;
70 0 @{ $template->{t} }{ @template_keys } = @$query{ @rule_params };
71 0 ▶ if ( @select_params ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@select_params) 0 0 0
72 0 @{ $template->{t} }{
73 0 map "$conf{fill_prefix}${_}_$query->{$_}", @select_params
74 } = (1) x @select_params;
75 }
76 }
77  
78 0 ▶ if ( defined(my $error = $self->_error) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $error = $self->_error)) { } 0 0 0
79 0 $template->{t}{plug_form_checker_error} = $error;
80 0 ▶ if ( exists $conf{fail_code} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $conf{"fail_code"}) 0 0 0
81 0 $conf{fail_code}->( $template, $query, $config, $error );
82 }
83 }
84 else {
85 0 $template->{ $conf{ok_key} }{plug_form_checker_ok} = 1;
86 0 ▶ if ( exists $conf{ok_code} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $conf{"ok_code"}) 0 0 0
87 0 $conf{ok_code}->( $template, $query, $config );
88 }
89 0 ▶ if ( exists $conf{ok_redirect} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $conf{"ok_redirect"}) 0 0 0
90 0 print $config->cgi->redirect( $conf{ok_redirect} );
91 0 exit;
92 }
93 }
94 }
95  
96 0 ▶sub _rule_ok {
Subroutine _rule_ok: not called CC=28 SCAR=67.0
97 0 my ( $self, $param, $rule, $value, $query ) = @_;
98  
99 0 ▶ my $name = defined $rule->{name} ? $rule->{name} : ucfirst $param;
Branch: 1 branch
BranchTrueFalseTotal
defined $rule->{'name'} ? : 0 0 0
100  
101 0 ▶ unless ( defined $value and length $value ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $value and length $value) 0 0 0
Condition0%0/3
defined $value and length $value
!ll&&!rl&&r
000
MC/DC0%0/2
defined $value and length $value
defined $value length $value
Truth table0%0/3
A and B
A= defined $valueB= length $value
ABresult
0X0
100
111
102 0 ▶ if ( $rule->{optional} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rule->{'optional'}) { } 0 0 0
103 0 ▶ if ( $rule->{either_or} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rule->{"either_or"}) 0 0 0
104 my $which = ref $rule->{either_or}
105 ? $rule->{either_or}
106 0 ▶ : [ $rule->{either_or} ];
Branch: 1 branch
BranchTrueFalseTotal
ref $rule->{'either_or'} ? : 0 0 0
107  
108 0 for ( @$which, $param ) {
109 0 ▶ if ( defined $query->{$_} and length $query->{$_} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $query->{$_} and length $query->{$_}) 0 0 0
Condition0%0/3
defined $query->{$_} and length $query->{$_}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $query->{$_} and length $query->{$_}
defined $query->{$_} length $query->{$_}
Truth table0%0/3
A and B
A= defined $query->{$_}B= length $query->{$_}
ABresult
0X0
100
111
110 0 return 1;
111 }
112 }
113  
114 0 return $self->_fail( $name, 'either_or_error', $rule );
115 }
116 0 return 1;
117 }
118 else {
119 0 return $self->_fail( $name, 'mandatory_error', $rule );
120 }
121 }
122  
123 0 ▶ if ( $rule->{num} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rule->{"num"}) 0 0 0
124 0 ▶ return $self->_fail( $name, 'num_error', $rule )
Branch: 1 branch
BranchTrueFalseTotal
if $value =~ /\D/ 0 0 0
125 if $value =~ /\D/;
126 }
127  
128 return $self->_fail( $name, 'min_error', $rule )
129 if defined $rule->{min}
130 0 ▶ and length($value) < $rule->{min};
Branch: 1 branch
BranchTrueFalseTotal
if defined $rule->{"min"} and length $value < $rule->{"min"} 0 0 0
Condition0%0/3
defined $rule->{"min"} and length $value < $rule->{"min"}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $rule->{"min"} and length $value < $rule->{"min"}
defined $rule->{"min"} length $value < $rule->{"min"}
Truth table0%0/3
A and B
A= defined $rule->{"min"}B= length $value < $rule->{"min"}
ABresult
0X0
100
111
131  
132 return $self->_fail( $name, 'max_error', $rule )
133 if defined $rule->{max}
134 0 ▶ and length($value) > $rule->{max};
Branch: 1 branch
BranchTrueFalseTotal
if defined $rule->{"max"} and length $value > $rule->{"max"} 0 0 0
Condition0%0/3
defined $rule->{"max"} and length $value > $rule->{"max"}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $rule->{"max"} and length $value > $rule->{"max"}
defined $rule->{"max"} length $value > $rule->{"max"}
Truth table0%0/3
A and B
A= defined $rule->{"max"}B= length $value > $rule->{"max"}
ABresult
0X0
100
111
135  
136 0 ▶ if ( $rule->{must_match} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rule->{"must_match"}) 0 0 0
137 0 ▶ return $self->_fail( $name, 'must_match_error', $rule )
Branch: 1 branch
BranchTrueFalseTotal
unless $value =~ /$rule->{"must_match"}/ 0 0 0
138 if $value !~ /$rule->{must_match}/;
139 }
140  
141 0 ▶ if ( $rule->{must_not_match} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rule->{"must_not_match"}) 0 0 0
142 0 ▶ return $self->_fail( $name, 'must_not_match_error', $rule )
Branch: 1 branch
BranchTrueFalseTotal
if $value =~ /$rule->{"must_not_match"}/ 0 0 0
143 if $value =~ /$rule->{must_not_match}/;
144 }
145  
146 0 ▶ if ( $rule->{code} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rule->{"code"}) 0 0 0
147 return $self->_fail( $name, 'code_error', $rule )
148 0 ▶ unless $rule->{code}->( $value, map $self->$_, qw/template query config/ );
Branch: 1 branch
BranchTrueFalseTotal
unless $rule->{"code"}->($value, map($self->$_, ("template", "query", "config"))) 0 0 0
149 }
150  
151 0 ▶ if ( my @values = @{ $rule->{valid_values} || [] } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@values) = @{$rule->{"valid_values"} or [];}) 0 0 0
Condition0%0/2
$rule->{'valid_values'} || []
l!l
00
MC/DC0%0/1
$rule->{'valid_values'} || []
$rule->{'valid_values'}
Truth table0%0/2
A
A= $rule->{'valid_values'}
Aresult
11
00
152 0 my %valid;
153 0 @valid{ @values} = (1) x @values;
154  
155 return $self->_fail( $name, 'valid_values_error', $rule )
156 0 ▶ unless exists $valid{$value};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $valid{$value} 0 0 0
157 }
158  
159 0 ▶ if ( $rule->{param} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rule->{"param"}) 0 0 0
160 0 my $param_match = $query->{ $rule->{param} };
161 0 ▶ defined $param_match
Branch: 1 branch
BranchTrueFalseTotal
defined $param_match or $param_match = "" 0 0 0
162 or $param_match = '';
163  
164 0 ▶ return $self->_fail( $name, 'param_error', $rule )
Branch: 1 branch
BranchTrueFalseTotal
unless $value eq $param_match 0 0 0
165 unless $value eq $param_match;
166 }
167  
168 0 return 1;
169 }
170  
171 0 ▶sub _make_error {
Subroutine _make_error: not called CC=4 SCAR=30.0
172 0 my ( $self, $name, $err_name, $rule ) = @_;
173  
174 return $rule->{ $err_name }
175 0 ▶ if exists $rule->{ $err_name };
Branch: 1 branch
BranchTrueFalseTotal
if exists $rule->{$err_name} 0 0 0
176  
177 my %errors = (
178 mandatory_error => "You must specify parameter $name",
179 num_error => "Parameter $name must contain digits only",
180 min_error => "Parameter $name must be at least $rule->{min} characters long",
181 max_error => "Parameter $name cannot be longer than $rule->{max} characters",
182 code_error => "Parameter $name contains incorrect data",
183 must_match_error => "Parameter $name contains incorrect data",
184 must_not_match_error => "Parameter $name contains incorrect data",
185 param_error => "Parameter $name does not match parameter $rule->{param}",
186 either_or_error => "Parameter $name must contain data if other parameters are not set",
187 valid_values_error
188 0 => "Parameter $name must be " . do {
189 0 ▶ my $last = pop @{ $rule->{valid_values} || [''] };
Condition0%0/2
$rule->{'valid_values'} || ['']
l!l
00
MC/DC0%0/1
$rule->{'valid_values'} || ['']
$rule->{'valid_values'}
Truth table0%0/2
A
A= $rule->{'valid_values'}
Aresult
11
00
190 0 ▶ join(', ', @{ $rule->{valid_values} || [] } ) . " or $last"
Condition0%0/2
$rule->{'valid_values'} || []
l!l
00
MC/DC0%0/1
$rule->{'valid_values'} || []
$rule->{'valid_values'}
Truth table0%0/2
A
A= $rule->{'valid_values'}
Aresult
11
00
191 },
192 );
193  
194 0 return $errors{ $err_name };
195 }
196  
197 0 ▶sub _fail {
Subroutine _fail: not called CC=1 SCAR=6.9
198 0 my ( $self, $name, $err_name, $rule ) = @_;
199  
200 0 push @{ $self->{FAIL} }, $self->_make_error( $name, $err_name, $rule );
201 0 return;
202 }
203  
204 0 ▶sub _error {
Subroutine _error: not called CC=5 SCAR=34.0
205 0 my $self = shift;
206 return
207 0 ▶ unless defined $self->{FAIL};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"FAIL"} 0 0 0
208  
209 0 ▶ if ( $self->plug_conf->{all_errors} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->plug_conf->{'all_errors'}) { } 0 0 0
210 0 ▶ my %errors = map +( $_ => 1 ), @{ $self->{FAIL} || [] };
Condition0%0/2
$self->{'FAIL'} || []
l!l
00
MC/DC0%0/1
$self->{'FAIL'} || []
$self->{'FAIL'}
Truth table0%0/2
A
A= $self->{'FAIL'}
Aresult
11
00
211 0 return [ map +{ error => $_ }, sort keys %errors ];
212 }
213 else {
214 0 ▶ return shift @{ $self->{FAIL} || [] };
Condition0%0/2
$self->{'FAIL'} || []
l!l
00
MC/DC0%0/1
$self->{'FAIL'} || []
$self->{'FAIL'}
Truth table0%0/2
A
A= $self->{'FAIL'}
Aresult
11
00
215 }
216 }
217  
218 0 ▶sub template {
Subroutine template: not called CC=2 SCAR=17.9
Pod undocumented
219 0 my $self = shift;
220 0 ▶ @_ and $self->{TEMPLATE} = shift;
Branch: 1 branch
BranchTrueFalseTotal
@_ and $self->{"TEMPLATE"} = shift() 0 0 0
221 0 return $self->{TEMPLATE};
222 }
223  
224 0 ▶sub config {
Subroutine config: not called CC=2 SCAR=17.9
Pod undocumented
225 0 my $self = shift;
226 0 ▶ @_ and $self->{CONFIG} = shift;
Branch: 1 branch
BranchTrueFalseTotal
@_ and $self->{"CONFIG"} = shift() 0 0 0
227 0 return $self->{CONFIG};
228 }
229  
230 0 ▶sub query {
Subroutine query: not called CC=2 SCAR=17.9
Pod undocumented
231 0 my $self = shift;
232 0 ▶ @_ and $self->{QUERY} = shift;
Branch: 1 branch
BranchTrueFalseTotal
@_ and $self->{"QUERY"} = shift() 0 0 0
233 0 return $self->{QUERY};
234 }
235 0 ▶sub plug_conf {
Subroutine plug_conf: not called CC=2 SCAR=17.9
Pod undocumented
236 0 my $self = shift;
237 0 ▶ @_ and $self->{PLUG_CONF} = shift;
Branch: 1 branch
BranchTrueFalseTotal
@_ and $self->{"PLUG_CONF"} = shift() 0 0 0
238 0 return $self->{PLUG_CONF};
239 }
240 1 1;
241 __END__
« ZofCMS/Plugin/HTMLMailer.pm ↑ Summary ZofCMS/Plugin/FileUpload.pm »