blib/lib/Mojolicious/Plugin/FormChecker.pm

filter: Statementstmt 5.3% 5 / 94 Branchbran 0.0% 0 / 66 Conditioncond 0.0% 0 / 35 MC/DCmcdc 0.0% 0 / 19 Subroutinesub 7.6% 1 / 13 Podpod 100% 1 / 1 totaltotal 3.0% 7 / 228 CC 50 SCAR 58.9
CC
50
Cov
3%
_rule_ok
756.0
_perform_checks
132.0
_make_error
56.0
CRAP 362.1
↑ Summary
1 package Mojolicious::Plugin::FormChecker;
2  
3 1 ▶use Mojo::Base 'Mojolicious::Plugin';
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 1 our $VERSION = '1.001004'; # VERSION
6  
7 0 ▶sub register {
Subroutine register: not called CC=1 SCAR=6.9
Pod documented
8 0 my ($self, $app) = @_;
9  
10 $app->helper(
11 0 ▶ form_checker => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
12 0 $self->_perform_checks( @_ );
13 },
14 0 );
15  
16 $app->helper(
17 0 ▶ form_checker_error => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
18 0 ▶ shift->stash('form_checker_error') // [];
Condition0%0/2
(shift())->stash("form_checker_error") // []
l!l
00
MC/DC0%0/1
(shift())->stash("form_checker_error") // []
(shift())->stash("form_checker_error")
Truth table0%0/2
A
A= (shift())->stash("form_checker_error")
Aresult
11
00
19 },
20 0 );
21  
22 $app->helper(
23 0 ▶ form_checker_error_wrapped => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
24 0 shift->stash('form_checker_error_wrapped');
25 },
26 0 );
27  
28 $app->helper(
29 0 ▶ form_checker_ok => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
30 0 shift->stash('form_checker_ok');
31 },
32 0 );
33 }
34  
35 0 ▶sub _perform_checks {
Subroutine _perform_checks: not called CC=11 SCAR=48.8
36 0 my ( $self, $c, %conf ) = @_;
37  
38 0 %conf = (
39 all_errors => 1, # TODO: get rid of this and always do all_errors
40 error_class => 'alert alert-extra-danger',
41 %conf,
42 );
43  
44 0 $self->_conf( \%conf );
45 0 $self->_check_ok(0);
46 0 $self->{FAIL} = []; # TODO: refactor all of the $self->{CAPS} stuff
47  
48 0 keys %{ $conf{rules} };
49 0 ▶ while ( my ( $param, $rule ) = each %{ $conf{rules} } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$conf{"rules"};}) 0 0 0
50 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
51 0 $rule = { $rule => 1 };
52 }
53 elsif ( ref $rule eq 'CODE' ) {
54 0 $rule = { code => $rule };
55 }
56 elsif ( ref $rule eq 'Regexp' ) {
57 0 $rule = { must_match => $rule };
58 }
59 elsif ( ref $rule eq 'ARRAY' ) {
60 0 $rule = { map +( $_ => 1 ), @$rule };
61 }
62 elsif ( ref $rule eq 'SCALAR' ) {
63 0 $rule = { param => $$rule };
64 }
65  
66 0 $self->_rule_ok( $param, $rule, $c->param( $param ), $c );
67 }
68  
69 $self->_check_ok(1)
70 0 ▶ unless ( ref $self->_error and @{ $self->_error } )
Branch: 1 branch
BranchTrueFalseTotal
unless ref $self->_error and @{$self->_error;} or not ref $self->_error 0 0 0
Condition0%0/6
ref $self->_error and @{$self->_error;} or not ref $self->_error
l!l&&r!l&&!r
000
ref $self->_error and @{$self->_error;}
!ll&&!rl&&r
000
MC/DC0%0/3
ref $self->_error and @{$self->_error;} or not ref $self->_error
ref $self->_error @{$self->_error;} not ref $self->_error
Truth table0%0/5
A and B or not(C)
A= ref $self->_errorB= @{$self->_error;}C= not ref $self->_error
ABCresult
11X1
0X11
1011
0X00
1000
71 or ( ! ref $self->_error );
72  
73 $c->stash(
74 form_checker_ok => $self->_check_ok,
75 form_checker_error => $self->_error,
76 form_checker_error_wrapped =>
77 join "\n", map qq{<p class="} . $conf{error_class}
78 . q{"><i class="glyphicon glyphicon-warning-sign"></i> }
79 0 ▶ . qq{$_</p>}, @{ $self->_error || [] },
Condition0%0/2
$self->_error || []
l!l
00
MC/DC0%0/1
$self->_error || []
$self->_error
Truth table0%0/2
A
A= $self->_error
Aresult
11
00
80 );
81 }
82  
83 0 ▶sub _rule_ok {
Subroutine _rule_ok: not called CC=27 SCAR=66.3
84 0 my ( $self, $param, $rule, $value, $c ) = @_;
85  
86 0 ▶ my $name = defined $rule->{name} ? $rule->{name} : ucfirst $param;
Branch: 1 branch
BranchTrueFalseTotal
defined $rule->{'name'} ? : 0 0 0
87  
88 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
89 0 ▶ if ( $rule->{optional} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rule->{'optional'}) { } 0 0 0
90 0 ▶ if ( $rule->{either_or} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rule->{"either_or"}) 0 0 0
91 my $which = ref $rule->{either_or}
92 ? $rule->{either_or}
93 0 ▶ : [ $rule->{either_or} ];
Branch: 1 branch
BranchTrueFalseTotal
ref $rule->{'either_or'} ? : 0 0 0
94  
95 0 for ( @$which, $param ) {
96 0 ▶ if ( length $c->param($_) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $c->param($_)) 0 0 0
97 0 return 1;
98 }
99 }
100  
101 0 return $self->_fail( $name, 'either_or_error', $rule );
102 }
103 0 return 1;
104 }
105 else {
106 0 return $self->_fail( $name, 'mandatory_error', $rule );
107 }
108 }
109  
110 0 ▶ if ( $rule->{num} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rule->{"num"}) 0 0 0
111 0 ▶ return $self->_fail( $name, 'num_error', $rule )
Branch: 1 branch
BranchTrueFalseTotal
if $value =~ /\D/u 0 0 0
112 if $value =~ /\D/;
113 }
114  
115 return $self->_fail( $name, 'min_error', $rule )
116 if defined $rule->{min}
117 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
118  
119 return $self->_fail( $name, 'max_error', $rule )
120 if defined $rule->{max}
121 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
122  
123 0 ▶ if ( $rule->{must_match} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rule->{"must_match"}) 0 0 0
124 0 ▶ return $self->_fail( $name, 'must_match_error', $rule )
Branch: 1 branch
BranchTrueFalseTotal
unless $value =~ /$rule->{"must_match"}/u 0 0 0
125 if $value !~ /$rule->{must_match}/;
126 }
127  
128 0 ▶ if ( $rule->{must_not_match} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rule->{"must_not_match"}) 0 0 0
129 0 ▶ return $self->_fail( $name, 'must_not_match_error', $rule )
Branch: 1 branch
BranchTrueFalseTotal
if $value =~ /$rule->{"must_not_match"}/u 0 0 0
130 if $value =~ /$rule->{must_not_match}/;
131 }
132  
133 0 ▶ if ( $rule->{code} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rule->{"code"}) 0 0 0
134 return $self->_fail( $name, 'code_error', $rule )
135 0 ▶ unless $rule->{code}->( $value, $param, $c );
Branch: 1 branch
BranchTrueFalseTotal
unless $rule->{"code"}->($value, $param, $c) 0 0 0
136 }
137  
138 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
139 0 my %valid;
140 0 @valid{ @values} = (1) x @values;
141  
142 return $self->_fail( $name, 'valid_values_error', $rule )
143 0 ▶ unless exists $valid{$value};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $valid{$value} 0 0 0
144 }
145  
146 0 ▶ if ( $rule->{param} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rule->{"param"}) 0 0 0
147 0 my $param_match = $c->param( $rule->{param} );
148 0 ▶ defined $param_match
Branch: 1 branch
BranchTrueFalseTotal
defined $param_match or $param_match = "" 0 0 0
149 or $param_match = '';
150  
151 0 ▶ return $self->_fail( $name, 'param_error', $rule )
Branch: 1 branch
BranchTrueFalseTotal
unless $value eq $param_match 0 0 0
152 unless $value eq $param_match;
153 }
154  
155 0 return 1;
156 }
157  
158 0 ▶sub _fail {
Subroutine _fail: not called CC=1 SCAR=6.9
159 0 my ( $self, $name, $err_name, $rule ) = @_;
160  
161 0 push @{ $self->{FAIL} }, $self->_make_error( $name, $err_name, $rule );
162 0 return;
163 }
164  
165 0 ▶sub _make_error {
Subroutine _make_error: not called CC=7 SCAR=40.3
166 0 my ( $self, $name, $err_name, $rule ) = @_;
167  
168 return $rule->{ $err_name }
169 0 ▶ if exists $rule->{ $err_name };
Branch: 1 branch
BranchTrueFalseTotal
if exists $rule->{$err_name} 0 0 0
170  
171 my %errors = (
172 mandatory_error
173 => "You must specify parameter $name",
174 num_error
175 => "Parameter $name must contain digits only",
176 min_error
177 => "Parameter $name must be at least "
178 . ($rule->{min} // '') . ' characters long',
179 max_error
180 => "Parameter $name cannot be longer "
181 . 'than ' . ($rule->{max} // '') . ' characters',
182 code_error
183 => "Parameter $name contains incorrect data",
184 must_match_error
185 => "Parameter $name contains incorrect data",
186 must_not_match_error
187 => "Parameter $name contains incorrect data",
188 param_error
189 => "Parameter $name does not match parameter "
190 . ( $rule->{param} // ''),
191 either_or_error
192 => "Parameter $name must contain data if "
193 . "other parameters are not set",
194 valid_values_error
195 0 ▶ => "Parameter $name must be " . do {
Condition0%0/6
$rule->{"min"} // ""
l!l
00
$rule->{"max"} // ""
l!l
00
$rule->{"param"} // ""
l!l
00
MC/DC0%0/1
$rule->{"min"} // ""
$rule->{"min"}
MC/DC0%0/1
$rule->{"max"} // ""
$rule->{"max"}
MC/DC0%0/1
$rule->{"param"} // ""
$rule->{"param"}
Truth table0%0/2
A
A= $rule->{"min"}
Aresult
11
00
Truth table0%0/2
A
A= $rule->{"max"}
Aresult
11
00
Truth table0%0/2
A
A= $rule->{"param"}
Aresult
11
00
196 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
197 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
198 },
199 );
200  
201 0 return $errors{ $err_name };
202 }
203  
204 0 ▶sub _error {
Subroutine _error: not called CC=5 SCAR=34.0
205 0 my $self = shift;
206  
207 return
208 0 ▶ unless defined $self->{FAIL};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"FAIL"} 0 0 0
209  
210 0 ▶ if ( $self->_conf->{all_errors} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_conf->{'all_errors'}) { } 0 0 0
211 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
212 0 return [ sort keys %errors ];
213 }
214 else {
215 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
216 }
217 }
218  
219 0 ▶sub _conf {
Subroutine _conf: not called CC=2 SCAR=17.9
220 0 my $self = shift;
221  
222 0 ▶ if ( @_ ) { $self->{CONF} = shift; }
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
223  
224 0 return $self->{CONF};
225 }
226  
227 0 ▶sub _check_ok {
Subroutine _check_ok: not called CC=2 SCAR=17.9
228 0 my $self = shift;
229  
230 0 ▶ if ( @_ ) { $self->{CHECK_OK} = shift; }
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
231  
232 0 return $self->{CHECK_OK};
233 }
234  
235  
236 1 '
237 "Can I tell you a TCP Joke?"
238 "Yes, Please tell me a TCP Joke."
239 "Ok, I will tell you a TCP Joke."
240  
241 "Can I tell you a UDP joke?"
242 "Can I tell you a UDP joke?"
243 "Can I tell you a UDP joke?"
244 ';
245  
246 __END__
↑ Summary