AsObject/Hash.pm

filter: Statementstmt 91.6% 66 / 72 Branchbran 75.0% 27 / 36 Conditioncond 41.6% 10 / 24 MC/DCmcdc 8.3% 1 / 12 Subroutinesub 100% 17 / 17 Podpod 0.0% 0 / 1 totaltotal 74.6% 121 / 162 CC 27 SCAR 24.2
CC
27
Cov
78%
AUTOLOAD
27.5
__get_data
13.1
__guess_data
4.1
CRAP 11.3
↑ Summary AsObject/Array.pm »
1 package Data::AsObject::Hash;
2 7 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
3 7 $Data::AsObject::Hash::VERSION = '0.07';
4 }
5  
6 # ABSTRACT: Base class for Data::AsObject hashrefs
7  
8 7 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 7 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 7 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 7 ▶use Data::AsObject ();
Subroutine BEGIN: called CC=1 SCAR=0.0
12 7 ▶use namespace::clean -except => [qw/AUTOLOAD/];
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 7 our $AUTOLOAD;
15  
16 5 ▶sub can {
Subroutine can: called CC=3 SCAR=11.6
Pod undocumented
17 5 ▶ if(@_ != 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ != 2) 0 5 5
18 0 local $AUTOLOAD = ref($_[0]) .'::can';
19 0 return $_[0]->AUTOLOAD(@_);
20 }
21  
22 5 my($self, $key) = @_;
23 5 ▶ return unless( exists $self->{$key} );
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{$key} 3 2 5
24 2 ▶ return sub { __get_data($self, $key) };
Subroutine __ANON__: called CC=1 SCAR=0.0
25 }
26  
27 29 ▶sub AUTOLOAD {
Subroutine AUTOLOAD: called CC=10 SCAR=33.1
28 29 my $self = shift;
29 29 my $index = shift;
30 29 my $data;
31  
32 29 my $key = $AUTOLOAD;
33 29 $key =~ s/.*:://;
34 29 undef $AUTOLOAD;
35  
36 29 ▶ if ($key eq "can" && defined $index && $index != /\d+/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq "can" and defined $index and $index != /\d+/) 0 29 29
Condition33%2/6
$key eq "can" and defined $index and $index != /\d+/
!ll&&!rl&&r
2900
$key eq "can" and defined $index
!ll&&!rl&&r
2900
MC/DC0%0/3
$key eq "can" and defined $index and $index != /\d+/
$key eq "can" defined $index $index != /\d+/
Truth table25%1/4
A and B and C
A= $key eq "can"B= defined $indexC= $index != /\d+/
ABCresult
0XX0
10X0
1100
1111
37 0 return undef;
38 }
39  
40 29 ▶ if ($key eq "isa" && defined $index && $index != /\d+/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq "isa" and defined $index and $index != /\d+/) 0 29 29
Condition33%2/6
$key eq "isa" and defined $index and $index != /\d+/
!ll&&!rl&&r
2900
$key eq "isa" and defined $index
!ll&&!rl&&r
2900
MC/DC0%0/3
$key eq "isa" and defined $index and $index != /\d+/
$key eq "isa" defined $index $index != /\d+/
Truth table25%1/4
A and B and C
A= $key eq "isa"B= defined $indexC= $index != /\d+/
ABCresult
0XX0
10X0
1100
1111
41 0 ▶ $index eq ref($self) or
Branch: 2 branches
BranchTrueFalseTotal
$index eq ref $self or $index eq "Data::AsObject::Hash" or $index eq "UNIVERSAL" ? (return 1) : (return 0) 0 0 0
$index eq 'UNIVERSAL' ? : 0 0 0
Condition0%0/3
$index eq ref $self or $index eq "Data::AsObject::Hash"
l!l&&r!l&&!r
000
MC/DC0%0/2
$index eq ref $self or $index eq "Data::AsObject::Hash"
$index eq ref $self $index eq "Data::AsObject::Hash"
Truth table0%0/3
A or B
A= $index eq ref $selfB= $index eq "Data::AsObject::Hash"
ABresult
1X1
011
000
42 $index eq "Data::AsObject::Hash" or
43 $index eq "UNIVERSAL"
44 ? return 1
45 : return 0;
46 }
47  
48 29 return __get_data($self, $key, $index);
49 }
50  
51 31 ▶sub __get_data {
Subroutine __get_data: called CC=13 SCAR=25.8
52 31 my ($self, $key, $index) = @_;
53 31 ▶ my $data = exists $self->{$key} ? $self->{$key} : __guess_data($self, $key);
Branch: 1 branch
BranchTrueFalseTotal
exists $self->{$key} ? : 15 16 31
54 31 ▶ my $mode = ref($self) =~ /^.*::(\w+)$/ ? $1 : '';
Branch: 1 branch
BranchTrueFalseTotal
ref($self) =~ /^.*::(\w+)$/ ? : 31 0 31
55  
56 31 ▶ if ( !$data ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($data) 14 17 31
57 14 ▶ return if $key eq "DESTROY";
Branch: 1 branch
BranchTrueFalseTotal
if $key eq "DESTROY" 10 4 14
58  
59 4 my $msg = "Attempting to access non-existing hash key $key!";
60  
61 4 ▶ carp $msg if $mode eq 'Loose';
Branch: 1 branch
BranchTrueFalseTotal
if $mode eq "Loose" 1 3 4
62 4 ▶ croak $msg if $mode eq 'Strict';
Branch: 1 branch
BranchTrueFalseTotal
if $mode eq "Strict" 2 2 4
63 2 return;
64 }
65  
66 17 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if (defined $index and $index =~ /\d+/ and &$Data::AsObject::__check_type($data) eq "ARRAY" and exists $data->[$index]) 5 12 17
Condition66%6/9
defined $index and $index =~ /\d+/ and &$Data::AsObject::__check_type($data) eq "ARRAY" and exists $data->[$index]
!ll&&!rl&&r
1205
defined $index and $index =~ /\d+/ and &$Data::AsObject::__check_type($data) eq "ARRAY"
!ll&&!rl&&r
1205
defined $index and $index =~ /\d+/
!ll&&!rl&&r
1205
MC/DC25%1/4
defined $index and $index =~ /\d+/ and &$Data::AsObject::__check_type($data) eq "ARRAY" and exists $data->[$index]
defined $index $index =~ /\d+/ &$Data::AsObject::__check_type($data) eq "ARRAY" exists $data->[$index]
Truth table40%2/5
A and B and C and D
A= defined $indexB= $index =~ /\d+/C= &$Data::AsObject::__check_type($data) eq "ARRAY"D= exists $data->[$index]
ABCDresult
0XXX0
10XX0
110X0
11100
11111
67 defined $index
68 && $index =~ /\d+/
69 && $Data::AsObject::__check_type->($data) eq "ARRAY"
70 && exists $data->[$index]
71 )
72 {
73 5 $data = $data->[$index];
74 }
75  
76 17 ▶ if ( $Data::AsObject::__check_type->($data) eq "ARRAY" ) {
Branch: 2 branches
BranchTrueFalseTotal
if (&$Data::AsObject::__check_type($data) eq 'ARRAY') { } 4 13 17
elsif (&$Data::AsObject::__check_type($data) eq 'HASH') { } 2 11 13
77 4 bless $data, "Data::AsObject::Array::$mode";
78 } elsif ( $Data::AsObject::__check_type->($data) eq "HASH" ) {
79 2 bless $data, "Data::AsObject::Hash::$mode";
80 }
81  
82 17 return $data;
83 }
84  
85 16 ▶sub __guess_data {
Subroutine __guess_data: called CC=4 SCAR=14.2
86 16 my $self = shift;
87 16 my $key_regex = shift;
88 16 my $has_colon_or_dash = $key_regex =~ s/_/[-:]/g;
89 16 ▶ my @matches = grep(/$key_regex/, keys %$self) if $has_colon_or_dash;
Branch: 1 branch
BranchTrueFalseTotal
if $has_colon_or_dash 2 14 16
90  
91 16 ▶ if ( @matches == 1 ) {
Branch: 2 branches
BranchTrueFalseTotal
if (@matches == 1) { } 2 14 16
elsif (@matches > 1) { } 0 14 14
92 2 return $self->{$matches[0]};
93 } elsif ( @matches > 1 ) {
94 0 carp "Attempt to disambiguate hash key $key_regex returns multiple matches!";
95 0 return $self->{$matches[0]};
96 }
97  
98 14 return;
99 }
100  
101  
102 package Data::AsObject::Hash::Strict;
103 7 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
104 7 $Data::AsObject::Hash::Strict::VERSION = '0.07';
105 }
106 7 ▶use base 'Data::AsObject::Hash';
Subroutine BEGIN: called CC=1 SCAR=0.0
107  
108 package Data::AsObject::Hash::Loose;
109 7 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
110 7 $Data::AsObject::Hash::Loose::VERSION = '0.07';
111 }
112 7 ▶use base 'Data::AsObject::Hash';
Subroutine BEGIN: called CC=1 SCAR=0.0
113  
114 package Data::AsObject::Hash::Silent;
115 7 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
116 7 $Data::AsObject::Hash::Silent::VERSION = '0.07';
117 }
118 7 ▶use base 'Data::AsObject::Hash';
Subroutine BEGIN: called CC=1 SCAR=0.0
119  
120 7 1;
121  
122  
123 __END__
↑ Summary AsObject/Array.pm »