blib/lib/Hash/Typed.pm

filter: Statementstmt 100% 115 / 115 Branchbran 100% 40 / 40 Conditioncond 100% 17 / 17 MC/DCmcdc 100% 9 / 9 Subroutinesub 100% 17 / 17 Podpod 0.0% 0 / 3 totaltotal 98.5% 198 / 201 CC 30 SCAR 18.8
CC
30
Cov
100%
STORE
14.0
TIEHASH
6.0
PARSE
5.0
CRAP 6.5
↑ Summary
1 package Hash::Typed;
2 6 ▶use strict; use warnings; our $VERSION = '0.07';
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
3 6 ▶use Carp qw/croak/; use Tie::Hash; our (@ISA);
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 6 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
6 6 @ISA = qw(Tie::Hash);
7 }
8  
9 6 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod undocumented
10 6 my ($package) = (shift);
11  
12 6 my $self = { };
13
14 6 tie %{$self}, 'Hash::Typed', @_;
15  
16 5 bless $self, $package;
17 }
18  
19 47 ▶sub TIEHASH {
Subroutine TIEHASH: called CC=6 SCAR=17.9
20 47 my ($pkg) = shift;
21 47 my($self) = [];
22 47 push @{$self}, {}, [], [], 0;
23 47 $self = bless $self, $pkg;
24 47 ▶ if (ref $_[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[0]) 16 31 47
25 16 my $spec = $self->PARSE(shift);
26 15 push @{$self}, $spec;
27 }
28 46 ▶ while (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 50 43 93
29 50 $self->STORE(shift, shift);
30 }
31 43 ▶ if ($self->[4] && $self->[4]->{required}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->[4] and $self->[4]{"required"}) 4 39 43
Condition100%3/3
$self->[4] and $self->[4]{"required"}
!ll&&!rl&&r
3184
MC/DC100%2/2
$self->[4] and $self->[4]{"required"}
$self->[4] $self->[4]{"required"}
Truth table100%3/3
A and B
A= $self->[4]B= $self->[4]{"required"}
ABresult
0X0
100
111
32 4 ▶ if (ref $self->[4]->{required}) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self->[4]{'required'}) { } 3 1 4
33 3 $self->REQUIRED($self->[4]->{required});
34 } else {
35 1 $self->REQUIRED([keys %{$self->[4]->{ordered_keys}}]);
36 }
37 }
38 42 return $self;
39 }
40  
41 387 ▶sub FETCH {
Subroutine FETCH: called CC=2 SCAR=6.9
42 387 my($self, $key) = (shift, shift);
43 387 ▶ return exists( $self->[0]{$key} ) ? $self->[2][ $self->[0]{$key} ] : undef;
Branch: 1 branch
BranchTrueFalseTotal
exists $self->[0]{$key} ? : 353 34 387
44 }
45  
46 147 ▶sub STORE {
Subroutine STORE: called CC=14 SCAR=26.4
47 147 my ($self, $key, $value) = @_;
48  
49 147 ▶ if ($self->[4]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->[4]) 56 91 147
50 56 my $described = $self->[4]->{keys}->{$key};
51  
52 56 ▶ if ($self->[4]->{strict} && !$described) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->[4]{"strict"} and not $described) 3 53 56
Condition100%3/3
$self->[4]{"strict"} and not $described
!ll&&!rl&&r
20333
MC/DC100%2/2
$self->[4]{"strict"} and not $described
$self->[4]{"strict"} not $described
Truth table100%3/3
A and not(B)
A= $self->[4]{"strict"}B= not $described
ABresult
0X0
100
111
53 3 croak "Strict mode enabled and passed key \"${key}\" does not exist in the specification.";
54 }
55  
56 53 ▶ $value = $described->($value)
Branch: 1 branch
BranchTrueFalseTotal
if $described 50 3 53
57 if ($described);
58 }
59  
60 141 ▶ if (exists $self->[0]{$key}) {
Branch: 2 branches
BranchTrueFalseTotal
if (exists $self->[0]{$key}) { } 3 138 141
elsif ($self->[4] and defined $self->[4]{'ordered_keys'}{$key} and $self->[4]{'ordered_keys'}{$key} <= scalar @{$self->[1];}) { } 37 101 138
61 3 my($i) = $self->[0]{$key};
62 3 $self->[1][$i] = $key;
63 3 $self->[2][$i] = $value;
64 3 $self->[0]{$key} = $i;
65 44 ▶ } elsif ($self->[4] && defined $self->[4]{ordered_keys}{$key} && $self->[4]{ordered_keys}{$key} <= scalar @{$self->[1]}) {
Condition100%6/6
$self->[4] and defined $self->[4]{"ordered_keys"}{$key} and $self->[4]{"ordered_keys"}{$key} <= scalar @{$self->[1];}
!ll&&!rl&&r
94737
$self->[4] and defined $self->[4]{"ordered_keys"}{$key}
!ll&&!rl&&r
91344
MC/DC100%3/3
$self->[4] and defined $self->[4]{"ordered_keys"}{$key} and $self->[4]{"ordered_keys"}{$key} <= scalar @{$self->[1];}
$self->[4] defined $self->[4]{"ordered_keys"}{$key} $self->[4]{"ordered_keys"}{$key} <= scalar @{$self->[1];}
Truth table100%4/4
A and B and C
A= $self->[4]B= defined $self->[4]{"ordered_keys"}{$key}C= $self->[4]{"ordered_keys"}{$key} <= scalar @{$self->[1];}
ABCresult
0XX0
10X0
1100
1111
66 37 my $i = $self->[4]{ordered_keys}{$key};
67 37 my $before = $self->[1]->[$i - 1];
68 37 ▶ $i = $i == 0 ? $i : --$i if ($before && ($self->[4]{ordered_keys}{$before} || -1) >= $i);
Branch: 2 branches
BranchTrueFalseTotal
if $before and ($self->[4]{"ordered_keys"}{$before} || -1) >= $i 13 24 37
$i == 0 ? : 6 7 13
Condition100%5/5
$before and ($self->[4]{"ordered_keys"}{$before} || -1) >= $i
!ll&&!rl&&r
81613
$self->[4]{'ordered_keys'}{$before} || -1
l!l
236
MC/DC100%2/2
$before and ($self->[4]{"ordered_keys"}{$before} || -1) >= $i
$before $self->[4]{'ordered_keys'}{$before}
Truth table100%3/3
A and B
A= $beforeB= $self->[4]{'ordered_keys'}{$before}
ABresult
0X0
100
111
69 37 splice(@{$self->[1]}, $i, 0, $key);
70 37 splice(@{$self->[2]}, $i, 0, $value);
71 37 $self->[0]{$key} = $i;
72 $self->[0]{ $self->[1][$_] }++
73 37 for ($i+1..$#{$self->[1]});
74 } else {
75 101 push(@{$self->[1]}, $key);
76 101 push(@{$self->[2]}, $value);
77 101 $self->[0]{$key} = $#{$self->[1]};
78 }
79 }
80  
81 5 ▶sub DELETE {
Subroutine DELETE: called CC=4 SCAR=13.9
82 5 my ($self, $key) = @_;
83  
84 5 ▶ if (exists $self->[0]{$key}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->[0]{$key}) 4 1 5
85 4 my($i) = $self->[0]{$key};
86 $self->[0]{ $self->[1][$_] }--
87 4 for ($i+1..$#{$self->[1]});
88 4 ▶ $self->[3]-- if ( $i == $self->[3]-1 );
Branch: 1 branch
BranchTrueFalseTotal
if $i == $self->[3] - 1 3 1 4
89 4 delete $self->[0]{$key};
90 4 splice @{$self->[1]}, $i, 1;
91 4 return (splice(@{$self->[2]}, $i, 1))[0];
92 }
93 1 return undef;
94 }
95  
96 1 ▶sub CLEAR {
Subroutine CLEAR: called CC=1 SCAR=0.0
97 1 my ($self) = @_;
98 1 push @{$self}, {}, [], [], 0;
99 }
100  
101 29 ▶sub EXISTS { exists $_[0]->[0]{ $_[1] }; }
Subroutine EXISTS: called CC=1 SCAR=0.0
102  
103 28 ▶sub FIRSTKEY {
Subroutine FIRSTKEY: called CC=1 SCAR=0.0
104 28 $_[0][3] = 0;
105 28 &NEXTKEY;
106 }
107  
108 112 ▶sub NEXTKEY {
Subroutine NEXTKEY: called CC=2 SCAR=6.9
109 112 ▶ return $_[0][1][ $_[0][3]++ ] if ($_[0][3] <= $#{ $_[0][1] } );
Branch: 1 branch
BranchTrueFalseTotal
if $_[0][3] <= $#{$_[0][1];} 84 28 112
110 28 return undef;
111 }
112  
113 1 ▶sub SCALAR { scalar(@{$_[0]->[1]}); }
Subroutine SCALAR: called CC=1 SCAR=0.0
114  
115 31 ▶sub PARSE {
Subroutine PARSE: called CC=5 SCAR=16.1
Pod undocumented
116 31 my ($self, $spec) = @_;
117 31 my (%keys, %described);
118 31 tie(%described, 'Hash::Typed');
119 31 ▶ while (@{$spec}) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$spec;}) 77 30 107
120 77 my ($key, $value) = (shift @{$spec}, shift @{$spec});
121 77 ▶ if ($key eq 'keys') {
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq "keys") 16 61 77
122 16 ▶ if (ref $value eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $value eq 'ARRAY') { } 15 1 16
123 15 ($value) = $self->PARSE($value);
124 15 my $i = 0;
125 15 %keys = map { $_ => $i++ } keys %{$value};
126 } else {
127 1 croak "keys spec must currently be an ARRAY";
128 }
129 }
130 76 $described{$key} = $value;
131 }
132 30 ▶ $described{ordered_keys} = \%keys if scalar keys %keys;
Branch: 1 branch
BranchTrueFalseTotal
if scalar keys %keys 15 15 30
133 30 return \%described;
134 }
135  
136 4 ▶sub REQUIRED {
Subroutine REQUIRED: called CC=3 SCAR=11.0
Pod undocumented
137 4 my ($self, $keys) = @_;
138 4 for my $key (@{$keys}) {
139 11 ▶ if (! defined $self->[0]{$key}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->[0]{$key}) 1 10 11
140 1 croak "Required key $key not set.";
141 }
142 }
143 }
144  
145 6 1;
146  
147 __END__;
↑ Summary