blib/lib/Moose/Meta/TypeConstraint/Union.pm

filter: Statementstmt 99.0% 106 / 107 Branchbran 88.4% 23 / 26 Conditioncond 100% 3 / 3 MC/DCmcdc 100% 2 / 2 Subroutinesub 96.1% 25 / 26 Podpod 100% 12 / 12 totaltotal 97.1% 171 / 176 CC 20 SCAR 10.3
CC
20
Cov
97%
equals
6.0
validate
5.0
coercion
3.0
CRAP 2.8
« blib/lib/Moose/Meta/Method/Constructor.pm ↑ Summary blib/lib/Moose/Exception/Role/EitherAttributeOrAttributeName.pm »
1 package Moose::Meta::TypeConstraint::Union;
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 398 ▶use Moose::Meta::TypeCoercion::Union;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 398 ▶use List::Util 1.33 qw(first all);
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 398 ▶use parent 'Moose::Meta::TypeConstraint';
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 __PACKAGE__->meta->add_attribute('type_constraints' => (
15 accessor => 'type_constraints',
16 0 ▶ default => sub { [] },
Subroutine __ANON__: not called CC=1 SCAR=6.9
17 398 Class::MOP::_definition_context(),
18 ));
19  
20 73 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
21 73 my ($class, %options) = @_;
22  
23 77 my $name = join '|' => sort { $a cmp $b }
24 73 map { $_->name } @{ $options{type_constraints} };
25  
26 73 my $self = $class->SUPER::new(
27 name => $name,
28 %options,
29 );
30  
31 73 $self->_set_constraint( $self->_compiled_type_constraint );
32  
33 73 return $self;
34 }
35  
36 # XXX - this is a rather gross implementation of laziness for the benefit of
37 # MX::Types. If we try to call ->has_coercion on the objects during object
38 # construction, this does not work when defining a recursive constraint with
39 # MX::Types.
40 75 ▶sub coercion {
Subroutine coercion: called CC=3 SCAR=11.0
Pod documented
41 75 my $self = shift;
42  
43 75 ▶ return $self->{coercion} if exists $self->{coercion};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"coercion"} 34 41 75
44  
45 # Using any instead of grep here causes a weird error with some corner
46 # cases when MX::Types is in use. See RT #61001.
47 41 ▶ if ( grep { $_->has_coercion } @{ $self->type_constraints } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (grep {$_->has_coercion;} @{$self->type_constraints;}) { } 2 39 41
48 2 return $self->{coercion} = Moose::Meta::TypeCoercion::Union->new(
49 type_constraint => $self );
50 }
51 else {
52 39 return $self->{coercion} = undef;
53 }
54 }
55  
56 72 ▶sub has_coercion {
Subroutine has_coercion: called CC=1 SCAR=0.0
Pod documented
57 72 return defined $_[0]->coercion;
58 }
59  
60 73 ▶sub _actually_compile_type_constraint {
Subroutine _actually_compile_type_constraint: called CC=1 SCAR=0.0
61 73 my $self = shift;
62  
63 73 my @constraints = @{ $self->type_constraints };
64  
65 1976 ▶ return sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
66 1976 my $value = shift;
67 1976 foreach my $type (@constraints) {
68 3375 ▶ return 1 if $type->check($value);
Branch: 1 branch
BranchTrueFalseTotal
if $type->check($value) 650 2725 3375
69 }
70 1326 return undef;
71 73 };
72 }
73  
74 232 ▶sub can_be_inlined {
Subroutine can_be_inlined: called CC=3 SCAR=11.0
Pod documented
75 232 my $self = shift;
76  
77 # This was originally done with all() from List::MoreUtils, but that
78 # caused some sort of bizarro parsing failure under 5.10.
79 232 for my $tc ( @{ $self->type_constraints } ) {
80 460 ▶ return 0 unless $tc->can_be_inlined;
Branch: 1 branch
BranchTrueFalseTotal
unless $tc->can_be_inlined 9 451 460
81 }
82  
83 223 return 1;
84 }
85  
86 108 ▶sub _inline_check {
Subroutine _inline_check: called CC=1 SCAR=0.0
87 108 my $self = shift;
88 108 my $val = shift;
89  
90 return '('
91 . (
92 218 join ' || ', map { '(' . $_->_inline_check($val) . ')' }
93 108 @{ $self->type_constraints }
94 )
95 . ')';
96 }
97  
98 163 ▶sub inline_environment {
Subroutine inline_environment: called CC=1 SCAR=0.0
Pod documented
99 163 my $self = shift;
100  
101 327 return { map { %{ $_->inline_environment } }
102 163 @{ $self->type_constraints } };
103 }
104  
105 8 ▶sub equals {
Subroutine equals: called CC=6 SCAR=18.0
Pod documented
106 8 my ( $self, $type_or_name ) = @_;
107  
108 8 my $other = Moose::Util::TypeConstraints::find_type_constraint($type_or_name);
109  
110 8 ▶ return unless $other->isa(__PACKAGE__);
Branch: 1 branch
BranchTrueFalseTotal
unless $other->isa(__PACKAGE__) 5 3 8
111  
112 3 my @self_constraints = @{ $self->type_constraints };
113 3 my @other_constraints = @{ $other->type_constraints };
114  
115 3 ▶ return unless @self_constraints == @other_constraints;
Branch: 1 branch
BranchTrueFalseTotal
unless @self_constraints == @other_constraints 0 3 3
116  
117 # FIXME presort type constraints for efficiency?
118 3 constraint: foreach my $constraint ( @self_constraints ) {
119 6 ▶ for ( my $i = 0; $i < @other_constraints; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @other_constraints) 7 0 7
120 7 ▶ if ( $constraint->equals($other_constraints[$i]) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($constraint->equals($other_constraints[$i])) 6 1 7
121 6 splice @other_constraints, $i, 1;
122 6 next constraint;
123 }
124 }
125 }
126  
127 3 return @other_constraints == 0;
128 }
129  
130 13 ▶sub parent {
Subroutine parent: called CC=3 SCAR=11.0
Pod documented
131 13 my $self = shift;
132  
133 13 my ($first, @rest) = @{ $self->type_constraints };
134  
135 13 for my $parent ( $first->_collect_all_parents ) {
136 30 ▶ return $parent if all { $_->is_a_type_of($parent) } @rest;
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
if &all(sub { $_->is_a_type_of($parent); } , @rest) 12 18 30
137 }
138  
139 1 return;
140 }
141  
142 6 ▶sub validate {
Subroutine validate: called CC=5 SCAR=16.1
Pod documented
143 6 my ($self, $value) = @_;
144 6 my $message;
145 6 foreach my $type (@{$self->type_constraints}) {
146 10 my $err = $type->validate($value);
147 10 ▶ return unless defined $err;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $err 3 7 10
148 7 ▶ $message .= ($message ? ' and ' : '') . $err
Branch: 2 branches
BranchTrueFalseTotal
if defined $err 7 0 7
$message ? : 3 4 7
149 if defined $err;
150 }
151 3 return ($message . ' in (' . $self->name . ')') ;
152 }
153  
154 3 ▶sub find_type_for {
Subroutine find_type_for: called CC=1 SCAR=0.0
Pod documented
155 3 my ($self, $value) = @_;
156  
157 3 ▶ return first { $_->check($value) } @{ $self->type_constraints };
Subroutine __ANON__: called CC=1 SCAR=0.0
158 }
159  
160 13 ▶sub is_a_type_of {
Subroutine is_a_type_of: called CC=1 SCAR=0.0
Pod documented
161 13 my ($self, $type_name) = @_;
162  
163 13 ▶ return all { $_->is_a_type_of($type_name) } @{ $self->type_constraints };
Subroutine __ANON__: called CC=1 SCAR=0.0
164 }
165  
166 13 ▶sub is_subtype_of {
Subroutine is_subtype_of: called CC=1 SCAR=0.0
Pod documented
167 13 my ($self, $type_name) = @_;
168  
169 13 ▶ return all { $_->is_subtype_of($type_name) } @{ $self->type_constraints };
Subroutine __ANON__: called CC=1 SCAR=0.0
170 }
171  
172 5 ▶sub create_child_type {
Subroutine create_child_type: called CC=3 SCAR=11.0
Pod documented
173 5 my ( $self, %opts ) = @_;
174  
175 5 my $constraint
176 = Moose::Meta::TypeConstraint->new( %opts, parent => $self );
177  
178 # if we have a type constraint union, and no
179 # type check, this means we are just aliasing
180 # the union constraint, which means we need to
181 # handle this differently.
182 # - SL
183 5 ▶ if ( not( defined $opts{constraint} )
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $opts{"constraint"} and $self->has_coercion) 1 4 5
Condition100%3/3
not defined $opts{"constraint"} and $self->has_coercion
!ll&&!rl&&r
131
MC/DC100%2/2
not defined $opts{"constraint"} and $self->has_coercion
not defined $opts{"constraint"} $self->has_coercion
Truth table100%3/3
not(A) and B
A= not defined $opts{"constraint"}B= $self->has_coercion
ABresult
0X0
100
111
184 && $self->has_coercion ) {
185 1 $constraint->coercion(
186 Moose::Meta::TypeCoercion::Union->new(
187 type_constraint => $self,
188 )
189 );
190 }
191  
192 5 return $constraint;
193 }
194  
195 398 1;
196  
197 # ABSTRACT: A union of Moose type constraints
198  
199 __END__
« blib/lib/Moose/Meta/Method/Constructor.pm ↑ Summary blib/lib/Moose/Exception/Role/EitherAttributeOrAttributeName.pm »