blib/lib/Mo/utils.pm

filter: Statementstmt 100% 118 / 118 Branchbran 100% 64 / 64 Conditioncond 100% 12 / 12 MC/DCmcdc 85.7% 6 / 7 Subroutinesub 100% 22 / 22 Podpod 100% 13 / 13 totaltotal 99.5% 235 / 236 SCAR 36.9
CC
40
Cov
100%
check_isa
6.0
check_bool
5.0
check_strings
5.0
CRAP 40.0
↑ Summary
1 package Mo::utils;
2  
3 15 ▶use base qw(Exporter);
Subroutine BEGIN: called CC=1 SCAR=0.0
4 15 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 15 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 15 ▶use Error::Pure qw(err);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 15 ▶use List::Util qw(none);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 15 ▶use Readonly;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 15 ▶use Scalar::Util qw(blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 15 Readonly::Array our @EXPORT_OK => qw(check_array check_array_object check_array_required
13 check_bool check_code check_isa check_length check_number check_number_of_items
14 check_regexp check_required check_string_begin check_strings);
15  
16 15 our $VERSION = 0.19;
17  
18 sub check_array {
19 11 ▶ my ($self, $key) = @_;
Subroutine check_array: called CC=3 SCAR=11.0
Pod documented
20  
21 11 ▶ if (! exists $self->{$key}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->{$key}) 1 10 11
22 1 return;
23 }
24  
25 10 ▶ if (ref $self->{$key} ne 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self->{$key} ne "ARRAY") 5 5 10
26 err "Parameter '".$key."' must be a array.",
27 'Value', $self->{$key},
28 5 'Reference', (ref $self->{$key}),
29 ;
30 }
31  
32 5 return;
33 }
34  
35 sub check_array_object {
36 4 ▶ my ($self, $key, $class, $class_name) = @_;
Subroutine check_array_object: called CC=4 SCAR=13.9
Pod documented
37  
38 4 ▶ if (! exists $self->{$key}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->{$key}) 1 3 4
39 1 return;
40 }
41  
42 3 check_array($self, $key);
43  
44 2 foreach my $obj (@{$self->{$key}}) {
45 2 ▶ if (! $obj->isa($class)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($obj->isa($class)) 1 1 2
46 1 err $class_name." isn't '".$class."' object.";
47 }
48 }
49  
50 1 return;
51 }
52  
53 sub check_array_required {
54 5 ▶ my ($self, $key) = @_;
Subroutine check_array_required: called CC=3 SCAR=11.0
Pod documented
55  
56 5 ▶ if (! exists $self->{$key}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->{$key}) 1 4 5
57 1 err "Parameter '$key' is required.";
58 }
59  
60 4 check_array($self, $key);
61  
62 2 ▶ if (! @{$self->{$key}}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@{$self->{$key};}) 1 1 2
63 1 err "Parameter '".$key."' with array must have at least one item.";
64 }
65  
66 1 return;
67 }
68  
69 sub check_bool {
70 6 ▶ my ($self, $key) = @_;
Subroutine check_bool: called CC=5 SCAR=16.1
Pod documented
71  
72 6 ▶ _check_key($self, $key) && return;
Branch: 1 branch
BranchTrueFalseTotal
_check_key($self, $key) and return 2 4 6
73  
74 4 ▶ if ($self->{$key} !~ m/^\d+$/ms || ($self->{$key} != 0 && $self->{$key} != 1)) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->{$key} =~ /^\d+$/ms or $self->{$key} != 0 and $self->{$key} != 1) 2 2 4
Condition100%6/6
not $self->{$key} =~ /^\d+$/ms or $self->{$key} != 0 and $self->{$key} != 1
l!l&&r!l&&!r
112
$self->{$key} != 0 and $self->{$key} != 1
!ll&&!rl&&r
111
MC/DC66%2/3
not $self->{$key} =~ /^\d+$/ms or $self->{$key} != 0 and $self->{$key} != 1
not $self->{$key} =~ /^\d+$/ms $self->{$key} != 0 $self->{$key} != 1
Truth table75%3/4
not(A) or B and C
A= not $self->{$key} =~ /^\d+$/msB= $self->{$key} != 0C= $self->{$key} != 1
ABCresult
1XX1
0111
00X0
0100
75 err "Parameter '$key' must be a bool (0/1).",
76 2 'Value', $self->{$key},
77 ;
78 }
79  
80 2 return;
81 }
82  
83 sub check_code {
84 4 ▶ my ($self, $key) = @_;
Subroutine check_code: called CC=3 SCAR=11.0
Pod documented
85  
86 4 ▶ _check_key($self, $key) && return;
Branch: 1 branch
BranchTrueFalseTotal
_check_key($self, $key) and return 2 2 4
87  
88 2 ▶ if (ref $self->{$key} ne 'CODE') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self->{$key} ne "CODE") 1 1 2
89 err "Parameter '$key' must be a code.",
90 1 'Value', $self->{$key},
91 ;
92 }
93  
94 1 return;
95 }
96  
97 sub check_isa {
98 7 ▶ my ($self, $key, $class) = @_;
Subroutine check_isa: called CC=6 SCAR=17.9
Pod documented
99  
100 7 ▶ _check_key($self, $key) && return;
Branch: 1 branch
BranchTrueFalseTotal
_check_key($self, $key) and return 2 5 7
101  
102 5 ▶ if (! blessed($self->{$key})) {
Branch: 1 branch
BranchTrueFalseTotal
unless (builtin::blessed($self->{$key})) 3 2 5
103 err "Parameter '$key' must be a '$class' object.",
104  
105 # Only, if value is scalar.
106 (ref $self->{$key} eq '') ? (
107 'Value', $self->{$key},
108 ) : (),
109  
110 # Only if value is reference.
111 (ref $self->{$key} ne '') ? (
112 3 ▶ 'Reference', (ref $self->{$key}),
Branch: 2 branches
BranchTrueFalseTotal
ref $self->{$key} eq '' ? : 1 2 3
ref $self->{$key} ne '' ? : 2 1 3
113 ) : (),
114 }
115  
116 2 ▶ if (! $self->{$key}->isa($class)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{$key}->isa($class)) 1 1 2
117 err "Parameter '$key' must be a '$class' object.",
118 1 'Reference', (ref $self->{$key}),
119 ;
120 }
121  
122 1 return;
123 }
124  
125 sub check_length {
126 5 ▶ my ($self, $key, $max_length) = @_;
Subroutine check_length: called CC=3 SCAR=11.0
Pod documented
127  
128 5 ▶ _check_key($self, $key) && return;
Branch: 1 branch
BranchTrueFalseTotal
_check_key($self, $key) and return 2 3 5
129  
130 3 ▶ if (length $self->{$key} > $max_length) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $self->{$key} > $max_length) 1 2 3
131 err "Parameter '$key' has length greater than '$max_length'.",
132 1 'Value', $self->{$key},
133 ;
134 }
135  
136 2 return;
137 }
138  
139 sub check_number {
140 7 ▶ my ($self, $key) = @_;
Subroutine check_number: called CC=3 SCAR=11.0
Pod documented
141  
142 7 ▶ _check_key($self, $key) && return;
Branch: 1 branch
BranchTrueFalseTotal
_check_key($self, $key) and return 2 5 7
143  
144 5 ▶ if ($self->{$key} !~ m/^[-+]?\d+(\.\d+)?$/ms) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{$key} =~ /^[-+]?\d+(\.\d+)?$/ms) 1 4 5
145 err "Parameter '$key' must be a number.",
146 1 'Value', $self->{$key},
147 ;
148 }
149  
150 4 return;
151 }
152  
153 sub check_number_of_items {
154 2 ▶ my ($self, $list_method, $item_method, $object_name, $item_name) = @_;
Subroutine check_number_of_items: called CC=4 SCAR=13.9
Pod documented
155  
156 2 my $item_hr = {};
157 2 foreach my $item (@{$self->$list_method}) {
158 4 $item_hr->{$item->$item_method} += 1;
159 }
160  
161 2 foreach my $item (keys %{$item_hr}) {
162 3 ▶ if ($item_hr->{$item} > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($item_hr->{$item} > 1) 1 2 3
163 1 err "$object_name for $item_name '$item' has multiple values."
164 }
165 }
166  
167 1 return;
168 }
169  
170 sub check_regexp {
171 4 ▶ my ($self, $key, $regexp) = @_;
Subroutine check_regexp: called CC=4 SCAR=13.9
Pod documented
172  
173 4 ▶ _check_key($self, $key) && return;
Branch: 1 branch
BranchTrueFalseTotal
_check_key($self, $key) and return 1 3 4
174  
175 3 ▶ if (! defined $regexp) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $regexp) 1 2 3
176 1 err "Parameter '$key' must have defined regexp.";
177 }
178 2 ▶ if ($self->{$key} !~ m/^$regexp/ms) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{$key} =~ /^$regexp/ms) 1 1 2
179 err "Parameter '$key' does not match the specified regular expression.",
180 1 'String', $self->{$key},
181 'Regexp', $regexp,
182 ;
183 }
184  
185 1 return;
186 }
187  
188 sub check_required {
189 3 ▶ my ($self, $key) = @_;
Subroutine check_required: called CC=3 SCAR=11.0
Pod documented
190  
191 3 ▶ if (! exists $self->{$key} || ! defined $self->{$key}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->{$key} and defined $self->{$key}) 2 1 3
Condition100%3/3
exists $self->{$key} and defined $self->{$key}
!ll&&!rl&&r
111
MC/DC100%2/2
exists $self->{$key} and defined $self->{$key}
exists $self->{$key} defined $self->{$key}
Truth table100%3/3
A and B
A= exists $self->{$key}B= defined $self->{$key}
ABresult
0X0
100
111
192 2 err "Parameter '$key' is required.";
193 }
194  
195 1 return;
196 }
197  
198 sub check_string_begin {
199 4 ▶ my ($self, $key, $string_base) = @_;
Subroutine check_string_begin: called CC=4 SCAR=13.9
Pod documented
200  
201 4 ▶ _check_key($self, $key) && return;
Branch: 1 branch
BranchTrueFalseTotal
_check_key($self, $key) and return 1 3 4
202  
203 3 ▶ if (! defined $string_base) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $string_base) 1 2 3
204 1 err "Parameter '$key' must have defined string base.";
205 }
206 2 ▶ if ($self->{$key} !~ m/^$string_base/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{$key} =~ /^$string_base/) 1 1 2
207 err "Parameter '$key' must begin with defined string base.",
208 1 'String', $self->{$key},
209 'String base', $string_base,
210 ;
211 }
212  
213 1 return;
214 }
215  
216 sub check_strings {
217 6 ▶ my ($self, $key, $strings_ar) = @_;
Subroutine check_strings: called CC=5 SCAR=16.1
Pod documented
218  
219 6 ▶ _check_key($self, $key) && return;
Branch: 1 branch
BranchTrueFalseTotal
_check_key($self, $key) and return 1 5 6
220  
221 5 ▶ if (! defined $strings_ar) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $strings_ar) 1 4 5
222 1 err "Parameter '$key' must have strings definition.";
223 }
224 4 ▶ if (ref $strings_ar ne 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $strings_ar ne "ARRAY") 1 3 4
225 1 err "Parameter '$key' must have right string definition.";
226 }
227 3 ▶ if (none { $self->{$key} eq $_ } @{$strings_ar}) {
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
if (&none(sub { $self->{$key} eq $_; } , @{$strings_ar;})) 1 2 3
228 err "Parameter '$key' must be one of defined strings.",
229 'String', $self->{$key},
230 1 'Possible strings', (join ', ', @{$strings_ar}),
231 ;
232 }
233  
234 2 return;
235 }
236  
237 sub _check_key {
238 43 ▶ my ($self, $key) = @_;
Subroutine _check_key: called CC=3 SCAR=11.0
239  
240 43 ▶ if (! exists $self->{$key} || ! defined $self->{$key}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->{$key} and defined $self->{$key}) 13 30 43
Condition100%3/3
exists $self->{$key} and defined $self->{$key}
!ll&&!rl&&r
5830
MC/DC100%2/2
exists $self->{$key} and defined $self->{$key}
exists $self->{$key} defined $self->{$key}
Truth table100%3/3
A and B
A= exists $self->{$key}B= defined $self->{$key}
ABresult
0X0
100
111
241 13 return 1;
242 }
243  
244 30 return 0;
245 }
246  
247 15 1;
248  
249 __END__
↑ Summary