Path/Role/Path.pm

filter: Statementstmt 75.4% 212 / 281 Branchbran 59.8% 79 / 132 Conditioncond 32.0% 17 / 53 MC/DCmcdc 12.9% 4 / 31 Subroutinesub 70.9% 39 / 55 Podpod 0.0% 0 / 20 totaltotal 61.3% 351 / 572 CC 100 SCAR 30.2
CC
100
Cov
66%
_coerce_step
114.0
splice
42.0
cleanup
17.0
CRAP 20.4
↑ Summary Path.pm »
1 package Parse::Path::Role::Path;
2  
3 11 our $VERSION = '0.92'; # VERSION
4 # ABSTRACT: Role for paths
5  
6 #############################################################################
7 # Modules
8  
9 11 ▶use Moo::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 11 ▶use Types::Standard qw(Dict Bool Str Int Enum ArrayRef HashRef RegexpRef CodeRef Tuple Maybe Optional);
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 11 ▶use sanity;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 11 ▶use Scalar::Util qw( blessed );
Subroutine BEGIN: called CC=1 SCAR=0.0
15 11 ▶use Storable qw( dclone );
Subroutine BEGIN: called CC=1 SCAR=0.0
16 11 ▶use List::AllUtils qw( first all any );
Subroutine BEGIN: called CC=1 SCAR=0.0
17 11 ▶use Sub::Name;
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 11 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 11 ▶no warnings 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 #############################################################################
23 # Overloading
24  
25 use overload
26 # with_assign (XXX: No idea why it can't use '0+')
27 11 ▶ '+' => subname(_overload_plus => sub {
Subroutine BEGIN: called CC=1 SCAR=0.3
Subroutine _overload_plus: not called CC=1 SCAR=1.2
Subroutine __ANON__: called CC=1 SCAR=1.2
28 0 my ($self, $thing, $swap) = @_;
29 0 $self->depth + $thing;
30 }),
31 2 ▶ '-' => subname(_overload_minus => sub {
Subroutine _overload_minus: called CC=2 SCAR=7.2
32 2 my ($self, $thing, $swap) = @_;
33 2 ▶ $swap ?
Branch: 1 branch
BranchTrueFalseTotal
$swap ? : 2 0 2
34 $thing - $self->depth :
35 $self->depth - $thing
36 ;
37 }),
38  
39 # assign
40 2 ▶ '.=' => subname(_overload_concat => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
Subroutine _overload_concat: not called CC=1 SCAR=0.0
41 2 my ($self, $thing) = @_;
42 2 $self->push($thing);
43 2 $self;
44 }),
45  
46 # 3way_comparison
47 14 ▶ '<=>' => subname(_overload_cmp_num => sub {
Subroutine __ANON__: called CC=2 SCAR=7.6
48 14 my ($self, $thing, $swap) = @_;
49 14 ▶ $swap ?
Branch: 2 branches
BranchTrueFalseTotal
$swap ? : 7 7 14
$swap ? : 0 0 0
50 $thing <=> $self->depth :
51 $self->depth <=> $thing
52 ;
53 }),
54 14 ▶ 'cmp' => subname(_overload_cmp => sub {
Subroutine __ANON__: called CC=10 SCAR=25.5
55 14 my ($self, $thing, $swap) = @_;
56  
57 # If both of these are Parse::Path objects, run through the key comparisons
58 14 ▶ if (blessed $thing and $thing->does('Parse::Path::Role::Path')) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($thing) and $thing->does("Parse::Path::Role::Path")) 14 0 14
Condition33%1/3
builtin::blessed($thing) and $thing->does("Parse::Path::Role::Path")
!ll&&!rl&&r
0014
MC/DC0%0/2
builtin::blessed($thing) and $thing->does("Parse::Path::Role::Path")
builtin::blessed($thing) $thing->does("Parse::Path::Role::Path")
Truth table33%1/3
A and B
A= builtin::blessed($thing)B= $thing->does("Parse::Path::Role::Path")
ABresult
0X0
100
111
59 14 ▶ ($self, $thing) = ($thing, $self) if $swap;
Branch: 1 branch
BranchTrueFalseTotal
if $swap 0 14 14
60  
61 14 my ($cmp, $i) = (0, 0);
62 14 ▶ for (; $i <= $#{$self->_path} and $i <= $#{$thing->_path}; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $#{$self->_path;} and $i <= $#{$thing->_path;}) 45 8 53
Condition100%3/3
$i <= $#{$self->_path;} and $i <= $#{$thing->_path;}
!ll&&!rl&&r
4445
MC/DC100%2/2
$i <= $#{$self->_path;} and $i <= $#{$thing->_path;}
$i <= $#{$self->_path;} $i <= $#{$thing->_path;}
Truth table100%3/3
A and B
A= $i <= $#{$self->_path;}B= $i <= $#{$thing->_path;}
ABresult
0X0
100
111
63 45 my ($stepA, $stepB) = ($self->_path->[$i], $thing->_path->[$i]);
64 my $cmp = $stepA->{type} eq 'ARRAY' && $stepB->{type} eq 'ARRAY' ?
65 $stepA->{key} <=> $stepB->{key} :
66 $stepA->{key} cmp $stepB->{key}
67 45 ▶ ;
Branch: 1 branch
BranchTrueFalseTotal
$$stepA{'type'} eq 'ARRAY' && $$stepB{'type'} eq 'ARRAY' ? : 0 45 45
Condition33%1/3
$$stepA{"type"} eq "ARRAY" and $$stepB{"type"} eq "ARRAY"
!ll&&!rl&&r
4500
MC/DC0%0/2
$$stepA{"type"} eq "ARRAY" and $$stepB{"type"} eq "ARRAY"
$$stepA{"type"} eq "ARRAY" $$stepB{"type"} eq "ARRAY"
Truth table33%1/3
A and B
A= $$stepA{"type"} eq "ARRAY"B= $$stepB{"type"} eq "ARRAY"
ABresult
0X0
100
111
68  
69 45 ▶ return $cmp if $cmp;
Branch: 1 branch
BranchTrueFalseTotal
if $cmp 6 39 45
70 }
71  
72 # Now it's down to step counts
73 8 return $self->step_count <=> $thing->step_count;
74 }
75  
76 # Fallback to string comparison
77 0 ▶ return $swap ?
Branch: 1 branch
BranchTrueFalseTotal
$swap ? : 0 0 0
78 $thing cmp $self->as_string :
79 $self->as_string cmp $thing
80 ;
81 }),
82  
83 # conversion
84 0 ▶ 'bool' => subname(_overload_bool => sub { !!shift->step_count }),
Subroutine __ANON__: not called CC=1 SCAR=6.9
85 1 ▶ '""' => subname(_overload_string => sub { shift->as_string }),
Subroutine __ANON__: called CC=1 SCAR=0.0
86 0 ▶ '0+' => subname(_overload_numify => sub { shift->depth }),
Subroutine _overload_numify: not called CC=1 SCAR=6.9
87  
88 # dereferencing
89 0 ▶ '${}' => subname(_overload_scalar => sub { \(shift->as_string) }),
Subroutine __ANON__: not called CC=1 SCAR=6.9
90 1 ▶ '@{}' => subname(_overload_array => sub { shift->as_array }),
Subroutine __ANON__: called CC=1 SCAR=0.0
91  
92 # special
93 0 ▶ '=' => subname(_overload_clone => sub { shift->clone })
Subroutine __ANON__: not called CC=1 SCAR=6.9
94 11 ;
95  
96 #############################################################################
97 # Requirements
98  
99 11 requires '_build_blueprint';
100  
101 # One-time validation for speed
102 11 my $BLUEPRINT_VALIDATED = 0;
103 11 my $_blueprint_type = Dict[
104 hash_step_regexp => RegexpRef,
105 array_step_regexp => RegexpRef,
106 delimiter_regexp => RegexpRef,
107  
108 unescape_translation => ArrayRef[Tuple[RegexpRef, CodeRef]],
109 pos_translation => ArrayRef[Tuple[RegexpRef, Str]],
110  
111 delimiter_placement => HashRef[Str],
112  
113 array_key_sprintf => Str,
114  
115 hash_key_stringification => ArrayRef[Tuple[RegexpRef, Str, Optional[CodeRef]]]
116 ];
117  
118 has _blueprint => (
119 is => 'ro',
120 builder => '_build_blueprint',
121 lazy => 1,
122 init_arg => undef,
123 80 ▶ isa => sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
124 80 ▶ return 1 if $BLUEPRINT_VALIDATED;
Branch: 1 branch
BranchTrueFalseTotal
if $BLUEPRINT_VALIDATED 74 6 80
125 6 $_blueprint_type->assert_valid($_[0]);
126 6 $BLUEPRINT_VALIDATED = 1;
127 },
128 11 );
129  
130 #############################################################################
131 # Attributes
132  
133 # NOTE: hot attr; bypass isa
134 11 has _path => (
135 is => 'rw',
136 #isa => ArrayRef[Dict[
137 # type => Enum[qw( ARRAY HASH )],
138 # key => Str,
139 # step => Str,
140 # pos => Int,
141 #]],
142 predicate => 1,
143 );
144  
145 11 has _tmp_path_thing => (
146 is => 'ro',
147 init_arg => 'path',
148 required => 1,
149 clearer => 1,
150 );
151  
152 has auto_normalize => (
153 is => 'rw',
154 isa => Bool,
155 39 ▶ default => sub { 0 },
Subroutine __ANON__: called CC=1 SCAR=0.0
156 11 );
157  
158 has auto_cleanup => (
159 is => 'rw',
160 isa => Bool,
161 46 ▶ default => sub { 0 },
Subroutine __ANON__: called CC=1 SCAR=0.0
162 11 );
163  
164 #############################################################################
165 # Pre/post-BUILD
166  
167 81 ▶sub BUILD {
Subroutine BUILD: called CC=4 SCAR=13.9
Pod undocumented
168 81 my $self = $_[0];
169  
170 # Post-build coercion of path
171 81 ▶ unless ($self->_has_path) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->_has_path) 80 1 81
172 80 my $path_array = $self->_coerce_step( $self->_tmp_path_thing );
173  
174 54 $self->_path( $path_array );
175 54 ▶ $self->cleanup if ($self->auto_cleanup and @$path_array);
Branch: 1 branch
BranchTrueFalseTotal
if $self->auto_cleanup and @$path_array 24 30 54
Condition100%3/3
$self->auto_cleanup and @$path_array
!ll&&!rl&&r
28224
MC/DC100%2/2
$self->auto_cleanup and @$path_array
$self->auto_cleanup @$path_array
Truth table100%3/3
A and B
A= $self->auto_cleanupB= @$path_array
ABresult
0X0
100
111
176 }
177 53 $self->_clear_tmp_path_thing; # ...and may it never return...
178  
179 53 return $self;
180 }
181  
182 #############################################################################
183 # Methods
184  
185 # XXX: The array-based methods makes internal CORE calls ambiguous
186 11 ▶no warnings 'ambiguous';
Subroutine BEGIN: called CC=1 SCAR=0.0
187  
188 276 ▶sub step_count { scalar @{shift->_path}; }
Subroutine step_count: called CC=1 SCAR=0.0
Pod undocumented
189  
190 17 ▶sub depth {
Subroutine depth: called CC=4 SCAR=14.8
Pod undocumented
191 17 my $self = shift;
192  
193 17 my $depth;
194 17 foreach my $step_hash (@{$self->_path}) {
195 84 my $pos = $step_hash->{pos};
196  
197 # Process depth
198 0 ▶ if ($pos =~ /^(\d+)$/) { $depth = $1; } # absolute
Branch: 2 branches
BranchTrueFalseTotal
if ($pos =~ /^(\d+)$/) { } 0 84 84
elsif ($pos =~ /^X([+\-]\d+)$/) { } 84 0 84
199 84 elsif ($pos =~ /^X([+\-]\d+)$/) { $depth += $1; } # relative
200 else { # WTF is this?
201 0 die sprintf("Found unparsable pos: %s (step: %s)", $pos, $step_hash->{step});
202 }
203 }
204  
205 17 return $depth;
206 }
207  
208 0 ▶sub is_absolute {
Subroutine is_absolute: not called CC=2 SCAR=17.9
Pod undocumented
209 0 my $self = shift;
210 0 ▶ $self->step_count ? $self->_path->[0]{pos} !~ /^X/ : undef;
Branch: 1 branch
BranchTrueFalseTotal
$self->step_count ? : 0 0 0
211 }
212  
213 2 ▶sub as_array { dclone(shift->_path) }
Subroutine as_array: called CC=1 SCAR=0.0
Pod undocumented
214 0 ▶sub blueprint { dclone(shift->_blueprint) }
Subroutine blueprint: not called CC=1 SCAR=6.9
Pod undocumented
215  
216 0 ▶sub shift { {%{ shift @{shift->_path} }} }
Subroutine shift: not called CC=1 SCAR=6.9
Pod undocumented
217 0 ▶sub pop { {%{ pop @{shift->_path} }} }
Subroutine pop: not called CC=1 SCAR=6.9
Pod undocumented
218 0 ▶sub unshift {
Subroutine unshift: not called CC=3 SCAR=24.8
Pod undocumented
219 0 my $self = shift;
220 0 my $step_hashs = $self->_coerce_step([@_]);
221  
222 0 my $return = unshift @{$self->_path}, @$step_hashs;
223 0 ▶ $self->cleanup if ($self->auto_cleanup and @$step_hashs);
Branch: 1 branch
BranchTrueFalseTotal
if $self->auto_cleanup and @$step_hashs 0 0 0
Condition0%0/3
$self->auto_cleanup and @$step_hashs
!ll&&!rl&&r
000
MC/DC0%0/2
$self->auto_cleanup and @$step_hashs
$self->auto_cleanup @$step_hashs
Truth table0%0/3
A and B
A= $self->auto_cleanupB= @$step_hashs
ABresult
0X0
100
111
224 0 return $return;
225 }
226 2 ▶sub push {
Subroutine push: called CC=3 SCAR=11.6
Pod undocumented
227 2 my $self = shift;
228 2 my $step_hashs = $self->_coerce_step([@_]);
229  
230 2 my $return = push @{$self->_path}, @$step_hashs;
231 2 ▶ $self->cleanup if ($self->auto_cleanup and @$step_hashs);
Branch: 1 branch
BranchTrueFalseTotal
if $self->auto_cleanup and @$step_hashs 0 2 2
Condition33%1/3
$self->auto_cleanup and @$step_hashs
!ll&&!rl&&r
200
MC/DC0%0/2
$self->auto_cleanup and @$step_hashs
$self->auto_cleanup @$step_hashs
Truth table33%1/3
A and B
A= $self->auto_cleanupB= @$step_hashs
ABresult
0X0
100
111
232 2 return $return;
233 }
234 0 ▶sub splice {
Subroutine splice: not called CC=6 SCAR=37.4
Pod undocumented
235 0 my ($self, $offset, $length) = (shift, shift, shift);
236 0 my $step_hashs = $self->_coerce_step([@_]);
237  
238 # Perl syntax getting retardo here...
239 0 ▶ my @params = ( $offset, defined $length ? ($length, @$step_hashs) : () );
Branch: 1 branch
BranchTrueFalseTotal
defined $length ? : 0 0 0
240 0 my @return = splice( @{$self->_path}, @params );
241 #my $return = splice( @{$self->_path}, $offset, (defined $length ? ($length, @$step_hashs) : ()) );
242  
243 0 ▶ $self->cleanup if ($self->auto_cleanup and defined $length and @$step_hashs);
Branch: 1 branch
BranchTrueFalseTotal
if $self->auto_cleanup and defined $length and @$step_hashs 0 0 0
Condition0%0/6
$self->auto_cleanup and defined $length and @$step_hashs
!ll&&!rl&&r
000
$self->auto_cleanup and defined $length
!ll&&!rl&&r
000
MC/DC0%0/3
$self->auto_cleanup and defined $length and @$step_hashs
$self->auto_cleanup defined $length @$step_hashs
Truth table0%0/4
A and B and C
A= $self->auto_cleanupB= defined $lengthC= @$step_hashs
ABCresult
0XX0
10X0
1100
1111
244 0 ▶ return (wantarray ? {%{ $return[-1] }} : @{ dclone(\@return) });
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
245 }
246  
247 0 ▶sub clear {
Subroutine clear: not called CC=1 SCAR=6.9
Pod undocumented
248 0 my $self = shift;
249 0 $self->_path([]);
250 0 return $self;
251 }
252 0 ▶sub replace {
Subroutine replace: not called CC=1 SCAR=6.9
Pod undocumented
253 0 my $self = shift;
254 0 $self->clear->push(@_);
255 }
256  
257 1 ▶sub clone {
Subroutine clone: called CC=2 SCAR=7.1
Pod undocumented
258 1 my $self = shift;
259  
260 # if an argument is passed, assume it's a path
261 1 ▶ my %path_args = @_ ? (
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 0 1 1
262 path => shift,
263 ) : (
264 _path => dclone($self->_path),
265 path => '', # ignored
266 );
267  
268 1 $self->new(
269 %path_args,
270 auto_normalize => $self->auto_normalize,
271 auto_cleanup => $self->auto_cleanup,
272 );
273 }
274  
275 0 ▶sub normalize {
Subroutine normalize: not called CC=1 SCAR=6.9
Pod undocumented
276 0 my $self = $_[0];
277 0 $self->_normalize( $self->_path );
278 0 return $self;
279 }
280  
281 29 ▶sub _normalize {
Subroutine _normalize: called CC=2 SCAR=6.9
282 29 my ($self, $path_array) = @_;
283  
284 # For normalization, can't trust the original step, so we make new ones
285 29 my $new_array = [];
286 29 foreach my $item (@$path_array) {
287 90 push @$new_array, $self->key2hash( @$item{qw(key type pos)} );
288 }
289  
290 29 return $new_array;
291 }
292  
293 24 ▶sub cleanup {
Subroutine cleanup: called CC=15 SCAR=28.3
Pod undocumented
294 24 my $self = $_[0];
295 24 my $path = $self->_path;
296 24 my $new_path = [];
297  
298 24 my ($old_pos, $old_type);
299 24 foreach my $step_hash (@$path) {
300 73 my $full_pos = $step_hash->{pos};
301  
302 # Process pos
303 73 my ($pos, $type);
304 73 ▶ if ($full_pos =~ /^(\d+)$/) { ($pos, $type) = ($1, 'A'); } # absolute
Branch: 2 branches
BranchTrueFalseTotal
if ($full_pos =~ /^(\d+)$/) { } 13 60 73
elsif ($full_pos =~ /^X([+\-]\d+)$/) { } 60 0 60
305 60 elsif ($full_pos =~ /^X([+\-]\d+)$/) { ($pos, $type) = ($1, 'R'); } # relative
306 else { # WTF is this?
307 0 die sprintf("During path cleanup, found unparsable pos: %s (step: %s)", $full_pos, $step_hash->{step});
308 }
309 73 $pos = int($pos);
310  
311 ### XXX: We may not need this level of complexity if we are only using 0, 1, X-1, X-0, X+1
312  
313 73 my $new_step_hash = { %$step_hash };
314  
315 # The most important pos is the first one
316 73 ▶ unless (defined $old_pos) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $old_pos) 24 49 73
317 24 $old_pos = $pos;
318 24 $old_type = $type;
319  
320 24 push(@$new_path, $new_step_hash);
321 24 $new_step_hash->{pos} = $step_hash->{pos};
322 24 next;
323 }
324  
325 # Relative is going to continue the status quo
326 49 ▶ if ($type eq 'R') {
Branch: 2 branches
BranchTrueFalseTotal
if ($type eq 'R') { } 49 0 49
elsif ($type eq 'A') { } 0 0 0
327 49 $old_pos += $pos;
328 49 ▶ $new_step_hash->{pos} = $old_type eq 'A' ? $old_pos : sprintf 'X%+d', $pos;
Branch: 1 branch
BranchTrueFalseTotal
$old_type eq 'A' ? : 24 25 49
329  
330 # Don't use the pos for placement. Follow the chain of the index, using the array offset.
331 # IOW, if it started out with something like X+3, we won't end up with a bunch of starter blanks.
332 49 my $array_index = $#$new_path + $pos;
333  
334 # If the index ends up in the negative, we can't clean it up yet.
335 49 ▶ if ($array_index < 0) {
Branch: 2 branches
BranchTrueFalseTotal
if ($array_index < 0) { } 4 45 49
elsif ($pos <= 0) { } 8 37 45
336 4 ▶ if ($old_type eq 'A') {
Branch: 1 branch
BranchTrueFalseTotal
if ($old_type eq "A") 2 2 4
337 # An absolute path should never go into the negative index (ie: /..)
338 2 die sprintf("During path cleanup, an absolute path dropped into a negative depth (full path: %s)", $self->as_string);
339 }
340  
341 2 push(@$new_path, $new_step_hash);
342 }
343 # Backtracking
344 elsif ($pos <= 0) {
345 # If the slicing would carve off past the end, just append and move on...
346 8 ▶ if (@$new_path < abs($pos)) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$new_path < abs $pos) 0 8 8
347 0 push(@$new_path, $new_step_hash);
348 0 next;
349 }
350  
351 # Just ignore zero-pos (ie: /./)
352 8 ▶ next unless $pos;
Branch: 1 branch
BranchTrueFalseTotal
unless $pos 4 4 8
353  
354 # Carve off a slice of the $new_path
355 4 my @back_path = splice(@$new_path, $pos);
356  
357 # If any of the steps in the path are a relative negative, we have to keep all of them.
358 4 ▶ if (any { $_->{pos} =~ /^X-/ } @back_path) { push(@$new_path, @back_path, $new_step_hash); }
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
if (&any(sub { $$_{"pos"} =~ /^X-/; } , @back_path)) 2 2 4
359  
360 # Otherwise, we won't save this virtual step, and trash the slice.
361 }
362 # Moving ahead
363 else {
364 37 $new_path->[$array_index] = $new_step_hash;
365 }
366 }
367 # Absolute is a bit more error prone...
368 elsif ($type eq 'A') {
369 0 ▶ if ($old_type eq 'R') {
Branch: 1 branch
BranchTrueFalseTotal
if ($old_type eq "R") 0 0 0
370 # What the hell is ..\C:\ ?
371 0 die sprintf("During path cleanup, a relative path found an illegal absolute step (full path: %s)", $self->as_string);
372 }
373  
374 # Now this is just A/A, which is rarer, but still legal
375 0 $new_step_hash->{pos} = $old_pos = $pos;
376 0 $new_path->[$pos] = $new_step_hash;
377 }
378 }
379  
380 # Replace
381 22 $self->_path( $new_path );
382  
383 22 return $self;
384 }
385  
386 86 ▶sub _coerce_step {
Subroutine _coerce_step: called CC=18 SCAR=47.4
387 86 my ($self, $thing) = @_;
388  
389 # A string step/path to be converted to a HASH step
390 86 ▶ unless (ref $thing) {
Branch: 5 branches
BranchTrueFalseTotal
if (ref $thing) { } 3 83 86
if (do { builtin::blessed($thing) and $thing->does('Parse::Path::Role::Path') }) { } 0 3 3
elsif (builtin::blessed($thing)) { } 0 3 3
elsif (ref $thing eq 'HASH') { } 0 3 3
elsif (ref $thing eq 'ARRAY') { } 3 0 3
391 83 my $path_array = $self->path_str2array($thing);
392 57 ▶ return $path_array unless $self->auto_normalize;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->auto_normalize 28 29 57
393 29 return $self->_normalize($path_array);
394 }
395  
396 # Another DP path object
397 ▶ elsif (blessed $thing and $thing->does('Parse::Path::Role::Path')) {
Condition33%1/3
builtin::blessed($thing) and $thing->does("Parse::Path::Role::Path")
!ll&&!rl&&r
300
MC/DC0%0/2
builtin::blessed($thing) and $thing->does("Parse::Path::Role::Path")
builtin::blessed($thing) $thing->does("Parse::Path::Role::Path")
Truth table33%1/3
A and B
A= builtin::blessed($thing)B= $thing->does("Parse::Path::Role::Path")
ABresult
0X0
100
111
398 # If the class is the same, it's the same type of path and we can do a
399 # direct transfer. And only if the path is normalized, or we don't care
400 # about it.
401 0 ▶ return dclone($thing->_path) if (
Branch: 1 branch
BranchTrueFalseTotal
if $thing->isa($self) and $thing->auto_normalize || !$self->auto_normalize 0 0 0
Condition0%0/6
$thing->isa($self) and $thing->auto_normalize || !$self->auto_normalize
!ll&&!rl&&r
000
$thing->auto_normalize or not $self->auto_normalize
l!l&&r!l&&!r
000
MC/DC0%0/3
$thing->isa($self) and $thing->auto_normalize || !$self->auto_normalize
$thing->isa($self) $thing->auto_normalize not $self->auto_normalize
Truth table0%0/4
A and B or not(C)
A= $thing->isa($self)B= $thing->auto_normalizeC= not $self->auto_normalize
ABCresult
0XX0
1000
11X1
1011
402 $thing->isa($self) and
403 $thing->auto_normalize || !$self->auto_normalize
404 );
405  
406 0 return $self->_normalize($thing->_path);
407 }
408  
409 # WTF is this?
410 elsif (blessed $thing) {
411 0 die sprintf( "Found incoercible %s step (blessed)", blessed $thing );
412 }
413  
414 # A potential HASH step
415 elsif (ref $thing eq 'HASH') {
416 die 'Found incoercible HASH step with ref values'
417 0 ▶ if (grep { ref $_ } values %$thing);
Branch: 1 branch
BranchTrueFalseTotal
if grep {ref $_;} values %$thing 0 0 0
418  
419 0 ▶ if ( all { exists $thing->{$_} } qw(key type step pos) ) {
Subroutine __ANON__: not called CC=1 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
if (&all(sub { exists $$thing{$_}; } , "key", "type", "step", "pos")) 0 0 0
420 # We have no idea what data is in $thing, so we just soft clone it into
421 # something else. Our own methods will bypass the validation if we
422 # pass the right thing, by accessing _path directly.
423 return [{
424 type => $thing->{type},
425 key => $thing->{key},
426 step => $thing->{step},
427 pos => $thing->{pos},
428 0 }];
429 }
430  
431 # It's better to have a key/type pair than a step
432 0 ▶ if (exists $thing->{key} and exists $thing->{type}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $$thing{"key"} and exists $$thing{"type"}) 0 0 0
Condition0%0/3
exists $$thing{"key"} and exists $$thing{"type"}
!ll&&!rl&&r
000
MC/DC0%0/2
exists $$thing{"key"} and exists $$thing{"type"}
exists $$thing{"key"} exists $$thing{"type"}
Truth table0%0/3
A and B
A= exists $$thing{"key"}B= exists $$thing{"type"}
ABresult
0X0
100
111
433 0 my $step_hash = $self->key2hash( @$thing{qw(key type pos)} );
434 0 return [ $step_hash ];
435 }
436  
437 0 ▶ return $self->path_str2array( $thing->{step} ) if (exists $thing->{step});
Branch: 1 branch
BranchTrueFalseTotal
if exists $$thing{"step"} 0 0 0
438  
439 0 die 'Found incoercible HASH step with wrong keys/data';
440 }
441  
442 # A collection of HASH steps?
443 elsif (ref $thing eq 'ARRAY') {
444 3 my $path_array = [];
445 3 foreach my $item (@$thing) {
446 4 my $step_hash = $self->_coerce_step($item);
447 4 ▶ push @$path_array, (ref $step_hash eq 'ARRAY') ? @$step_hash : $step_hash;
Branch: 1 branch
BranchTrueFalseTotal
ref $step_hash eq 'ARRAY' ? : 4 0 4
448 }
449  
450 3 return $path_array;
451 }
452  
453 # WTF is this?
454 else {
455 0 die sprintf( "Found incoercible %s step", ref $thing );
456 }
457 }
458  
459 90 ▶sub key2hash {
Subroutine key2hash: called CC=11 SCAR=27.0
Pod undocumented
460 90 my ($self, $key, $type, $pos) = @_;
461  
462 # Sanity checks
463 90 ▶ die sprintf( "type not HASH or ARRAY (found %s)", $type )
Branch: 1 branch
BranchTrueFalseTotal
unless $type =~ /^HASH$|^ARRAY$/ 0 90 90
464 unless ($type =~ /^HASH$|^ARRAY$/);
465  
466 90 my $bp = $self->_blueprint;
467 90 my $hash_bp = $bp->{hash_key_stringification};
468 90 my $hash_re = $bp->{hash_step_regexp};
469 90 my $array_re = $bp->{array_step_regexp};
470  
471 # Transform the key to a string step
472 90 my $step = $key;
473 90 ▶ if ($type eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq 'HASH') { } 87 3 90
474 87 ▶ my $tuple = first { $step =~ $_->[0] } @$hash_bp;
Subroutine __ANON__: called CC=1 SCAR=0.0
475 87 ▶ die "Cannot match stringification for hash step; hash_step_stringification is not setup right!" unless $tuple;
Branch: 1 branch
BranchTrueFalseTotal
unless $tuple 0 87 87
476  
477 87 ▶ $step = $tuple->[2]->($step) if $tuple->[2];
Branch: 1 branch
BranchTrueFalseTotal
if $$tuple[2] 7 80 87
478 87 $step = sprintf ($tuple->[1], $step);
479 }
480 else {
481 3 $step = sprintf ($bp->{array_key_sprintf}, $step);
482 }
483  
484 # Validate the new step
485 90 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "HASH" and !($step =~ /^$hash_re$/) || $type eq "ARRAY" and not $step =~ /^$array_re$/) 0 90 90
Condition44%4/9
$type eq "HASH" and !($step =~ /^$hash_re$/) || $type eq "ARRAY" and not $step =~ /^$array_re$/
!ll&&!rl&&r
9000
$type eq "HASH" and !($step =~ /^$hash_re$/) || $type eq "ARRAY"
!ll&&!rl&&r
3870
not $step =~ /^$hash_re$/ or $type eq "ARRAY"
l!l&&r!l&&!r
0087
MC/DC0%0/4
$type eq "HASH" and !($step =~ /^$hash_re$/) || $type eq "ARRAY" and not $step =~ /^$array_re$/
$type eq "HASH" not $step =~ /^$hash_re$/ $type eq "ARRAY" not $step =~ /^$array_re$/
Truth table33%2/6
A and not(B) or C and not(D)
A= $type eq "HASH"B= not $step =~ /^$hash_re$/C= $type eq "ARRAY"D= not $step =~ /^$array_re$/
ABCDresult
0XXX0
100X0
11X00
10100
11X11
10111
486 $type eq 'HASH' and $step !~ /^$hash_re$/ ||
487 $type eq 'ARRAY' and $step !~ /^$array_re$/
488 ) {
489 0 ▶ die sprintf( "Found %s key than didn't validate against regexp: '%s' --> '%s' (pos: %s)", $type, $key, $step, $pos // '???' );
Condition0%0/2
$pos // "???"
l!l
00
MC/DC0%0/1
$pos // "???"
$pos
Truth table0%0/2
A
A= $pos
Aresult
11
00
490 }
491  
492 return {
493 90 ▶ type => $type,
Condition33%1/3
$pos // $self->_find_pos($step)
l!l&&r!l&&!r
9000
MC/DC0%0/2
$pos // $self->_find_pos($step)
$pos $self->_find_pos($step)
Truth table33%1/3
A // B
A= $posB= $self->_find_pos($step)
ABresult
1X1
011
000
494 key => $key,
495 step => $step,
496 ### XXX: No +delimiter in latter case. Not our fault; doing the best we can with the data we've got! ###
497 pos => $pos // $self->_find_pos($step),
498 };
499 }
500  
501 83 ▶sub path_str2array {
Subroutine path_str2array: called CC=3 SCAR=11.0
Pod undocumented
502 83 my ($self, $path) = @_;
503 83 my $path_array = [];
504  
505 83 ▶ while (length $path) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $path) 235 57 292
506 235 my $step_hash = $self->shift_path_str(\$path);
507  
508 209 push(@$path_array, $step_hash);
509 209 ▶ die sprintf( "In path '%s', too deep down the rabbit hole, stopped at '%s'", $_[1], $path )
Branch: 1 branch
BranchTrueFalseTotal
if @$path_array > 255 0 209 209
510 if (@$path_array > 255);
511 };
512  
513 57 return $path_array;
514 }
515  
516 217 ▶sub _find_pos {
Subroutine _find_pos: called CC=2 SCAR=7.0
517 217 my ($self, $step_plus_delimiter) = @_;
518  
519 # Find a matching pos key
520 217 my $dt = $self->_blueprint->{pos_translation};
521  
522 217 ▶ my $tuple = first { $step_plus_delimiter =~ $_->[0] } @$dt;
Subroutine __ANON__: called CC=1 SCAR=0.0
523 217 ▶ die "Cannot match a position for step; pos_translation is not setup right!" unless $tuple;
Branch: 1 branch
BranchTrueFalseTotal
unless $tuple 0 217 217
524  
525 217 return $tuple->[1];
526 }
527  
528 235 ▶sub shift_path_str {
Subroutine shift_path_str: called CC=5 SCAR=16.1
Pod undocumented
529 235 my ($self, $pathref) = @_;
530  
531 235 my $orig_path = $$pathref;
532  
533 235 my $bp = $self->_blueprint;
534 235 my $hash_re = $bp->{hash_step_regexp};
535 235 my $array_re = $bp->{array_step_regexp};
536 235 my $delim_re = $bp->{delimiter_regexp};
537  
538 235 my $step_hash;
539 # Array first because hash could have zero-length string
540 235 ▶ if ($$pathref =~ s/^(?<step>$array_re)//) {
Branch: 2 branches
BranchTrueFalseTotal
if ($$pathref =~ s/^(?<step>$array_re)//) { } 24 211 235
elsif ($$pathref =~ s/^(?<step>$hash_re)//) { } 193 18 211
541 $step_hash = {
542 type => 'ARRAY',
543 key => $+{key},
544 step => $+{step},
545 24 };
546 }
547 elsif ($$pathref =~ s/^(?<step>$hash_re)//) {
548 $step_hash = {
549 type => 'HASH',
550 key => $+{key},
551 step => $+{step},
552 193 };
553  
554 # Support quote escaping
555 193 my $ut = $self->_blueprint->{unescape_translation};
556 193 ▶ my $tuple = first { $+{quote} =~ $_->[0] } @$ut;
Subroutine __ANON__: called CC=1 SCAR=0.0
557 193 ▶ $step_hash->{key} = $tuple->[1]->($step_hash->{key}) if defined $tuple;
Branch: 1 branch
BranchTrueFalseTotal
if defined $tuple 11 182 193
558 }
559 else {
560 18 die sprintf( "Found unparsable step: '%s'", $$pathref );
561 }
562  
563 217 $$pathref =~ s/^($delim_re)//;
564  
565 # Re-piece the step + delimiter to use with _find_pos
566 217 $step_hash->{pos} = $self->_find_pos( $step_hash->{step}.$1 );
567  
568 # If the path is not shifting at all, then something is wrong with REs
569 217 ▶ if (length $$pathref == length $orig_path) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $$pathref == length $orig_path) 8 209 217
570 8 die sprintf( "Found unshiftable step: '%s'", $$pathref );
571 }
572  
573 209 return $step_hash;
574 }
575  
576 59 ▶sub as_string {
Subroutine as_string: called CC=11 SCAR=25.2
Pod undocumented
577 59 my $self = $_[0];
578  
579 59 my $dlp = $self->_blueprint->{delimiter_placement};
580  
581 59 my $str = '';
582 59 for my $i (0 .. $self->step_count - 1) {
583 200 my $step_hash = $self->_path->[$i];
584 200 ▶ my $next_step = ($i == $self->step_count - 1) ? undef : $self->_path->[$i+1];
Branch: 1 branch
BranchTrueFalseTotal
$i == $self->step_count - 1 ? : 57 143 200
585  
586 200 my $d = $step_hash->{pos};
587  
588 ### Left side delimiter placement
589 0 ▶ if ( exists $dlp->{$d.'L'}) { $str .= $dlp->{$d.'L'}; } # pos-specific
Branch: 2 branches
BranchTrueFalseTotal
if (exists $$dlp{$d . 'L'}) { } 0 200 200
elsif (not $next_step and exists $$dlp{'-1L'}) { } 0 343 343
590 0 ▶ elsif (not $next_step and exists $dlp->{'-1L'} ) { $str .= $dlp->{'-1L'}; } # ending pos
Condition66%2/3
not $next_step and exists $$dlp{"-1L"}
!ll&&!rl&&r
143570
MC/DC0%0/2
not $next_step and exists $$dlp{"-1L"}
not $next_step exists $$dlp{"-1L"}
Truth table66%2/3
not(A) and B
A= not $next_stepB= exists $$dlp{"-1L"}
ABresult
0X0
100
111
591  
592 # Add the step
593 200 $str .= $step_hash->{step};
594  
595 ### Right side delimiter placement
596 200 my $L = substr($step_hash->{type}, 0, 1);
597 200 ▶ if (exists $dlp->{$d.'R'}) { # pos-specific (supercedes other right side options)
Branch: 2 branches
BranchTrueFalseTotal
if (exists $$dlp{$d . 'R'}) { } 13 187 200
elsif ($next_step) { } 133 54 187
598 13 $str .= $dlp->{$d.'R'};
599 }
600 elsif ($next_step) { # ref-specific
601 133 my $R = substr($next_step->{type}, 0, 1);
602 133 ▶ $str .= $dlp->{$L.$R} if (exists $dlp->{$L.$R});
Branch: 1 branch
BranchTrueFalseTotal
if exists $$dlp{$L . $R} 113 20 133
603 }
604 else { # ending pos
605 0 ▶ if (exists $dlp->{'-1R'}) { $str .= $dlp->{'-1R'}; } # pos-specific
Branch: 2 branches
BranchTrueFalseTotal
if (exists $$dlp{'-1R'}) { } 0 54 54
elsif (exists $$dlp{$L}) { } 0 54 54
606 0 elsif (exists $dlp->{$L}) { $str .= $dlp->{$L}; } # ref-specific
607 }
608 }
609  
610 59 return $str;
611 }
612  
613 11 42;
614  
615 __END__
↑ Summary Path.pm »