blib/lib/Mouse/Meta/TypeConstraint.pm

filter: Statementstmt 92.7% 115 / 124 Branchbran 87.5% 63 / 72 Conditioncond 70.0% 14 / 20 MC/DCmcdc 23.0% 3 / 13 Subroutinesub 93.3% 14 / 15 Podpod 85.7% 6 / 7 totaltotal 85.6% 215 / 251 CC 52 SCAR 21.6
CC
52
Cov
89%
new
17.1
is_a_type_of
11.4
_compiled_type_coercion
7.0
CRAP 8.7
« blib/lib/Mouse/Meta/Role/Application.pm ↑ Summary blib/lib/Mouse/Meta/Method/Delegation.pm »
1 package Mouse::Meta::TypeConstraint;
2 287 ▶use Mouse::Util qw(:meta); # enables strict and warnings
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 6936 ▶sub new {
Subroutine new: called CC=17 SCAR=28.4
Pod documented
5 6936 my $class = shift;
6 6936 ▶ my %args = @_ == 1 ? %{$_[0]} : @_;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 ? : 2 6934 6936
7  
8 6936 ▶ $args{name} = '__ANON__' if !defined $args{name};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $args{"name"} 12 6924 6936
9  
10 6936 my $type_parameter;
11 6936 ▶ if(defined $args{parent}) { # subtyping
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args{"parent"}) 6614 322 6936
12 6614 %args = (%{$args{parent}}, %args);
13  
14 # a child type must not inherit 'compiled_type_constraint'
15 # and 'hand_optimized_type_constraint' from the parent
16 6614 delete $args{compiled_type_constraint}; # don't inherit it
17 6614 delete $args{hand_optimized_type_constraint}; # don't inherit it
18  
19 6614 $type_parameter = $args{type_parameter};
20 6614 ▶ if(defined(my $parent_tp = $args{parent}{type_parameter})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $parent_tp = $args{"parent"}{"type_parameter"})) 16 6598 6614
21 16 ▶ if($parent_tp != $type_parameter) {
Branch: 1 branch
BranchTrueFalseTotal
if ($parent_tp != $type_parameter) { } 3 13 16
22 3 ▶ $type_parameter->is_a_type_of($parent_tp)
Branch: 1 branch
BranchTrueFalseTotal
$type_parameter->is_a_type_of($parent_tp) or $class->throw_error("$type_parameter is not a subtype of $parent_tp") 2 1 3
23 or $class->throw_error(
24 "$type_parameter is not a subtype of $parent_tp",
25 );
26 }
27 else {
28 13 $type_parameter = undef;
29 }
30 }
31 }
32  
33 6935 my $check;
34  
35 6935 ▶ if($check = delete $args{optimized}) { # likely to be builtins
Branch: 2 branches
BranchTrueFalseTotal
if ($check = delete $args{'optimized'}) { } 5928 1007 6935
elsif (defined $type_parameter) { } 53 954 1007
36 5928 $args{hand_optimized_type_constraint} = $check;
37 5928 $args{compiled_type_constraint} = $check;
38 }
39 elsif(defined $type_parameter) { # parameterizing
40 my $generator = $args{constraint_generator}
41 53 ▶ || $class->throw_error(
Condition66%2/3
$args{'constraint_generator'} || $class->throw_error("The $args{'name'} constraint cannot be used," . " because $type_parameter doesn't subtype" . ' from a parameterizable type')
l!l&&r!l&&!r
5102
MC/DC0%0/2
$args{'constraint_generator'} || $class->throw_error("The $args{'name'} constraint cannot be used," . " because $type_parameter doesn't subtype" . ' from a parameterizable type')
$args{'constraint_generator'} $class->throw_error("The $args{'name'} constraint cannot be used," . " because $type_parameter doesn't subtype" . ' from a parameterizable type')
Truth table33%1/3
A || B
A= $args{'constraint_generator'}B= $class->throw_error("The $args{'name'} constraint cannot be used," . " because $type_parameter doesn't subtype" . ' from a parameterizable type')
ABresult
1X1
011
000
42 "The $args{name} constraint cannot be used,"
43 . " because $type_parameter doesn't subtype"
44 . " from a parameterizable type");
45  
46 51 my $parameterized_check = $generator->($type_parameter);
47 51 ▶ if(defined(my $my_check = $args{constraint})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $my_check = $args{'constraint'})) { } 7 44 51
48 19 ▶ $check = sub {
Subroutine __ANON__: called CC=2 SCAR=7.2
49 19 ▶ return $parameterized_check->($_) && $my_check->($_);
Condition66%2/3
&$parameterized_check($_) && &$my_check($_)
!ll&&!rl&&r
6013
MC/DC50%1/2
&$parameterized_check($_) && &$my_check($_)
&$parameterized_check($_) &$my_check($_)
Truth table66%2/3
A && B
A= &$parameterized_check($_)B= &$my_check($_)
ABresult
0X0
100
111
50 7 };
51 }
52 else {
53 44 $check = $parameterized_check;
54 }
55 51 $args{constraint} = $check;
56 }
57 else { # common cases
58 954 $check = $args{constraint};
59 }
60  
61 6933 ▶ if(defined($check) && ref($check) ne 'CODE'){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $check and ref $check ne "CODE") 0 6933 6933
Condition66%2/3
defined $check and ref $check ne "CODE"
!ll&&!rl&&r
88760460
MC/DC0%0/2
defined $check and ref $check ne "CODE"
defined $check ref $check ne "CODE"
Truth table66%2/3
A and B
A= defined $checkB= ref $check ne "CODE"
ABresult
0X0
100
111
62 0 $class->throw_error(
63 "Constraint for $args{name} is not a CODE reference");
64 }
65  
66 6933 my $self = bless \%args, $class;
67 $self->compile_type_constraint()
68 6933 ▶ if !$args{hand_optimized_type_constraint};
Branch: 1 branch
BranchTrueFalseTotal
unless $args{"hand_optimized_type_constraint"} 1005 5928 6933
69  
70 6933 ▶ if($args{type_constraints}) { # union types
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"type_constraints"}) 20 6913 6933
71 20 foreach my $type(@{$self->{type_constraints}}){
72 40 ▶ if($type->has_coercion){
Branch: 1 branch
BranchTrueFalseTotal
if ($type->has_coercion) 3 37 40
73 # set undef for has_coercion()
74 3 $self->{_compiled_type_coercion} = undef;
75 3 last;
76 }
77 }
78 }
79  
80 6933 return $self;
81 }
82  
83 0 ▶sub create_child_type {
Subroutine create_child_type: not called CC=1 SCAR=6.9
Pod documented
84 0 my $self = shift;
85 0 return ref($self)->new(@_, parent => $self);
86 }
87  
88 sub name;
89 sub parent;
90 sub message;
91 sub has_coercion;
92  
93 sub check;
94  
95 sub type_parameter;
96 sub __is_parameterized;
97  
98 sub _compiled_type_constraint;
99 sub _compiled_type_coercion;
100  
101 sub compile_type_constraint;
102  
103  
104 30 ▶sub _add_type_coercions { # ($self, @pairs)
Subroutine _add_type_coercions: called CC=6 SCAR=17.9
105 30 my $self = shift;
106  
107 30 ▶ if(exists $self->{type_constraints}){ # union type
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"type_constraints"}) 0 30 30
108 0 $self->throw_error(
109 "Cannot add additional type coercions to Union types '$self'");
110 }
111  
112 30 ▶ my $coercion_map = ($self->{coercion_map} ||= []);
Condition100%2/2
$self->{"coercion_map"} ||= []
l!l
327
MC/DC100%1/1
$self->{"coercion_map"} ||= []
$self->{"coercion_map"}
Truth table100%2/2
A
A= $self->{"coercion_map"}
Aresult
11
00
113 30 my %has = map{ $_->[0]->name => undef } @{$coercion_map};
114  
115 30 ▶ for(my $i = 0; $i < @_; $i++){
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @_) 36 28 64
116 36 my $from = $_[ $i];
117 36 my $action = $_[++$i];
118  
119 36 ▶ if(exists $has{$from}){
Branch: 1 branch
BranchTrueFalseTotal
if (exists $has{$from}) 1 35 36
120 1 $self->throw_error("A coercion action already exists for '$from'");
121 }
122  
123 35 ▶ my $type = Mouse::Util::TypeConstraints::find_or_parse_type_constraint($from)
Branch: 1 branch
BranchTrueFalseTotal
my $type = Mouse::Util::TypeConstraints::find_or_parse_type_constraint($from) or $self->throw_error("Could not find the type constraint ($from) to coerce from") 34 1 35
124 or $self->throw_error(
125 "Could not find the type constraint ($from) to coerce from");
126  
127 34 push @{$coercion_map}, [ $type => $action ];
128 }
129  
130 28 $self->{_compiled_type_coercion} = undef;
131 28 return;
132 }
133  
134 130 ▶sub _compiled_type_coercion {
Subroutine _compiled_type_coercion: called CC=7 SCAR=19.5
135 130 my($self) = @_;
136  
137 130 my $coercion = $self->{_compiled_type_coercion};
138 130 ▶ return $coercion if defined $coercion;
Branch: 1 branch
BranchTrueFalseTotal
if defined $coercion 103 27 130
139  
140 27 ▶ if(!$self->{type_constraints}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->{'type_constraints'}) { } 24 3 27
141 24 my @coercions;
142 24 foreach my $pair(@{$self->{coercion_map}}) {
143 30 push @coercions,
144 [ $pair->[0]->_compiled_type_constraint, $pair->[1] ];
145 }
146  
147 125 ▶ $coercion = sub {
Subroutine __ANON__: called CC=4 SCAR=13.9
148 125 my($thing) = @_;
149 125 foreach my $pair (@coercions) {
150 #my ($constraint, $converter) = @$pair;
151 133 ▶ if ($pair->[0]->($thing)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pair->[0]->($thing)) 117 16 133
152 117 return $pair->[1]->($thing) for $thing; # local $_ will cancel tie-ness due to perl's bug
153 }
154 }
155 8 return $thing;
156 24 };
157 }
158 else { # for union type
159 3 my @coercions;
160 3 foreach my $type(@{$self->{type_constraints}}){
161 8 ▶ if($type->has_coercion){
Branch: 1 branch
BranchTrueFalseTotal
if ($type->has_coercion) 4 4 8
162 4 push @coercions, $type;
163 }
164 }
165 3 ▶ if(@coercions){
Branch: 1 branch
BranchTrueFalseTotal
if (@coercions) 3 0 3
166 5 ▶ $coercion = sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
167 5 my($thing) = @_;
168 5 foreach my $type(@coercions){
169 6 my $value = $type->coerce($thing);
170 6 ▶ return $value if $self->check($value);
Branch: 1 branch
BranchTrueFalseTotal
if $self->check($value) 3 3 6
171 }
172 2 return $thing;
173 3 };
174 }
175 }
176  
177 27 return( $self->{_compiled_type_coercion} = $coercion );
178 }
179  
180 146 ▶sub coerce {
Subroutine coerce: called CC=3 SCAR=11.0
Pod documented
181 146 my $self = shift;
182 146 ▶ return $_[0] if $self->check(@_);
Branch: 1 branch
BranchTrueFalseTotal
if $self->check(@_) 16 130 146
183  
184 130 ▶ my $coercion = $self->_compiled_type_coercion
Branch: 1 branch
BranchTrueFalseTotal
my $coercion = $self->_compiled_type_coercion or $self->throw_error("Cannot coerce without a type coercion") 130 0 130
185 or $self->throw_error("Cannot coerce without a type coercion");
186 130 return $coercion->(@_);
187 }
188  
189 835 ▶sub get_message {
Subroutine get_message: called CC=6 SCAR=17.9
Pod documented
190 835 my ($self, $value) = @_;
191 835 ▶ if ( my $msg = $self->message ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $msg = $self->message) { } 11 824 835
192 11 return $msg->($value) for $value; # local $_ will cancel tie-ness due to perl's bug
193 }
194 else {
195 824 ▶ if(not defined $value) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $value) { } 42 782 824
elsif (ref $value and defined &overload::StrVal) { } 373 818 1191
196 42 $value = 'undef';
197 }
198 ▶ elsif( ref($value) && defined(&overload::StrVal) ) {
Condition66%2/3
ref $value and defined &overload::StrVal
!ll&&!rl&&r
4090373
MC/DC50%1/2
ref $value and defined &overload::StrVal
ref $value defined &overload::StrVal
Truth table66%2/3
A and B
A= ref $valueB= defined &overload::StrVal
ABresult
0X0
100
111
199 373 $value = overload::StrVal($value);
200 }
201 824 return "Validation failed for '$self' with value $value";
202 }
203 }
204  
205 73 ▶sub is_a_type_of {
Subroutine is_a_type_of: called CC=9 SCAR=24.3
Pod documented
206 73 my($self, $other) = @_;
207  
208 # ->is_a_type_of('__ANON__') is always false
209 73 ▶ return 0 if !ref($other) && $other eq '__ANON__';
Branch: 1 branch
BranchTrueFalseTotal
if not ref $other and $other eq "__ANON__" 0 73 73
Condition66%2/3
not ref $other and $other eq "__ANON__"
!ll&&!rl&&r
7660
MC/DC0%0/2
not ref $other and $other eq "__ANON__"
not ref $other $other eq "__ANON__"
Truth table66%2/3
not(A) and B
A= not ref $otherB= $other eq "__ANON__"
ABresult
0X0
100
111
210  
211 73 (my $other_name = $other) =~ s/\s+//g;
212  
213 73 ▶ return 1 if $self->name eq $other_name;
Branch: 1 branch
BranchTrueFalseTotal
if $self->name eq $other_name 22 51 73
214  
215 51 ▶ if(exists $self->{type_constraints}){ # union
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"type_constraints"}) 0 51 51
216 0 foreach my $type(@{$self->{type_constraints}}) {
217 0 ▶ return 1 if $type->name eq $other_name;
Branch: 1 branch
BranchTrueFalseTotal
if $type->name eq $other_name 0 0 0
218 }
219 }
220  
221 51 ▶ for(my $p = $self->parent; defined $p; $p = $p->parent) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $p) 122 20 142
222 122 ▶ return 1 if $p->name eq $other_name;
Branch: 1 branch
BranchTrueFalseTotal
if $p->name eq $other_name 31 91 122
223 }
224  
225 20 return 0;
226 }
227  
228 # See also Moose::Meta::TypeConstraint::Parameterizable
229 45 ▶sub parameterize {
Subroutine parameterize: called CC=3 SCAR=12.3
Pod undocumented
230 45 my($self, $param, $name) = @_;
231  
232 45 ▶ if(!ref $param){
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $param) 0 45 45
233 0 require Mouse::Util::TypeConstraints;
234 0 $param = Mouse::Util::TypeConstraints::find_or_create_isa_type_constraint($param);
235 }
236  
237 45 ▶ $name ||= sprintf '%s[%s]', $self->name, $param->name;
Condition66%2/3
$name ||= sprintf("%s[%s]", $self->name, $param->name)
l!l&&r!l&&!r
4410
MC/DC0%0/2
$name ||= sprintf("%s[%s]", $self->name, $param->name)
$name sprintf("%s[%s]", $self->name, $param->name)
Truth table66%2/3
A ||= B
A= $nameB= sprintf("%s[%s]", $self->name, $param->name)
ABresult
1X1
011
000
238 45 return Mouse::Meta::TypeConstraint->new(
239 name => $name,
240 parent => $self,
241 type_parameter => $param,
242 );
243 }
244  
245 2 ▶sub assert_valid {
Subroutine assert_valid: called CC=2 SCAR=6.9
Pod documented
246 2 my ($self, $value) = @_;
247  
248 2 ▶ if(!$self->check($value)){
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->check($value)) 1 1 2
249 1 $self->throw_error($self->get_message($value));
250 }
251 1 return 1;
252 }
253  
254 # overloading stuff
255  
256 1000 ▶sub _as_string { $_[0]->name } # overload ""
Subroutine _as_string: called CC=1 SCAR=0.0
257 sub _identity; # overload 0+
258  
259 6 ▶sub _unite { # overload infix:<|>
Subroutine _unite: called CC=1 SCAR=0.0
260 6 my($lhs, $rhs) = @_;
261 6 require Mouse::Util::TypeConstraints;
262 6 return Mouse::Util::TypeConstraints::_find_or_create_union_type(
263 $lhs,
264 Mouse::Util::TypeConstraints::find_or_create_isa_type_constraint($rhs),
265 );
266 }
267  
268 287 1;
269 __END__
« blib/lib/Mouse/Meta/Role/Application.pm ↑ Summary blib/lib/Mouse/Meta/Method/Delegation.pm »