StatsMix/Params.pm

filter: Statementstmt 87.8% 72 / 82 Branchbran 78.9% 30 / 38 Conditioncond 36.8% 21 / 57 MC/DCmcdc 25.0% 8 / 32 Subroutinesub 81.8% 27 / 33 Podpod 0.0% 0 / 10 totaltotal 62.7% 158 / 252 SCAR 44.9
CC
41
Cov
69%
check_date
74.1
validate
13.0
check_zero_or_one
12.0
CRAP 88.7
« StatsMix.pm ↑ Summary StatsMix/UserAgent.pm »
1 package WWW::StatsMix::Params;
2  
3 12 $WWW::StatsMix::Params::VERSION = '0.07';
4  
5 =head1 NAME
6  
7 WWW::StatsMix::Params - Placeholder for parameters for WWW::StatsMix
8  
9 =head1 VERSION
10  
11 Version 0.07
12  
13 =cut
14  
15 12 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 12 ▶use strict; use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
17 12 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 12 ▶use vars qw(@ISA @EXPORT @EXPORT_OK);
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 12 require Exporter;
22 12 @ISA = qw(Exporter);
23 12 @EXPORT_OK = qw(validate $FIELDS);
24  
25 12 our $SHARING = { public => 1, none => 1 };
26 0 ▶our $Sharing = sub { check_sharing($_[0]) };
Subroutine __ANON__: not called CC=1 SCAR=1.2
27 0 ▶our $XmlOrJson = sub { check_format($_[0]) };
Subroutine __ANON__: not called CC=1 SCAR=1.2
28 0 ▶our $ZeroOrOne = sub { check_zero_or_one($_[0]) };
Subroutine __ANON__: not called CC=1 SCAR=1.2
29  
30 sub check_format {
31 5 ▶ my ($str) = @_;
Subroutine check_format: called CC=3 SCAR=13.1
Pod undocumented
32  
33 5 ▶ die "ERROR: Invalid data found [$str]"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $str or $str =~ /^\bjson\b|\bxml\b$/i 0 5 5
Condition33%1/3
defined $str or $str =~ /^\bjson\b|\bxml\b$/i
l!l&&r!l&&!r
500
MC/DC0%0/2
defined $str or $str =~ /^\bjson\b|\bxml\b$/i
defined $str $str =~ /^\bjson\b|\bxml\b$/i
Truth table33%1/3
A or B
A= defined $strB= $str =~ /^\bjson\b|\bxml\b$/i
ABresult
1X1
011
000
34 unless (defined($str) || ($str =~ m(^\bjson\b|\bxml\b$)i))
35 }
36  
37 sub check_sharing {
38 5 ▶ my ($str) = @_;
Subroutine check_sharing: called CC=3 SCAR=11.1
Pod undocumented
39  
40 die "ERROR: Invalid data type 'sharing' found [$str]"
41 5 ▶ unless (defined $str && exists $SHARING->{$str});
Branch: 1 branch
BranchTrueFalseTotal
unless defined $str and exists $WWW::StatsMix::Params::SHARING->{$str} 3 2 5
Condition66%2/3
defined $str and exists $WWW::StatsMix::Params::SHARING->{$str}
!ll&&!rl&&r
032
MC/DC50%1/2
defined $str and exists $WWW::StatsMix::Params::SHARING->{$str}
defined $str exists $WWW::StatsMix::Params::SHARING->{$str}
Truth table66%2/3
A and B
A= defined $strB= exists $WWW::StatsMix::Params::SHARING->{$str}
ABresult
0X0
100
111
42 };
43  
44 sub check_zero_or_one {
45 0 ▶ my ($str) = @_;
Subroutine check_zero_or_one: not called CC=3 SCAR=24.8
Pod undocumented
46  
47 0 ▶ die "ERROR: Expected data is 0 or 1 but found [$str]"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $str and $str =~ /^[0|1]$/ 0 0 0
Condition0%0/3
defined $str and $str =~ /^[0|1]$/
!ll&&!rl&&r
000
MC/DC0%0/2
defined $str and $str =~ /^[0|1]$/
defined $str $str =~ /^[0|1]$/
Truth table0%0/3
A and B
A= defined $strB= $str =~ /^[0|1]$/
ABresult
0X0
100
111
48 unless (defined $str && $str =~ /^[0|1]$/);
49 };
50  
51 sub check_num {
52 19 ▶ my ($num) = @_;
Subroutine check_num: called CC=3 SCAR=11.1
Pod undocumented
53  
54 19 ▶ die "ERROR: Invalid NUM data type [$num]"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $num and $num =~ /^\d+$/ 9 10 19
Condition66%2/3
defined $num and $num =~ /^\d+$/
!ll&&!rl&&r
0910
MC/DC50%1/2
defined $num and $num =~ /^\d+$/
defined $num $num =~ /^\d+$/
Truth table66%2/3
A and B
A= defined $numB= $num =~ /^\d+$/
ABresult
0X0
100
111
55 unless (defined $num && $num =~ /^\d+$/);
56 };
57  
58 sub check_str {
59 18 ▶ my ($str) = @_;
Subroutine check_str: called CC=3 SCAR=13.1
Pod undocumented
60  
61 18 ▶ die "ERROR: Invalid STR data type [$str]"
Branch: 1 branch
BranchTrueFalseTotal
if defined $str and $str =~ /^\d+$/ 0 18 18
Condition33%1/3
defined $str and $str =~ /^\d+$/
!ll&&!rl&&r
0180
MC/DC0%0/2
defined $str and $str =~ /^\d+$/
defined $str $str =~ /^\d+$/
Truth table33%1/3
A and B
A= defined $strB= $str =~ /^\d+$/
ABresult
0X0
100
111
62 if (defined $str && $str =~ /^\d+$/);
63 };
64  
65 sub check_date {
66 15 ▶ my ($str) = @_;
Subroutine check_date: called CC=14 SCAR=43.1
Pod undocumented
67  
68 15 ▶ if ($str =~ m!^((?:19|20)\d\d)\-(0[1-9]|1[012])\-(0[1-9]|[12][0-9]|3[01])$!) {
Branch: 1 branch
BranchTrueFalseTotal
if ($str =~ /^((?:19|20)\d\d)\-(0[1-9]|1[012])\-(0[1-9]|[12][0-9]|3[01])$/) { } 6 9 15
69 # At this point, $1 holds the year, $2 the month and $3 the day of the date entered
70 6 ▶ if ($3 == 31 and ($2 == 4 or $2 == 6 or $2 == 9 or $2 == 11)) {
Branch: 3 branches
BranchTrueFalseTotal
if ($3 == 31 and $2 == 4 || $2 == 6 || $2 == 9 || $2 == 11) { } 0 6 6
elsif ($3 >= 30 and $2 == 2) { } 0 6 6
elsif ($2 == 2 and $3 == 29 and not $1 % 4 == 0 && ($1 % 100 != 0 || $1 % 400 == 0)) { } 0 6 6
Condition8%1/12
$3 == 31 and $2 == 4 || $2 == 6 || $2 == 9 || $2 == 11
!ll&&!rl&&r
600
$2 == 4 or $2 == 6 or $2 == 9 or $2 == 11
l!l&&r!l&&!r
000
$2 == 4 or $2 == 6 or $2 == 9
l!l&&r!l&&!r
000
$2 == 4 or $2 == 6
l!l&&r!l&&!r
000
MC/DC0%0/5
$3 == 31 and $2 == 4 || $2 == 6 || $2 == 9 || $2 == 11
$3 == 31 $2 == 4 $2 == 6 $2 == 9 $2 == 11
Truth table16%1/6
A and B or C or D or E
A= $3 == 31B= $2 == 4C= $2 == 6D= $2 == 9E= $2 == 11
ABCDEresult
0XXXX0
100000
11XXX1
101XX1
1001X1
100011
71 # 31st of a month with 30 days
72 0 die "ERROR: Invalid data of type 'date' found [$str]"
73 ▶ } elsif ($3 >= 30 and $2 == 2) {
Condition33%1/3
$3 >= 30 and $2 == 2
!ll&&!rl&&r
600
MC/DC0%0/2
$3 >= 30 and $2 == 2
$3 >= 30 $2 == 2
Truth table33%1/3
A and B
A= $3 >= 30B= $2 == 2
ABresult
0X0
100
111
74 # February 30th or 31st
75 0 die "ERROR: Invalid data of type 'date' found [$str]"
76 ▶ } elsif ($2 == 2 and $3 == 29 and not ($1 % 4 == 0 and ($1 % 100 != 0 or $1 % 400 == 0))) {
Condition16%2/12
$2 == 2 and $3 == 29 and not $1 % 4 == 0 && ($1 % 100 != 0 || $1 % 400 == 0)
!ll&&!rl&&r
600
$2 == 2 and $3 == 29
!ll&&!rl&&r
600
$1 % 4 == 0 && ($1 % 100 != 0 || $1 % 400 == 0)
!ll&&!rl&&r
000
$1 % 100 != 0 or $1 % 400 == 0
l!l&&r!l&&!r
000
MC/DC0%0/5
$2 == 2 and $3 == 29 and not $1 % 4 == 0 && ($1 % 100 != 0 || $1 % 400 == 0)
$2 == 2 $3 == 29 $1 % 4 == 0 $1 % 100 != 0 $1 % 400 == 0
Truth table16%1/6
A and B and not(C && D or E)
A= $2 == 2B= $3 == 29C= $1 % 4 == 0D= $1 % 100 != 0E= $1 % 400 == 0
ABCDEresult
0XXXX0
10XXX0
1111X0
111010
110XX1
111001
77 # February 29th outside a leap year
78 0 die "ERROR: Invalid data of type 'date' found [$str]"
79 } else {
80 6 return 1; # Valid date
81 }
82 } else {
83 # Not a date
84 9 die "ERROR: Invalid data of type 'date' found [$str]"
85 }
86 }
87  
88 sub check_url {
89 6 ▶ my ($str) = @_;
Subroutine check_url: called CC=3 SCAR=11.1
Pod undocumented
90  
91 6 ▶ die "ERROR: Invalid data type 'url' found [$str]"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $str and $str =~ m[^(http(?:s)?\://[a-zA-Z0-9\-]+(?:\.[a-zA-Z0-9\-]+)*\.[a-zA-Z]{2,6}(?:/?|(?:/[\w\-]+)*)(?:/?|/\w+\.[a-zA-Z]{2,4}(?:\?[\w]+\=[\w\-]+)?)?(?:\&[\w]+\=[\w\-]+)*)$] 3 3 6
Condition66%2/3
defined $str and $str =~ m[^(http(?:s)?\://[a-zA-Z0-9\-]+(?:\.[a-zA-Z0-9\-]+)*\.[a-zA-Z]{2,6}(?:/?|(?:/[\w\-]+)*)(?:/?|/\w+\.[a-zA-Z]{2,4}(?:\?[\w]+\=[\w\-]+)?)?(?:\&[\w]+\=[\w\-]+)*)$]
!ll&&!rl&&r
033
MC/DC50%1/2
defined $str and $str =~ m[^(http(?:s)?\://[a-zA-Z0-9\-]+(?:\.[a-zA-Z0-9\-]+)*\.[a-zA-Z]{2,6}(?:/?|(?:/[\w\-]+)*)(?:/?|/\w+\.[a-zA-Z]{2,4}(?:\?[\w]+\=[\w\-]+)?)?(?:\&[\w]+\=[\w\-]+)*)$]
defined $str $str =~ m[^(http(?:s)?\://[a-zA-Z0-9\-]+(?:\.[a-zA-Z0-9\-]+)*\.[a-zA-Z]{2,6}(?:/?|(?:/[\w\-]+)*)(?:/?|/\w+\.[a-zA-Z]{2,4}(?:\?[\w]+\=[\w\-]+)?)?(?:\&[\w]+\=[\w\-]+)*)$]
Truth table66%2/3
A and B
A= defined $strB= $str =~ m[^(http(?:s)?\://[a-zA-Z0-9\-]+(?:\.[a-zA-Z0-9\-]+)*\.[a-zA-Z]{2,6}(?:/?|(?:/[\w\-]+)*)(?:/?|/\w+\.[a-zA-Z]{2,4}(?:\?[\w]+\=[\w\-]+)?)?(?:\&[\w]+\=[\w\-]+)*)$]
ABresult
0X0
100
111
92 unless (defined $str
93 && $str =~ /^(http(?:s)?\:\/\/[a-zA-Z0-9\-]+(?:\.[a-zA-Z0-9\-]+)*\.[a-zA-Z]{2,6}(?:\/?|(?:\/[\w\-]+)*)(?:\/?|\/\w+\.[a-zA-Z]{2,4}(?:\?[\w]+\=[\w\-]+)?)?(?:\&[\w]+\=[\w\-]+)*)$/);
94 };
95  
96 sub check_value {
97 16 ▶ my ($str) = @_;
Subroutine check_value: called CC=3 SCAR=11.1
Pod undocumented
98  
99 16 ▶ die "ERROR: Invalid data type 'value' found [$str]."
Branch: 1 branch
BranchTrueFalseTotal
unless defined $str and $str =~ /^\d{0,11}\.?\d{0,2}$/ 3 13 16
Condition66%2/3
defined $str and $str =~ /^\d{0,11}\.?\d{0,2}$/
!ll&&!rl&&r
0313
MC/DC50%1/2
defined $str and $str =~ /^\d{0,11}\.?\d{0,2}$/
defined $str $str =~ /^\d{0,11}\.?\d{0,2}$/
Truth table66%2/3
A and B
A= defined $strB= $str =~ /^\d{0,11}\.?\d{0,2}$/
ABresult
0X0
100
111
100 unless (defined $str && $str =~ /^\d{0,11}\.?\d{0,2}$/);
101 }
102  
103 sub check_hash_ref {
104 4 ▶ my ($str) = @_;
Subroutine check_hash_ref: called CC=2 SCAR=8.1
Pod undocumented
105  
106 4 ▶ return (defined $str && (ref($str) eq 'HASH'));
Condition33%1/3
defined $str && ref $str eq 'HASH'
!ll&&!rl&&r
040
MC/DC0%0/2
defined $str && ref $str eq 'HASH'
defined $str ref $str eq 'HASH'
Truth table33%1/3
A && B
A= defined $strB= ref $str eq 'HASH'
ABresult
0X0
100
111
107 }
108  
109 our $FIELDS = {
110 3 ▶ 'id' => { check => sub { check_num(@_) }, type => 'd' },
Subroutine __ANON__: called CC=1 SCAR=0.0
111 0 ▶ 'ref_id' => { check => sub { check_str(@_) }, type => 's' },
Subroutine __ANON__: not called CC=1 SCAR=6.9
112 2 ▶ 'profile_id' => { check => sub { check_num(@_) }, type => 'd' },
Subroutine __ANON__: called CC=1 SCAR=0.0
113 12 ▶ 'metric_id' => { check => sub { check_num(@_) }, type => 'd' },
Subroutine __ANON__: called CC=1 SCAR=0.0
114 2 ▶ 'limit' => { check => sub { check_num(@_) }, type => 'd' },
Subroutine __ANON__: called CC=1 SCAR=0.0
115 16 ▶ 'value' => { check => sub { check_value(@_) }, type => 'd' },
Subroutine __ANON__: called CC=1 SCAR=0.0
116 18 ▶ 'name' => { check => sub { check_str(@_) }, type => 's' },
Subroutine __ANON__: called CC=1 SCAR=0.0
117 5 ▶ 'sharing' => { check => sub { check_sharing(@_) }, type => 's' },
Subroutine __ANON__: called CC=1 SCAR=0.0
118 0 ▶ 'include_in_email' => { check => sub { check_zero_or_one(@_) }, type => 'd' },
Subroutine __ANON__: not called CC=1 SCAR=6.9
119 5 ▶ 'format' => { check => sub { check_format(@_) }, type => 's' },
Subroutine __ANON__: called CC=1 SCAR=0.0
120 6 ▶ 'url' => { check => sub { check_url(@_) }, type => 's' },
Subroutine __ANON__: called CC=1 SCAR=0.0
121 4 ▶ 'meta' => { check => sub { check_hash_ref(@_) }, type => 's' },
Subroutine __ANON__: called CC=1 SCAR=0.0
122 4 ▶ 'generated_at' => { check => sub { check_date(@_) }, type => 's' },
Subroutine __ANON__: called CC=1 SCAR=0.0
123 8 ▶ 'start_date' => { check => sub { check_date(@_) }, type => 's' },
Subroutine __ANON__: called CC=1 SCAR=0.0
124 3 ▶ 'end_date' => { check => sub { check_date(@_) }, type => 's' },
Subroutine __ANON__: called CC=1 SCAR=0.0
125 };
126  
127 sub validate {
128 71 ▶ my ($fields, $values) = @_;
Subroutine validate: called CC=13 SCAR=25.7
Pod undocumented
129  
130 71 ▶ die "ERROR: Missing params list." unless (defined $values);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $values 6 65 71
131  
132 65 ▶ die "ERROR: Parameters have to be hash ref" unless (ref($values) eq 'HASH');
Branch: 1 branch
BranchTrueFalseTotal
unless ref $values eq "HASH" 11 54 65
133  
134 54 my $keys = [];
135 54 foreach my $row (@$fields) {
136 158 my $field = $row->{key};
137 158 my $required = $row->{required};
138 158 push @$keys, $field;
139  
140 die "ERROR: Received invalid param: $field"
141 158 ▶ unless (exists $FIELDS->{$field});
Branch: 1 branch
BranchTrueFalseTotal
unless exists $WWW::StatsMix::Params::FIELDS->{$field} 0 158 158
142  
143 die "ERROR: Missing mandatory param: $field"
144 158 ▶ if ($required && !exists $values->{$field});
Branch: 1 branch
BranchTrueFalseTotal
if $required and not exists $values->{$field} 5 153 158
Condition100%3/3
$required and not exists $values->{$field}
!ll&&!rl&&r
120335
MC/DC100%2/2
$required and not exists $values->{$field}
$required not exists $values->{$field}
Truth table100%3/3
A and not(B)
A= $requiredB= not exists $values->{$field}
ABresult
0X0
100
111
145  
146 die "ERROR: Received undefined mandatory param: $field"
147 153 ▶ if ($required && !defined $values->{$field});
Branch: 1 branch
BranchTrueFalseTotal
if $required and not defined $values->{$field} 2 151 153
Condition100%3/3
$required and not defined $values->{$field}
!ll&&!rl&&r
120312
MC/DC100%2/2
$required and not defined $values->{$field}
$required not defined $values->{$field}
Truth table100%3/3
A and not(B)
A= $requiredB= not defined $values->{$field}
ABresult
0X0
100
111
148  
149 $FIELDS->{$field}->{check}->($values->{$field})
150 151 ▶ if defined $values->{$field};
Branch: 1 branch
BranchTrueFalseTotal
if defined $values->{$field} 70 81 151
151 }
152  
153 20 foreach my $value (keys %$values) {
154 30 ▶ die "ERROR: Invalid key found in params." unless (grep /\b$value\b/, @$keys);
Branch: 1 branch
BranchTrueFalseTotal
unless grep /\b$value\b/, @$keys 8 22 30
155 22 ▶ die "ERROR: Received undefined param: $value" unless (defined $values->{$value});
Branch: 1 branch
BranchTrueFalseTotal
unless defined $values->{$value} 4 18 22
156 18 $FIELDS->{$value}->{check}->($values->{$value});
157 }
158 }
159  
160 =head1 AUTHOR
161  
162 Mohammad S Anwar, C<< <mohammad.anwar at yahoo.com> >>
163  
164 =head1 REPOSITORY
165  
166 L<https://github.com/Manwar/WWW-StatsMix>
167  
168 =head1 BUGS
169  
170 Please report any bugs or feature requests to C<bug-www-statsmix at rt.cpan.org>,
171 or through the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=WWW-StatsMix>.
172 I will be notified, and then you'll automatically be notified of progress on your
173 bug as I make changes.
174  
175 =head1 SUPPORT
176  
177 You can find documentation for this module with the perldoc command.
178  
179 perldoc WWW::StatsMix::Params
180  
181 You can also look for information at:
182  
183 =over 4
184  
185 =item * RT: CPAN's request tracker (report bugs here)
186  
187 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=WWW-StatsMix>
188  
189 =item * AnnoCPAN: Annotated CPAN documentation
190  
191 L<http://annocpan.org/dist/WWW-StatsMix>
192  
193 =item * CPAN Ratings
194  
195 L<http://cpanratings.perl.org/d/WWW-StatsMix>
196  
197 =item * Search CPAN
198  
199 L<http://search.cpan.org/dist/WWW-StatsMix/>
200  
201 =back
202  
203 =head1 LICENSE AND COPYRIGHT
204  
205 Copyright (C) 2014 - 2015 Mohammad S Anwar.
206  
207 This program is free software; you can redistribute it and/or modify it under
208 the terms of the the Artistic License (2.0). You may obtain a copy of the full
209 license at:
210  
211 L<http://www.perlfoundation.org/artistic_license_2_0>
212  
213 Any use, modification, and distribution of the Standard or Modified Versions is
214 governed by this Artistic License.By using, modifying or distributing the Package,
215 you accept this license. Do not use, modify, or distribute the Package, if you do
216 not accept this license.
217  
218 If your Modified Version has been derived from a Modified Version made by someone
219 other than you,you are nevertheless required to ensure that your Modified Version
220 complies with the requirements of this license.
221  
222 This license does not grant you the right to use any trademark, service mark,
223 tradename, or logo of the Copyright Holder.
224  
225 This license includes the non-exclusive, worldwide, free-of-charge patent license
226 to make, have made, use, offer to sell, sell, import and otherwise transfer the
227 Package with respect to any patent claims licensable by the Copyright Holder that
228 are necessarily infringed by the Package. If you institute patent litigation
229 (including a cross-claim or counterclaim) against any party alleging that the
230 Package constitutes direct or contributory patent infringement,then this Artistic
231 License to you shall terminate on the date that such litigation is filed.
232  
233 Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER AND
234 CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES. THE IMPLIED
235 WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR
236 NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY YOUR LOCAL LAW. UNLESS
237 REQUIRED BY LAW, NO COPYRIGHT HOLDER OR CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT,
238 INDIRECT, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE
239 OF THE PACKAGE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
240  
241 =cut
242  
243 12 1; # End of WWW::StatsMix::Params
« StatsMix.pm ↑ Summary StatsMix/UserAgent.pm »