blib/lib/Mo/utils/Hash.pm

filter: Statementstmt 100% 52 / 52 Branchbran 100% 16 / 16 Conditioncond 100% 3 / 3 MC/DCmcdc 100% 2 / 2 Subroutinesub 100% 10 / 10 Podpod 100% 3 / 3 totaltotal 100% 86 / 86 CC 12 SCAR 13.5
CC
12
Cov
100%
check_hash_keys
7.0
check_hash_optional_keys
4.0
check_hash
3.0
CRAP 3.9
↑ Summary
1 package Mo::utils::Hash;
2  
3 5 ▶use base qw(Exporter);
Subroutine BEGIN: called CC=1 SCAR=0.0
4 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 5 ▶use Error::Pure qw(err);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 5 ▶use List::Util 1.33 qw(none);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 5 ▶use Readonly;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 5 Readonly::Array our @EXPORT_OK => qw(check_hash check_hash_keys
12 check_hash_optional_keys);
13  
14 5 our $VERSION = 0.03;
15  
16 10 ▶sub check_hash {
Subroutine check_hash: called CC=3 SCAR=11.0
Pod documented
17 10 my ($self, $key) = @_;
18  
19 10 ▶ if (! exists $self->{$key}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->{$key}) 1 9 10
20 1 return;
21 }
22  
23 9 ▶ if (ref $self->{$key} ne 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self->{$key} ne "HASH") 4 5 9
24 err "Parameter '$key' isn't hash reference.",
25 4 'Reference', (ref $self->{$key}),
26 ;
27 }
28  
29 5 return;
30 }
31  
32 6 ▶sub check_hash_keys {
Subroutine check_hash_keys: called CC=7 SCAR=19.5
Pod documented
33 6 my ($self, $key, @hash_keys) = @_;
34  
35 6 ▶ if (! exists $self->{$key}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->{$key}) 1 5 6
36 1 return;
37 }
38  
39 5 ▶ if (! @hash_keys) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@hash_keys) 1 4 5
40 1 err "Expected keys doesn't exists.";
41 }
42  
43 4 my $hash = $self->{$key};
44 4 my $printable_keys = '';
45 4 foreach my $hash_key (@hash_keys) {
46 7 ▶ if ($printable_keys) {
Branch: 1 branch
BranchTrueFalseTotal
if ($printable_keys) 3 4 7
47 3 $printable_keys .= '.';
48 }
49 7 $printable_keys .= $hash_key;
50 7 ▶ if (ref $hash ne 'HASH' || ! exists $hash->{$hash_key}) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $hash ne "HASH" or not exists $hash->{$hash_key}) 3 4 7
Condition100%3/3
ref $hash ne "HASH" or not exists $hash->{$hash_key}
l!l&&r!l&&!r
124
MC/DC100%2/2
ref $hash ne "HASH" or not exists $hash->{$hash_key}
ref $hash ne "HASH" not exists $hash->{$hash_key}
Truth table100%3/3
A or not(B)
A= ref $hash ne "HASH"B= not exists $hash->{$hash_key}
ABresult
1X1
011
000
51 3 err "Parameter '$key' doesn't contain expected keys.",
52 'Keys', $printable_keys,
53 ;
54 }
55 4 $hash = $hash->{$hash_key};
56 }
57  
58 1 return;
59 }
60  
61 6 ▶sub check_hash_optional_keys {
Subroutine check_hash_optional_keys: called CC=4 SCAR=13.9
Pod documented
62 6 my ($self, $key, @optional_root_keys) = @_;
63  
64 6 ▶ if (! exists $self->{$key}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->{$key}) 1 5 6
65 1 return;
66 }
67  
68 5 check_hash($self, $key);
69  
70 4 foreach my $hash_key (keys %{$self->{$key}}) {
71 3 ▶ if (none { $hash_key eq $_ } @optional_root_keys) {
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
if (&none(sub { $hash_key eq $_; } , @optional_root_keys)) 1 2 3
72 1 err "Parameter '$key' contain bad hash key.",
73 'Bad key' => $hash_key,
74 ;
75 }
76 }
77  
78 3 return;
79 }
80  
81 5 1;
82  
83 __END__
↑ Summary