blib/lib/WWW/FieldValidator.pm

filter: Statementstmt 74.6% 50 / 67 Branchbran 50.0% 10 / 20 Conditioncond 30.7% 4 / 13 MC/DCmcdc 0.0% 0 / 7 Subroutinesub 80.0% 12 / 15 Podpod 100% 3 / 3 totaltotal 63.2% 79 / 125 SCAR 34.2
CC
17
Cov
64%
validate
16.0
new
12.9
_validateEmail
2.0
CRAP 30.5
« blib/lib/WWW/Form.pm ↑ Summary Build »
1 package WWW::FieldValidator;
2  
3 4 ▶use 5.014;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 # The following constants represent the validator types that can be used to
8 # validate the specified user input.
9 4 ▶use constant WELL_FORMED_EMAIL => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 4 ▶use constant MIN_STR_LENGTH => 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 4 ▶use constant MAX_STR_LENGTH => 5;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 4 ▶use constant REGEX_MATCH => 3;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 4 ▶use constant USER_DEFINED_SUB => 4;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 # Create a new Validator object.
16 sub new {
17 2 ▶ my $class = shift;
Subroutine new: called CC=8 SCAR=25.6
Pod documented
18  
19 # The type of validation the instance will use
20 2 my $validatorType = shift;
21  
22 # The error feedback to return if the test data does not pass validation
23 2 my $feedback = shift;
24  
25 2 my $self = {};
26  
27 2 bless($self, $class);
28  
29 2 $self->{feedback} = $feedback;
30 2 $self->{validatorType} = $validatorType;
31  
32 2 ▶ if ($validatorType == MIN_STR_LENGTH) {
Branch: 4 branches
BranchTrueFalseTotal
if ($validatorType == 2) { } 0 2 2
elsif ($validatorType == 3) { } 1 1 2
elsif ($validatorType == 4) { } 0 1 1
elsif ($validatorType == 5) { } 0 1 1
33  
34 # Set min length to 1 by default
35 0 ▶ my $minLength = shift || 1;
Condition0%0/2
shift() || 1
l!l
00
MC/DC0%0/1
shift() || 1
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
36 0 $self->{minLength} = $minLength;
37  
38 }
39 elsif ($validatorType == REGEX_MATCH) {
40  
41 1 my $regex = shift;
42 1 $self->{regex} = $regex;
43  
44 }
45 elsif ($validatorType == USER_DEFINED_SUB) {
46  
47 0 my $subRef = shift;
48 0 $self->{usub} = $subRef;
49  
50 }
51 elsif ($validatorType == MAX_STR_LENGTH) {
52  
53 # Set max_length to 30 by default
54 0 ▶ my $maxLength = shift || 30;
Condition0%0/2
shift() || 30
l!l
00
MC/DC0%0/1
shift() || 30
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
55 0 $self->{maxLength} = $maxLength;
56 }
57  
58 # is_optional - if true, field is only validated if it's not empty
59 2 ▶ my $isOptional = shift || 0;
Condition50%1/2
shift() || 0
l!l
02
MC/DC0%0/1
shift() || 0
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
60 2 $self->{isOptional} = $isOptional;
61  
62 2 return $self;
63 }
64  
65 #-----------------------------------------------------------------------------
66 # These methods should really be treated as private because the Form module
67 # handles calling these methods, for most purposes all you will need to know
68 # how to do is to instantiate FieldValidators.
69 #-----------------------------------------------------------------------------
70  
71 # Checks to see what the validation type of the instance is and conditionally
72 # calls the appropriate validation method.
73 sub validate {
74 13 ▶ my $self = shift;
Subroutine validate: called CC=8 SCAR=27.7
Pod documented
75 13 my $input = shift; # User entered data
76  
77 # If the field is optional and is empty, don't validate
78 13 ▶ return 1 if ($self->{isOptional} && !$input);
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"isOptional"} and not $input 0 13 13
Condition33%1/3
$self->{"isOptional"} and not $input
!ll&&!rl&&r
1300
MC/DC0%0/2
$self->{"isOptional"} and not $input
$self->{"isOptional"} not $input
Truth table33%1/3
A and not(B)
A= $self->{"isOptional"}B= not $input
ABresult
0X0
100
111
79  
80 13 ▶ if ($self->{validatorType} == WELL_FORMED_EMAIL) {
Branch: 5 branches
BranchTrueFalseTotal
if ($self->{'validatorType'} == 1) { } 2 11 13
elsif ($self->{'validatorType'} == 2) { } 0 11 11
elsif ($self->{'validatorType'} == 3) { } 11 0 11
elsif ($self->{'validatorType'} == 4) { } 0 0 0
elsif ($self->{'validatorType'} == 5) { } 0 0 0
81  
82 2 return $self->_validateEmail($input);
83  
84 }
85 elsif ($self->{validatorType} == MIN_STR_LENGTH) {
86  
87 0 return $self->_validateStrLength($input, $self->{minLength});
88  
89 }
90 elsif ($self->{validatorType} == REGEX_MATCH) {
91  
92 11 return $self->_validateRegex($input, $self->{regex});
93  
94 }
95 elsif ($self->{validatorType} == USER_DEFINED_SUB) {
96  
97 0 return $self->{usub}($input);
98  
99 }
100 elsif ($self->{validatorType} == MAX_STR_LENGTH) {
101  
102 0 return $self->_validateMaxStrLength($input, $self->{maxLength});
103 }
104 }
105  
106 sub getFeedback {
107 0 ▶ my $self = shift;
Subroutine getFeedback: not called CC=1 SCAR=6.9
Pod documented
108 0 return $self->{feedback};
109 }
110  
111 4 *get_feedback = \&getFeedback;
112  
113 # Checks to see if input is a well formed email address.
114 # TODO: Maybe use Email::Valid for this?
115 sub _validateEmail {
116 2 ▶ my $self = shift;
Subroutine _validateEmail: called CC=2 SCAR=7.1
117 2 ▶ my $input = shift || '';
Condition50%1/2
shift() || ''
l!l
20
MC/DC0%0/1
shift() || ''
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
118 2 return ($input =~ /^[\w\-\.\+]+@([\w\-]+)(\.([\w\-]+))+$/);
119 }
120  
121 # Checks to see if input is a minimum string length.
122 sub _validateStrLength {
123 0 ▶ my $self = shift;
Subroutine _validateStrLength: not called CC=1 SCAR=6.9
124 0 my $input = shift;
125 0 return (length($input) >= $self->{minLength});
126 }
127  
128 sub _validateMaxStrLength {
129 0 ▶ my $self = shift;
Subroutine _validateMaxStrLength: not called CC=1 SCAR=6.9
130 0 my $input = shift;
131 0 return (length($input) <= $self->{maxLength});
132 }
133  
134 # Checks to see if input matches the specified pattern.
135 sub _validateRegex {
136 11 ▶ my $self = shift;
Subroutine _validateRegex: called CC=2 SCAR=7.1
137 11 ▶ my $input = shift // '';
Condition50%1/2
shift() // ""
l!l
110
MC/DC0%0/1
shift() // ""
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
138  
139 11 return ($input =~ /$self->{regex}/);
140 }
141  
142 4 1;
143  
144 __END__
« blib/lib/WWW/Form.pm ↑ Summary Build »