blib/lib/Moose/Meta/Attribute.pm

filter: Statementstmt 96.3% 499 / 518 Branchbran 86.0% 258 / 300 Conditioncond 74.4% 128 / 172 MC/DCmcdc 51.0% 51 / 100 Subroutinesub 98.7% 76 / 77 Podpod 94.1% 16 / 17 totaltotal 86.8% 1028 / 1184 CC 218 SCAR 22.9
CC
218
Cov
89%
get_value
34.8
_process_accessors
31.7
interpolate_class
16.5
CRAP 9.9
« blib/lib/Moose/Meta/Role.pm ↑ Summary blib/lib/Moose/Util.pm »
1 398 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 398 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 package Moose::Meta::Attribute;
4 398 our $VERSION = '2.4001';
5  
6 398 ▶use B ();
Subroutine BEGIN: called CC=1 SCAR=0.0
7 398 ▶use Scalar::Util 'blessed';
Subroutine BEGIN: called CC=1 SCAR=0.0
8 398 ▶use List::Util 1.33 'any';
Subroutine BEGIN: called CC=1 SCAR=0.0
9 398 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 398 ▶use overload ();
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 398 ▶use Moose::Deprecated;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 398 ▶use Moose::Meta::Method::Accessor;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 398 ▶use Moose::Meta::Method::Delegation;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 398 ▶use Moose::Util 'throw_exception';
Subroutine BEGIN: called CC=1 SCAR=0.0
16 398 ▶use Moose::Util::TypeConstraints ();
Subroutine BEGIN: called CC=1 SCAR=0.0
17 398 ▶use Class::MOP::MiniTrait;
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 398 ▶use parent 'Class::MOP::Attribute', 'Moose::Meta::Mixin::AttributeCore';
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 398 ▶use Carp 'confess';
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 398 Class::MOP::MiniTrait::apply(__PACKAGE__, 'Moose::Meta::Object::Trait');
24  
25 398 __PACKAGE__->meta->add_attribute('traits' => (
26 reader => 'applied_traits',
27 predicate => 'has_applied_traits',
28 Class::MOP::_definition_context(),
29 ));
30  
31 398 __PACKAGE__->meta->add_attribute('role_attribute' => (
32 reader => 'role_attribute',
33 predicate => 'has_role_attribute',
34 Class::MOP::_definition_context(),
35 ));
36  
37 # we need to have a ->does method in here to
38 # more easily support traits, and the introspection
39 # of those traits. We extend the does check to look
40 # for metatrait aliases.
41 217 ▶sub does {
Subroutine does: called CC=2 SCAR=6.9
Pod documented
42 217 my ($self, $role_name) = @_;
43 217 ▶ my $name = try {
Subroutine __ANON__: called CC=1 SCAR=0.0
44 217 Moose::Util::resolve_metatrait_alias(Attribute => $role_name)
45 217 };
46 217 ▶ return 0 if !defined($name); # failed to load class
Branch: 1 branch
BranchTrueFalseTotal
unless defined $name 2 215 217
47 215 return $self->Moose::Object::does($name);
48 }
49  
50 4061 ▶sub _inline_throw_exception {
Subroutine _inline_throw_exception: called CC=2 SCAR=7.2
51 4061 my ( $self, $exception_type, $throw_args ) = @_;
52 4061 ▶ return 'die Module::Runtime::use_module("Moose::Exception::' . $exception_type . '")->new(' . ($throw_args || '') . ')';
Condition50%1/2
$throw_args || ''
l!l
40610
MC/DC0%0/1
$throw_args || ''
$throw_args
Truth table50%1/2
A
A= $throw_args
Aresult
11
00
53 }
54  
55 2723 ▶sub new {
Subroutine new: called CC=8 SCAR=21.0
Pod documented
56 2723 my ($class, $name, %options) = @_;
57 2723 ▶ $class->_process_options($name, \%options) unless $options{__hack_no_process_options}; # used from clone()... YECHKKK FIXME ICKY YUCK GROSS
Branch: 1 branch
BranchTrueFalseTotal
unless $options{"__hack_no_process_options"} 2692 31 2723
58  
59 2699 delete $options{__hack_no_process_options};
60  
61 my %attrs =
62 78469 ( map { $_ => 1 }
63 78469 grep { defined }
64 2699 map { $_->init_arg() }
65 $class->meta()->get_all_attributes()
66 );
67  
68 2699 my @bad = sort grep { ! $attrs{$_} } keys %options;
69  
70 2699 ▶ if (@bad)
Branch: 1 branch
BranchTrueFalseTotal
if (@bad) 1 2698 2699
71 {
72 1 ▶ my $s = @bad > 1 ? 's' : '';
Branch: 1 branch
BranchTrueFalseTotal
@bad > 1 ? : 0 1 1
73 1 my $list = join "', '", @bad;
74  
75 1 my $package = $options{definition_context}{package};
76 my $context = $options{definition_context}{context}
77 1 ▶ || 'attribute constructor';
Condition50%1/2
$options{'definition_context'}{'context'} || 'attribute constructor'
l!l
10
MC/DC0%0/1
$options{'definition_context'}{'context'} || 'attribute constructor'
$options{'definition_context'}{'context'}
Truth table50%1/2
A
A= $options{'definition_context'}{'context'}
Aresult
11
00
78 1 ▶ my $type = $options{definition_context}{type} || 'class';
Condition50%1/2
$options{'definition_context'}{'type'} || 'class'
l!l
10
MC/DC0%0/1
$options{'definition_context'}{'type'} || 'class'
$options{'definition_context'}{'type'}
Truth table50%1/2
A
A= $options{'definition_context'}{'type'}
Aresult
11
00
79  
80 1 my $location = '';
81 1 ▶ if (defined($package)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $package) 1 0 1
82 1 $location = " in ";
83 1 ▶ $location .= "$type " if $type;
Branch: 1 branch
BranchTrueFalseTotal
if $type 1 0 1
84 1 $location .= $package;
85 }
86  
87 1 Carp::cluck "Found unknown argument$s '$list' in the $context for '$name'$location";
88 }
89  
90 2699 return $class->SUPER::new($name, %options);
91 }
92  
93 2292 ▶sub interpolate_class_and_new {
Subroutine interpolate_class_and_new: called CC=3 SCAR=11.0
Pod documented
94 2292 my $class = shift;
95 2292 my $name = shift;
96  
97 2292 ▶ throw_exception( MustPassEvenNumberOfAttributeOptions => attribute_name => $name,
Branch: 1 branch
BranchTrueFalseTotal
if @_ % 2 == 1 2 2290 2292
98 options => \@_
99 )
100 if @_ % 2 == 1;
101  
102 2290 my %args = @_;
103  
104 2290 my ( $new_class, @traits ) = $class->interpolate_class(\%args);
105 2288 ▶ $new_class->new($name, %args, ( scalar(@traits) ? ( traits => \@traits ) : () ) );
Branch: 1 branch
BranchTrueFalseTotal
scalar @traits ? : 200 2088 2288
106 }
107  
108 2330 ▶sub interpolate_class {
Subroutine interpolate_class: called CC=16 SCAR=28.0
Pod undocumented
109 2330 my ($class, $options) = @_;
110  
111 2330 ▶ $class = ref($class) || $class;
Condition66%2/3
ref $class || $class
l!l&&r!l&&!r
3122990
MC/DC0%0/2
ref $class || $class
ref $class $class
Truth table66%2/3
A || B
A= ref $classB= $class
ABresult
1X1
011
000
112  
113 2330 ▶ if ( my $metaclass_name = delete $options->{metaclass} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $metaclass_name = delete $options->{"metaclass"}) 10 2320 2330
114 10 my $new_class = Moose::Util::resolve_metaclass_alias( Attribute => $metaclass_name );
115  
116 10 ▶ if ( $class ne $new_class ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($class ne $new_class) 10 0 10
117 10 ▶ if ( $new_class->can("interpolate_class") ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($new_class->can('interpolate_class')) { } 9 1 10
118 9 return $new_class->interpolate_class($options);
119 } else {
120 1 $class = $new_class;
121 }
122 }
123 }
124  
125 2321 my @traits;
126  
127 2321 ▶ if (my $traits = $options->{traits}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $traits = $options->{"traits"}) 203 2118 2321
128 203 my $i = 0;
129 203 my $has_foreign_options = 0;
130  
131 203 ▶ while ($i < @$traits) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @$traits) 212 201 413
132 212 my $trait = $traits->[$i++];
133 212 ▶ next if ref($trait); # options to a trait we discarded
Branch: 1 branch
BranchTrueFalseTotal
if ref $trait 0 212 212
134  
135 212 ▶ $trait = Moose::Util::resolve_metatrait_alias(Attribute => $trait)
Condition33%1/3
Moose::Util::resolve_metatrait_alias('Attribute', $trait) || $trait
l!l&&r!l&&!r
21000
MC/DC0%0/2
Moose::Util::resolve_metatrait_alias('Attribute', $trait) || $trait
Moose::Util::resolve_metatrait_alias('Attribute', $trait) $trait
Truth table33%1/3
A || B
A= Moose::Util::resolve_metatrait_alias('Attribute', $trait)B= $trait
ABresult
1X1
011
000
136 || $trait;
137  
138 210 ▶ next if $class->does($trait);
Branch: 1 branch
BranchTrueFalseTotal
if $class->does($trait) 0 210 210
139  
140 210 push @traits, $trait;
141  
142 # are there options?
143 210 ▶ if ($traits->[$i] && ref($traits->[$i])) {
Branch: 1 branch
BranchTrueFalseTotal
if ($traits->[$i] and ref $traits->[$i]) 5 205 210
Condition100%3/3
$traits->[$i] and ref $traits->[$i]
!ll&&!rl&&r
19695
MC/DC100%2/2
$traits->[$i] and ref $traits->[$i]
$traits->[$i] ref $traits->[$i]
Truth table100%3/3
A and B
A= $traits->[$i]B= ref $traits->[$i]
ABresult
0X0
100
111
144 $has_foreign_options = 1
145 5 ▶ if any { $_ ne '-alias' && $_ ne '-excludes' } keys %{ $traits->[$i] };
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 2 branches
BranchTrueFalseTotal
if &any(sub { $_ ne "-alias" and $_ ne "-excludes"; } , keys %{$$traits[$i];}) 1 4 5
$_ ne "-alias" and $_ ne "-excludes" 3 3 6
146  
147 5 push @traits, $traits->[$i++];
148 }
149 }
150  
151 201 ▶ if (@traits) {
Branch: 1 branch
BranchTrueFalseTotal
if (@traits) 201 0 201
152 201 my %options = (
153 superclasses => [ $class ],
154 roles => [ @traits ],
155 );
156  
157 201 ▶ if ($has_foreign_options) {
Branch: 1 branch
BranchTrueFalseTotal
if ($has_foreign_options) { } 1 200 201
158 1 $options{weaken} = 0;
159 }
160 else {
161 200 $options{cache} = 1;
162 }
163  
164 201 my $anon_class = Moose::Meta::Class->create_anon_class(%options);
165 201 $class = $anon_class->name;
166 }
167 }
168  
169 2319 ▶ return ( wantarray ? ( $class, @traits ) : $class );
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 2319 0 2319
170 }
171  
172 # ...
173  
174 # method-generating options shouldn't be overridden
175 38 ▶sub illegal_options_for_inheritance {
Subroutine illegal_options_for_inheritance: called CC=1 SCAR=0.0
Pod documented
176 38 qw(reader writer accessor clearer predicate)
177 }
178  
179 # NOTE/TODO
180 # This method *must* be able to handle
181 # Class::MOP::Attribute instances as
182 # well. Yes, I know that is wrong, but
183 # apparently we didn't realize it was
184 # doing that and now we have some code
185 # which is dependent on it. The real
186 # solution of course is to push this
187 # feature back up into Class::MOP::Attribute
188 # but I not right now, I am too lazy.
189 # However if you are reading this and
190 # looking for something to do,.. please
191 # be my guest.
192 # - stevan
193 37 ▶sub clone_and_inherit_options {
Subroutine clone_and_inherit_options: called CC=9 SCAR=22.1
Pod documented
194 37 my ($self, %options) = @_;
195  
196 # NOTE:
197 # we may want to extends a Class::MOP::Attribute
198 # in which case we need to be able to use the
199 # core set of legal options that have always
200 # been here. But we allows Moose::Meta::Attribute
201 # instances to changes them.
202 # - SL
203 37 ▶ my @illegal_options = $self->can('illegal_options_for_inheritance')
Branch: 1 branch
BranchTrueFalseTotal
$self->can('illegal_options_for_inheritance') ? : 37 0 37
204 ? $self->illegal_options_for_inheritance
205 : ();
206  
207 37 ▶ my @found_illegal_options = grep { exists $options{$_} && exists $self->{$_} ? $_ : undef } @illegal_options;
Branch: 1 branch
BranchTrueFalseTotal
exists $options{$_} && exists $self->{$_} ? : 8 180 188
Condition100%3/3
exists $options{$_} and exists $self->{$_}
!ll&&!rl&&r
17738
MC/DC100%2/2
exists $options{$_} and exists $self->{$_}
exists $options{$_} exists $self->{$_}
Truth table100%3/3
A and B
A= exists $options{$_}B= exists $self->{$_}
ABresult
0X0
100
111
208 37 ▶ (scalar @found_illegal_options == 0)
Branch: 1 branch
BranchTrueFalseTotal
scalar @found_illegal_options == 0 or throw_exception("IllegalInheritedOptions", "illegal_options", \@found_illegal_options, "params", \%options) 31 6 37
209 || throw_exception( IllegalInheritedOptions => illegal_options => \@found_illegal_options,
210 params => \%options
211 );
212  
213 31 $self->_process_isa_option( $self->name, \%options );
214 31 $self->_process_does_option( $self->name, \%options );
215  
216 # NOTE:
217 # this doesn't apply to Class::MOP::Attributes,
218 # so we can ignore it for them.
219 # - SL
220 31 ▶ if ($self->can('interpolate_class')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->can("interpolate_class")) 31 0 31
221 31 ( $options{metaclass}, my @traits ) = $self->interpolate_class(\%options);
222  
223 31 my %seen;
224 31 ▶ my @all_traits = grep { $seen{$_}++ } @{ $self->applied_traits || [] }, @traits;
Condition100%2/2
$self->applied_traits || []
l!l
229
MC/DC100%1/1
$self->applied_traits || []
$self->applied_traits
Truth table100%2/2
A
A= $self->applied_traits
Aresult
11
00
225 31 ▶ $options{traits} = \@all_traits if @all_traits;
Branch: 1 branch
BranchTrueFalseTotal
if @all_traits 0 31 31
226 }
227  
228 # This method can be called on a CMOP::Attribute object, so we need to
229 # make sure we can call this method.
230 31 ▶ $self->_process_lazy_build_option( $self->name, \%options )
Branch: 1 branch
BranchTrueFalseTotal
if $self->can("_process_lazy_build_option") 31 0 31
231 if $self->can('_process_lazy_build_option');
232  
233 31 $self->clone(%options);
234 }
235  
236 31 ▶sub clone {
Subroutine clone: called CC=5 SCAR=16.6
Pod documented
237 31 my ( $self, %params ) = @_;
238  
239 31 ▶ my $class = delete $params{metaclass} || ref $self;
Condition33%1/3
delete $params{'metaclass'} || ref $self
l!l&&r!l&&!r
3100
MC/DC0%0/2
delete $params{'metaclass'} || ref $self
delete $params{'metaclass'} ref $self
Truth table33%1/3
A || B
A= delete $params{'metaclass'}B= ref $self
ABresult
1X1
011
000
240  
241 31 my ( @init, @non_init );
242  
243 31 foreach my $attr ( grep { $_->has_value($self) } Class::MOP::class_of($self)->get_all_attributes ) {
244 309 ▶ push @{ $attr->has_init_arg ? \@init : \@non_init }, $attr;
Branch: 1 branch
BranchTrueFalseTotal
$attr->has_init_arg ? : 309 0 309
245 }
246  
247 31 my %new_params = ( ( map { $_->init_arg => $_->get_value($self) } @init ), %params );
248  
249 31 my $name = delete $new_params{name};
250  
251 31 my $clone = $class->new($name, %new_params, __hack_no_process_options => 1 );
252  
253 31 foreach my $attr ( @non_init ) {
254 0 $attr->set_value($clone, $attr->get_value($self));
255 }
256  
257 31 return $clone;
258 }
259  
260 2691 ▶sub _process_options {
Subroutine _process_options: called CC=1 SCAR=0.0
261 2691 my ( $class, $name, $options ) = @_;
262  
263 2691 $class->_process_is_option( $name, $options );
264 2688 $class->_process_isa_option( $name, $options );
265 2683 $class->_process_does_option( $name, $options );
266 2683 $class->_process_coerce_option( $name, $options );
267 2677 $class->_process_trigger_option( $name, $options );
268 2674 $class->_process_auto_deref_option( $name, $options );
269 2672 $class->_process_lazy_build_option( $name, $options );
270 2671 $class->_process_lazy_option( $name, $options );
271 2669 $class->_process_required_option( $name, $options );
272 }
273  
274 2691 ▶sub _process_is_option {
Subroutine _process_is_option: called CC=10 SCAR=23.1
275 2691 my ( $class, $name, $options ) = @_;
276  
277 2691 ▶ return unless $options->{is};
Branch: 1 branch
BranchTrueFalseTotal
unless $options->{"is"} 515 2176 2691
278  
279 ### -------------------------
280 ## is => ro, writer => _foo # turns into (reader => foo, writer => _foo) as before
281 ## is => rw, writer => _foo # turns into (reader => foo, writer => _foo)
282 ## is => rw, accessor => _foo # turns into (accessor => _foo)
283 ## is => ro, accessor => _foo # error, accesor is rw
284 ### -------------------------
285  
286 2176 ▶ if ( $options->{is} eq 'ro' ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($options->{'is'} eq 'ro') { } 1879 297 2176
elsif ($options->{'is'} eq 'rw') { } 270 27 297
elsif ($options->{'is'} eq 'bare') { } 26 1 27
287 throw_exception("AccessorMustReadWrite" => attribute_name => $name,
288 params => $options,
289 )
290 1879 ▶ if exists $options->{accessor};
Branch: 1 branch
BranchTrueFalseTotal
if exists $options->{"accessor"} 2 1877 1879
291 1877 ▶ $options->{reader} ||= $name;
Condition66%2/3
$options->{"reader"} ||= $name
l!l&&r!l&&!r
218750
MC/DC0%0/2
$options->{"reader"} ||= $name
$options->{"reader"} $name
Truth table66%2/3
A ||= B
A= $options->{"reader"}B= $name
ABresult
1X1
011
000
292 }
293 elsif ( $options->{is} eq 'rw' ) {
294 270 ▶ if ( ! $options->{accessor} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $options->{"accessor"}) 267 3 270
295 267 ▶ if ( $options->{writer}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options->{'writer'}) { } 1 266 267
296 1 ▶ $options->{reader} ||= $name;
Condition33%1/3
$options->{"reader"} ||= $name
l!l&&r!l&&!r
010
MC/DC0%0/2
$options->{"reader"} ||= $name
$options->{"reader"} $name
Truth table33%1/3
A ||= B
A= $options->{"reader"}B= $name
ABresult
1X1
011
000
297 }
298 else {
299 266 $options->{accessor} = $name;
300 }
301 }
302 }
303 elsif ( $options->{is} eq 'bare' ) {
304 26 return;
305 # do nothing, but don't complain (later) about missing methods
306 }
307 else {
308 1 throw_exception( InvalidValueForIs => attribute_name => $name,
309 params => $options,
310 );
311 }
312 }
313  
314 2719 ▶sub _process_isa_option {
Subroutine _process_isa_option: called CC=7 SCAR=19.5
315 2719 my ( $class, $name, $options ) = @_;
316  
317 2719 ▶ return unless exists $options->{isa};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $options->{"isa"} 691 2028 2719
318  
319 2028 ▶ if ( exists $options->{does} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $options->{"does"}) 5 2023 2028
320 5 ▶ if ( try { $options->{isa}->can('does') } ) {
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
if (&try(sub { $options->{'isa'}->can('does'); } )) { } 3 2 5
321 3 ▶ ( $options->{isa}->does( $options->{does} ) )
Branch: 1 branch
BranchTrueFalseTotal
$options->{"isa"}->does($options->{"does"}) or throw_exception("IsaDoesNotDoTheRole", "attribute_name", $name, "params", $options) 1 2 3
322 || throw_exception( IsaDoesNotDoTheRole => attribute_name => $name,
323 params => $options,
324 );
325 }
326 else {
327 2 throw_exception( IsaLacksDoesMethod => attribute_name => $name,
328 params => $options,
329 );
330 }
331 }
332  
333 # allow for anon-subtypes here ...
334 #
335 # There are a _lot_ of methods that we expect from TC objects, but
336 # checking for a specific parent class via ->isa is gross, so we'll check
337 # for at least one method.
338 2024 ▶ if ( blessed( $options->{isa} )
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($options->{'isa'}) and $options->{'isa'}->can('has_coercion')) { } 84 1940 2024
Condition66%2/3
builtin::blessed($options->{"isa"}) and $options->{"isa"}->can("has_coercion")
!ll&&!rl&&r
1940084
MC/DC50%1/2
builtin::blessed($options->{"isa"}) and $options->{"isa"}->can("has_coercion")
builtin::blessed($options->{"isa"}) $options->{"isa"}->can("has_coercion")
Truth table66%2/3
A and B
A= builtin::blessed($options->{"isa"})B= $options->{"isa"}->can("has_coercion")
ABresult
0X0
100
111
339 && $options->{isa}->can('has_coercion') ) {
340  
341 84 $options->{type_constraint} = $options->{isa};
342 }
343 else {
344 $options->{type_constraint}
345 = Moose::Util::TypeConstraints::find_or_create_isa_type_constraint(
346 $options->{isa},
347 { package_defined_in => $options->{definition_context}->{package} }
348 1940 );
349 }
350 }
351  
352 2714 ▶sub _process_does_option {
Subroutine _process_does_option: called CC=5 SCAR=16.1
353 2714 my ( $class, $name, $options ) = @_;
354  
355 2714 ▶ return unless exists $options->{does} && ! exists $options->{isa};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $options->{"does"} and not exists $options->{"isa"} 2705 9 2714
Condition100%3/3
exists $options->{"does"} and not exists $options->{"isa"}
!ll&&!rl&&r
270419
MC/DC100%2/2
exists $options->{"does"} and not exists $options->{"isa"}
exists $options->{"does"} not exists $options->{"isa"}
Truth table100%3/3
A and not(B)
A= exists $options->{"does"}B= not exists $options->{"isa"}
ABresult
0X0
100
111
356  
357 # allow for anon-subtypes here ...
358 9 ▶ if ( blessed( $options->{does} )
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($options->{'does'}) and $options->{'does'}->can('has_coercion')) { } 1 8 9
Condition66%2/3
builtin::blessed($options->{"does"}) and $options->{"does"}->can("has_coercion")
!ll&&!rl&&r
801
MC/DC50%1/2
builtin::blessed($options->{"does"}) and $options->{"does"}->can("has_coercion")
builtin::blessed($options->{"does"}) $options->{"does"}->can("has_coercion")
Truth table66%2/3
A and B
A= builtin::blessed($options->{"does"})B= $options->{"does"}->can("has_coercion")
ABresult
0X0
100
111
359 && $options->{does}->can('has_coercion') ) {
360  
361 1 $options->{type_constraint} = $options->{does};
362 }
363 else {
364 $options->{type_constraint}
365 = Moose::Util::TypeConstraints::find_or_create_does_type_constraint(
366 $options->{does},
367 { package_defined_in => $options->{definition_context}->{package} }
368 8 );
369 }
370 }
371  
372 2683 ▶sub _process_coerce_option {
Subroutine _process_coerce_option: called CC=5 SCAR=16.1
373 2683 my ( $class, $name, $options ) = @_;
374  
375 2683 ▶ return unless $options->{coerce};
Branch: 1 branch
BranchTrueFalseTotal
unless $options->{"coerce"} 2642 41 2683
376  
377 ( exists $options->{type_constraint} )
378 41 ▶ || throw_exception( CoercionNeedsTypeConstraint => attribute_name => $name,
Branch: 1 branch
BranchTrueFalseTotal
exists $options->{"type_constraint"} or throw_exception("CoercionNeedsTypeConstraint", "attribute_name", $name, "params", $options) 40 1 41
379 params => $options,
380 );
381  
382 throw_exception( CannotCoerceAWeakRef => attribute_name => $name,
383 params => $options,
384 )
385 40 ▶ if $options->{weak_ref};
Branch: 1 branch
BranchTrueFalseTotal
if $options->{"weak_ref"} 1 39 40
386  
387 39 ▶ unless ( $options->{type_constraint}->has_coercion ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($options->{"type_constraint"}->has_coercion) 4 35 39
388 4 my $type = $options->{type_constraint}->name;
389  
390 4 throw_exception( CannotCoerceAttributeWhichHasNoCoercion => attribute_name => $name,
391 type_name => $type,
392 params => $options
393 );
394 }
395 }
396  
397 2677 ▶sub _process_trigger_option {
Subroutine _process_trigger_option: called CC=3 SCAR=11.0
398 2677 my ( $class, $name, $options ) = @_;
399  
400 2677 ▶ return unless exists $options->{trigger};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $options->{"trigger"} 2648 29 2677
401  
402 ( 'CODE' eq ref $options->{trigger} )
403 29 ▶ || throw_exception( TriggerMustBeACodeRef => attribute_name => $name,
Branch: 1 branch
BranchTrueFalseTotal
"CODE" eq ref $options->{"trigger"} or throw_exception("TriggerMustBeACodeRef", "attribute_name", $name, "params", $options) 26 3 29
404 params => $options,
405 );
406 }
407  
408 2674 ▶sub _process_auto_deref_option {
Subroutine _process_auto_deref_option: called CC=5 SCAR=16.1
409 2674 my ( $class, $name, $options ) = @_;
410  
411 2674 ▶ return unless $options->{auto_deref};
Branch: 1 branch
BranchTrueFalseTotal
unless $options->{"auto_deref"} 2661 13 2674
412  
413 ( exists $options->{type_constraint} )
414 13 ▶ || throw_exception( CannotAutoDerefWithoutIsa => attribute_name => $name,
Branch: 1 branch
BranchTrueFalseTotal
exists $options->{"type_constraint"} or throw_exception("CannotAutoDerefWithoutIsa", "attribute_name", $name, "params", $options) 12 1 13
415 params => $options,
416 );
417  
418 ( $options->{type_constraint}->is_a_type_of('ArrayRef')
419 12 ▶ || $options->{type_constraint}->is_a_type_of('HashRef') )
Branch: 1 branch
BranchTrueFalseTotal
$options->{"type_constraint"}->is_a_type_of("ArrayRef") or $options->{"type_constraint"}->is_a_type_of("HashRef") or throw_exception("AutoDeRefNeedsArrayRefOrHashRef", "attribute_name", $name, "params", $options) 11 1 12
Condition100%3/3
$options->{"type_constraint"}->is_a_type_of("ArrayRef") or $options->{"type_constraint"}->is_a_type_of("HashRef")
l!l&&r!l&&!r
831
MC/DC100%2/2
$options->{"type_constraint"}->is_a_type_of("ArrayRef") or $options->{"type_constraint"}->is_a_type_of("HashRef")
$options->{"type_constraint"}->is_a_type_of("ArrayRef") $options->{"type_constraint"}->is_a_type_of("HashRef")
Truth table100%3/3
A or B
A= $options->{"type_constraint"}->is_a_type_of("ArrayRef")B= $options->{"type_constraint"}->is_a_type_of("HashRef")
ABresult
1X1
011
000
420 || throw_exception( AutoDeRefNeedsArrayRefOrHashRef => attribute_name => $name,
421 params => $options,
422 );
423 }
424  
425 2703 ▶sub _process_lazy_build_option {
Subroutine _process_lazy_build_option: called CC=9 SCAR=22.6
426 2703 my ( $class, $name, $options ) = @_;
427  
428 2703 ▶ return unless $options->{lazy_build};
Branch: 1 branch
BranchTrueFalseTotal
unless $options->{"lazy_build"} 2679 24 2703
429  
430 throw_exception( CannotUseLazyBuildAndDefaultSimultaneously => attribute_name => $name,
431 params => $options,
432 )
433 24 ▶ if exists $options->{default};
Branch: 1 branch
BranchTrueFalseTotal
if exists $options->{"default"} 1 23 24
434  
435 23 $options->{lazy} = 1;
436 23 ▶ $options->{builder} ||= "_build_${name}";
Condition50%1/2
$options->{"builder"} ||= "_build_$name"
l!l
023
MC/DC0%0/1
$options->{"builder"} ||= "_build_$name"
$options->{"builder"}
Truth table50%1/2
A
A= $options->{"builder"}
Aresult
11
00
437  
438 23 ▶ if ( $name =~ /^_/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($name =~ /^_/) { } 1 22 23
439 1 ▶ $options->{clearer} ||= "_clear${name}";
Condition50%1/2
$options->{"clearer"} ||= "_clear$name"
l!l
01
MC/DC0%0/1
$options->{"clearer"} ||= "_clear$name"
$options->{"clearer"}
Truth table50%1/2
A
A= $options->{"clearer"}
Aresult
11
00
440 1 ▶ $options->{predicate} ||= "_has${name}";
Condition50%1/2
$options->{"predicate"} ||= "_has$name"
l!l
01
MC/DC0%0/1
$options->{"predicate"} ||= "_has$name"
$options->{"predicate"}
Truth table50%1/2
A
A= $options->{"predicate"}
Aresult
11
00
441 }
442 else {
443 22 ▶ $options->{clearer} ||= "clear_${name}";
Condition50%1/2
$options->{"clearer"} ||= "clear_$name"
l!l
022
MC/DC0%0/1
$options->{"clearer"} ||= "clear_$name"
$options->{"clearer"}
Truth table50%1/2
A
A= $options->{"clearer"}
Aresult
11
00
444 22 ▶ $options->{predicate} ||= "has_${name}";
Condition50%1/2
$options->{"predicate"} ||= "has_$name"
l!l
022
MC/DC0%0/1
$options->{"predicate"} ||= "has_$name"
$options->{"predicate"}
Truth table50%1/2
A
A= $options->{"predicate"}
Aresult
11
00
445 }
446 }
447  
448 2671 ▶sub _process_lazy_option {
Subroutine _process_lazy_option: called CC=4 SCAR=13.9
449 2671 my ( $class, $name, $options ) = @_;
450  
451 2671 ▶ return unless $options->{lazy};
Branch: 1 branch
BranchTrueFalseTotal
unless $options->{"lazy"} 2148 523 2671
452  
453 ( exists $options->{default} || defined $options->{builder} )
454 523 ▶ || throw_exception( LazyAttributeNeedsADefault => params => $options,
Branch: 1 branch
BranchTrueFalseTotal
exists $options->{"default"} or defined $options->{"builder"} or throw_exception("LazyAttributeNeedsADefault", "params", $options, "attribute_name", $name) 521 2 523
Condition100%3/3
exists $options->{"default"} or defined $options->{"builder"}
l!l&&r!l&&!r
624592
MC/DC100%2/2
exists $options->{"default"} or defined $options->{"builder"}
exists $options->{"default"} defined $options->{"builder"}
Truth table100%3/3
A or B
A= exists $options->{"default"}B= defined $options->{"builder"}
ABresult
1X1
011
000
455 attribute_name => $name,
456 );
457 }
458  
459 2669 ▶sub _process_required_option {
Subroutine _process_required_option: called CC=6 SCAR=18.0
460 2669 my ( $class, $name, $options ) = @_;
461  
462 2669 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if ($options->{"required"} and not !exists $options->{"init_arg"} || defined $options->{"init_arg"} || exists $options->{"default"} || defined $options->{"builder"}) 1 2668 2669
Condition83%10/12
$options->{"required"} and not !exists $options->{"init_arg"} || defined $options->{"init_arg"} || exists $options->{"default"} || defined $options->{"builder"}
!ll&&!rl&&r
16929761
!exists $options->{'init_arg'} || defined $options->{'init_arg'} || exists $options->{'default'} || defined $options->{'builder'}
l!l&&r!l&&!r
97601
not exists $options->{"init_arg"} or defined $options->{"init_arg"} or exists $options->{"default"}
l!l&&r!l&&!r
97601
not exists $options->{"init_arg"} or defined $options->{"init_arg"}
l!l&&r!l&&!r
97511
MC/DC60%3/5
$options->{"required"} and not !exists $options->{"init_arg"} || defined $options->{"init_arg"} || exists $options->{"default"} || defined $options->{"builder"}
$options->{"required"} not exists $options->{"init_arg"} defined $options->{"init_arg"} exists $options->{"default"} defined $options->{'builder'}
Truth table66%4/6
A and not(not(B) or C or D || E)
A= $options->{"required"}B= not exists $options->{"init_arg"}C= defined $options->{"init_arg"}D= exists $options->{"default"}E= defined $options->{'builder'}
ABCDEresult
0XXXX0
11XXX0
101XX0
1001X0
100010
100001
463 $options->{required}
464 && !(
465 ( !exists $options->{init_arg} || defined $options->{init_arg} )
466 || exists $options->{default}
467 || defined $options->{builder}
468 )
469 ) {
470 1 throw_exception( RequiredAttributeNeedsADefault => params => $options,
471 attribute_name => $name,
472 );
473 }
474 }
475  
476 6178 ▶sub initialize_instance_slot {
Subroutine initialize_instance_slot: called CC=13 SCAR=25.7
Pod documented
477 6178 my ($self, $meta_instance, $instance, $params) = @_;
478 6178 my $init_arg = $self->init_arg();
479 # try to fetch the init arg from the %params ...
480  
481 6178 my $val;
482 my $value_is_set;
483 6178 ▶ if ( defined($init_arg) and exists $params->{$init_arg}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $init_arg and exists $params->{$init_arg}) { } 1937 4241 6178
Condition100%3/3
defined $init_arg and exists $params->{$init_arg}
!ll&&!rl&&r
342381937
MC/DC100%2/2
defined $init_arg and exists $params->{$init_arg}
defined $init_arg exists $params->{$init_arg}
Truth table100%3/3
A and B
A= defined $init_argB= exists $params->{$init_arg}
ABresult
0X0
100
111
484 1937 $val = $params->{$init_arg};
485 1937 $value_is_set = 1;
486 }
487 else {
488 # skip it if it's lazy
489 4241 ▶ return if $self->is_lazy;
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_lazy 306 3935 4241
490 # and die if it's required and doesn't have a default value
491 3935 my $class_name = blessed( $instance );
492 3935 ▶ throw_exception(
Branch: 2 branches
BranchTrueFalseTotal
if $self->is_required and not $self->has_default and not $self->has_builder 52 3883 3935
defined $init_arg ? : 52 0 52
Condition100%6/6
$self->is_required and not $self->has_default and not $self->has_builder
!ll&&!rl&&r
3878552
$self->is_required and not $self->has_default
!ll&&!rl&&r
3873557
MC/DC100%3/3
$self->is_required and not $self->has_default and not $self->has_builder
$self->is_required not $self->has_default not $self->has_builder
Truth table100%4/4
A and not(B) and not(C)
A= $self->is_requiredB= not $self->has_defaultC= not $self->has_builder
ABCresult
0XX0
10X0
1100
1111
493 'AttributeIsRequired',
494 attribute_name => $self->name,
495 ( defined $init_arg ? ( attribute_init_arg => $init_arg ) : () ),
496 class_name => $class_name,
497 params => $params,
498 )
499 if $self->is_required
500 && !$self->has_default
501 && !$self->has_builder;
502  
503 # if nothing was in the %params, we can use the
504 # attribute's default value (if it has one)
505 3883 ▶ if ($self->has_default) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->has_default) { } 2287 1596 3883
elsif ($self->has_builder) { } 15 1581 1596
506 2287 $val = $self->default($instance);
507 2287 $value_is_set = 1;
508 }
509 elsif ($self->has_builder) {
510 15 $val = $self->_call_builder($instance);
511 13 $value_is_set = 1;
512 }
513 }
514  
515 5818 ▶ return unless $value_is_set;
Branch: 1 branch
BranchTrueFalseTotal
unless $value_is_set 1581 4237 5818
516  
517 4237 $val = $self->_coerce_and_verify( $val, $instance );
518  
519 3293 $self->set_initial_value($instance, $val);
520  
521 3293 ▶ if ( ref $val && $self->is_weak_ref ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $val and $self->is_weak_ref) 47 3246 3293
Condition100%3/3
ref $val and $self->is_weak_ref
!ll&&!rl&&r
685256147
MC/DC100%2/2
ref $val and $self->is_weak_ref
ref $val $self->is_weak_ref
Truth table100%3/3
A and B
A= ref $valB= $self->is_weak_ref
ABresult
0X0
100
111
522 47 $self->_weaken_value($instance);
523 }
524 }
525  
526 15 ▶sub _call_builder {
Subroutine _call_builder: called CC=2 SCAR=6.9
527 15 my ( $self, $instance ) = @_;
528  
529 15 my $builder = $self->builder();
530  
531 15 ▶ return $instance->$builder()
Branch: 1 branch
BranchTrueFalseTotal
if $instance->can($self->builder) 13 2 15
532 if $instance->can( $self->builder );
533  
534 2 throw_exception( BuilderDoesNotExist => instance => $instance,
535 attribute => $self,
536 );
537 }
538  
539 ## Slot management
540  
541 9 ▶sub _make_initializer_writer_callback {
Subroutine _make_initializer_writer_callback: called CC=1 SCAR=0.0
542 9 my $self = shift;
543 9 my ($meta_instance, $instance, $slot_name) = @_;
544 9 my $old_callback = $self->SUPER::_make_initializer_writer_callback(@_);
545 9 ▶ return sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
546 9 $old_callback->($self->_coerce_and_verify($_[0], $instance));
547 9 };
548 }
549  
550 53 ▶sub set_value {
Subroutine set_value: called CC=9 SCAR=22.5
Pod documented
551 53 my ($self, $instance, @args) = @_;
552 53 my $value = $args[0];
553  
554 53 my $class_name = blessed( $instance );
555 53 ▶ if ($self->is_required and not @args) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->is_required and not @args) 1 52 53
Condition66%2/3
$self->is_required and not @args
!ll&&!rl&&r
5201
MC/DC50%1/2
$self->is_required and not @args
$self->is_required not @args
Truth table66%2/3
A and not(B)
A= $self->is_requiredB= not @args
ABresult
0X0
100
111
556 1 ▶ throw_exception(
Branch: 1 branch
BranchTrueFalseTotal
defined $self->init_arg ? : 1 0 1
557 'AttributeIsRequired',
558 attribute_name => $self->name,
559 (
560 defined $self->init_arg
561 ? ( attribute_init_arg => $self->init_arg )
562 : ()
563 ),
564 class_name => $class_name,
565 );
566 }
567  
568 52 $value = $self->_coerce_and_verify( $value, $instance );
569  
570 52 my @old;
571 52 ▶ if ( $self->has_trigger && $self->has_value($instance) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_trigger and $self->has_value($instance)) 1 51 52
Condition100%3/3
$self->has_trigger and $self->has_value($instance)
!ll&&!rl&&r
5011
MC/DC100%2/2
$self->has_trigger and $self->has_value($instance)
$self->has_trigger $self->has_value($instance)
Truth table100%3/3
A and B
A= $self->has_triggerB= $self->has_value($instance)
ABresult
0X0
100
111
572 1 @old = $self->get_value($instance, 'for trigger');
573 }
574  
575 52 $self->SUPER::set_value($instance, $value);
576  
577 52 ▶ if ( ref $value && $self->is_weak_ref ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $value and $self->is_weak_ref) 0 52 52
Condition33%1/3
ref $value and $self->is_weak_ref
!ll&&!rl&&r
5200
MC/DC0%0/2
ref $value and $self->is_weak_ref
ref $value $self->is_weak_ref
Truth table33%1/3
A and B
A= ref $valueB= $self->is_weak_ref
ABresult
0X0
100
111
578 0 $self->_weaken_value($instance);
579 }
580  
581 52 ▶ if ($self->has_trigger) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_trigger) 2 50 52
582 2 $self->trigger->($instance, $value, @old);
583 }
584 }
585  
586 3631 ▶sub _inline_set_value {
Subroutine _inline_set_value: called CC=8 SCAR=20.8
587 3631 my $self = shift;
588 3631 my ($instance, $value, $tc, $coercion, $message, $for_constructor) = @_;
589  
590 3631 my $old = '@old';
591 3631 my $copy = '$val';
592 3631 ▶ $tc ||= '$type_constraint';
Condition100%2/2
$tc ||= "\$type_constraint"
l!l
25641067
MC/DC100%1/1
$tc ||= "\$type_constraint"
$tc
Truth table100%2/2
A
A= $tc
Aresult
11
00
593 3631 ▶ $coercion ||= '$type_coercion';
Condition100%2/2
$coercion ||= "\$type_coercion"
l!l
25641067
MC/DC100%1/1
$coercion ||= "\$type_coercion"
$coercion
Truth table100%2/2
A
A= $coercion
Aresult
11
00
594 3631 ▶ $message ||= '$type_message';
Condition100%2/2
$message ||= "\$type_message"
l!l
25641067
MC/DC100%1/1
$message ||= "\$type_message"
$message
Truth table100%2/2
A
A= $message
Aresult
11
00
595  
596 3631 my @code;
597 3631 ▶ if ($self->_writer_value_needs_copy) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_writer_value_needs_copy) 42 3589 3631
598 42 push @code, $self->_inline_copy_value($value, $copy);
599 42 $value = $copy;
600 }
601  
602 # constructors already handle required checks
603 3631 ▶ push @code, $self->_inline_check_required
Branch: 1 branch
BranchTrueFalseTotal
unless $for_constructor 1067 2564 3631
604 unless $for_constructor;
605  
606 3631 push @code, $self->_inline_tc_code($value, $tc, $coercion, $message);
607  
608 # constructors do triggers all at once at the end
609 3631 ▶ push @code, $self->_inline_get_old_value_for_trigger($instance, $old)
Branch: 1 branch
BranchTrueFalseTotal
unless $for_constructor 1067 2564 3631
610 unless $for_constructor;
611  
612 3631 push @code, (
613 $self->SUPER::_inline_set_value($instance, $value),
614 $self->_inline_weaken_value($instance, $value),
615 );
616  
617 # constructors do triggers all at once at the end
618 3631 ▶ push @code, $self->_inline_trigger($instance, $value, $old)
Branch: 1 branch
BranchTrueFalseTotal
unless $for_constructor 1067 2564 3631
619 unless $for_constructor;
620  
621 3631 return @code;
622 }
623  
624 4345 ▶sub _writer_value_needs_copy {
Subroutine _writer_value_needs_copy: called CC=1 SCAR=0.0
625 4345 my $self = shift;
626 4345 return $self->should_coerce;
627 }
628  
629 42 ▶sub _inline_copy_value {
Subroutine _inline_copy_value: called CC=1 SCAR=0.0
630 42 my $self = shift;
631 42 my ($value, $copy) = @_;
632  
633 42 return 'my ' . $copy . ' = ' . $value . ';'
634 }
635  
636 1067 ▶sub _inline_check_required {
Subroutine _inline_check_required: called CC=3 SCAR=11.0
637 1067 my $self = shift;
638  
639 1067 ▶ return unless $self->is_required;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->is_required 1054 13 1067
640  
641 13 my $throw_params = sprintf( <<'EOF', quotemeta( $self->name ) );
642 attribute_name => "%s",
643 class_name => $class_name,
644 EOF
645 13 ▶ $throw_params .= sprintf(
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->init_arg 13 0 13
646 'attribute_init_arg => "%s",',
647 quotemeta( $self->init_arg )
648 ) if defined $self->init_arg;
649  
650 13 my $throw = $self->_inline_throw_exception(
651 'AttributeIsRequired',
652 $throw_params
653 );
654  
655 13 return sprintf( <<'EOF', $throw );
656 if ( @_ < 2 ) {
657 %s;
658 }
659 EOF
660 }
661  
662 3745 ▶sub _inline_tc_code {
Subroutine _inline_tc_code: called CC=1 SCAR=0.0
663 3745 my $self = shift;
664 3745 my ($value, $tc, $coercion, $message, $is_lazy) = @_;
665 return (
666 3745 $self->_inline_check_coercion(
667 $value, $tc, $coercion, $is_lazy,
668 ),
669 $self->_inline_check_constraint(
670 $value, $tc, $message, $is_lazy,
671 ),
672 );
673 }
674  
675 4479 ▶sub _inline_check_coercion {
Subroutine _inline_check_coercion: called CC=4 SCAR=13.9
676 4479 my $self = shift;
677 4479 my ($value, $tc, $coercion) = @_;
678  
679 4479 ▶ return unless $self->should_coerce && $self->type_constraint->has_coercion;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->should_coerce and $self->type_constraint->has_coercion 4333 146 4479
Condition66%2/3
$self->should_coerce and $self->type_constraint->has_coercion
!ll&&!rl&&r
43330146
MC/DC50%1/2
$self->should_coerce and $self->type_constraint->has_coercion
$self->should_coerce $self->type_constraint->has_coercion
Truth table66%2/3
A and B
A= $self->should_coerceB= $self->type_constraint->has_coercion
ABresult
0X0
100
111
680  
681 146 ▶ if ( $self->type_constraint->can_be_inlined ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->type_constraint->can_be_inlined) { } 38 108 146
682 return (
683 38 'if (! (' . $self->type_constraint->_inline_check($value) . ')) {',
684 $value . ' = ' . $coercion . '->(' . $value . ');',
685 '}',
686 );
687 }
688 else {
689 return (
690 108 'if (!' . $tc . '->(' . $value . ')) {',
691 $value . ' = ' . $coercion . '->(' . $value . ');',
692 '}',
693 );
694 }
695 }
696  
697 4479 ▶sub _inline_check_constraint {
Subroutine _inline_check_constraint: called CC=3 SCAR=11.0
698 4479 my $self = shift;
699 4479 my ($value, $tc, $message) = @_;
700  
701 4479 ▶ return unless $self->has_type_constraint;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->has_type_constraint 898 3581 4479
702  
703 3581 my $attr_name = quotemeta($self->name);
704  
705 3581 ▶ if ( $self->type_constraint->can_be_inlined ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->type_constraint->can_be_inlined) { } 3368 213 3581
706 return (
707 3368 'if (! (' . $self->type_constraint->_inline_check($value) . ')) {',
708 'my $msg = do { local $_ = ' . $value . '; '
709 . $message . '->(' . $value . ');'
710 . '};'.
711 $self->_inline_throw_exception( ValidationFailedForInlineTypeConstraint =>
712 'type_constraint_message => $msg , '.
713 'class_name => $class_name, '.
714 'attribute_name => "'.$attr_name.'",'.
715 'value => '.$value
716 ).';',
717 '}',
718 );
719 }
720 else {
721 return (
722 213 'if (!' . $tc . '->(' . $value . ')) {',
723 'my $msg = do { local $_ = ' . $value . '; '
724 . $message . '->(' . $value . ');'
725 . '};'.
726 $self->_inline_throw_exception( ValidationFailedForInlineTypeConstraint =>
727 'type_constraint_message => $msg , '.
728 'class_name => $class_name, '.
729 'attribute_name => "'.$attr_name.'",'.
730 'value => '.$value
731 ).';',
732 '}',
733 );
734 }
735 }
736  
737 1323 ▶sub _inline_get_old_value_for_trigger {
Subroutine _inline_get_old_value_for_trigger: called CC=2 SCAR=6.9
738 1323 my $self = shift;
739 1323 my ($instance, $old) = @_;
740  
741 1323 ▶ return unless $self->has_trigger;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->has_trigger 1265 58 1323
742  
743 return (
744 58 'my ' . $old . ' = ' . $self->_inline_instance_has($instance),
745 '? ' . $self->_inline_instance_get($instance),
746 ': ();',
747 );
748 }
749  
750 4291 ▶sub _inline_weaken_value {
Subroutine _inline_weaken_value: called CC=2 SCAR=6.9
751 4291 my $self = shift;
752 4291 my ($instance, $value) = @_;
753  
754 4291 ▶ return unless $self->is_weak_ref;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->is_weak_ref 3514 777 4291
755  
756 777 my $mi = $self->associated_class->get_meta_instance;
757 return (
758 777 $mi->inline_weaken_slot_value($instance, $self->name),
759 'if ref ' . $value . ';',
760 );
761 }
762  
763 1680 ▶sub _inline_trigger {
Subroutine _inline_trigger: called CC=2 SCAR=6.9
764 1680 my $self = shift;
765 1680 my ($instance, $value, $old) = @_;
766  
767 1680 ▶ return unless $self->has_trigger;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->has_trigger 1587 93 1680
768  
769 93 return '$trigger->(' . $instance . ', ' . $value . ', ' . $old . ');';
770 }
771  
772 4254 ▶sub _eval_environment {
Subroutine _eval_environment: called CC=9 SCAR=22.0
773 4254 my $self = shift;
774  
775 4254 my $env = { };
776  
777 4254 ▶ $env->{'$trigger'} = \($self->trigger)
Branch: 1 branch
BranchTrueFalseTotal
if $self->has_trigger 145 4109 4254
778 if $self->has_trigger;
779 4254 ▶ $env->{'$attr_default'} = \($self->default)
Branch: 1 branch
BranchTrueFalseTotal
if $self->has_default 1167 3087 4254
780 if $self->has_default;
781  
782 4254 ▶ if ($self->has_type_constraint) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_type_constraint) 3188 1066 4254
783 3188 my $tc_obj = $self->type_constraint;
784  
785 3188 ▶ $env->{'$type_constraint'} = \(
Branch: 1 branch
BranchTrueFalseTotal
unless $tc_obj->can_be_inlined 276 2912 3188
786 $tc_obj->_compiled_type_constraint
787 ) unless $tc_obj->can_be_inlined;
788 # these two could probably get inlined versions too
789 3188 ▶ $env->{'$type_coercion'} = \(
Branch: 1 branch
BranchTrueFalseTotal
if $tc_obj->has_coercion 179 3009 3188
790 $tc_obj->coercion->_compiled_type_coercion
791 ) if $tc_obj->has_coercion;
792 3188 ▶ $env->{'$type_message'} = \(
Branch: 1 branch
BranchTrueFalseTotal
$tc_obj->has_message ? : 6 3182 3188
793 $tc_obj->has_message ? $tc_obj->message : $tc_obj->_default_message
794 );
795  
796 3188 $env = { %$env, %{ $tc_obj->inline_environment } };
797 }
798  
799 4254 $env->{'$class_name'} = \($self->associated_class->name);
800  
801 # XXX ugh, fix these
802 4254 ▶ $env->{'$attr'} = \$self
Branch: 1 branch
BranchTrueFalseTotal
if $self->has_initializer and $self->is_lazy 2 4252 4254
Condition100%3/3
$self->has_initializer and $self->is_lazy
!ll&&!rl&&r
424392
MC/DC100%2/2
$self->has_initializer and $self->is_lazy
$self->has_initializer $self->is_lazy
Truth table100%3/3
A and B
A= $self->has_initializerB= $self->is_lazy
ABresult
0X0
100
111
803 if $self->has_initializer && $self->is_lazy;
804 # pretty sure this is only going to be closed over if you use a custom
805 # error class at this point, but we should still get rid of this
806 # at some point
807 4254 $env->{'$meta'} = \($self->associated_class);
808  
809 4254 return $env;
810 }
811  
812 48 ▶sub _weaken_value {
Subroutine _weaken_value: called CC=1 SCAR=0.0
813 48 my ( $self, $instance ) = @_;
814  
815 48 my $meta_instance = Class::MOP::Class->initialize( blessed($instance) )
816 ->get_meta_instance;
817  
818 48 $meta_instance->weaken_slot_value( $instance, $self->name );
819 }
820  
821 73 ▶sub get_value {
Subroutine get_value: called CC=15 SCAR=35.5
Pod documented
822 73 my ($self, $instance, $for_trigger) = @_;
823  
824 73 ▶ if ($self->is_lazy) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->is_lazy) 7 66 73
825 7 ▶ unless ($self->has_value($instance)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->has_value($instance)) 3 4 7
826 3 my $value;
827 3 ▶ if ($self->has_default) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->has_default) { } 3 0 3
elsif ($self->has_builder) { } 0 0 0
828 3 $value = $self->default($instance);
829 } elsif ( $self->has_builder ) {
830 0 $value = $self->_call_builder($instance);
831 }
832  
833 3 $value = $self->_coerce_and_verify( $value, $instance );
834  
835 3 $self->set_initial_value($instance, $value);
836  
837 3 ▶ if ( ref $value && $self->is_weak_ref ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $value and $self->is_weak_ref) 1 2 3
Condition66%2/3
ref $value and $self->is_weak_ref
!ll&&!rl&&r
201
MC/DC50%1/2
ref $value and $self->is_weak_ref
ref $value $self->is_weak_ref
Truth table66%2/3
A and B
A= ref $valueB= $self->is_weak_ref
ABresult
0X0
100
111
838 1 $self->_weaken_value($instance);
839 }
840 }
841 }
842  
843 73 ▶ if ( $self->should_auto_deref && ! $for_trigger ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->should_auto_deref and not $for_trigger) { } 1 72 73
Condition66%2/3
$self->should_auto_deref and not $for_trigger
!ll&&!rl&&r
7201
MC/DC50%1/2
$self->should_auto_deref and not $for_trigger
$self->should_auto_deref not $for_trigger
Truth table66%2/3
A and not(B)
A= $self->should_auto_derefB= not $for_trigger
ABresult
0X0
100
111
844  
845 1 my $type_constraint = $self->type_constraint;
846  
847 1 ▶ if ($type_constraint->is_a_type_of('ArrayRef')) {
Branch: 2 branches
BranchTrueFalseTotal
if ($type_constraint->is_a_type_of('ArrayRef')) { } 0 1 1
elsif ($type_constraint->is_a_type_of('HashRef')) { } 0 1 1
848 0 my $rv = $self->SUPER::get_value($instance);
849 0 ▶ return unless defined $rv;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $rv 0 0 0
850 0 ▶ return wantarray ? @{ $rv } : $rv;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
851 }
852 elsif ($type_constraint->is_a_type_of('HashRef')) {
853 0 my $rv = $self->SUPER::get_value($instance);
854 0 ▶ return unless defined $rv;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $rv 0 0 0
855 0 ▶ return wantarray ? %{ $rv } : $rv;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
856 }
857 else {
858 1 throw_exception( CannotAutoDereferenceTypeConstraint => type_name => $type_constraint->name,
859 instance => $instance,
860 attribute => $self
861 );
862 }
863  
864 }
865 else {
866  
867 72 return $self->SUPER::get_value($instance);
868 }
869 }
870  
871 2969 ▶sub _inline_get_value {
Subroutine _inline_get_value: called CC=4 SCAR=14.3
872 2969 my $self = shift;
873 2969 my ($instance, $tc, $coercion, $message) = @_;
874  
875 2969 my $slot_access = $self->_inline_instance_get($instance);
876 2969 ▶ $tc ||= '$type_constraint';
Condition50%1/2
$tc ||= "\$type_constraint"
l!l
02969
MC/DC0%0/1
$tc ||= "\$type_constraint"
$tc
Truth table50%1/2
A
A= $tc
Aresult
11
00
877 2969 ▶ $coercion ||= '$type_coercion';
Condition50%1/2
$coercion ||= "\$type_coercion"
l!l
02969
MC/DC0%0/1
$coercion ||= "\$type_coercion"
$coercion
Truth table50%1/2
A
A= $coercion
Aresult
11
00
878 2969 ▶ $message ||= '$type_message';
Condition50%1/2
$message ||= "\$type_message"
l!l
02969
MC/DC0%0/1
$message ||= "\$type_message"
$message
Truth table50%1/2
A
A= $message
Aresult
11
00
879  
880 return (
881 2969 $self->_inline_check_lazy($instance, $tc, $coercion, $message),
882 $self->_inline_return_auto_deref($slot_access),
883 );
884 }
885  
886 4085 ▶sub _inline_check_lazy {
Subroutine _inline_check_lazy: called CC=2 SCAR=6.9
887 4085 my $self = shift;
888 4085 my ($instance, $tc, $coercion, $message) = @_;
889  
890 4085 ▶ return unless $self->is_lazy;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->is_lazy 3425 660 4085
891  
892 660 my $slot_exists = $self->_inline_instance_has($instance);
893  
894 return (
895 660 'if (!' . $slot_exists . ') {',
896 $self->_inline_init_from_default($instance, '$default', $tc, $coercion, $message, 'lazy'),
897 '}',
898 );
899 }
900  
901 660 ▶sub _inline_init_from_default {
Subroutine _inline_init_from_default: called CC=4 SCAR=14.5
902 660 my $self = shift;
903 660 my ($instance, $default, $tc, $coercion, $message, $for_lazy) = @_;
904  
905 660 ▶ if (!($self->has_default || $self->has_builder)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->has_default or $self->has_builder) 0 660 660
Condition66%2/3
$self->has_default or $self->has_builder
l!l&&r!l&&!r
1974630
MC/DC0%0/2
$self->has_default or $self->has_builder
$self->has_default $self->has_builder
Truth table66%2/3
A or B
A= $self->has_defaultB= $self->has_builder
ABresult
1X1
011
000
906 0 throw_exception( LazyAttributeNeedsADefault => attribute => $self );
907 }
908  
909 return (
910 660 ▶ $self->_inline_generate_default($instance, $default),
Branch: 1 branch
BranchTrueFalseTotal
$self->has_type_constraint ? : 637 23 660
911 # intentionally not using _inline_tc_code, since that can be overridden
912 # to do things like possibly only do member tc checks, which isn't
913 # appropriate for checking the result of a default
914 $self->has_type_constraint
915 ? ($self->_inline_check_coercion($default, $tc, $coercion, $for_lazy),
916 $self->_inline_check_constraint($default, $tc, $message, $for_lazy))
917 : (),
918 $self->_inline_init_slot($instance, $default),
919 $self->_inline_weaken_value($instance, $default),
920 );
921 }
922  
923 676 ▶sub _inline_generate_default {
Subroutine _inline_generate_default: called CC=4 SCAR=13.9
924 676 my $self = shift;
925 676 my ($instance, $default) = @_;
926  
927 676 ▶ if ($self->has_default) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->has_default) { } 209 467 676
elsif ($self->has_builder) { } 467 0 467
928 209 my $source = 'my ' . $default . ' = $attr_default';
929 209 ▶ $source .= '->(' . $instance . ')'
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_default_a_coderef 112 97 209
930 if $self->is_default_a_coderef;
931 209 return $source . ';';
932 }
933 elsif ($self->has_builder) {
934 467 my $builder = B::perlstring($self->builder);
935 467 my $builder_str = quotemeta($self->builder);
936 467 my $attr_name_str = quotemeta($self->name);
937 return (
938 467 'my ' . $default . ';',
939 'if (my $builder = ' . $instance . '->can(' . $builder . ')) {',
940 $default . ' = ' . $instance . '->$builder;',
941 '}',
942 'else {',
943 'my $class = ref(' . $instance . ') || ' . $instance . ';',
944 $self->_inline_throw_exception(
945 BuilderMethodNotSupportedForInlineAttribute =>
946 'class_name => $class,'.
947 'attribute_name => "'.$attr_name_str.'",'.
948 'instance => '.$instance.','.
949 'builder => "'.$builder_str.'"'
950 ) . ';',
951 '}',
952 );
953 }
954 else {
955 0 confess(
956 "Can't generate a default for " . $self->name
957 . " since no default or builder was specified"
958 );
959 }
960 }
961  
962 660 ▶sub _inline_init_slot {
Subroutine _inline_init_slot: called CC=2 SCAR=6.9
963 660 my $self = shift;
964 660 my ($inv, $value) = @_;
965  
966 660 ▶ if ($self->has_initializer) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_initializer) { } 2 658 660
967 2 return '$attr->set_initial_value(' . $inv . ', ' . $value . ');';
968 }
969 else {
970 658 return $self->_inline_instance_set($inv, $value) . ';';
971 }
972 }
973  
974 2969 ▶sub _inline_return_auto_deref {
Subroutine _inline_return_auto_deref: called CC=1 SCAR=0.0
975 2969 my $self = shift;
976  
977 2969 return 'return ' . $self->_auto_deref(@_) . ';';
978 }
979  
980 2969 ▶sub _auto_deref {
Subroutine _auto_deref: called CC=4 SCAR=13.9
981 2969 my $self = shift;
982 2969 my ($ref_value) = @_;
983  
984 2969 ▶ return $ref_value unless $self->should_auto_deref;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->should_auto_deref 2959 10 2969
985  
986 10 my $type_constraint = $self->type_constraint;
987  
988 10 my $sigil;
989 10 ▶ if ($type_constraint->is_a_type_of('ArrayRef')) {
Branch: 2 branches
BranchTrueFalseTotal
if ($type_constraint->is_a_type_of('ArrayRef')) { } 7 3 10
elsif ($type_constraint->is_a_type_of('HashRef')) { } 3 0 3
990 7 $sigil = '@';
991 }
992 elsif ($type_constraint->is_a_type_of('HashRef')) {
993 3 $sigil = '%';
994 }
995 else {
996 0 confess(
997 'Can not auto de-reference the type constraint \''
998 . $type_constraint->name
999 . '\''
1000 );
1001 }
1002  
1003 10 return 'wantarray '
1004 . '? ' . $sigil . '{ (' . $ref_value . ') || return } '
1005 . ': (' . $ref_value . ')';
1006 }
1007  
1008 ## installing accessors
1009  
1010 5287 ▶sub accessor_metaclass { 'Moose::Meta::Method::Accessor' }
Subroutine accessor_metaclass: called CC=1 SCAR=0.0
Pod documented
1011  
1012 3082 ▶sub install_accessors {
Subroutine install_accessors: called CC=2 SCAR=6.9
Pod documented
1013 3082 my $self = shift;
1014 3082 $self->SUPER::install_accessors(@_);
1015 3082 ▶ $self->install_delegation if $self->has_handles;
Branch: 1 branch
BranchTrueFalseTotal
if $self->has_handles 230 2852 3082
1016 3064 return;
1017 }
1018  
1019 2281 ▶sub _check_associated_methods {
Subroutine _check_associated_methods: called CC=4 SCAR=13.9
1020 2281 my $self = shift;
1021 2281 ▶ unless (
Branch: 1 branch
BranchTrueFalseTotal
unless (@{$self->associated_methods;} or ($self->_is_metadata || "") eq "bare") 2 2279 2281
Condition100%3/3
@{$self->associated_methods;} or ($self->_is_metadata || "") eq "bare"
l!l&&r!l&&!r
2252272
MC/DC100%2/2
@{$self->associated_methods;} or ($self->_is_metadata || "") eq "bare"
@{$self->associated_methods;} ($self->_is_metadata || "") eq "bare"
Truth table100%3/3
A or B
A= @{$self->associated_methods;}B= ($self->_is_metadata || "") eq "bare"
ABresult
1X1
011
000
1022 2281 ▶ @{ $self->associated_methods }
Condition100%2/2
$self->_is_metadata || ''
l!l
272
MC/DC100%1/1
$self->_is_metadata || ''
$self->_is_metadata
Truth table100%2/2
A
A= $self->_is_metadata
Aresult
11
00
1023 || ($self->_is_metadata || '') eq 'bare'
1024 ) {
1025 2 Carp::cluck(
1026 'Attribute (' . $self->name . ') of class '
1027 . $self->associated_class->name
1028 . ' has no associated methods'
1029 . ' (did you mean to provide an "is" argument?)'
1030 . "\n"
1031 )
1032 }
1033 }
1034  
1035 3225 ▶sub _process_accessors {
Subroutine _process_accessors: called CC=21 SCAR=34.6
1036 3225 my $self = shift;
1037 3225 my ($type, $accessor, $generate_as_inline_methods) = @_;
1038  
1039 3225 ▶ $accessor = ( keys %$accessor )[0] if ( ref($accessor) || '' ) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if (ref $accessor || "") eq "HASH" 0 3225 3225
Condition50%1/2
ref $accessor || ''
l!l
03225
MC/DC0%0/1
ref $accessor || ''
ref $accessor
Truth table50%1/2
A
A= ref $accessor
Aresult
11
00
1040 3225 my $method = $self->associated_class->get_method($accessor);
1041  
1042 3225 ▶ if ( $method
Branch: 1 branch
BranchTrueFalseTotal
if ($method and $method->isa("Class::MOP::Method::Accessor")) 387 2838 3225
Condition100%3/3
$method and $method->isa("Class::MOP::Method::Accessor")
!ll&&!rl&&r
28362387
MC/DC100%2/2
$method and $method->isa("Class::MOP::Method::Accessor")
$method $method->isa("Class::MOP::Method::Accessor")
Truth table100%3/3
A and B
A= $methodB= $method->isa("Class::MOP::Method::Accessor")
ABresult
0X0
100
111
1043 && $method->isa('Class::MOP::Method::Accessor') ) {
1044  
1045 # This is a special case that is very unlikely to occur outside of the
1046 # Moose bootstrapping process. We do not want to warn if the method
1047 # we're about to replace is for this same attribute, _and_ we're
1048 # replacing a non-inline method with an inlined version.
1049 #
1050 # This would never occur in normal user code because Moose inlines all
1051 # accessors. However, Moose metaclasses are instances of
1052 # Class::MOP::Class, which _does not_ inline accessors by
1053 # default. However, in Class::MOP & Moose.pm, we iterate over all of
1054 # our internal metaclasses and make them immutable after they're fully
1055 # defined. This ends up replacing the attribute accessors.
1056 387 ▶ unless ( $method->associated_attribute->name eq $self->name
Branch: 1 branch
BranchTrueFalseTotal
unless ($method->associated_attribute->name eq $self->name and ($generate_as_inline_methods and not $method->is_inline)) 2 385 387
Condition83%5/6
$method->associated_attribute->name eq $self->name and ($generate_as_inline_methods and not $method->is_inline)
!ll&&!rl&&r
11385
$generate_as_inline_methods and not $method->is_inline
!ll&&!rl&&r
10385
MC/DC66%2/3
$method->associated_attribute->name eq $self->name and ($generate_as_inline_methods and not $method->is_inline)
$method->associated_attribute->name eq $self->name $generate_as_inline_methods not $method->is_inline
Truth table75%3/4
A and B and not(C)
A= $method->associated_attribute->name eq $self->nameB= $generate_as_inline_methodsC= not $method->is_inline
ABCresult
0XX0
10X0
1100
1111
1057 && ( $generate_as_inline_methods && !$method->is_inline ) ) {
1058  
1059 2 my $other_attr = $method->associated_attribute;
1060  
1061 2 my $msg = sprintf(
1062 'You are overwriting a %s (%s) for the %s attribute',
1063 $method->accessor_type,
1064 $accessor,
1065 $other_attr->name,
1066 );
1067  
1068 2 ▶ if ( my $method_context = $method->definition_context ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $method_context = $method->definition_context) 2 0 2
1069 $msg .= sprintf(
1070 ' (defined at %s line %s)',
1071 $method_context->{file},
1072 $method_context->{line},
1073 )
1074 if defined $method_context->{file}
1075 2 ▶ && $method_context->{line};
Branch: 1 branch
BranchTrueFalseTotal
if defined $method_context->{"file"} and $method_context->{"line"} 2 0 2
Condition33%1/3
defined $method_context->{"file"} and $method_context->{"line"}
!ll&&!rl&&r
002
MC/DC0%0/2
defined $method_context->{"file"} and $method_context->{"line"}
defined $method_context->{"file"} $method_context->{"line"}
Truth table33%1/3
A and B
A= defined $method_context->{"file"}B= $method_context->{"line"}
ABresult
0X0
100
111
1076 }
1077  
1078 2 $msg .= sprintf(
1079 ' with a new %s method for the %s attribute',
1080 $type,
1081 $self->name,
1082 );
1083  
1084 2 ▶ if ( my $self_context = $self->definition_context ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $self_context = $self->definition_context) 2 0 2
1085 $msg .= sprintf(
1086 ' (defined at %s line %s)',
1087 $self_context->{file},
1088 $self_context->{line},
1089 )
1090 if defined $self_context->{file}
1091 2 ▶ && $self_context->{line};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self_context->{"file"} and $self_context->{"line"} 2 0 2
Condition33%1/3
defined $self_context->{"file"} and $self_context->{"line"}
!ll&&!rl&&r
002
MC/DC0%0/2
defined $self_context->{"file"} and $self_context->{"line"}
defined $self_context->{"file"} $self_context->{"line"}
Truth table33%1/3
A and B
A= defined $self_context->{"file"}B= $self_context->{"line"}
ABresult
0X0
100
111
1092 }
1093  
1094 2 Carp::cluck($msg);
1095 }
1096 }
1097  
1098 3225 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if ($method and not $method->is_stub and not $method->isa("Class::MOP::Method::Accessor") and !$self->definition_context || $method->package_name eq $self->definition_context->{"package"}) 0 3225 3225
Condition66%8/12
$method and not $method->is_stub and not $method->isa("Class::MOP::Method::Accessor") and !$self->definition_context || $method->package_name eq $self->definition_context->{"package"}
!ll&&!rl&&r
322320
$method and not $method->is_stub and not $method->isa("Class::MOP::Method::Accessor")
!ll&&!rl&&r
28363872
$method and not $method->is_stub
!ll&&!rl&&r
28360389
not $self->definition_context or $method->package_name eq $self->definition_context->{"package"}
l!l&&r!l&&!r
002
MC/DC0%0/5
$method and not $method->is_stub and not $method->isa("Class::MOP::Method::Accessor") and !$self->definition_context || $method->package_name eq $self->definition_context->{"package"}
$method not $method->is_stub not $method->isa("Class::MOP::Method::Accessor") not $self->definition_context $method->package_name eq $self->definition_context->{"package"}
Truth table50%3/6
A and not(B) and not(C) and not(D) or E
A= $methodB= not $method->is_stubC= not $method->isa("Class::MOP::Method::Accessor")D= not $self->definition_contextE= $method->package_name eq $self->definition_context->{"package"}
ABCDEresult
0XXXX0
10XXX0
110XX0
111000
1111X1
111011
1099 $method
1100 && !$method->is_stub
1101 && !$method->isa('Class::MOP::Method::Accessor')
1102 && ( !$self->definition_context
1103 || $method->package_name eq $self->definition_context->{package} )
1104 ) {
1105  
1106 0 Carp::cluck(
1107 "You are overwriting a locally defined method ($accessor) with "
1108 . "an accessor" );
1109 }
1110  
1111 3225 ▶ if ( !$self->associated_class->has_method($accessor)
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->associated_class->has_method($accessor) and $self->associated_class->has_package_symbol('&' . $accessor)) 0 3225 3225
Condition66%2/3
not $self->associated_class->has_method($accessor) and $self->associated_class->has_package_symbol('&' . $accessor)
!ll&&!rl&&r
38928360
MC/DC0%0/2
not $self->associated_class->has_method($accessor) and $self->associated_class->has_package_symbol('&' . $accessor)
not $self->associated_class->has_method($accessor) $self->associated_class->has_package_symbol('&' . $accessor)
Truth table66%2/3
not(A) and B
A= not $self->associated_class->has_method($accessor)B= $self->associated_class->has_package_symbol('&' . $accessor)
ABresult
0X0
100
111
1112 && $self->associated_class->has_package_symbol( '&' . $accessor ) ) {
1113  
1114 0 Carp::cluck(
1115 "You are overwriting a locally defined function ($accessor) with "
1116 . "an accessor" );
1117 }
1118  
1119 3225 $self->SUPER::_process_accessors(@_);
1120 }
1121  
1122 54 ▶sub remove_accessors {
Subroutine remove_accessors: called CC=2 SCAR=6.9
Pod documented
1123 54 my $self = shift;
1124 54 $self->SUPER::remove_accessors(@_);
1125 54 ▶ $self->remove_delegation if $self->has_handles;
Branch: 1 branch
BranchTrueFalseTotal
if $self->has_handles 24 30 54
1126 37 return;
1127 }
1128  
1129 230 ▶sub install_delegation {
Subroutine install_delegation: called CC=7 SCAR=19.5
Pod documented
1130 230 my $self = shift;
1131  
1132 # NOTE:
1133 # Here we canonicalize the 'handles' option
1134 # this will sort out any details and always
1135 # return an hash of methods which we want
1136 # to delegate to, see that method for details
1137 230 my %handles = $self->_canonicalize_handles;
1138  
1139 # install the delegation ...
1140 216 my $associated_class = $self->associated_class;
1141 216 my $class_name = $associated_class->name;
1142  
1143 216 foreach my $handle ( sort keys %handles ) {
1144 1128 my $method_to_call = $handles{$handle};
1145 1128 my $name = "${class_name}::${handle}";
1146  
1147 1128 ▶ if ( my $method = $associated_class->get_method($handle) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $method = $associated_class->get_method($handle)) 6 1122 1128
1148 6 ▶ throw_exception(
Branch: 1 branch
BranchTrueFalseTotal
unless $method->is_stub 4 2 6
1149 CannotDelegateLocalMethodIsPresent => attribute => $self,
1150 method => $method,
1151 ) unless $method->is_stub;
1152 }
1153  
1154 # NOTE:
1155 # handles is not allowed to delegate
1156 # any of these methods, as they will
1157 # override the ones in your class, which
1158 # is almost certainly not what you want.
1159  
1160 # FIXME warn when $handle was explicitly specified, but not if the source is a regex or something
1161 #cluck("Not delegating method '$handle' because it is a core method") and
1162 next
1163 1124 ▶ if $class_name->isa("Moose::Object")
Branch: 1 branch
BranchTrueFalseTotal
if $class_name->isa("Moose::Object") and $handle =~ /^BUILD|DEMOLISH$/ || "Moose::Object"->can($handle) 42 1082 1124
Condition100%6/6
$class_name->isa("Moose::Object") and $handle =~ /^BUILD|DEMOLISH$/ || "Moose::Object"->can($handle)
!ll&&!rl&&r
3107942
$handle =~ /^BUILD|DEMOLISH$/ or "Moose::Object"->can($handle)
l!l&&r!l&&!r
4381079
MC/DC100%3/3
$class_name->isa("Moose::Object") and $handle =~ /^BUILD|DEMOLISH$/ || "Moose::Object"->can($handle)
$class_name->isa("Moose::Object") $handle =~ /^BUILD|DEMOLISH$/ "Moose::Object"->can($handle)
Truth table100%4/4
A and B or C
A= $class_name->isa("Moose::Object")B= $handle =~ /^BUILD|DEMOLISH$/C= "Moose::Object"->can($handle)
ABCresult
0XX0
1000
11X1
1011
1164 and $handle =~ /^BUILD|DEMOLISH$/ || Moose::Object->can($handle);
1165  
1166 1082 my $method = $self->_make_delegation_method($handle, $method_to_call);
1167  
1168 1082 $self->associated_class->add_method($method->name, $method);
1169 1082 $self->associate_method($method);
1170 }
1171 }
1172  
1173 24 ▶sub remove_delegation {
Subroutine remove_delegation: called CC=3 SCAR=11.0
Pod documented
1174 24 my $self = shift;
1175 24 my %handles = $self->_canonicalize_handles;
1176 7 my $associated_class = $self->associated_class;
1177 7 foreach my $handle (keys %handles) {
1178 45 ▶ next unless any { $handle eq $_ }
Subroutine __ANON__: called CC=1 SCAR=0.0
1179 48 map { $_->name }
1180 24 ▶ @{ $self->associated_methods };
Branch: 1 branch
BranchTrueFalseTotal
unless &any(sub { $handle eq $_; } , map({$_->name;} @{$self->associated_methods;})) 19 5 24
1181 5 $self->associated_class->remove_method($handle);
1182 }
1183 }
1184  
1185 # private methods to help delegation ...
1186  
1187 73 ▶sub _canonicalize_handles {
Subroutine _canonicalize_handles: called CC=13 SCAR=25.7
1188 73 my $self = shift;
1189 73 my $handles = $self->handles;
1190 73 ▶ if (my $handle_type = ref($handles)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $handle_type = ref $handles) 68 5 73
1191 68 ▶ if ($handle_type eq 'HASH') {
Branch: 6 branches
BranchTrueFalseTotal
if ($handle_type eq 'HASH') { } 7 61 68
elsif ($handle_type eq 'ARRAY') { } 20 41 61
elsif ($handle_type eq 'Regexp') { } 29 12 41
elsif ($handle_type eq 'CODE') { } 8 4 12
elsif (builtin::blessed($handles) and $handles->isa('Moose::Meta::TypeConstraint::DuckType')) { } 1 3 4
elsif (builtin::blessed($handles) and $handles->isa('Moose::Meta::TypeConstraint::Role')) { } 1 2 3
1192 7 return %{$handles};
1193 }
1194 elsif ($handle_type eq 'ARRAY') {
1195 20 return map { $_ => $_ } @{$handles};
1196 }
1197 elsif ($handle_type eq 'Regexp') {
1198 29 ▶ ($self->has_type_constraint)
Branch: 1 branch
BranchTrueFalseTotal
$self->has_type_constraint or throw_exception("CannotDelegateWithoutIsa", "attribute", $self) 23 6 29
1199 || throw_exception( CannotDelegateWithoutIsa => attribute => $self );
1200 31 return map { ($_ => $_) }
1201 23 grep { /$handles/ } $self->_get_delegate_method_list;
1202 }
1203 elsif ($handle_type eq 'CODE') {
1204 8 return $handles->($self, $self->_find_delegate_metaclass);
1205 }
1206 ▶ elsif (blessed($handles) && $handles->isa('Moose::Meta::TypeConstraint::DuckType')) {
Condition100%3/3
builtin::blessed($handles) and $handles->isa("Moose::Meta::TypeConstraint::DuckType")
!ll&&!rl&&r
211
MC/DC100%2/2
builtin::blessed($handles) and $handles->isa("Moose::Meta::TypeConstraint::DuckType")
builtin::blessed($handles) $handles->isa("Moose::Meta::TypeConstraint::DuckType")
Truth table100%3/3
A and B
A= builtin::blessed($handles)B= $handles->isa("Moose::Meta::TypeConstraint::DuckType")
ABresult
0X0
100
111
1207 1 return map { $_ => $_ } @{ $handles->methods };
1208 }
1209 ▶ elsif (blessed($handles) && $handles->isa('Moose::Meta::TypeConstraint::Role')) {
Condition66%2/3
builtin::blessed($handles) and $handles->isa("Moose::Meta::TypeConstraint::Role")
!ll&&!rl&&r
201
MC/DC50%1/2
builtin::blessed($handles) and $handles->isa("Moose::Meta::TypeConstraint::Role")
builtin::blessed($handles) $handles->isa("Moose::Meta::TypeConstraint::Role")
Truth table66%2/3
A and B
A= builtin::blessed($handles)B= $handles->isa("Moose::Meta::TypeConstraint::Role")
ABresult
0X0
100
111
1210 1 $handles = $handles->role;
1211 }
1212 else {
1213 2 throw_exception( UnableToCanonicalizeHandles => attribute => $self,
1214 handles => $handles
1215 );
1216 }
1217 }
1218  
1219 6 Moose::Util::_load_user_class($handles);
1220 6 my $role_meta = Class::MOP::class_of($handles);
1221  
1222 6 ▶ (blessed $role_meta && $role_meta->isa('Moose::Meta::Role'))
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($role_meta) and $role_meta->isa("Moose::Meta::Role") or throw_exception("UnableToCanonicalizeNonRolePackage", "attribute", $self, "handles", $handles) 4 2 6
Condition66%2/3
builtin::blessed($role_meta) and $role_meta->isa("Moose::Meta::Role")
!ll&&!rl&&r
024
MC/DC50%1/2
builtin::blessed($role_meta) and $role_meta->isa("Moose::Meta::Role")
builtin::blessed($role_meta) $role_meta->isa("Moose::Meta::Role")
Truth table66%2/3
A and B
A= builtin::blessed($role_meta)B= $role_meta->isa("Moose::Meta::Role")
ABresult
0X0
100
111
1223 || throw_exception( UnableToCanonicalizeNonRolePackage => attribute => $self,
1224 handles => $handles
1225 );
1226  
1227 6 return map { $_ => $_ }
1228 6 map { $_->name }
1229 4 grep { !$_->isa('Class::MOP::Method::Meta') } (
1230 $role_meta->_get_local_methods,
1231 $role_meta->get_required_method_list,
1232 );
1233 }
1234  
1235 23 ▶sub _get_delegate_method_list {
Subroutine _get_delegate_method_list: called CC=4 SCAR=14.0
1236 23 my $self = shift;
1237 23 my $meta = $self->_find_delegate_metaclass;
1238 9 ▶ if ($meta->isa('Class::MOP::Class')) {
Branch: 2 branches
BranchTrueFalseTotal
if ($meta->isa('Class::MOP::Class')) { } 7 2 9
elsif ($meta->isa('Moose::Meta::Role')) { } 0 2 2
1239 57 return map { $_->name } # NOTE: !never! delegate &meta
1240 7 ▶ grep { $_->package_name ne 'Moose::Object' && !$_->isa('Class::MOP::Method::Meta') }
Condition100%3/3
$_->package_name ne 'Moose::Object' && !$_->isa('Class::MOP::Method::Meta')
!ll&&!rl&&r
40557
MC/DC100%2/2
$_->package_name ne 'Moose::Object' && !$_->isa('Class::MOP::Method::Meta')
$_->package_name ne 'Moose::Object' !$_->isa('Class::MOP::Method::Meta')
Truth table100%3/3
A && not(B)
A= $_->package_name ne 'Moose::Object'B= !$_->isa('Class::MOP::Method::Meta')
ABresult
0X0
100
111
1241 $meta->get_all_methods;
1242 }
1243 elsif ($meta->isa('Moose::Meta::Role')) {
1244 0 return $meta->get_method_list;
1245 }
1246 else {
1247 2 throw_exception( UnableToRecognizeDelegateMetaclass => attribute => $self,
1248 delegate_metaclass => $meta
1249 );
1250 }
1251 }
1252  
1253 31 ▶sub _find_delegate_metaclass {
Subroutine _find_delegate_metaclass: called CC=6 SCAR=18.0
1254 31 my $self = shift;
1255 31 my $class = $self->_isa_metadata;
1256 31 my $role = $self->_does_metadata;
1257  
1258 31 ▶ if ( $class ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($class) { } 23 8 31
elsif ($role) { } 4 4 8
1259 # make sure isa is actually a class
1260 23 ▶ unless ( $self->type_constraint->isa("Moose::Meta::TypeConstraint::Class") ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->type_constraint->isa("Moose::Meta::TypeConstraint::Class")) 4 19 23
1261 4 throw_exception( DelegationToATypeWhichIsNotAClass => attribute => $self );
1262 }
1263  
1264 # make sure the class is loaded
1265 19 ▶ unless ( Moose::Util::_is_package_loaded($class) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (Moose::Util::_is_package_loaded($class)) 4 15 19
1266 4 throw_exception( DelegationToAClassWhichIsNotLoaded => attribute => $self,
1267 class_name => $class
1268 );
1269 }
1270 # we might be dealing with a non-Moose class,
1271 # and need to make our own metaclass. if there's
1272 # already a metaclass, it will be returned
1273 15 return Class::MOP::Class->initialize($class);
1274 }
1275 elsif ( $role ) {
1276 4 ▶ unless ( Moose::Util::_is_package_loaded($role) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (Moose::Util::_is_package_loaded($role)) 4 0 4
1277 4 throw_exception( DelegationToARoleWhichIsNotLoaded => attribute => $self,
1278 role_name => $role
1279 );
1280 }
1281  
1282 0 return Class::MOP::class_of($role);
1283 }
1284 else {
1285 4 throw_exception( CannotFindDelegateMetaclass => attribute => $self );
1286 }
1287 }
1288  
1289 2114 ▶sub delegation_metaclass { 'Moose::Meta::Method::Delegation' }
Subroutine delegation_metaclass: called CC=1 SCAR=0.0
Pod documented
1290  
1291 50 ▶sub _make_delegation_method {
Subroutine _make_delegation_method: called CC=2 SCAR=6.9
1292 50 my ( $self, $handle_name, $method_to_call ) = @_;
1293  
1294 50 my @curried_arguments;
1295  
1296 50 ▶ ($method_to_call, @curried_arguments) = @$method_to_call
Branch: 1 branch
BranchTrueFalseTotal
if "ARRAY" eq ref $method_to_call 2 48 50
1297 if 'ARRAY' eq ref($method_to_call);
1298  
1299 50 return $self->delegation_metaclass->new(
1300 name => $handle_name,
1301 package_name => $self->associated_class->name,
1302 attribute => $self,
1303 delegate_to_method => $method_to_call,
1304 curried_arguments => \@curried_arguments,
1305 );
1306 }
1307  
1308 4301 ▶sub _coerce_and_verify {
Subroutine _coerce_and_verify: called CC=4 SCAR=13.9
1309 4301 my $self = shift;
1310 4301 my $val = shift;
1311 4301 my $instance = shift;
1312  
1313 4301 ▶ return $val unless $self->has_type_constraint;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->has_type_constraint 255 4046 4301
1314  
1315 4046 ▶ $val = $self->type_constraint->coerce($val)
Branch: 1 branch
BranchTrueFalseTotal
if $self->should_coerce and $self->type_constraint->has_coercion 116 3930 4046
Condition66%2/3
$self->should_coerce and $self->type_constraint->has_coercion
!ll&&!rl&&r
39300116
MC/DC50%1/2
$self->should_coerce and $self->type_constraint->has_coercion
$self->should_coerce $self->type_constraint->has_coercion
Truth table66%2/3
A and B
A= $self->should_coerceB= $self->type_constraint->has_coercion
ABresult
0X0
100
111
1316 if $self->should_coerce && $self->type_constraint->has_coercion;
1317  
1318 4046 $self->verify_against_type_constraint($val, instance => $instance);
1319  
1320 3101 return $val;
1321 }
1322  
1323 4049 ▶sub verify_against_type_constraint {
Subroutine verify_against_type_constraint: called CC=3 SCAR=11.0
Pod documented
1324 4049 my $self = shift;
1325 4049 my $val = shift;
1326  
1327 4049 ▶ return 1 if !$self->has_type_constraint;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->has_type_constraint 1 4048 4049
1328  
1329 4048 my $type_constraint = $self->type_constraint;
1330  
1331 4048 ▶ $type_constraint->check($val)
Branch: 1 branch
BranchTrueFalseTotal
$type_constraint->check($val) or throw_exception("ValidationFailedForTypeConstraint", "type", $type_constraint, "value", $val, "attribute", $self) 3102 946 4048
1332 || throw_exception( ValidationFailedForTypeConstraint => type => $type_constraint,
1333 value => $val,
1334 attribute => $self,
1335 );
1336 }
1337  
1338 package # hide from PAUSE
1339 Moose::Meta::Attribute::Custom::Moose;
1340  
1341 0 ▶sub register_implementation { 'Moose::Meta::Attribute' }
Subroutine register_implementation: not called CC=1 SCAR=6.9
1342 398 1;
1343  
1344 # ABSTRACT: The Moose attribute metaclass
1345  
1346 __END__
« blib/lib/Moose/Meta/Role.pm ↑ Summary blib/lib/Moose/Util.pm »