TypeMap.pm

filter: Statementstmt 91.7% 89 / 97 Branchbran 71.4% 20 / 28 Conditioncond 100% 3 / 3 MC/DCmcdc 100% 2 / 2 Subroutinesub 86.6% 13 / 15 Podpod 75.0% 3 / 4 totaltotal 87.2% 130 / 149 CC 22 SCAR 17.7
CC
22
Cov
88%
find_matching_entry
11.2
_build__sorted_entries
7.0
__ANON__
4.4
CRAP 5.9
↑ Summary TypeMap/Entry.pm »
1 package MooseX::TypeMap;
2  
3 1 ▶use Moose 1.02;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use MooseX::TypeMap::Entry;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use Scalar::Util qw(refaddr blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use MooseX::Types::Moose qw(ArrayRef HashRef);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Moose::Util::TypeConstraints qw(find_type_constraint);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use namespace::clean -except => [qw( meta )];
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 1 our $VERSION = '0.003000';
11 1 $VERSION = eval $VERSION;
12  
13 1 has type_entries => (
14 traits => ['Array'],
15 isa => ArrayRef['MooseX::TypeMap::Entry'],
16 lazy => 1,
17 builder => '_build_type_entries',
18 handles => {
19 'type_entries' => 'elements',
20 }
21 );
22  
23 1 has subtype_entries => (
24 traits => ['Array'],
25 isa => ArrayRef['MooseX::TypeMap::Entry'],
26 lazy => 1,
27 builder => '_build_subtype_entries',
28 handles => {
29 'subtype_entries' => 'elements',
30 }
31 );
32  
33 1 has _sorted_entries => (
34 traits => ['Array'],
35 isa => ArrayRef[ArrayRef['MooseX::TypeMap::Entry']],
36 lazy => 1,
37 init_arg => undef,
38 builder => '_build__sorted_entries',
39 handles => {
40 '_sorted_entries' => 'elements',
41 }
42 );
43  
44 1 has _type_to_entry_cache => (
45 is => 'ro',
46 isa => HashRef['MooseX::TypeMap::Entry'],
47 lazy => 1,
48 init_arg => undef,
49 builder => '_build__type_to_entry_cache',
50 );
51  
52 2 ▶sub _build_type_entries { [] }
Subroutine _build_type_entries: called CC=1 SCAR=0.0
53 0 ▶sub _build_subtype_entries { [] }
Subroutine _build_subtype_entries: not called CC=1 SCAR=6.9
54 3 ▶sub _build__type_to_entry_cache { {} }
Subroutine _build__type_to_entry_cache: called CC=1 SCAR=0.0
55  
56 3 ▶sub _build__sorted_entries {
Subroutine _build__sorted_entries: called CC=7 SCAR=19.5
57 3 my $self = shift;
58  
59 3 my %subtypes;
60 my %tc_entry_map;
61 3 for my $entry ( $self->subtype_entries ) {
62 11 my $entry_tc = $entry->type_constraint;
63 11 my $entry_ident = refaddr $entry_tc;
64 11 $subtypes{$entry_ident} = {};
65 11 $tc_entry_map{$entry_ident} = $entry;
66  
67 11 for my $other ( $self->subtype_entries ) {
68 69 my $other_tc = $other->type_constraint;
69 69 ▶ if( $other_tc->is_subtype_of($entry_tc) ){
Branch: 1 branch
BranchTrueFalseTotal
if ($other_tc->is_subtype_of($entry_tc)) 16 53 69
70 16 $subtypes{$entry_ident}->{refaddr $other_tc} = undef;
71 }
72 }
73 }
74 3 my @sorted;
75 3 ▶ while (keys %subtypes) {
Branch: 1 branch
BranchTrueFalseTotal
if (keys %subtypes) 8 3 11
76 8 my @slot;
77 8 for my $ident (keys %subtypes) {
78 23 ▶ if (!keys %{ $subtypes{$ident} }) {
Branch: 1 branch
BranchTrueFalseTotal
unless (keys %{$subtypes{$ident};}) 11 12 23
79 11 delete $subtypes{$ident};
80 11 push(@slot, $ident);
81 }
82 }
83  
84 8 map { delete @{$_}{@slot} } values %subtypes;
85 8 push @sorted, [ @tc_entry_map{@slot} ];
86 }
87 3 return \@sorted;
88 }
89  
90 #back compat for a year or so
91 3 ▶around BUILDARGS => sub {
Subroutine __ANON__: called CC=3 SCAR=14.8
92 3 my $orig = shift;
93 3 my $class = shift;
94 3 my $args = $class->$orig(@_);
95 3 ▶ if( my $entries = delete $args->{entries} ){
Branch: 1 branch
BranchTrueFalseTotal
if (my $entries = delete $args->{"entries"}) 0 3 3
96 0 my @type_entries = @$entries;
97 0 ▶ if( my $orig_type_entries = delete $args->{type_entries} ){
Branch: 1 branch
BranchTrueFalseTotal
if (my $orig_type_entries = delete $args->{"type_entries"}) 0 0 0
98 0 push(@type_entries, @$orig_type_entries);
99 }
100 0 $args->{type_entries} = \@type_entries;
101 }
102 3 return $args;
103 1 };
104  
105  
106 1 ▶sub clone_with_additional_entries {
Subroutine clone_with_additional_entries: called CC=3 SCAR=11.1
Pod undocumented
107 1 my($self, $additional) = @_;
108 1 my @type_entries = $self->type_entries;
109 1 ▶ if( defined $additional->{type_entries} ){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $additional->{"type_entries"}) 1 0 1
110 1 push(@type_entries, @{$additional->{type_entries}});
111 }
112 1 my @subtype_entries = $self->subtype_entries;
113 1 ▶ if( defined $additional->{subtype_entries} ){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $additional->{"subtype_entries"}) 1 0 1
114 1 push(@subtype_entries, @{$additional->{subtype_entries}});
115 }
116 1 return $self->new(
117 type_entries => \@type_entries,
118 subtype_entries => \@subtype_entries
119 );
120 }
121  
122 7 ▶sub find_matching_entry {
Subroutine find_matching_entry: called CC=11 SCAR=24.2
Pod documented
123 7 my($self, $type_or_name) = @_;
124 7 ▶ my $type = find_type_constraint($type_or_name) or return;
Branch: 1 branch
BranchTrueFalseTotal
my $type = find_type_constraint($type_or_name) or return 7 0 7
125 7 ▶ $type = $type->__type_constraint
Branch: 1 branch
BranchTrueFalseTotal
if builtin::blessed($type) eq "MooseX::Types::TypeDecorator" 7 0 7
126 if blessed($type) eq 'MooseX::Types::TypeDecorator';
127  
128 7 my $cache = $self->_type_to_entry_cache;
129 7 my $type_ident = refaddr $type;
130 7 ▶ if( exists $cache->{$type_ident} ){
Branch: 1 branch
BranchTrueFalseTotal
if (exists $cache->{$type_ident}) 1 6 7
131 1 ▶ return $cache->{$type_ident} if defined $cache->{$type_ident};
Branch: 1 branch
BranchTrueFalseTotal
if defined $cache->{$type_ident} 1 0 1
132 0 return;
133 }
134  
135 6 for my $entry ($self->type_entries) {
136 2 ▶ if( $entry->type_constraint->equals($type) ){
Branch: 1 branch
BranchTrueFalseTotal
if ($entry->type_constraint->equals($type)) 1 1 2
137 1 return $cache->{$type_ident} = $entry;
138 }
139 }
140  
141 5 for my $family ($self->_sorted_entries) {
142 10 for my $entry (@$family) {
143 18 my $tc = $entry->type_constraint;
144 18 ▶ if( $type->equals($tc) || $type->is_subtype_of($tc) ){
Branch: 1 branch
BranchTrueFalseTotal
if ($type->equals($tc) or $type->is_subtype_of($tc)) 4 14 18
Condition100%3/3
$type->equals($tc) or $type->is_subtype_of($tc)
l!l&&r!l&&!r
2214
MC/DC100%2/2
$type->equals($tc) or $type->is_subtype_of($tc)
$type->equals($tc) $type->is_subtype_of($tc)
Truth table100%3/3
A or B
A= $type->equals($tc)B= $type->is_subtype_of($tc)
ABresult
1X1
011
000
145 4 return $cache->{$type_ident} = $entry;
146 }
147 }
148 }
149 1 $cache->{$type_ident} = undef;
150 1 return;
151 }
152  
153 0 ▶sub has_entry_for {
Subroutine has_entry_for: not called CC=1 SCAR=6.9
Pod documented
154 0 my($self, $type) = @_;
155 0 return defined $self->find_matching_entry($type);
156 }
157  
158 7 ▶sub resolve {
Subroutine resolve: called CC=2 SCAR=6.9
Pod documented
159 7 my($self, $type) = @_;
160 7 ▶ if( my $entry = $self->find_matching_entry($type) ){
Branch: 1 branch
BranchTrueFalseTotal
if (my $entry = $self->find_matching_entry($type)) 6 1 7
161 6 return $entry->data;
162 }
163 1 return;
164 }
165  
166 1 __PACKAGE__->meta->make_immutable;
167  
168 1 1;
169  
170 __END__;
↑ Summary TypeMap/Entry.pm »