Smart/Engine.pm

filter: Statementstmt 68.5% 122 / 178 Branchbran 47.9% 69 / 144 Conditioncond 38.6% 22 / 57 MC/DCmcdc 20.5% 7 / 34 Subroutinesub 87.5% 14 / 16 Podpod 0.0% 0 / 1 totaltotal 54.4% 234 / 430 CC 98 SCAR 40.6
CC
98
Cov
56%
_apply_wildcard
272.0
_walk_to_parent
54.1
_remove_value
31.3
CRAP 57.9
↑ Summary Smart.pm »
1 package Data::Hash::Patch::Smart::Engine;
2  
3 8 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 8 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 8 ▶use Scalar::Util qw(refaddr);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 8 ▶use Storable qw(dclone);
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 18 ▶sub patch {
Subroutine patch: called CC=2 SCAR=6.9
Pod undocumented
10 18 my ($data, $changes, %opts) = @_;
11  
12 18 my $copy = dclone($data);
13  
14 18 for my $c (@$changes) {
15 18 _apply_change($copy, $c, \%opts);
16 }
17  
18 14 return $copy;
19 }
20  
21 18 ▶sub _apply_change {
Subroutine _apply_change: called CC=10 SCAR=25.2
22 18 my ($root, $c, $opts) = @_;
23  
24 18 ▶ my $op = $c->{op} or die 'change missing op';
Branch: 1 branch
BranchTrueFalseTotal
my $op = $c->{"op"} or die "change missing op" 18 0 18
25 18 ▶ my $path = $c->{path} or die 'change missing path';
Branch: 1 branch
BranchTrueFalseTotal
my $path = $c->{"path"} or die "change missing path" 18 0 18
26  
27 # Split path into segments like ('items', '0') or ('items', '*')
28 18 my @parts = _split_path($path);
29  
30 # Leaf is the last segment; parent is everything before it
31 18 my $leaf = pop @parts;
32  
33 # Structural wildcard (in parent path)
34 18 ▶ if (grep { $_ eq '*' } @parts) {
Branch: 1 branch
BranchTrueFalseTotal
if (grep {$_ eq "*";} @parts) 12 6 18
35 12 return _apply_structural_wildcard($root, \@parts, $leaf, $c, $opts);
36 }
37  
38 # Walk down to the parent container (hash or array)
39 6 my $parent = _walk_to_parent($root, \@parts, $leaf, $opts);
40  
41 # Unordered array semantics: leaf is '*'
42 6 ▶ if ($leaf eq '*') {
Branch: 1 branch
BranchTrueFalseTotal
if ($leaf eq "*") 2 4 6
43 2 ▶ if ($op eq 'add') {
Branch: 2 branches
BranchTrueFalseTotal
if ($op eq 'add') { } 0 2 2
elsif ($op eq 'remove') { } 2 0 2
44 0 _add_unordered($parent, $c->{value}, $opts);
45 } elsif ($op eq 'remove') {
46 2 _remove_unordered($parent, $c->{from}, $opts);
47 } else {
48 0 die "Unsupported op '$op' for unordered path '$path'";
49 }
50 1 return;
51 }
52  
53 # Normal index/hash semantics
54 4 ▶ if ($op eq 'change') {
Branch: 3 branches
BranchTrueFalseTotal
if ($op eq 'change') { } 3 1 4
elsif ($op eq 'add') { } 1 0 1
elsif ($op eq 'remove') { } 0 0 0
55 3 _set_value($parent, $leaf, $c->{to}, $opts);
56 } elsif ($op eq 'add') {
57 1 _add_value($parent, $leaf, $c->{value}, $opts);
58 } elsif ($op eq 'remove') {
59 0 _remove_value($parent, $leaf, $opts);
60 } else {
61 0 die "Unsupported op: $op";
62 }
63 }
64  
65 18 ▶sub _split_path {
Subroutine _split_path: called CC=3 SCAR=11.8
66 18 my $path = $_[0];
67  
68 18 ▶ return () if !defined $path || $path eq '';
Branch: 1 branch
BranchTrueFalseTotal
if not defined $path or $path eq "" 0 18 18
Condition33%1/3
not defined $path or $path eq ""
l!l&&r!l&&!r
0018
MC/DC0%0/2
not defined $path or $path eq ""
not defined $path $path eq ""
Truth table33%1/3
not(A) or B
A= not defined $pathB= $path eq ""
ABresult
1X1
011
000
69 18 my @parts = grep { length $_ } split m{/}, $path;
70 18 return @parts;
71 }
72  
73 # Walk down the structure following the given path segments,
74 # stopping at the parent of the leaf. In strict mode, we die on
75 # invalid paths. With create_missing => 1, we auto-create
76 # intermediate hashes/arrays as needed.
77 6 ▶sub _walk_to_parent {
Subroutine _walk_to_parent: called CC=20 SCAR=39.9
78 6 my ($cur, $parts, $leaf, $opts) = @_;
79  
80 # Walk all segments that lead to the parent of $leaf
81 6 ▶ for (my $i = 0; $i < @$parts; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @$parts) 9 6 15
82 9 my $p = $parts->[$i];
83 9 my $is_last = ($i == $#$parts);
84  
85 # For container creation, "next" is either the next part,
86 # or, if we're at the last part, the leaf segment.
87 9 ▶ my $next = $is_last ? $leaf : $parts->[$i + 1];
Branch: 1 branch
BranchTrueFalseTotal
$is_last ? : 6 3 9
88  
89 # -----------------------------
90 # HASH navigation
91 # -----------------------------
92 9 ▶ if (ref($cur) eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $cur eq "HASH") 8 1 9
93  
94 # Missing key
95 8 ▶ if (!exists $cur->{$p}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $cur->{$p}) 5 3 8
96 5 ▶ if ($opts->{create_missing}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($opts->{'create_missing'}) { } 5 0 5
elsif ($opts->{'strict'}) { } 0 0 0
97 # Decide container type based on what comes after
98 5 ▶ if (defined $next && $next =~ /^\d+$/) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $next and $next =~ /^\d+$/) { } 3 2 5
Condition66%2/3
defined $next and $next =~ /^\d+$/
!ll&&!rl&&r
023
MC/DC50%1/2
defined $next and $next =~ /^\d+$/
defined $next $next =~ /^\d+$/
Truth table66%2/3
A and B
A= defined $nextB= $next =~ /^\d+$/
ABresult
0X0
100
111
99 3 $cur->{$p} = [];
100 } else {
101 2 $cur->{$p} = {};
102 }
103 }
104 elsif ($opts->{strict}) {
105 0 die "Invalid path: missing hash key '$p'";
106 }
107 else {
108 0 return undef;
109 }
110 }
111  
112 8 $cur = $cur->{$p};
113 8 next;
114 }
115  
116 # -----------------------------
117 # ARRAY navigation
118 # -----------------------------
119 1 ▶ if (ref($cur) eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $cur eq "ARRAY") 1 0 1
120  
121 # Index must be numeric
122 1 ▶ if ($p !~ /^\d+$/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($p =~ /^\d+$/) 0 1 1
123 die "Invalid path: non-numeric array index '$p'"
124 0 ▶ if $opts->{strict};
Branch: 1 branch
BranchTrueFalseTotal
if $opts->{"strict"} 0 0 0
125 0 return undef;
126 }
127  
128 # Out of bounds
129 1 ▶ if ($p > $#$cur) {
Branch: 1 branch
BranchTrueFalseTotal
if ($p > $cur->$#*) 1 0 1
130 1 ▶ if ($opts->{create_missing}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($opts->{'create_missing'}) { } 1 0 1
elsif ($opts->{'strict'}) { } 0 0 0
131 # Extend array
132 1 $#$cur = $p;
133  
134 # Decide container type for this new slot
135 1 ▶ if (defined $next && $next =~ /^\d+$/) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $next and $next =~ /^\d+$/) { } 0 1 1
Condition33%1/3
defined $next and $next =~ /^\d+$/
!ll&&!rl&&r
010
MC/DC0%0/2
defined $next and $next =~ /^\d+$/
defined $next $next =~ /^\d+$/
Truth table33%1/3
A and B
A= defined $nextB= $next =~ /^\d+$/
ABresult
0X0
100
111
136 0 $cur->[$p] = [];
137 } else {
138 1 $cur->[$p] = {};
139 }
140 }
141 elsif ($opts->{strict}) {
142 0 die "Invalid path: array index '$p' out of bounds";
143 }
144 else {
145 0 return undef;
146 }
147 }
148  
149 1 $cur = $cur->[$p];
150 1 next;
151 }
152  
153 # -----------------------------
154 # Undef or non-container
155 # -----------------------------
156 0 ▶ if (!defined $cur) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $cur) 0 0 0
157 die "Invalid path: encountered undef while walking"
158 0 ▶ if $opts->{strict};
Branch: 1 branch
BranchTrueFalseTotal
if $opts->{"strict"} 0 0 0
159 0 return undef;
160 }
161  
162 die "Invalid path: cannot descend into non-container"
163 0 ▶ if $opts->{strict};
Branch: 1 branch
BranchTrueFalseTotal
if $opts->{"strict"} 0 0 0
164  
165 0 return undef;
166 }
167  
168 6 return $cur;
169 }
170  
171  
172 16 ▶sub _set_value {
Subroutine _set_value: called CC=9 SCAR=23.5
173 16 my ($parent, $leaf, $value, $opts) = @_;
174  
175 16 ▶ if (ref($parent) eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $parent eq "HASH") 15 1 16
176 15 ▶ if (!exists $parent->{$leaf} && $opts->{strict}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not exists $parent->{$leaf} and $opts->{"strict"}) 1 14 15
Condition100%3/3
not exists $parent->{$leaf} and $opts->{"strict"}
!ll&&!rl&&r
1311
MC/DC100%2/2
not exists $parent->{$leaf} and $opts->{"strict"}
not exists $parent->{$leaf} $opts->{"strict"}
Truth table100%3/3
not(A) and B
A= not exists $parent->{$leaf}B= $opts->{"strict"}
ABresult
0X0
100
111
177 1 die "Strict mode: cannot change missing hash key '$leaf'";
178 }
179 14 $parent->{$leaf} = $value;
180 14 return;
181 }
182  
183 1 ▶ if (ref($parent) eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $parent eq "ARRAY") 1 0 1
184 1 ▶ if ($leaf !~ /^\d+$/ || $leaf > $#$parent) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $leaf =~ /^\d+$/ or $leaf > $parent->$#*) 1 0 1
Condition33%1/3
not $leaf =~ /^\d+$/ or $leaf > $parent->$#*
l!l&&r!l&&!r
010
MC/DC0%0/2
not $leaf =~ /^\d+$/ or $leaf > $parent->$#*
not $leaf =~ /^\d+$/ $leaf > $parent->$#*
Truth table33%1/3
not(A) or B
A= not $leaf =~ /^\d+$/B= $leaf > $parent->$#*
ABresult
1X1
011
000
185 die "Strict mode: array index '$leaf' out of bounds"
186 1 ▶ if $opts->{strict};
Branch: 1 branch
BranchTrueFalseTotal
if $opts->{"strict"} 0 1 1
187 }
188 1 $parent->[$leaf] = $value;
189 1 return;
190 }
191  
192 0 ▶ die 'Strict mode: cannot set value on non-container' if $opts->{strict};
Branch: 1 branch
BranchTrueFalseTotal
if $opts->{"strict"} 0 0 0
193 }
194  
195 3 ▶sub _add_value {
Subroutine _add_value: called CC=9 SCAR=27.6
196 3 my ($parent, $leaf, $value, $opts) = @_;
197  
198 3 ▶ if (ref($parent) eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $parent eq "HASH") 2 1 3
199 2 ▶ if (exists $parent->{$leaf} && $opts->{strict}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $parent->{$leaf} and $opts->{"strict"}) 0 2 2
Condition33%1/3
exists $parent->{$leaf} and $opts->{"strict"}
!ll&&!rl&&r
200
MC/DC0%0/2
exists $parent->{$leaf} and $opts->{"strict"}
exists $parent->{$leaf} $opts->{"strict"}
Truth table33%1/3
A and B
A= exists $parent->{$leaf}B= $opts->{"strict"}
ABresult
0X0
100
111
200 0 die "Strict mode: cannot add existing hash key '$leaf'";
201 }
202 2 $parent->{$leaf} = $value;
203 2 return;
204 }
205  
206 1 ▶ if (ref($parent) eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $parent eq "ARRAY") 1 0 1
207 # Leaf must be numeric
208 1 ▶ if ($leaf !~ /^\d+$/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($leaf =~ /^\d+$/) 0 1 1
209 die "Strict mode: invalid array index '$leaf'"
210 0 ▶ if $opts->{strict};
Branch: 1 branch
BranchTrueFalseTotal
if $opts->{"strict"} 0 0 0
211 0 return;
212 }
213  
214 # Extend array if needed
215 1 ▶ if ($leaf > $#$parent) {
Branch: 1 branch
BranchTrueFalseTotal
if ($leaf > $parent->$#*) 1 0 1
216 1 $#$parent = $leaf;
217 }
218  
219 # Insert value at exact index
220 1 $parent->[$leaf] = $value;
221 1 return;
222 }
223  
224 0 ▶ die 'Strict mode: cannot add value to non-container' if $opts->{strict};
Branch: 1 branch
BranchTrueFalseTotal
if $opts->{"strict"} 0 0 0
225 }
226  
227 2 ▶sub _remove_value {
Subroutine _remove_value: called CC=8 SCAR=34.4
228 2 my ($parent, $leaf, $opts) = @_;
229  
230 2 ▶ if (ref($parent) eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $parent eq "HASH") 2 0 2
231 2 ▶ if (!exists $parent->{$leaf} && $opts->{strict}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not exists $parent->{$leaf} and $opts->{"strict"}) 0 2 2
Condition33%1/3
not exists $parent->{$leaf} and $opts->{"strict"}
!ll&&!rl&&r
200
MC/DC0%0/2
not exists $parent->{$leaf} and $opts->{"strict"}
not exists $parent->{$leaf} $opts->{"strict"}
Truth table33%1/3
not(A) and B
A= not exists $parent->{$leaf}B= $opts->{"strict"}
ABresult
0X0
100
111
232 0 die "Strict mode: cannot remove missing hash key '$leaf'";
233 }
234 2 delete $parent->{$leaf};
235 2 return;
236 }
237  
238 0 ▶ if (ref($parent) eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $parent eq "ARRAY") 0 0 0
239 0 ▶ if ($leaf !~ /^\d+$/ || $leaf > $#$parent) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $leaf =~ /^\d+$/ or $leaf > $parent->$#*) 0 0 0
Condition0%0/3
not $leaf =~ /^\d+$/ or $leaf > $parent->$#*
l!l&&r!l&&!r
000
MC/DC0%0/2
not $leaf =~ /^\d+$/ or $leaf > $parent->$#*
not $leaf =~ /^\d+$/ $leaf > $parent->$#*
Truth table0%0/3
not(A) or B
A= not $leaf =~ /^\d+$/B= $leaf > $parent->$#*
ABresult
1X1
011
000
240 0 die "Strict mode: array index '$leaf' out of bounds";
241 }
242 0 splice @$parent, $leaf, 1;
243 0 return;
244 }
245  
246 0 ▶ die 'Strict mode: cannot remove value from non-container' if $opts->{strict};
Branch: 1 branch
BranchTrueFalseTotal
if $opts->{"strict"} 0 0 0
247 }
248  
249 # Add a value to an unordered array.
250 # We treat the parent as an arrayref and simply push the new value.
251 0 ▶sub _add_unordered {
Subroutine _add_unordered: not called CC=2 SCAR=17.9
252 0 my ($parent, $value) = @_;
253  
254 0 ▶ die 'Unordered add requires an array parent' unless ref($parent) eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $parent eq "ARRAY" 0 0 0
255  
256 0 push @$parent, $value;
257 }
258  
259 # Remove a single matching value from an unordered array.
260 # We scan linearly and delete the first element that compares equal.
261 2 ▶sub _remove_unordered {
Subroutine _remove_unordered: called CC=9 SCAR=25.1
262 2 my ($parent, $value, $opts) = @_;
263  
264 2 ▶ die "Unordered remove requires an array parent"
Branch: 1 branch
BranchTrueFalseTotal
unless ref $parent eq "ARRAY" 0 2 2
265 unless ref($parent) eq 'ARRAY';
266  
267 2 ▶ for (my $i = 0; $i < @$parent; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @$parent) 5 1 6
268 5 ▶ if (!defined $parent->[$i] && !defined $value) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $parent->[$i] or defined $value) 0 5 5
Condition33%1/3
defined $parent->[$i] or defined $value
l!l&&r!l&&!r
500
MC/DC0%0/2
defined $parent->[$i] or defined $value
defined $parent->[$i] defined $value
Truth table33%1/3
A or B
A= defined $parent->[$i]B= defined $value
ABresult
1X1
011
000
269 0 splice @$parent, $i, 1;
270 0 return;
271 }
272 5 ▶ if (defined $parent->[$i] && defined $value && $parent->[$i] eq $value) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $parent->[$i] and defined $value and $parent->[$i] eq $value) 1 4 5
Condition50%3/6
defined $parent->[$i] and defined $value and $parent->[$i] eq $value
!ll&&!rl&&r
041
defined $parent->[$i] and defined $value
!ll&&!rl&&r
005
MC/DC33%1/3
defined $parent->[$i] and defined $value and $parent->[$i] eq $value
defined $parent->[$i] defined $value $parent->[$i] eq $value
Truth table50%2/4
A and B and C
A= defined $parent->[$i]B= defined $valueC= $parent->[$i] eq $value
ABCresult
0XX0
10X0
1100
1111
273 1 splice @$parent, $i, 1;
274 1 return;
275 }
276 }
277  
278 1 ▶ die "Unordered remove: value '$value' not found" if $opts->{strict};
Branch: 1 branch
BranchTrueFalseTotal
if $opts->{"strict"} 1 0 1
279  
280 # Non-strict: silently ignore
281 }
282  
283 # Apply a change to all paths matching a wildcard pattern.
284 # Example pattern: ['users', '*', 'password']
285 #
286 # We recursively walk the data structure, matching literal segments
287 # and branching on '*' segments.
288 0 ▶sub _apply_wildcard {
Subroutine _apply_wildcard: not called CC=16 SCAR=56.1
289 0 my ($cur, $parts, $change, $opts, $depth) = @_;
290  
291 0 ▶ $depth //= 0;
Condition0%0/2
$depth //= 0
l!l
00
MC/DC0%0/1
$depth //= 0
$depth
Truth table0%0/2
A
A= $depth
Aresult
11
00
292  
293 # If we've consumed all parts, we are at the leaf.
294 0 ▶ if ($depth == @$parts) {
Branch: 1 branch
BranchTrueFalseTotal
if ($depth == @$parts) 0 0 0
295 # Apply the operation to this exact location.
296 # We treat this as a non-wildcard leaf.
297 0 my $op = $change->{op};
298  
299 0 ▶ if ($op eq 'change') {
Branch: 3 branches
BranchTrueFalseTotal
if ($op eq 'change') { } 0 0 0
elsif ($op eq 'add') { } 0 0 0
elsif ($op eq 'remove') { } 0 0 0
300 # Replace the entire subtree
301 0 return $change->{to};
302 } elsif ($op eq 'add') {
303 # For wildcard add, we push into arrays or set hash keys
304 # but since wildcard leafs are ambiguous, we do nothing here.
305 # Wildcard adds are only meaningful when the leaf is '*'
306 0 return $cur;
307 } elsif ($op eq 'remove') {
308 # Remove the entire subtree
309 0 return undef;
310 } else {
311 0 die "Unsupported wildcard op: $op";
312 }
313 }
314  
315 0 my $seg = $parts->[$depth];
316  
317 # Literal segment: descend into matching child
318 0 ▶ if ($seg ne '*') {
Branch: 1 branch
BranchTrueFalseTotal
if ($seg ne "*") 0 0 0
319 0 ▶ if (ref($cur) eq 'HASH' && exists $cur->{$seg}) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $cur eq 'HASH' and exists $cur->{$seg}) { } 0 0 0
elsif (ref $cur eq 'ARRAY' and $seg =~ /^\d+$/ and $seg <= $cur->$#*) { } 0 0 0
Condition0%0/3
ref $cur eq "HASH" and exists $cur->{$seg}
!ll&&!rl&&r
000
MC/DC0%0/2
ref $cur eq "HASH" and exists $cur->{$seg}
ref $cur eq "HASH" exists $cur->{$seg}
Truth table0%0/3
A and B
A= ref $cur eq "HASH"B= exists $cur->{$seg}
ABresult
0X0
100
111
320 0 $cur->{$seg} = _apply_wildcard($cur->{$seg}, $parts, $change, $opts, $depth+1);
321 ▶ } elsif (ref($cur) eq 'ARRAY' && $seg =~ /^\d+$/ && $seg <= $#$cur) {
Condition0%0/6
ref $cur eq "ARRAY" and $seg =~ /^\d+$/ and $seg <= $cur->$#*
!ll&&!rl&&r
000
ref $cur eq "ARRAY" and $seg =~ /^\d+$/
!ll&&!rl&&r
000
MC/DC0%0/3
ref $cur eq "ARRAY" and $seg =~ /^\d+$/ and $seg <= $cur->$#*
ref $cur eq "ARRAY" $seg =~ /^\d+$/ $seg <= $cur->$#*
Truth table0%0/4
A and B and C
A= ref $cur eq "ARRAY"B= $seg =~ /^\d+$/C= $seg <= $cur->$#*
ABCresult
0XX0
10X0
1100
1111
322 0 $cur->[$seg] = _apply_wildcard($cur->[$seg], $parts, $change, $opts, $depth+1);
323 }
324 0 return;
325 }
326  
327 # Wildcard segment: match all children at this level
328 0 ▶ if (ref($cur) eq 'HASH') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $cur eq 'HASH') { } 0 0 0
elsif (ref $cur eq 'ARRAY') { } 0 0 0
329 0 for my $k (sort keys %$cur) {
330 0 $cur->{$k} = _apply_wildcard($cur->{$k}, $parts, $change, $opts, $depth+1);
331 }
332 }
333 elsif (ref($cur) eq 'ARRAY') {
334 0 for my $i (0 .. $#$cur) {
335 0 $cur->[$i] = _apply_wildcard($cur->[$i], $parts, $change, $opts, $depth+1);
336 }
337 }
338 }
339  
340 41 ▶sub _apply_structural_wildcard {
Subroutine _apply_structural_wildcard: called CC=17 SCAR=29.8
341 41 my ($cur, $parts, $leaf, $change, $opts, $depth, $seen) = @_;
342  
343 41 ▶ $depth //= 0;
Condition100%2/2
$depth //= 0
l!l
2912
MC/DC100%1/1
$depth //= 0
$depth
Truth table100%2/2
A
A= $depth
Aresult
11
00
344 41 ▶ $seen ||= {};
Condition100%2/2
$seen ||= {}
l!l
2912
MC/DC100%1/1
$seen ||= {}
$seen
Truth table100%2/2
A
A= $seen
Aresult
11
00
345  
346 # Detect cycles
347 41 ▶ if (ref($cur)) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $cur) 41 0 41
348 41 my $addr = refaddr($cur);
349 41 ▶ if ($seen->{$addr}++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($seen->{$addr}++) 2 39 41
350 die "Cycle detected during wildcard patch"
351 2 ▶ if $opts->{strict};
Branch: 1 branch
BranchTrueFalseTotal
if $opts->{"strict"} 2 0 2
352 0 return;
353 }
354 }
355  
356 # If we've matched all wildcard segments, apply leaf op
357 39 ▶ if ($depth == @$parts) {
Branch: 1 branch
BranchTrueFalseTotal
if ($depth == @$parts) 17 22 39
358 17 return _apply_leaf_op($cur, $leaf, $change, $opts);
359 }
360  
361 22 my $seg = $parts->[$depth];
362  
363 # Literal segment
364 22 ▶ if ($seg ne '*') {
Branch: 1 branch
BranchTrueFalseTotal
if ($seg ne "*") 13 9 22
365 13 ▶ if (ref($cur) eq 'HASH' && exists $cur->{$seg}) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $cur eq 'HASH' and exists $cur->{$seg}) { } 12 1 13
elsif (ref $cur eq 'ARRAY' and $seg =~ /^\d+$/ and $seg <= $cur->$#*) { } 0 2 2
Condition66%2/3
ref $cur eq "HASH" and exists $cur->{$seg}
!ll&&!rl&&r
0112
MC/DC50%1/2
ref $cur eq "HASH" and exists $cur->{$seg}
ref $cur eq "HASH" exists $cur->{$seg}
Truth table66%2/3
A and B
A= ref $cur eq "HASH"B= exists $cur->{$seg}
ABresult
0X0
100
111
366 12 _apply_structural_wildcard($cur->{$seg}, $parts, $leaf, $change, $opts, $depth+1, $seen);
367 ▶ } elsif (ref($cur) eq 'ARRAY' && $seg =~ /^\d+$/ && $seg <= $#$cur) {
Condition33%2/6
ref $cur eq "ARRAY" and $seg =~ /^\d+$/ and $seg <= $cur->$#*
!ll&&!rl&&r
100
ref $cur eq "ARRAY" and $seg =~ /^\d+$/
!ll&&!rl&&r
100
MC/DC0%0/3
ref $cur eq "ARRAY" and $seg =~ /^\d+$/ and $seg <= $cur->$#*
ref $cur eq "ARRAY" $seg =~ /^\d+$/ $seg <= $cur->$#*
Truth table25%1/4
A and B and C
A= ref $cur eq "ARRAY"B= $seg =~ /^\d+$/C= $seg <= $cur->$#*
ABCresult
0XX0
10X0
1100
1111
368 0 _apply_structural_wildcard($cur->[$seg], $parts, $leaf, $change, $opts, $depth+1, $seen);
369 }
370 11 return;
371 }
372  
373 # Wildcard segment
374 9 ▶ if (ref($cur) eq 'HASH') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $cur eq 'HASH') { } 8 1 9
elsif (ref $cur eq 'ARRAY') { } 1 0 1
375 8 for my $k (keys %$cur) {
376 14 _apply_structural_wildcard($cur->{$k}, $parts, $leaf, $change, $opts, $depth+1, $seen);
377 }
378 } elsif (ref($cur) eq 'ARRAY') {
379 1 for my $i (0 .. $#$cur) {
380 3 _apply_structural_wildcard($cur->[$i], $parts, $leaf, $change, $opts, $depth+1, $seen);
381 }
382 }
383 }
384  
385  
386 17 ▶sub _apply_leaf_op {
Subroutine _apply_leaf_op: called CC=4 SCAR=14.0
387 17 my ($parent, $leaf, $change, $opts) = @_;
388  
389 17 my $op = $change->{op};
390  
391 17 ▶ if ($op eq 'change') {
Branch: 3 branches
BranchTrueFalseTotal
if ($op eq 'change') { } 13 4 17
elsif ($op eq 'add') { } 2 2 4
elsif ($op eq 'remove') { } 2 0 2
392 13 return _set_value($parent, $leaf, $change->{to}, $opts);
393 } elsif ($op eq 'add') {
394 2 return _add_value($parent, $leaf, $change->{value}, $opts);
395 } elsif ($op eq 'remove') {
396 2 return _remove_value($parent, $leaf, $opts);
397 }
398  
399 0 die "Unsupported op '$op' in wildcard patch";
400 }
401  
402 8 1;
↑ Summary Smart.pm »