blib/lib/Moose/Meta/TypeConstraint.pm

filter: Statementstmt 97.2% 175 / 180 Branchbran 89.7% 70 / 78 Conditioncond 93.3% 14 / 15 MC/DCmcdc 88.8% 8 / 9 Subroutinesub 100% 42 / 42 Podpod 93.3% 14 / 15 totaltotal 95.2% 323 / 339 CC 64 SCAR 12.9
CC
64
Cov
95%
equals
10.4
new
9.0
_inline_check
4.0
CRAP 3.6
« blib/lib/Moose/Util/MetaRole.pm ↑ Summary blib/lib/Moose/Meta/Method/Accessor/Native/Collection.pm »
1 package Moose::Meta::TypeConstraint;
2 398 our $VERSION = '2.4001';
3  
4 398 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 398 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 398 ▶use metaclass;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 3529 ▶use overload '0+' => sub { refaddr(shift) }, # id an object
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
9 41 ▶ '""' => sub { shift->name }, # stringify to tc name
Subroutine __ANON__: called CC=1 SCAR=0.0
10 42320 ▶ bool => sub { 1 },
Subroutine __ANON__: called CC=1 SCAR=0.0
11 398 fallback => 1;
12  
13 398 ▶use Eval::Closure;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 398 ▶use Scalar::Util qw(refaddr);
Subroutine BEGIN: called CC=1 SCAR=0.0
15 398 ▶use Sub::Util qw(set_subname);
Subroutine BEGIN: called CC=1 SCAR=0.0
16 398 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 398 ▶use parent 'Class::MOP::Object';
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 398 ▶use Moose::Util 'throw_exception';
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 398 __PACKAGE__->meta->add_attribute('name' => (
23 reader => 'name',
24 Class::MOP::_definition_context(),
25 ));
26 398 __PACKAGE__->meta->add_attribute('parent' => (
27 reader => 'parent',
28 predicate => 'has_parent',
29 Class::MOP::_definition_context(),
30 ));
31  
32 1 ▶my $null_constraint = sub { 1 };
Subroutine __ANON__: called CC=1 SCAR=0.0
33 __PACKAGE__->meta->add_attribute('constraint' => (
34 reader => 'constraint',
35 writer => '_set_constraint',
36 669 ▶ default => sub { $null_constraint },
Subroutine __ANON__: called CC=1 SCAR=0.0
37 398 Class::MOP::_definition_context(),
38 ));
39  
40 398 __PACKAGE__->meta->add_attribute('message' => (
41 accessor => 'message',
42 predicate => 'has_message',
43 Class::MOP::_definition_context(),
44 ));
45  
46 398 __PACKAGE__->meta->add_attribute('_default_message' => (
47 accessor => '_default_message',
48 Class::MOP::_definition_context(),
49 ));
50  
51 # can't make this a default because it has to close over the type name, and
52 # cmop attributes don't have lazy
53 11788 ▶my $_default_message_generator = sub {
Subroutine __ANON__: called CC=1 SCAR=0.2
54 11788 my $name = shift;
55 1921 ▶ sub {
Subroutine __ANON__: called CC=3 SCAR=11.8
56 1921 my $value = shift;
57 # have to load it late like this, since it uses Moose itself
58 1921 ▶ my $can_partialdump = try {
Subroutine __ANON__: called CC=1 SCAR=2.6
59 # versions prior to 0.14 had a potential infinite loop bug
60 1921 require Devel::PartialDump;
61 0 Devel::PartialDump->VERSION(0.14);
62 0 1;
63 1921 };
64 1921 ▶ if ($can_partialdump) {
Branch: 1 branch
BranchTrueFalseTotal
if ($can_partialdump) { } 0 1921 1921
65 0 $value = Devel::PartialDump->new->dump($value);
66 }
67 else {
68 1921 ▶ $value = (defined $value ? overload::StrVal($value) : 'undef');
Branch: 1 branch
BranchTrueFalseTotal
defined $value ? : 1829 92 1921
69 }
70 1921 return "Validation failed for '" . $name . "' with value $value";
71 }
72 11788 };
73 398 __PACKAGE__->meta->add_attribute('coercion' => (
74 accessor => 'coercion',
75 predicate => 'has_coercion',
76 Class::MOP::_definition_context(),
77 ));
78  
79 398 __PACKAGE__->meta->add_attribute('inlined' => (
80 init_arg => 'inlined',
81 accessor => 'inlined',
82 predicate => '_has_inlined_type_constraint',
83 Class::MOP::_definition_context(),
84 ));
85  
86 __PACKAGE__->meta->add_attribute('inline_environment' => (
87 init_arg => 'inline_environment',
88 accessor => '_inline_environment',
89 11790 ▶ default => sub { {} },
Subroutine __ANON__: called CC=1 SCAR=0.0
90 398 Class::MOP::_definition_context(),
91 ));
92  
93 1 ▶sub parents {
Subroutine parents: called CC=1 SCAR=0.0
Pod documented
94 1 my $self = shift;
95 1 $self->parent;
96 }
97  
98 # private accessors
99  
100 398 __PACKAGE__->meta->add_attribute('compiled_type_constraint' => (
101 accessor => '_compiled_type_constraint',
102 predicate => '_has_compiled_type_constraint',
103 Class::MOP::_definition_context(),
104 ));
105  
106 398 __PACKAGE__->meta->add_attribute('package_defined_in' => (
107 accessor => '_package_defined_in',
108 Class::MOP::_definition_context(),
109 ));
110  
111 11807 ▶sub new {
Subroutine new: called CC=9 SCAR=22.0
Pod documented
112 11807 my $class = shift;
113 11807 my ($first, @rest) = @_;
114 11807 ▶ my %args = ref $first ? %$first : $first ? ($first, @rest) : ();
Branch: 2 branches
BranchTrueFalseTotal
ref $first ? : 3091 8716 11807
$first ? : 8714 2 8716
115 11807 ▶ $args{name} = $args{name} ? "$args{name}" : "__ANON__";
Branch: 1 branch
BranchTrueFalseTotal
$args{'name'} ? : 11780 27 11807
116  
117 11807 ▶ if ( exists $args{message}
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args{"message"} and !ref($args{"message"}) || ref $args{"message"} ne "CODE") 1 11806 11807
Condition83%5/6
exists $args{"message"} and !ref($args{"message"}) || ref $args{"message"} ne "CODE"
!ll&&!rl&&r
11794121
not ref $args{"message"} or ref $args{"message"} ne "CODE"
l!l&&r!l&&!r
1012
MC/DC66%2/3
exists $args{"message"} and !ref($args{"message"}) || ref $args{"message"} ne "CODE"
exists $args{"message"} not ref $args{"message"} ref $args{"message"} ne "CODE"
Truth table75%3/4
A and not(B) or C
A= exists $args{"message"}B= not ref $args{"message"}C= ref $args{"message"} ne "CODE"
ABCresult
0XX0
1000
11X1
1011
118 && (!ref($args{message}) || ref($args{message}) ne 'CODE') ) {
119 1 throw_exception( MessageParameterMustBeCodeRef => params => \%args,
120 class => $class
121 );
122 }
123  
124 11806 my $self = $class->_new(%args);
125 11806 ▶ $self->compile_type_constraint()
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_has_compiled_type_constraint 11806 0 11806
126 unless $self->_has_compiled_type_constraint;
127 11800 ▶ $self->_default_message($_default_message_generator->($self->name))
Branch: 1 branch
BranchTrueFalseTotal
unless $self->has_message 11788 12 11800
128 unless $self->has_message;
129 11800 return $self;
130 }
131  
132  
133  
134 134 ▶sub coerce {
Subroutine coerce: called CC=3 SCAR=11.0
Pod documented
135 134 my $self = shift;
136  
137 134 my $coercion = $self->coercion;
138  
139 134 ▶ unless ($coercion) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($coercion) 2 132 134
140 2 throw_exception( CoercingWithoutCoercions => type_name => $self->name );
141 }
142  
143 132 ▶ return $_[0] if $self->check($_[0]);
Branch: 1 branch
BranchTrueFalseTotal
if $self->check($_[0]) 15 117 132
144  
145 117 return $coercion->coerce(@_);
146 }
147  
148 3 ▶sub assert_coerce {
Subroutine assert_coerce: called CC=1 SCAR=0.0
Pod documented
149 3 my $self = shift;
150  
151 3 my $result = $self->coerce(@_);
152  
153 3 $self->assert_valid($result);
154  
155 2 return $result;
156 }
157  
158 9444 ▶sub check {
Subroutine check: called CC=2 SCAR=6.9
Pod documented
159 9444 my ($self, @args) = @_;
160 9444 my $constraint_subref = $self->_compiled_type_constraint;
161 9444 ▶ return $constraint_subref->(@args) ? 1 : undef;
Branch: 1 branch
BranchTrueFalseTotal
&$constraint_subref(@args) ? : 4422 5022 9444
162 }
163  
164 18 ▶sub validate {
Subroutine validate: called CC=2 SCAR=6.9
Pod documented
165 18 my ($self, $value) = @_;
166 18 ▶ if ($self->_compiled_type_constraint->($value)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_compiled_type_constraint->($value)) { } 6 12 18
167 6 return undef;
168 }
169 else {
170 12 $self->get_message($value);
171 }
172 }
173  
174 48148 ▶sub can_be_inlined {
Subroutine can_be_inlined: called CC=3 SCAR=11.0
Pod documented
175 48148 my $self = shift;
176  
177 48148 ▶ if ( $self->has_parent && $self->constraint == $null_constraint ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_parent and $self->constraint == $null_constraint) 1097 47051 48148
Condition100%3/3
$self->has_parent and $self->constraint == $null_constraint
!ll&&!rl&&r
2470445811097
MC/DC100%2/2
$self->has_parent and $self->constraint == $null_constraint
$self->has_parent $self->constraint == $null_constraint
Truth table100%3/3
A and B
A= $self->has_parentB= $self->constraint == $null_constraint
ABresult
0X0
100
111
178 1097 return $self->parent->can_be_inlined;
179 }
180  
181 47051 return $self->_has_inlined_type_constraint;
182 }
183  
184 24709 ▶sub _inline_check {
Subroutine _inline_check: called CC=4 SCAR=13.9
185 24709 my $self = shift;
186  
187 24709 ▶ unless ( $self->can_be_inlined ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->can_be_inlined) 2 24707 24709
188 2 throw_exception( CannotInlineTypeConstraintCheck => type_name => $self->name );
189 }
190  
191 24707 ▶ if ( $self->has_parent && $self->constraint == $null_constraint ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_parent and $self->constraint == $null_constraint) 518 24189 24707
Condition100%3/3
$self->has_parent and $self->constraint == $null_constraint
!ll&&!rl&&r
93923250518
MC/DC100%2/2
$self->has_parent and $self->constraint == $null_constraint
$self->has_parent $self->constraint == $null_constraint
Truth table100%3/3
A and B
A= $self->has_parentB= $self->constraint == $null_constraint
ABresult
0X0
100
111
192 518 return $self->parent->_inline_check(@_);
193 }
194  
195 24189 return '( do { ' . $self->inlined->( $self, @_ ) . ' } )';
196 }
197  
198 21712 ▶sub inline_environment {
Subroutine inline_environment: called CC=3 SCAR=11.0
Pod documented
199 21712 my $self = shift;
200  
201 21712 ▶ if ( $self->has_parent && $self->constraint == $null_constraint ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_parent and $self->constraint == $null_constraint) 500 21212 21712
Condition100%3/3
$self->has_parent and $self->constraint == $null_constraint
!ll&&!rl&&r
108120131500
MC/DC100%2/2
$self->has_parent and $self->constraint == $null_constraint
$self->has_parent $self->constraint == $null_constraint
Truth table100%3/3
A and B
A= $self->has_parentB= $self->constraint == $null_constraint
ABresult
0X0
100
111
202 500 return $self->parent->inline_environment;
203 }
204  
205 21212 return $self->_inline_environment;
206 }
207  
208 8 ▶sub assert_valid {
Subroutine assert_valid: called CC=2 SCAR=6.9
Pod documented
209 8 my ( $self, $value ) = @_;
210  
211 8 ▶ return 1 if $self->check($value);
Branch: 1 branch
BranchTrueFalseTotal
if $self->check($value) 5 3 8
212  
213 3 throw_exception(
214 'ValidationFailedForTypeConstraint',
215 type => $self,
216 value => $value
217 );
218 }
219  
220 926 ▶sub get_message {
Subroutine get_message: called CC=2 SCAR=6.9
Pod documented
221 926 my ($self, $value) = @_;
222 926 ▶ my $msg = $self->has_message
Branch: 1 branch
BranchTrueFalseTotal
$self->has_message ? : 34 892 926
223 ? $self->message
224 : $self->_default_message;
225 926 local $_ = $value;
226 926 return $msg->($value);
227 }
228  
229 ## type predicates ...
230  
231 1765 ▶sub equals {
Subroutine equals: called CC=8 SCAR=23.4
Pod documented
232 1765 my ( $self, $type_or_name ) = @_;
233  
234 1765 my $other = Moose::Util::TypeConstraints::find_type_constraint($type_or_name);
235 1765 ▶ return if not $other;
Branch: 1 branch
BranchTrueFalseTotal
unless $other 1 1764 1765
236  
237 1764 ▶ return 1 if $self == $other;
Branch: 1 branch
BranchTrueFalseTotal
if $self == $other 407 1357 1764
238  
239 1357 ▶ return unless $self->constraint == $other->constraint;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->constraint == $other->constraint 1356 1 1357
240  
241 1 ▶ if ( $self->has_parent ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_parent) { } 0 1 1
242 0 ▶ return unless $other->has_parent;
Branch: 1 branch
BranchTrueFalseTotal
unless $other->has_parent 0 0 0
243 0 ▶ return unless $self->parent->equals( $other->parent );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->parent->equals($other->parent) 0 0 0
244 } else {
245 1 ▶ return if $other->has_parent;
Branch: 1 branch
BranchTrueFalseTotal
if $other->has_parent 0 1 1
246 }
247  
248 1 return;
249 }
250  
251 265 ▶sub is_a_type_of {
Subroutine is_a_type_of: called CC=3 SCAR=11.0
Pod documented
252 265 my ($self, $type_or_name) = @_;
253  
254 265 my $type = Moose::Util::TypeConstraints::find_type_constraint($type_or_name);
255 265 ▶ return if not $type;
Branch: 1 branch
BranchTrueFalseTotal
unless $type 4 261 265
256  
257 261 ▶ ($self->equals($type) || $self->is_subtype_of($type));
Branch: 1 branch
BranchTrueFalseTotal
$self->equals($type) or $self->is_subtype_of($type) 70 191 261
258 }
259  
260 579 ▶sub is_subtype_of {
Subroutine is_subtype_of: called CC=4 SCAR=13.9
Pod documented
261 579 my ($self, $type_or_name) = @_;
262  
263 579 my $type = Moose::Util::TypeConstraints::find_type_constraint($type_or_name);
264 579 ▶ return if not $type;
Branch: 1 branch
BranchTrueFalseTotal
unless $type 4 575 579
265  
266 575 my $current = $self;
267  
268 575 ▶ while (my $parent = $current->parent) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $parent = $current->parent) 1602 258 1860
269 1602 ▶ return 1 if $parent->equals($type);
Branch: 1 branch
BranchTrueFalseTotal
if $parent->equals($type) 317 1285 1602
270 1285 $current = $parent;
271 }
272  
273 258 return 0;
274 }
275  
276 ## compiling the type constraint
277  
278 14848 ▶sub compile_type_constraint {
Subroutine compile_type_constraint: called CC=1 SCAR=0.0
Pod undocumented
279 14848 my $self = shift;
280 14848 $self->_compiled_type_constraint($self->_actually_compile_type_constraint);
281 }
282  
283 ## type compilers ...
284  
285 14775 ▶sub _actually_compile_type_constraint {
Subroutine _actually_compile_type_constraint: called CC=4 SCAR=13.9
286 14775 my $self = shift;
287  
288 14775 ▶ if ( $self->can_be_inlined ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->can_be_inlined) 14659 116 14775
289 14659 return eval_closure(
290 source => 'sub { ' . $self->_inline_check('$_[0]') . ' }',
291 environment => $self->inline_environment,
292 );
293 }
294  
295 116 my $check = $self->constraint;
296 116 ▶ unless ( defined $check ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $check) 1 115 116
297 1 throw_exception( NoConstraintCheckForTypeConstraint => type_name => $self->name );
298 }
299  
300 115 ▶ return $self->_compile_subtype($check)
Branch: 1 branch
BranchTrueFalseTotal
if $self->has_parent 96 19 115
301 if $self->has_parent;
302  
303 19 return $self->_compile_type($check);
304 }
305  
306 125 ▶sub _compile_subtype {
Subroutine _compile_subtype: called CC=4 SCAR=13.9
307 125 my ($self, $check) = @_;
308  
309 # gather all the parent constraints in order
310 125 my @parents;
311 125 foreach my $parent ($self->_collect_all_parents) {
312 627 push @parents => $parent->constraint;
313 }
314  
315 125 @parents = grep { $_ != $null_constraint } reverse @parents;
316  
317 125 ▶ unless ( @parents ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@parents) { } 123 2 125
318 2 return $self->_compile_type($check);
319 } else {
320 # general case, check all the constraints, from the first parent to ourselves
321 123 my @checks = @parents;
322 123 ▶ push @checks, $check if $check != $null_constraint;
Branch: 1 branch
BranchTrueFalseTotal
if $check != $null_constraint 118 5 123
323 return set_subname(
324 1316 ▶ $self->name => sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
Subroutine Header: called CC=3 SCAR=11.0
325 1316 my (@args) = @_;
326 1316 local $_ = $args[0];
327 1316 foreach my $check (@checks) {
328 5717 ▶ return undef unless $check->(@args);
Branch: 2 branches
BranchTrueFalseTotal
unless &$check(@args) 700 5017 5717
unless &$check(@args) 43 2338 2381
329 }
330 616 return 1;
331 }
332 123 );
333 }
334 }
335  
336 47 ▶sub _compile_type {
Subroutine _compile_type: called CC=2 SCAR=6.9
337 47 my ($self, $check) = @_;
338  
339 47 ▶ return $check if $check == $null_constraint; # Item, Any
Branch: 1 branch
BranchTrueFalseTotal
if $check == $null_constraint 8 39 47
340  
341 return set_subname(
342 746 ▶ $self->name => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
343 746 my (@args) = @_;
344 746 local $_ = $args[0];
345 746 $check->(@args);
346 }
347 39 );
348 }
349  
350 ## other utils ...
351  
352 138 ▶sub _collect_all_parents {
Subroutine _collect_all_parents: called CC=2 SCAR=6.9
353 138 my $self = shift;
354 138 my @parents;
355 138 my $current = $self->parent;
356 138 ▶ while (defined $current) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $current) 683 138 821
357 683 push @parents => $current;
358 683 $current = $current->parent;
359 }
360 138 return @parents;
361 }
362  
363 6440 ▶sub create_child_type {
Subroutine create_child_type: called CC=1 SCAR=0.0
Pod documented
364 6440 my ($self, %opts) = @_;
365 6440 my $class = ref $self;
366 6440 return $class->new(%opts, parent => $self);
367 }
368  
369 398 1;
370  
371 # ABSTRACT: The Moose Type Constraint metaclass
372  
373 __END__
« blib/lib/Moose/Util/MetaRole.pm ↑ Summary blib/lib/Moose/Meta/Method/Accessor/Native/Collection.pm »