blib/lib/Mo/utils/Array.pm

filter: Statementstmt 100% 75 / 75 Branchbran 100% 24 / 24 Conditioncond 100% 6 / 6 MC/DCmcdc 100% 4 / 4 Subroutinesub 100% 14 / 14 Podpod 100% 5 / 5 totaltotal 100% 128 / 128 CC 17 SCAR 13.5
CC
17
Cov
100%
check_array_strings
8.0
check_array
4.0
check_array_items
3.0
CRAP 3.9
↑ Summary
1 package Mo::utils::Array;
2  
3 7 ▶use base qw(Exporter);
Subroutine BEGIN: called CC=1 SCAR=0.0
4 7 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 7 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 7 ▶use Error::Pure qw(err);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 7 ▶use List::Util 1.33 qw(none);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 7 ▶use Mo::utils::common qw(check_object);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 7 ▶use Readonly;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 7 ▶use Scalar::Util qw(blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 7 Readonly::Array our @EXPORT_OK => qw(check_array check_array_items check_array_object
14 check_array_required check_array_strings);
15  
16 7 our $VERSION = 0.05;
17  
18 27 ▶sub check_array {
Subroutine check_array: called CC=4 SCAR=13.9
Pod documented
19 27 my ($self, $key) = @_;
20  
21 27 ▶ if (! exists $self->{$key}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->{$key}) 1 26 27
22 1 return;
23 }
24  
25 26 ▶ if (ref $self->{$key} ne 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self->{$key} ne "ARRAY") 10 16 26
26 10 my $ref = ref $self->{$key};
27 err "Parameter '".$key."' must be a array.",
28 10 ▶ 'Value', $self->{$key},
Branch: 1 branch
BranchTrueFalseTotal
$ref eq '' ? : 9 1 10
29 'Reference', ($ref eq '' ? 'SCALAR' : $ref),
30 ;
31 }
32  
33 16 return;
34 }
35  
36 5 ▶sub check_array_items {
Subroutine check_array_items: called CC=3 SCAR=11.0
Pod documented
37 5 my ($self, $key, $max_items) = @_;
38  
39 5 ▶ if (! exists $self->{$key}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->{$key}) 1 4 5
40 1 return;
41 }
42  
43 4 check_array($self, $key);
44  
45 3 ▶ if (@{$self->{$key}} > $max_items) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$self->{$key};} > $max_items) 1 2 3
46 err "Parameter '".$key."' has more items than expected.",
47 'Maximum items', $max_items,
48 1 'Number of items', (scalar @{$self->{$key}}),
49 ;
50 }
51  
52 2 return;
53 }
54  
55 6 ▶sub check_array_object {
Subroutine check_array_object: called CC=3 SCAR=11.0
Pod documented
56 6 my ($self, $key, $class) = @_;
57  
58 6 ▶ if (! exists $self->{$key}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->{$key}) 1 5 6
59 1 return;
60 }
61  
62 5 check_array($self, $key);
63  
64 4 foreach my $obj (@{$self->{$key}}) {
65 4 check_object($obj, $class,
66 "Parameter '%s' with array must contain '%s' objects.",
67 [$key, $class],
68 );
69 }
70  
71 1 return;
72 }
73  
74 5 ▶sub check_array_required {
Subroutine check_array_required: called CC=3 SCAR=11.0
Pod documented
75 5 my ($self, $key) = @_;
76  
77 5 ▶ if (! exists $self->{$key}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->{$key}) 1 4 5
78 1 err "Parameter '$key' is required.";
79 }
80  
81 4 check_array($self, $key);
82  
83 2 ▶ if (! @{$self->{$key}}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@{$self->{$key};}) 1 1 2
84 1 err "Parameter '".$key."' with array must have at least one item.";
85 }
86  
87 1 return;
88 }
89  
90 11 ▶sub check_array_strings {
Subroutine check_array_strings: called CC=8 SCAR=20.8
Pod documented
91 11 my ($self, $key, $strings_ar) = @_;
92  
93 11 ▶ if (! exists $self->{$key}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->{$key}) 1 10 11
94 1 return;
95 }
96  
97 10 ▶ if (defined $strings_ar && ref $strings_ar ne 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $strings_ar and ref $strings_ar ne "ARRAY") 1 9 10
Condition100%3/3
defined $strings_ar and ref $strings_ar ne "ARRAY"
!ll&&!rl&&r
361
MC/DC100%2/2
defined $strings_ar and ref $strings_ar ne "ARRAY"
defined $strings_ar ref $strings_ar ne "ARRAY"
Truth table100%3/3
A and B
A= defined $strings_arB= ref $strings_ar ne "ARRAY"
ABresult
0X0
100
111
98 1 err "Parameter '$key' must have right string definition.";
99 }
100  
101 9 check_array($self, $key);
102  
103 6 foreach my $value (@{$self->{$key}}) {
104 6 ▶ if (ref $value ne '') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $value ne "") 1 5 6
105 1 err "Parameter '$key' must contain a list of strings.",
106 'Value', $value,
107 'Reference', (ref $value),
108 ;
109 }
110 5 ▶ if (defined $strings_ar && none { $value eq $_ } @{$strings_ar}) {
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
if (defined $strings_ar and &none(sub { $value eq $_; } , @{$strings_ar;})) 1 4 5
Condition100%3/3
defined $strings_ar and &none(sub { $value eq $_; } , @{$strings_ar;})
!ll&&!rl&&r
131
MC/DC100%2/2
defined $strings_ar and &none(sub { $value eq $_; } , @{$strings_ar;})
defined $strings_ar &none(sub { $value eq $_; } , @{$strings_ar;})
Truth table100%3/3
A and B
A= defined $strings_arB= &none(sub { $value eq $_; } , @{$strings_ar;})
ABresult
0X0
100
111
111 err "Parameter '$key' must be one of the defined strings.",
112 'Value', $value,
113 1 'Possible strings', "'".(join "', '", @{$strings_ar})."'",
114 ;
115 }
116 }
117  
118 4 return;
119 }
120  
121 7 1;
122  
123 __END__
↑ Summary