Navigator/Node/Perl/Ref.pm

filter: Statementstmt 27.9% 24 / 86 Branchbran 0.0% 0 / 28 Conditioncond 0.0% 0 / 18 MC/DCmcdc 0.0% 0 / 11 Subroutinesub 50.0% 7 / 14 Podpod 100% 1 / 1 totaltotal 20.2% 32 / 158 CC 25 SCAR 42.1
CC
25
Cov
18%
_find_children_and_attrs
156.0
_find_ref
56.0
_child
42.0
CRAP 67.1
↑ Summary Navigator.pm »
1 package Tree::Navigator::Node::Perl::Ref;
2 1 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 extends 'Tree::Navigator::Node';
5  
6 1 ▶use Scalar::Util qw/reftype blessed/;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use List::Util qw/min/;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Params::Validate qw/validate HASHREF ARRAYREF SCALARREF/;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 1 ▶use constant MAX_CHILDREN => 99;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 0 ▶sub MOUNT {
Subroutine MOUNT: not called CC=2 SCAR=17.9
Pod documented
14 0 my ($class, $mount_args) = @_;
15 0 ▶ my @mount_point = %{$mount_args->{mount_point} || {}};
Condition0%0/2
$mount_args->{'mount_point'} || {}
l!l
00
MC/DC0%0/1
$mount_args->{'mount_point'} || {}
$mount_args->{'mount_point'}
Truth table0%0/2
A
A= $mount_args->{'mount_point'}
Aresult
11
00
16 0 $mount_args->{mount_point} = validate(@mount_point , {
17 ref => {type => HASHREF | ARRAYREF},
18 exclude => {type => SCALARREF, isa => 'Regexp', optional => 1},
19 });
20 }
21  
22  
23 0 ▶sub _find_ref {
Subroutine _find_ref: not called CC=7 SCAR=40.3
24 0 my $self = shift;
25 0 ▶ my $path = $self->path || "" ;
Condition0%0/2
$self->path || ''
l!l
00
MC/DC0%0/1
$self->path || ''
$self->path
Truth table0%0/2
A
A= $self->path
Aresult
11
00
26 0 my $ref = $self->mount_point->{ref};
27
28 0 foreach my $fragment (split m[/], $path) {
29 0 my $reftype = reftype $ref;
30 0 ▶ if ($reftype eq 'ARRAY') {
Branch: 3 branches
BranchTrueFalseTotal
if ($reftype eq 'ARRAY') { } 0 0 0
elsif ($reftype eq 'HASH') { } 0 0 0
elsif ($reftype eq 'SCALAR' or $reftype eq 'REF') { } 0 0 0
31 0 $ref = $ref->[$fragment];
32 }
33 elsif ($reftype eq 'HASH') {
34 0 $ref = $ref->{$fragment};
35 }
36 ▶ elsif ($reftype eq 'SCALAR' || $reftype eq 'REF') {
Condition0%0/3
$reftype eq "SCALAR" or $reftype eq "REF"
l!l&&r!l&&!r
000
MC/DC0%0/2
$reftype eq "SCALAR" or $reftype eq "REF"
$reftype eq "SCALAR" $reftype eq "REF"
Truth table0%0/3
A or B
A= $reftype eq "SCALAR"B= $reftype eq "REF"
ABresult
1X1
011
000
37 0 $ref = $$ref;
38 }
39 else {
40 0 die "no such path in data : '$path'";
41 }
42 }
43  
44 0 return $ref;
45 }
46  
47  
48 # inner hashrefs, arrayrefs and scalarrefs are considered 'children';
49 # other data are considered 'attributes'
50 0 ▶sub _ref_is_child {
Subroutine _ref_is_child: not called CC=2 SCAR=17.9
51 0 my $ref = shift;
52 0 ▶ my $reftype = reftype $ref || '';
Condition0%0/2
builtin::reftype($ref) || ''
l!l
00
MC/DC0%0/1
builtin::reftype($ref) || ''
builtin::reftype($ref)
Truth table0%0/2
A
A= builtin::reftype($ref)
Aresult
11
00
53 0 return $reftype =~ /^(?:HASH|ARRAY|SCALAR|REF)$/;
54 }
55  
56  
57 0 ▶sub _children {
Subroutine _children: not called CC=1 SCAR=6.9
58 0 my $self = shift;
59 0 $self->_find_children_and_attrs;
60 0 return $self->{children};
61 }
62  
63 0 ▶sub _attributes {
Subroutine _attributes: not called CC=1 SCAR=6.9
64 0 my $self = shift;
65 0 $self->_find_children_and_attrs;
66 0 return $self->{attributes};
67 }
68  
69 0 ▶sub _find_children_and_attrs {
Subroutine _find_children_and_attrs: not called CC=12 SCAR=50.5
70 0 my $self = shift;
71 0 my $ref = $self->_find_ref;
72 0 my @children;
73 my %attrs;
74  
75 0 my $reftype = reftype $ref;
76 0 ▶ if ($reftype eq 'ARRAY') {
Branch: 3 branches
BranchTrueFalseTotal
if ($reftype eq 'ARRAY') { } 0 0 0
elsif ($reftype eq 'HASH') { } 0 0 0
elsif ($reftype eq 'SCALAR' or $reftype eq 'REF') { } 0 0 0
77 0 my @indices = 0 .. min($#$ref, MAX_CHILDREN);
78 0 for my $i (@indices) {
79 0 my $val = $ref->[$i];
80 0 ▶ if (_ref_is_child($val)) {
Branch: 1 branch
BranchTrueFalseTotal
if (_ref_is_child($val)) { } 0 0 0
81 0 push @children, $i
82 } else {
83 0 $attrs{$i} = $val;
84 }
85 }
86 }
87 elsif ($reftype eq 'HASH') {
88 0 my @keys = keys %$ref;
89 0 my $regex = $self->mount_point->{exclude};
90 0 ▶ @keys = grep {$_ !~ $regex} @keys if $regex;
Branch: 1 branch
BranchTrueFalseTotal
if $regex 0 0 0
91 0 for my $k (@keys) {
92 0 my $val = $ref->{$k};
93 0 ▶ if (_ref_is_child($val)) {
Branch: 1 branch
BranchTrueFalseTotal
if (_ref_is_child($val)) { } 0 0 0
94 0 push @children, $k
95 } else {
96 0 $attrs{$k} = $val;
97 }
98 }
99 0 @children = sort {lc($a) cmp lc($b)} @children;
100 }
101 ▶ elsif ($reftype eq 'SCALAR' || $reftype eq 'REF') {
Condition0%0/3
$reftype eq "SCALAR" or $reftype eq "REF"
l!l&&r!l&&!r
000
MC/DC0%0/2
$reftype eq "SCALAR" or $reftype eq "REF"
$reftype eq "SCALAR" $reftype eq "REF"
Truth table0%0/3
A or B
A= $reftype eq "SCALAR"B= $reftype eq "REF"
ABresult
1X1
011
000
102 0 @children = '$';
103 }
104 0 my $blessed = blessed $ref;
105 0 ▶ $attrs{isa} //= $blessed if $blessed;
Branch: 1 branch
BranchTrueFalseTotal
if $blessed 0 0 0
Condition0%0/3
$attrs{"isa"} //= $blessed
l!l&&r!l&&!r
000
MC/DC0%0/2
$attrs{"isa"} //= $blessed
$attrs{"isa"} $blessed
Truth table0%0/3
A //= B
A= $attrs{"isa"}B= $blessed
ABresult
1X1
011
000
106 0 $self->{children} = \@children;
107 0 $self->{attributes} = \%attrs;
108 }
109  
110  
111 0 ▶sub _child {
Subroutine _child: not called CC=6 SCAR=37.4
112 0 my ($self, $child_path) = @_;
113 0 my $class = ref $self;
114  
115 # check if child exists
116 0 my $ref = $self->_find_ref;
117 0 my $child_ok;
118 0 my $reftype = reftype $ref;
119  
120 0 ▶ if ($reftype eq 'ARRAY') { $child_ok = exists $ref->[$child_path] }
Branch: 3 branches
BranchTrueFalseTotal
if ($reftype eq 'ARRAY') { } 0 0 0
elsif ($reftype eq 'HASH') { } 0 0 0
elsif ($reftype eq 'SCALAR' or $reftype eq 'REF') { } 0 0 0
121 0 elsif ($reftype eq'HASH') { $child_ok = exists $ref->{$child_path} }
122 0 ▶ elsif ($reftype eq 'SCALAR' || $reftype eq 'REF') { $child_ok = $child_path eq '$' }
Condition0%0/3
$reftype eq "SCALAR" or $reftype eq "REF"
l!l&&r!l&&!r
000
MC/DC0%0/2
$reftype eq "SCALAR" or $reftype eq "REF"
$reftype eq "SCALAR" $reftype eq "REF"
Truth table0%0/3
A or B
A= $reftype eq "SCALAR"B= $reftype eq "REF"
ABresult
1X1
011
000
123  
124 0 ▶ $child_ok or die "no child '$child_path' in " . $self->full_path;
Branch: 1 branch
BranchTrueFalseTotal
$child_ok or die "no child '${child_path}' in " . $self->full_path 0 0 0
125  
126 0 return $class->new(
127 mount_point => $self->mount_point,
128 path => $self->_join_path($self->path, $child_path),
129 );
130 }
131  
132  
133 1 __PACKAGE__->meta->make_immutable;
134  
135  
136 1 1; # End of Tree::Navigator::Node::Perl::Ref
137  
138  
139 __END__
↑ Summary Navigator.pm »