lib/PHP/Decode/Array.pm

filter: Statementstmt 90.4% 142 / 157 Branchbran 85.7% 60 / 70 Conditioncond 78.5% 33 / 42 MC/DCmcdc 64.0% 16 / 25 Subroutinesub 95.2% 20 / 21 Podpod 68.7% 11 / 16 totaltotal 85.2% 282 / 331 CC 53 SCAR 20.1
CC
53
Cov
87%
copy
13.0
get_keys_sorted
12.0
set
11.0
CRAP 7.5
« lib/PHP/Decode.pm ↑ Summary blib/lib/PHP/Decode/Test.pm »
1 #
2 # PHP arrays - a php array is an ordered map.
3 # http://www.php.net/manual/en/language.types.array.php
4 #
5 package PHP::Decode::Array;
6  
7 7 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 7 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 7 ▶use Tie::IxHash;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 7 ▶use Exporter qw(import);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 7 our @EXPORT_OK = qw(is_int_index);
12 7 our %EXPORT_TAGS = (all => \@EXPORT_OK);
13  
14 7 our $VERSION = '0.15';
15  
16 7 my $arridx = 1;
17 7 our $arrpfx = '#arr';
18 7 our $class_strmap; # client might override $PHP::Decode::Array::class_strmap = \%strmap;
19  
20 347 ▶sub new_name {
Subroutine new_name: called CC=1 SCAR=0.0
Pod undocumented
21 347 my $name = "$arrpfx$arridx";
22 347 $arridx++;
23 347 return $name;
24 }
25  
26 1250 ▶sub is_int_index {
Subroutine is_int_index: called CC=2 SCAR=6.9
Pod undocumented
27 1250 my ($k) = @_;
28  
29 1250 ▶ if ($k =~ /^\-?\d+$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($k =~ /^\-?\d+$/) 1112 138 1250
30 1112 return 1;
31 }
32 138 return 0;
33 }
34  
35 161 ▶sub _ordered_map {
Subroutine _ordered_map: called CC=3 SCAR=11.0
36 161 my ($self) = @_;
37  
38 # preserve the order of inserted keys
39 # https://perldoc.perl.org/perlfaq4#How-can-I-make-my-hash-remember-the-order-I-put-elements-into-it?
40 # https://metacpan.org/pod/Tie::IxHash
41 #
42 161 tie my %map, "Tie::IxHash";
43  
44 # convert existing consecutive numeric map to ordered map
45 #
46 161 ▶ if (exists $self->{map}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"map"}) 13 148 161
47 13 foreach my $k (sort { $a <=> $b } keys %{$self->{map}}) {
48 22 $map{$k} = $self->{map}{$k};
49 }
50 }
51 161 return \%map;
52 }
53  
54 347 ▶sub new {
Subroutine new: called CC=3 SCAR=11.0
Pod documented
55 347 my ($class, %args) = @_;
56  
57 # $self->{map} is created on demand and converted to ordered map
58 # if required. A native perl hashmap is much faster.
59 #
60 347 my $self = bless {
61 %args,
62 name => new_name(),
63 idx => undef,
64 pos => 0,
65 }, $class;
66 347 ▶ $self->{strmap} = $class_strmap unless exists $self->{strmap};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"strmap"} 2 345 347
67  
68 347 ▶ if (defined $self->{strmap}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"strmap"}) 343 4 347
69 343 $self->{strmap}{$self->{name}} = $self; # register name
70 }
71 347 return $self;
72 }
73  
74 # return number if key contains numeric value.
75 #
76 380 ▶sub get_index {
Subroutine get_index: called CC=8 SCAR=20.8
Pod undocumented
77 380 my ($self, $k) = @_;
78 380 my $k0 = $k;
79  
80 # float keys are truncated to int,
81 # http://php.net/manual/en/language.types.array.php
82 # (but only int-strings are converted to int-key)
83 #
84 380 ▶ if (defined $self->{strmap} && exists $self->{strmap}{$k}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"strmap"} and exists $self->{"strmap"}{$k}) 307 73 380
Condition100%3/3
defined $self->{"strmap"} and exists $self->{"strmap"}{$k}
!ll&&!rl&&r
370307
MC/DC100%2/2
defined $self->{"strmap"} and exists $self->{"strmap"}{$k}
defined $self->{"strmap"} exists $self->{"strmap"}{$k}
Truth table100%3/3
A and B
A= defined $self->{"strmap"}B= exists $self->{"strmap"}{$k}
ABresult
0X0
100
111
85 307 $k = $self->{strmap}{$k};
86 }
87 380 ▶ if (($k0 =~ /^#str\d+$/) && ($k =~ /^\-?(\d|[1-9]\d+)$/)) {
Branch: 2 branches
BranchTrueFalseTotal
if ($k0 =~ /^#str\d+$/ and $k =~ /^\-?(\d|[1-9]\d+)$/) { } 2 378 380
elsif (not $k0 =~ /^#str\d+$/ and ref $k eq '' and $k =~ /^\-?(\d|[1-9]\d+|\d+\.\d*|\d*\.\d+)([eE][+-]?\d+)?$/) { } 234 144 378
Condition100%3/3
$k0 =~ /^#str\d+$/ and $k =~ /^\-?(\d|[1-9]\d+)$/
!ll&&!rl&&r
2671112
MC/DC100%2/2
$k0 =~ /^#str\d+$/ and $k =~ /^\-?(\d|[1-9]\d+)$/
$k0 =~ /^#str\d+$/ $k =~ /^\-?(\d|[1-9]\d+)$/
Truth table100%3/3
A and B
A= $k0 =~ /^#str\d+$/B= $k =~ /^\-?(\d|[1-9]\d+)$/
ABresult
0X0
100
111
88 2 $k = int($k);
89 ▶ } elsif (($k0 !~ /^#str\d+$/) && (ref($k) eq '') && ($k =~ /^\-?(\d|[1-9]\d+|\d+\.\d*|\d*\.\d+)([eE][+-]?\d+)?$/)) {
Condition100%6/6
not $k0 =~ /^#str\d+$/ and ref $k eq "" and $k =~ /^\-?(\d|[1-9]\d+|\d+\.\d*|\d*\.\d+)([eE][+-]?\d+)?$/
!ll&&!rl&&r
11529234
not $k0 =~ /^#str\d+$/ and ref $k eq ""
!ll&&!rl&&r
1114263
MC/DC100%3/3
not $k0 =~ /^#str\d+$/ and ref $k eq "" and $k =~ /^\-?(\d|[1-9]\d+|\d+\.\d*|\d*\.\d+)([eE][+-]?\d+)?$/
not $k0 =~ /^#str\d+$/ ref $k eq "" $k =~ /^\-?(\d|[1-9]\d+|\d+\.\d*|\d*\.\d+)([eE][+-]?\d+)?$/
Truth table100%4/4
not(A) and B and C
A= not $k0 =~ /^#str\d+$/B= ref $k eq ""C= $k =~ /^\-?(\d|[1-9]\d+|\d+\.\d*|\d*\.\d+)([eE][+-]?\d+)?$/
ABCresult
0XX0
10X0
1100
1111
90 234 $k = int($k);
91 } else {
92 144 $k = $k0;
93 }
94 380 return $k;
95 }
96  
97 732 ▶sub set {
Subroutine set: called CC=11 SCAR=24.0
Pod documented
98 732 my ($self, $k, $v) = @_;
99  
100 # without key use the increment of the largest previously used int key
101 #
102 732 ▶ if (defined $k) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $k) { } 179 553 732
103 179 $k = $self->get_index($k);
104 179 ▶ if (is_int_index($k)) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_int_index($k)) { } 93 86 179
105 93 ▶ if (!defined $self->{idx} || ($k >= $self->{idx})) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $self->{"idx"} or $k >= $self->{"idx"}) 59 34 93
Condition100%3/3
not defined $self->{"idx"} or $k >= $self->{"idx"}
l!l&&r!l&&!r
461334
MC/DC100%2/2
not defined $self->{"idx"} or $k >= $self->{"idx"}
not defined $self->{"idx"} $k >= $self->{"idx"}
Truth table100%3/3
not(A) or B
A= not defined $self->{"idx"}B= $k >= $self->{"idx"}
ABresult
1X1
011
000
106 59 $self->{idx} = $k+1;
107 }
108 } else {
109 86 $self->{non_numeric} = 1;
110 }
111 179 ▶ if (!exists $self->{map} || !exists $self->{ordered}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->{"map"} and exists $self->{"ordered"}) 130 49 179
Condition100%3/3
exists $self->{"map"} and exists $self->{"ordered"}
!ll&&!rl&&r
1191149
MC/DC100%2/2
exists $self->{"map"} and exists $self->{"ordered"}
exists $self->{"map"} exists $self->{"ordered"}
Truth table100%3/3
A and B
A= exists $self->{"map"}B= exists $self->{"ordered"}
ABresult
0X0
100
111
112 130 $self->{map} = $self->_ordered_map();
113 130 $self->{ordered} = 1;
114 }
115 } else {
116 # use faster unordered map as long as no explicit key is used.
117 #
118 553 ▶ $self->{map} = {} unless exists $self->{map};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"map"} 150 403 553
119 553 ▶ $self->{idx} = 0 unless defined $self->{idx};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"idx"} 150 403 553
120 553 $k = $self->{idx};
121 553 $self->{idx} += 1;
122 }
123 732 ▶ if (defined $self->{strmap} && (ref($v) eq ref($self))) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{'strmap'} and ref $v eq ref $self) { } 0 732 732
Condition66%2/3
defined $self->{"strmap"} and ref $v eq ref $self
!ll&&!rl&&r
57270
MC/DC0%0/2
defined $self->{"strmap"} and ref $v eq ref $self
defined $self->{"strmap"} ref $v eq ref $self
Truth table66%2/3
A and B
A= defined $self->{"strmap"}B= ref $v eq ref $self
ABresult
0X0
100
111
124 0 $self->{map}{$k} = $v->{name};
125 } else {
126 732 $self->{map}{$k} = $v;
127 }
128 #printf ">> setarr: %s{%s} = %s\n", $self->{name}, $k, $v if $opt{v};
129 732 return $self;
130 }
131  
132 221 ▶sub get {
Subroutine get: called CC=3 SCAR=11.0
Pod documented
133 221 my ($self, $k) = @_;
134  
135 221 ▶ if (exists $self->{map}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"map"}) 199 22 221
136 199 $k = $self->get_index($k);
137  
138 199 ▶ if (exists $self->{map}{$k}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"map"}{$k}) 163 36 199
139 163 return $self->{map}{$k};
140 }
141 }
142 58 return;
143 }
144  
145 63 ▶sub copy {
Subroutine copy: called CC=13 SCAR=25.7
Pod documented
146 63 my ($self, $keys) = @_;
147  
148 # TODO: #arr$x.$y sub-name here?
149 #
150 63 my $c = PHP::Decode::Array->new(strmap => $self->{strmap});
151  
152 63 ▶ if (exists $self->{map}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"map"}) 51 12 63
153 51 ▶ if (exists $self->{ordered} || defined $keys) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{'ordered'} or defined $keys) { } 29 22 51
Condition100%3/3
exists $self->{"ordered"} or defined $keys
l!l&&r!l&&!r
28122
MC/DC100%2/2
exists $self->{"ordered"} or defined $keys
exists $self->{"ordered"} defined $keys
Truth table100%3/3
A or B
A= exists $self->{"ordered"}B= defined $keys
ABresult
1X1
011
000
154 29 $c->{map} = $c->_ordered_map();
155 29 $c->{ordered} = 1;
156 } else {
157 22 $c->{map} = {};
158 }
159 51 ▶ unless (defined $keys) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $keys) 50 1 51
160 50 $keys = [keys %{$self->{map}}]; # default: all keys
161 }
162 51 foreach my $k (@$keys) {
163 87 my $v0 = $self->{map}{$k};
164 87 my $v = $v0;
165  
166 87 ▶ if (defined $v && defined $self->{strmap} && exists $self->{strmap}{$v}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $v and defined $self->{"strmap"} and exists $self->{"strmap"}{$v}) 82 5 87
Condition66%4/6
defined $v and defined $self->{"strmap"} and exists $self->{"strmap"}{$v}
!ll&&!rl&&r
5082
defined $v and defined $self->{"strmap"}
!ll&&!rl&&r
0582
MC/DC33%1/3
defined $v and defined $self->{"strmap"} and exists $self->{"strmap"}{$v}
defined $v defined $self->{"strmap"} exists $self->{"strmap"}{$v}
Truth table50%2/4
A and B and C
A= defined $vB= defined $self->{"strmap"}C= exists $self->{"strmap"}{$v}
ABCresult
0XX0
10X0
1100
1111
167 82 $v = $self->{strmap}{$v};
168 }
169 87 ▶ if (defined $v && (ref($v) eq ref($self))) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $v and ref $v eq ref $self) { } 5 82 87
Condition66%2/3
defined $v and ref $v eq ref $self
!ll&&!rl&&r
0825
MC/DC50%1/2
defined $v and ref $v eq ref $self
defined $v ref $v eq ref $self
Truth table66%2/3
A and B
A= defined $vB= ref $v eq ref $self
ABresult
0X0
100
111
170 5 my $subarray = $v->copy();
171 5 ▶ if (defined $self->{strmap}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{'strmap'}) { } 4 1 5
172 4 $c->{map}{$k} = $subarray->{name};
173 } else {
174 1 $c->{map}{$k} = $subarray;
175 }
176 } else {
177 82 $c->{map}{$k} = $v0;
178 }
179 }
180 51 $c->{idx} = $self->{idx};
181 51 $c->{pos} = $self->{pos};
182 51 ▶ $c->{non_mumeric} = 1 if exists $self->{non_numeric};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"non_numeric"} 9 42 51
183 }
184 63 return $c;
185 }
186  
187 2 ▶sub delete {
Subroutine delete: called CC=3 SCAR=11.6
Pod documented
188 2 my ($self, $k) = @_;
189  
190 2 ▶ if (exists $self->{map}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"map"}) 2 0 2
191 # after deletion key order has to be preserved
192 #
193 2 ▶ unless (exists $self->{ordered}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->{"ordered"}) 2 0 2
194 2 $self->{map} = $self->_ordered_map();
195 2 $self->{ordered} = 1;
196 }
197 2 return delete $self->{map}{$k};
198 }
199 0 return;
200 }
201  
202 1388 ▶sub val {
Subroutine val: called CC=2 SCAR=7.1
Pod documented
203 1388 my ($self, $k) = @_;
204 1388 ▶ exists $self->{map}{$k} || die "assert: bad key $k passed to array->val()";
Branch: 1 branch
BranchTrueFalseTotal
exists $self->{"map"}{$k} or die "assert: bad key $k passed to array->val()" 1388 0 1388
205 1388 return $self->{map}{$k}; # for get_keys lookup
206 }
207  
208 1215 ▶sub get_keys {
Subroutine get_keys: called CC=3 SCAR=11.0
Pod documented
209 1215 my ($self) = @_;
210  
211 1215 ▶ if (exists $self->{map}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"map"}) 1122 93 1215
212 1122 my @keys;
213 1122 ▶ if (exists $self->{ordered}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{'ordered'}) { } 246 876 1122
214 # insertion order is preserved by Tie::IxHash
215 #
216 246 @keys = keys %{$self->{map}};
217 } else {
218 876 @keys = sort { $a <=> $b } keys %{$self->{map}};
219 }
220 1122 return \@keys;
221 }
222 93 return [];
223 }
224  
225 0 ▶sub get_keys_sorted {
Subroutine get_keys_sorted: not called CC=3 SCAR=24.8
Pod undocumented
226 0 my ($self) = @_;
227 0 my @keys;
228  
229 # sort non-hash arrays by index
230 #
231 0 ▶ if (exists $self->{map}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"map"}) 0 0 0
232 0 ▶ if (exists $self->{non_numeric}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{'non_numeric'}) { } 0 0 0
233 0 @keys = sort keys %{$self->{map}};
234 } else {
235 0 @keys = sort { $a <=> $b } keys %{$self->{map}};
236 }
237 0 return \@keys;
238 }
239 0 return [];
240 }
241  
242 11 ▶sub get_pos {
Subroutine get_pos: called CC=1 SCAR=0.0
Pod documented
243 11 my ($self) = @_;
244  
245 11 return $self->{pos};
246 }
247  
248 10 ▶sub set_pos {
Subroutine set_pos: called CC=1 SCAR=0.0
Pod documented
249 10 my ($self, $pos) = @_;
250  
251 10 $self->{pos} = $pos;
252 10 return;
253 }
254  
255 6 ▶sub is_numerical {
Subroutine is_numerical: called CC=2 SCAR=7.6
Pod undocumented
256 6 my ($self) = @_;
257  
258 6 ▶ if (exists $self->{non_numeric}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"non_numeric"}) 0 6 6
259 0 return 0;
260 }
261 6 return 1;
262 }
263  
264 21 ▶sub empty {
Subroutine empty: called CC=1 SCAR=0.0
Pod documented
265 21 my ($self) = @_;
266  
267 21 return (keys %{$self->{map}} == 0);
268 }
269  
270 8 ▶sub to_str {
Subroutine to_str: called CC=9 SCAR=22.9
Pod documented
271 8 my ($self) = @_;
272 8 my $keys = $self->get_keys();
273 8 my $str = '(';
274  
275 8 foreach my $k (@$keys) {
276 16 my $v = $self->{map}{$k};
277  
278 16 ▶ if (defined $v && defined $self->{strmap} && exists $self->{strmap}{$v}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $v and defined $self->{"strmap"} and exists $self->{"strmap"}{$v}) 0 16 16
Condition33%2/6
defined $v and defined $self->{"strmap"} and exists $self->{"strmap"}{$v}
!ll&&!rl&&r
1600
defined $v and defined $self->{"strmap"}
!ll&&!rl&&r
0160
MC/DC0%0/3
defined $v and defined $self->{"strmap"} and exists $self->{"strmap"}{$v}
defined $v defined $self->{"strmap"} exists $self->{"strmap"}{$v}
Truth table25%1/4
A and B and C
A= defined $vB= defined $self->{"strmap"}C= exists $self->{"strmap"}{$v}
ABCresult
0XX0
10X0
1100
1111
279 0 $v = $self->{strmap}{$v};
280 }
281 16 ▶ $str .= ', ' if ($str ne '(');
Branch: 1 branch
BranchTrueFalseTotal
if $str ne "(" 9 7 16
282 16 ▶ if (defined $v && (ref($v) eq ref($self))) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $v and ref $v eq ref $self) { } 2 14 16
Condition66%2/3
defined $v and ref $v eq ref $self
!ll&&!rl&&r
0142
MC/DC50%1/2
defined $v and ref $v eq ref $self
defined $v ref $v eq ref $self
Truth table66%2/3
A and B
A= defined $vB= ref $v eq ref $self
ABresult
0X0
100
111
283 2 $str .= $v->to_str();
284 } else {
285 14 ▶ if (is_int_index($k)) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_int_index($k)) { } 11 3 14
286 11 $str .= "$k => '$v'";
287 } else {
288 3 $str .= "'$k' => '$v'";
289 }
290 }
291 }
292 8 $str .= ')';
293 8 return $str;
294 }
295  
296 7 1;
297  
298 __END__
« lib/PHP/Decode.pm ↑ Summary blib/lib/PHP/Decode/Test.pm »