blib/lib/Class/MOP/Class.pm

filter: Statementstmt 97.8% 578 / 591 Branchbran 93.6% 221 / 236 Conditioncond 76.9% 87 / 113 MC/DCmcdc 50.7% 35 / 69 Subroutinesub 96.5% 113 / 117 Podpod 79.4% 31 / 39 totaltotal 91.4% 1065 / 1165 CC 190 SCAR 16.0
CC
190
Cov
94%
create
15.0
_new
10.6
reinitialize
10.4
CRAP 4.9
« blib/lib/Moose/Meta/Attribute/Native/Trait.pm ↑ Summary blib/lib/Class/MOP/Method/Generated.pm »
1 package Class::MOP::Class;
2 459 our $VERSION = '2.4001';
3  
4 459 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 459 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 459 ▶use Class::MOP::Instance;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 459 ▶use Class::MOP::Method::Wrapped;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 459 ▶use Class::MOP::Method::Accessor;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 459 ▶use Class::MOP::Method::Constructor;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 459 ▶use Class::MOP::MiniTrait;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 459 ▶use Carp 'confess';
Subroutine BEGIN: called CC=1 SCAR=0.0
14 459 ▶use Module::Runtime 'use_package_optimistically';
Subroutine BEGIN: called CC=1 SCAR=0.0
15 459 ▶use Scalar::Util 'blessed';
Subroutine BEGIN: called CC=1 SCAR=0.0
16 459 ▶use Sub::Util 1.40 'set_subname';
Subroutine BEGIN: called CC=1 SCAR=0.0
17 459 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 459 ▶use List::Util 1.33 'all';
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 459 ▶use parent 'Class::MOP::Module',
Subroutine BEGIN: called CC=1 SCAR=0.0
21 'Class::MOP::Mixin::HasAttributes',
22 'Class::MOP::Mixin::HasMethods',
23 459 'Class::MOP::Mixin::HasOverloads';
24  
25 # Creation
26  
27 696863 ▶sub initialize {
Subroutine initialize: called CC=6 SCAR=17.9
Pod documented
28 696863 my $class = shift;
29  
30 696863 my $package_name;
31  
32 696863 ▶ if ( @_ % 2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ % 2) { } 696861 2 696863
33 696861 $package_name = shift;
34 } else {
35 2 my %options = @_;
36 2 $package_name = $options{package};
37 }
38  
39 696863 ▶ ($package_name && !ref($package_name))
Branch: 1 branch
BranchTrueFalseTotal
$package_name and not ref $package_name or ($class || __PACKAGE__)->_throw_exception("InitializeTakesUnBlessedPackageName", "package_name", $package_name) 696856 7 696863
Condition100%5/5
$package_name and not ref $package_name
!ll&&!rl&&r
61696856
$class || __PACKAGE__
l!l
61
MC/DC100%2/2
$package_name and not ref $package_name
$package_name not ref $package_name
MC/DC100%1/1
$class || __PACKAGE__
$class
Truth table100%3/3
A and not(B)
A= $package_nameB= not ref $package_name
ABresult
0X0
100
111
Truth table100%2/2
A
A= $class
Aresult
11
00
40 || ($class||__PACKAGE__)->_throw_exception( InitializeTakesUnBlessedPackageName => package_name => $package_name );
41 696856 ▶ return Class::MOP::get_metaclass_by_name($package_name)
Condition66%2/3
Class::MOP::get_metaclass_by_name($package_name) || $class->_construct_class_instance('package', $package_name, @_)
l!l&&r!l&&!r
666853300030
MC/DC0%0/2
Class::MOP::get_metaclass_by_name($package_name) || $class->_construct_class_instance('package', $package_name, @_)
Class::MOP::get_metaclass_by_name($package_name) $class->_construct_class_instance('package', $package_name, @_)
Truth table66%2/3
A || B
A= Class::MOP::get_metaclass_by_name($package_name)B= $class->_construct_class_instance('package', $package_name, @_)
ABresult
1X1
011
000
42 || $class->_construct_class_instance(package => $package_name, @_);
43 }
44  
45 115 ▶sub reinitialize {
Subroutine reinitialize: called CC=10 SCAR=23.4
Pod documented
46 115 my ( $class, @args ) = @_;
47 115 ▶ unshift @args, "package" if @args % 2;
Branch: 1 branch
BranchTrueFalseTotal
if @args % 2 115 0 115
48 115 my %options = @args;
49 my $old_metaclass = blessed($options{package})
50 ? $options{package}
51 115 ▶ : Class::MOP::get_metaclass_by_name($options{package});
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($options{'package'}) ? : 97 18 115
52 $options{weaken} = Class::MOP::metaclass_is_weak($old_metaclass->name)
53 if !exists $options{weaken}
54 115 ▶ && blessed($old_metaclass)
Branch: 1 branch
BranchTrueFalseTotal
if not exists $options{"weaken"} and builtin::blessed($old_metaclass) and $old_metaclass->isa("Class::MOP::Class") 113 2 115
Condition83%5/6
not exists $options{"weaken"} and builtin::blessed($old_metaclass) and $old_metaclass->isa("Class::MOP::Class")
!ll&&!rl&&r
11113
not exists $options{"weaken"} and builtin::blessed($old_metaclass)
!ll&&!rl&&r
01114
MC/DC66%2/3
not exists $options{"weaken"} and builtin::blessed($old_metaclass) and $old_metaclass->isa("Class::MOP::Class")
not exists $options{"weaken"} builtin::blessed($old_metaclass) $old_metaclass->isa("Class::MOP::Class")
Truth table75%3/4
not(A) and B and C
A= not exists $options{"weaken"}B= builtin::blessed($old_metaclass)C= $old_metaclass->isa("Class::MOP::Class")
ABCresult
0XX0
10X0
1100
1111
55 && $old_metaclass->isa('Class::MOP::Class');
56 115 ▶ $old_metaclass->_remove_generated_metaobjects
Branch: 1 branch
BranchTrueFalseTotal
if $old_metaclass and $old_metaclass->isa("Class::MOP::Class") 113 2 115
Condition100%3/3
$old_metaclass and $old_metaclass->isa("Class::MOP::Class")
!ll&&!rl&&r
11113
MC/DC100%2/2
$old_metaclass and $old_metaclass->isa("Class::MOP::Class")
$old_metaclass $old_metaclass->isa("Class::MOP::Class")
Truth table100%3/3
A and B
A= $old_metaclassB= $old_metaclass->isa("Class::MOP::Class")
ABresult
0X0
100
111
57 if $old_metaclass && $old_metaclass->isa('Class::MOP::Class');
58 115 my $new_metaclass = $class->SUPER::reinitialize(%options);
59 112 ▶ $new_metaclass->_restore_metaobjects_from($old_metaclass)
Branch: 1 branch
BranchTrueFalseTotal
if $old_metaclass and $old_metaclass->isa("Class::MOP::Class") 112 0 112
Condition33%1/3
$old_metaclass and $old_metaclass->isa("Class::MOP::Class")
!ll&&!rl&&r
00112
MC/DC0%0/2
$old_metaclass and $old_metaclass->isa("Class::MOP::Class")
$old_metaclass $old_metaclass->isa("Class::MOP::Class")
Truth table33%1/3
A and B
A= $old_metaclassB= $old_metaclass->isa("Class::MOP::Class")
ABresult
0X0
100
111
60 if $old_metaclass && $old_metaclass->isa('Class::MOP::Class');
61 109 return $new_metaclass;
62 }
63  
64 # NOTE: (meta-circularity)
65 # this is a special form of _construct_instance
66 # (see below), which is used to construct class
67 # meta-object instances for any Class::MOP::*
68 # class. All other classes will use the more
69 # normal &construct_instance.
70 30013 ▶sub _construct_class_instance {
Subroutine _construct_class_instance: called CC=8 SCAR=20.8
71 30013 my $class = shift;
72 30013 ▶ my $options = @_ == 1 ? $_[0] : {@_};
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 ? : 0 30013 30013
73 30013 my $package_name = $options->{package};
74 30013 ▶ (defined $package_name && $package_name)
Branch: 1 branch
BranchTrueFalseTotal
defined $package_name and $package_name or $class->_throw_exception("ConstructClassInstanceTakesPackageName") 30009 4 30013
Condition66%2/3
defined $package_name and $package_name
!ll&&!rl&&r
4030009
MC/DC50%1/2
defined $package_name and $package_name
defined $package_name $package_name
Truth table66%2/3
A and B
A= defined $package_nameB= $package_name
ABresult
0X0
100
111
75 || $class->_throw_exception("ConstructClassInstanceTakesPackageName");
76 # NOTE:
77 # return the metaclass if we have it cached,
78 # and it is still defined (it has not been
79 # reaped by DESTROY yet, which can happen
80 # annoyingly enough during global destruction)
81  
82 30009 ▶ if (defined(my $meta = Class::MOP::get_metaclass_by_name($package_name))) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $meta = Class::MOP::get_metaclass_by_name($package_name))) 5 30004 30009
83 5 return $meta;
84 }
85  
86 $class
87 30004 ▶ = ref $class
Branch: 1 branch
BranchTrueFalseTotal
ref $class ? : 3549 26455 30004
88 ? $class->_real_ref_name
89 : $class;
90  
91 # now create the metaclass
92 30004 my $meta;
93 30004 ▶ if ($class eq 'Class::MOP::Class') {
Branch: 1 branch
BranchTrueFalseTotal
if ($class eq 'Class::MOP::Class') { } 27296 2708 30004
94 27296 $meta = $class->_new($options);
95 }
96 else {
97 # NOTE:
98 # it is safe to use meta here because
99 # class will always be a subclass of
100 # Class::MOP::Class, which defines meta
101 2708 $meta = $class->meta->_construct_instance($options)
102 }
103  
104 # and check the metaclass compatibility
105 30000 $meta->_check_metaclass_compatibility();
106  
107 29997 Class::MOP::store_metaclass_by_name($package_name, $meta);
108  
109 # NOTE:
110 # we need to weaken any anon classes
111 # so that they can call DESTROY properly
112 29997 ▶ Class::MOP::weaken_metaclass($package_name) if $options->{weaken};
Branch: 1 branch
BranchTrueFalseTotal
if $options->{"weaken"} 105 29892 29997
113  
114 29997 $meta;
115 }
116  
117 31068 ▶sub _real_ref_name {
Subroutine _real_ref_name: called CC=2 SCAR=6.9
118 31068 my $self = shift;
119  
120 # NOTE: we need to deal with the possibility of class immutability here,
121 # and then get the name of the class appropriately
122 31068 ▶ return $self->is_immutable
Branch: 1 branch
BranchTrueFalseTotal
$self->is_immutable ? : 11263 19805 31068
123 ? $self->_get_mutable_metaclass_name()
124 : ref $self;
125 }
126  
127 27296 ▶sub _new {
Subroutine _new: called CC=10 SCAR=23.6
128 27296 my $class = shift;
129  
130 27296 ▶ return Class::MOP::Class->initialize($class)->new_object(@_)
Branch: 1 branch
BranchTrueFalseTotal
if $class ne __PACKAGE__ 0 27296 27296
131 if $class ne __PACKAGE__;
132  
133 27296 ▶ my $options = @_ == 1 ? $_[0] : {@_};
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 ? : 27296 0 27296
134  
135 return bless {
136 # inherited from Class::MOP::Package
137 'package' => $options->{package},
138  
139 # NOTE:
140 # since the following attributes will
141 # actually be loaded from the symbol
142 # table, and actually bypass the instance
143 # entirely, we can just leave these things
144 # listed here for reference, because they
145 # should not actually have a value associated
146 # with the slot.
147 'namespace' => \undef,
148 'methods' => {},
149  
150 # inherited from Class::MOP::Module
151 'version' => \undef,
152 'authority' => \undef,
153  
154 # defined in Class::MOP::Class
155 'superclasses' => \undef,
156  
157 'attributes' => {},
158 'attribute_metaclass' =>
159 ( $options->{'attribute_metaclass'} || 'Class::MOP::Attribute' ),
160 'method_metaclass' =>
161 ( $options->{'method_metaclass'} || 'Class::MOP::Method' ),
162 'wrapped_method_metaclass' => (
163 $options->{'wrapped_method_metaclass'}
164 || 'Class::MOP::Method::Wrapped'
165 ),
166 'instance_metaclass' =>
167 ( $options->{'instance_metaclass'} || 'Class::MOP::Instance' ),
168 'immutable_trait' => (
169 $options->{'immutable_trait'}
170 || 'Class::MOP::Class::Immutable::Trait'
171 ),
172 'constructor_name' => ( $options->{constructor_name} || 'new' ),
173 'constructor_class' => (
174 $options->{constructor_class} || 'Class::MOP::Method::Constructor'
175 ),
176 'destructor_class' => $options->{destructor_class},
177 27296 ▶ }, $class;
Condition85%12/14
$options->{'attribute_metaclass'} || 'Class::MOP::Attribute'
l!l
1327283
$options->{'method_metaclass'} || 'Class::MOP::Method'
l!l
527291
$options->{'wrapped_method_metaclass'} || 'Class::MOP::Method::Wrapped'
l!l
127295
$options->{'instance_metaclass'} || 'Class::MOP::Instance'
l!l
2227274
$options->{'immutable_trait'} || 'Class::MOP::Class::Immutable::Trait'
l!l
027296
$options->{'constructor_name'} || 'new'
l!l
027296
$options->{'constructor_class'} || 'Class::MOP::Method::Constructor'
l!l
127295
MC/DC100%1/1
$options->{'attribute_metaclass'} || 'Class::MOP::Attribute'
$options->{'attribute_metaclass'}
MC/DC100%1/1
$options->{'method_metaclass'} || 'Class::MOP::Method'
$options->{'method_metaclass'}
MC/DC100%1/1
$options->{'wrapped_method_metaclass'} || 'Class::MOP::Method::Wrapped'
$options->{'wrapped_method_metaclass'}
MC/DC100%1/1
$options->{'instance_metaclass'} || 'Class::MOP::Instance'
$options->{'instance_metaclass'}
MC/DC0%0/1
$options->{'immutable_trait'} || 'Class::MOP::Class::Immutable::Trait'
$options->{'immutable_trait'}
MC/DC0%0/1
$options->{'constructor_name'} || 'new'
$options->{'constructor_name'}
MC/DC100%1/1
$options->{'constructor_class'} || 'Class::MOP::Method::Constructor'
$options->{'constructor_class'}
Truth table100%2/2
A
A= $options->{'attribute_metaclass'}
Aresult
11
00
Truth table100%2/2
A
A= $options->{'method_metaclass'}
Aresult
11
00
Truth table100%2/2
A
A= $options->{'wrapped_method_metaclass'}
Aresult
11
00
Truth table100%2/2
A
A= $options->{'instance_metaclass'}
Aresult
11
00
Truth table50%1/2
A
A= $options->{'immutable_trait'}
Aresult
11
00
Truth table50%1/2
A
A= $options->{'constructor_name'}
Aresult
11
00
Truth table100%2/2
A
A= $options->{'constructor_class'}
Aresult
11
00
178 }
179  
180 ## Metaclass compatibility
181 {
182 459 my %base_metaclass = (
183 attribute_metaclass => 'Class::MOP::Attribute',
184 method_metaclass => 'Class::MOP::Method',
185 wrapped_method_metaclass => 'Class::MOP::Method::Wrapped',
186 instance_metaclass => 'Class::MOP::Instance',
187 constructor_class => 'Class::MOP::Method::Constructor',
188 destructor_class => 'Class::MOP::Method::Destructor',
189 );
190  
191 41123 ▶ sub _base_metaclasses { %base_metaclass }
Subroutine _base_metaclasses: called CC=1 SCAR=0.0
192 }
193  
194 459 ▶sub _check_metaclass_compatibility {
Subroutine _check_metaclass_compatibility: called CC=8 SCAR=20.8
195 33576 my $self = shift;
196  
197 33576 ▶ my @superclasses = $self->superclasses
Branch: 1 branch
BranchTrueFalseTotal
my(@superclasses) = $self->superclasses or return 18100 15475 33575
198 or return;
199  
200 18100 $self->_fix_metaclass_incompatibility(@superclasses);
201  
202 18094 my %base_metaclass = $self->_base_metaclasses;
203  
204 # this is always okay ...
205 return
206 if ref($self) eq 'Class::MOP::Class'
207 90848 ▶ && all {
Subroutine __ANON__: called CC=2 SCAR=6.9
208 90848 my $meta = $self->$_;
209 90848 ▶ !defined($meta) || $meta eq $base_metaclass{$_};
Branch: 1 branch
BranchTrueFalseTotal
not defined $meta or $meta eq $base_metaclass{$_} 15140 75708 90848
210 }
211 18094 ▶ keys %base_metaclass;
Branch: 1 branch
BranchTrueFalseTotal
if ref $self eq "Class::MOP::Class" and &all(sub { my $meta = $self->$_; not defined $meta or $meta eq $base_metaclass{$_}; } , keys %base_metaclass) 15134 2960 18094
Condition100%3/3
ref $self eq "Class::MOP::Class" and &all(sub { my $meta = $self->$_; not defined $meta or $meta eq $base_metaclass{$_}; } , keys %base_metaclass)
!ll&&!rl&&r
29491115134
MC/DC100%2/2
ref $self eq "Class::MOP::Class" and &all(sub { my $meta = $self->$_; not defined $meta or $meta eq $base_metaclass{$_}; } , keys %base_metaclass)
ref $self eq "Class::MOP::Class" &all(sub { my $meta = $self->$_; not defined $meta or $meta eq $base_metaclass{$_}; } , keys %base_metaclass)
Truth table100%3/3
A and B
A= ref $self eq "Class::MOP::Class"B= &all(sub { my $meta = $self->$_; not defined $meta or $meta eq $base_metaclass{$_}; } , keys %base_metaclass)
ABresult
0X0
100
111
212  
213 2960 for my $superclass (@superclasses) {
214 3297 $self->_check_class_metaclass_compatibility($superclass);
215 }
216  
217 2954 for my $metaclass_type ( keys %base_metaclass ) {
218 17703 ▶ next unless defined $self->$metaclass_type;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->$metaclass_type 34 17669 17703
219 17669 for my $superclass (@superclasses) {
220 19691 $self->_check_single_metaclass_compatibility( $metaclass_type,
221 $superclass );
222 }
223 }
224 }
225  
226 3297 ▶sub _check_class_metaclass_compatibility {
Subroutine _check_class_metaclass_compatibility: called CC=2 SCAR=6.9
227 3297 my $self = shift;
228 3297 my ( $superclass_name ) = @_;
229  
230 3297 ▶ if (!$self->_class_metaclass_is_compatible($superclass_name)) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->_class_metaclass_is_compatible($superclass_name)) 6 3291 3297
231 6 my $super_meta = Class::MOP::get_metaclass_by_name($superclass_name);
232  
233 6 my $super_meta_type = $super_meta->_real_ref_name;
234  
235 6 $self->_throw_exception( IncompatibleMetaclassOfSuperclass => class_name => $self->name,
236 class_meta_type => ref( $self ),
237 superclass_name => $superclass_name,
238 superclass_meta_type => $super_meta_type
239 );
240 }
241 }
242  
243 3384 ▶sub _class_metaclass_is_compatible {
Subroutine _class_metaclass_is_compatible: called CC=2 SCAR=7.0
244 3384 my $self = shift;
245 3384 my ( $superclass_name ) = @_;
246  
247 3384 ▶ my $super_meta = Class::MOP::get_metaclass_by_name($superclass_name)
Condition50%1/2
Class::MOP::get_metaclass_by_name($superclass_name) || (return 1)
l!l
33840
MC/DC0%0/1
Class::MOP::get_metaclass_by_name($superclass_name) || (return 1)
Class::MOP::get_metaclass_by_name($superclass_name)
Truth table50%1/2
A
A= Class::MOP::get_metaclass_by_name($superclass_name)
Aresult
11
00
248 || return 1;
249  
250 3384 my $super_meta_name = $super_meta->_real_ref_name;
251  
252 3384 return $self->_is_compatible_with($super_meta_name);
253 }
254  
255 19691 ▶sub _check_single_metaclass_compatibility {
Subroutine _check_single_metaclass_compatibility: called CC=2 SCAR=6.9
256 19691 my $self = shift;
257 19691 my ( $metaclass_type, $superclass_name ) = @_;
258  
259 19691 ▶ if (!$self->_single_metaclass_is_compatible($metaclass_type, $superclass_name)) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->_single_metaclass_is_compatible($metaclass_type, $superclass_name)) 10 19681 19691
260 10 my $super_meta = Class::MOP::get_metaclass_by_name($superclass_name);
261  
262 10 $self->_throw_exception( MetaclassTypeIncompatible => class_name => $self->name,
263 superclass_name => $superclass_name,
264 metaclass_type => $metaclass_type
265 );
266 }
267 }
268  
269 20181 ▶sub _single_metaclass_is_compatible {
Subroutine _single_metaclass_is_compatible: called CC=5 SCAR=16.1
270 20181 my $self = shift;
271 20181 my ( $metaclass_type, $superclass_name ) = @_;
272  
273 20181 ▶ my $super_meta = Class::MOP::get_metaclass_by_name($superclass_name)
Condition50%1/2
Class::MOP::get_metaclass_by_name($superclass_name) || (return 1)
l!l
201810
MC/DC0%0/1
Class::MOP::get_metaclass_by_name($superclass_name) || (return 1)
Class::MOP::get_metaclass_by_name($superclass_name)
Truth table50%1/2
A
A= Class::MOP::get_metaclass_by_name($superclass_name)
Aresult
11
00
274 || return 1;
275  
276 # for instance, Moose::Meta::Class has a error_class attribute, but
277 # Class::MOP::Class doesn't - this shouldn't be an error
278 20181 ▶ return 1 unless $super_meta->can($metaclass_type);
Branch: 1 branch
BranchTrueFalseTotal
unless $super_meta->can($metaclass_type) 3 20178 20181
279 # for instance, Moose::Meta::Class has a destructor_class, but
280 # Class::MOP::Class doesn't - this shouldn't be an error
281 20178 ▶ return 1 unless defined $super_meta->$metaclass_type;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $super_meta->$metaclass_type 848 19330 20178
282 # if metaclass is defined in superclass but not here, it's not compatible
283 # this is a really odd case
284 19330 ▶ return 0 unless defined $self->$metaclass_type;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->$metaclass_type 19 19311 19330
285  
286 19311 return $self->$metaclass_type->_is_compatible_with($super_meta->$metaclass_type);
287 }
288  
289 18100 ▶sub _fix_metaclass_incompatibility {
Subroutine _fix_metaclass_incompatibility: called CC=9 SCAR=22.0
290 18100 my $self = shift;
291 18100 my @supers = map { Class::MOP::Class->initialize($_) } @_;
292  
293 18100 my $necessary = 0;
294 18100 for my $super (@supers) {
295 23017 ▶ $necessary = 1
Branch: 1 branch
BranchTrueFalseTotal
if $self->_can_fix_metaclass_incompatibility($super) 81 22935 23016
296 if $self->_can_fix_metaclass_incompatibility($super);
297 }
298 18099 ▶ return unless $necessary;
Branch: 1 branch
BranchTrueFalseTotal
unless $necessary 18018 81 18099
299  
300 81 for my $super (@supers) {
301 87 ▶ if (!$self->_class_metaclass_is_compatible($super->name)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->_class_metaclass_is_compatible($super->name)) 67 20 87
302 67 $self->_fix_class_metaclass_incompatibility($super);
303 }
304 }
305  
306 78 my %base_metaclass = $self->_base_metaclasses;
307 78 for my $metaclass_type (keys %base_metaclass) {
308 466 for my $super (@supers) {
309 490 ▶ if (!$self->_single_metaclass_is_compatible($metaclass_type, $super->name)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->_single_metaclass_is_compatible($metaclass_type, $super->name)) 136 354 490
310 136 $self->_fix_single_metaclass_incompatibility(
311 $metaclass_type, $super
312 );
313 }
314 }
315 }
316 }
317  
318 23017 ▶sub _can_fix_metaclass_incompatibility {
Subroutine _can_fix_metaclass_incompatibility: called CC=4 SCAR=13.9
319 23017 my $self = shift;
320 23017 my ($super_meta) = @_;
321  
322 23017 ▶ return 1 if $self->_class_metaclass_can_be_made_compatible($super_meta);
Branch: 1 branch
BranchTrueFalseTotal
if $self->_class_metaclass_can_be_made_compatible($super_meta) 66 22951 23017
323  
324 22951 my %base_metaclass = $self->_base_metaclasses;
325 22951 for my $metaclass_type (keys %base_metaclass) {
326 137673 ▶ return 1 if $self->_single_metaclass_can_be_made_compatible($super_meta, $metaclass_type);
Branch: 1 branch
BranchTrueFalseTotal
if $self->_single_metaclass_can_be_made_compatible($super_meta, $metaclass_type) 15 137657 137672
327 }
328  
329 22935 return;
330 }
331  
332 23119 ▶sub _class_metaclass_can_be_made_compatible {
Subroutine _class_metaclass_can_be_made_compatible: called CC=1 SCAR=0.0
333 23119 my $self = shift;
334 23119 my ($super_meta) = @_;
335  
336 23119 return $self->_can_be_made_compatible_with($super_meta->_real_ref_name);
337 }
338  
339 137933 ▶sub _single_metaclass_can_be_made_compatible {
Subroutine _single_metaclass_can_be_made_compatible: called CC=5 SCAR=16.1
340 137933 my $self = shift;
341 137933 my ($super_meta, $metaclass_type) = @_;
342  
343 137933 my $specific_meta = $self->$metaclass_type;
344  
345 137933 ▶ return unless $super_meta->can($metaclass_type);
Branch: 1 branch
BranchTrueFalseTotal
unless $super_meta->can($metaclass_type) 1 137932 137933
346 137932 my $super_specific_meta = $super_meta->$metaclass_type;
347  
348 # for instance, Moose::Meta::Class has a destructor_class, but
349 # Class::MOP::Class doesn't - this shouldn't be an error
350 137932 ▶ return unless defined $super_specific_meta;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $super_specific_meta 20572 117360 137932
351  
352 # if metaclass is defined in superclass but not here, it's fixable
353 # this is a really odd case
354 117360 ▶ return 1 unless defined $specific_meta;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $specific_meta 21 117339 117360
355  
356 117339 ▶ return 1 if $specific_meta->_can_be_made_compatible_with($super_specific_meta);
Branch: 1 branch
BranchTrueFalseTotal
if $specific_meta->_can_be_made_compatible_with($super_specific_meta) 129 117209 117338
357 }
358  
359 67 ▶sub _fix_class_metaclass_incompatibility {
Subroutine _fix_class_metaclass_incompatibility: called CC=3 SCAR=11.0
360 67 my $self = shift;
361 67 my ( $super_meta ) = @_;
362  
363 67 ▶ if ($self->_class_metaclass_can_be_made_compatible($super_meta)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_class_metaclass_can_be_made_compatible($super_meta)) 66 1 67
364 66 ▶ ($self->is_pristine)
Branch: 1 branch
BranchTrueFalseTotal
$self->is_pristine or $self->_throw_exception("CannotFixMetaclassCompatibility", "class_name", $self->name, "superclass", $super_meta) 63 3 66
365 || $self->_throw_exception( CannotFixMetaclassCompatibility => class_name => $self->name,
366 superclass => $super_meta
367 );
368  
369 63 my $super_meta_name = $super_meta->_real_ref_name;
370  
371 63 $self->_make_compatible_with($super_meta_name);
372 }
373 }
374  
375 136 ▶sub _fix_single_metaclass_incompatibility {
Subroutine _fix_single_metaclass_incompatibility: called CC=4 SCAR=13.9
376 136 my $self = shift;
377 136 my ( $metaclass_type, $super_meta ) = @_;
378  
379 136 ▶ if ($self->_single_metaclass_can_be_made_compatible($super_meta, $metaclass_type)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_single_metaclass_can_be_made_compatible($super_meta, $metaclass_type)) 135 1 136
380 135 ▶ ($self->is_pristine)
Branch: 1 branch
BranchTrueFalseTotal
$self->is_pristine or $self->_throw_exception("CannotFixMetaclassCompatibility", "class_name", $self->name, "superclass", $super_meta, "metaclass_type", $metaclass_type) 133 2 135
381 || $self->_throw_exception( CannotFixMetaclassCompatibility => class_name => $self->name,
382 superclass => $super_meta,
383 metaclass_type => $metaclass_type
384 );
385  
386 133 ▶ my $new_metaclass = $self->$metaclass_type
Branch: 1 branch
BranchTrueFalseTotal
$self->$metaclass_type ? : 114 19 133
387 ? $self->$metaclass_type->_get_compatible_metaclass($super_meta->$metaclass_type)
388 : $super_meta->$metaclass_type;
389 133 $self->{$metaclass_type} = $new_metaclass;
390 }
391 }
392  
393 112 ▶sub _restore_metaobjects_from {
Subroutine _restore_metaobjects_from: called CC=1 SCAR=0.0
394 112 my $self = shift;
395 112 my ($old_meta) = @_;
396  
397 112 $self->_restore_metamethods_from($old_meta);
398 109 $self->_restore_metaattributes_from($old_meta);
399 }
400  
401 113 ▶sub _remove_generated_metaobjects {
Subroutine _remove_generated_metaobjects: called CC=2 SCAR=6.9
402 113 my $self = shift;
403  
404 113 for my $attr (map { $self->get_attribute($_) } $self->get_attribute_list) {
405 25 $attr->remove_accessors;
406 }
407 }
408  
409 # creating classes with MOP ...
410  
411 1522 ▶sub create {
Subroutine create: called CC=15 SCAR=27.1
Pod documented
412 1522 my $class = shift;
413 1522 my @args = @_;
414  
415 1522 ▶ unshift @args, 'package' if @args % 2 == 1;
Branch: 1 branch
BranchTrueFalseTotal
if @args % 2 == 1 1517 5 1522
416 1522 my %options = @args;
417  
418 (ref $options{superclasses} eq 'ARRAY')
419 || __PACKAGE__->_throw_exception( CreateMOPClassTakesArrayRefOfSuperclasses => class => $class,
420 params => \%options
421 )
422 1522 ▶ if exists $options{superclasses};
Branch: 1 branch
BranchTrueFalseTotal
if exists $options{"superclasses"} 1430 92 1522
Condition100%4/4
exists $options{'superclasses'} && (ref $options{'superclasses'} eq 'ARRAY' || __PACKAGE__->_throw_exception('CreateMOPClassTakesArrayRefOfSuperclasses', 'class', $class, 'params', \%options))
!ll
921430
ref $options{"superclasses"} eq "ARRAY" or __PACKAGE__->_throw_exception("CreateMOPClassTakesArrayRefOfSuperclasses", "class", $class, "params", \%options)
l!l
14291
MC/DC0%0/3
exists $options{'superclasses'} && (ref $options{'superclasses'} eq 'ARRAY' || __PACKAGE__->_throw_exception('CreateMOPClassTakesArrayRefOfSuperclasses', 'class', $class, 'params', \%options))
exists $options{'superclasses'} ref $options{"superclasses"} eq "ARRAY" __PACKAGE__->_throw_exception("CreateMOPClassTakesArrayRefOfSuperclasses", "class", $class, "params", \%options)
Truth table0%0/4
A && B or C
A= exists $options{'superclasses'}B= ref $options{"superclasses"} eq "ARRAY"C= __PACKAGE__->_throw_exception("CreateMOPClassTakesArrayRefOfSuperclasses", "class", $class, "params", \%options)
ABCresult
0XX0
1000
11X1
1011
423  
424 (ref $options{attributes} eq 'ARRAY')
425 || __PACKAGE__->_throw_exception( CreateMOPClassTakesArrayRefOfAttributes => class => $class,
426 params => \%options
427 )
428 1521 ▶ if exists $options{attributes};
Branch: 1 branch
BranchTrueFalseTotal
if exists $options{"attributes"} 24 1497 1521
Condition80%4/5
exists $options{'attributes'} && (ref $options{'attributes'} eq 'ARRAY' || __PACKAGE__->_throw_exception('CreateMOPClassTakesArrayRefOfAttributes', 'class', $class, 'params', \%options))
!ll
149724
ref $options{"attributes"} eq "ARRAY" or __PACKAGE__->_throw_exception("CreateMOPClassTakesArrayRefOfAttributes", "class", $class, "params", \%options)
l!l&&r!l&&!r
2310
MC/DC0%0/3
exists $options{'attributes'} && (ref $options{'attributes'} eq 'ARRAY' || __PACKAGE__->_throw_exception('CreateMOPClassTakesArrayRefOfAttributes', 'class', $class, 'params', \%options))
exists $options{'attributes'} ref $options{"attributes"} eq "ARRAY" __PACKAGE__->_throw_exception("CreateMOPClassTakesArrayRefOfAttributes", "class", $class, "params", \%options)
Truth table0%0/4
A && B or C
A= exists $options{'attributes'}B= ref $options{"attributes"} eq "ARRAY"C= __PACKAGE__->_throw_exception("CreateMOPClassTakesArrayRefOfAttributes", "class", $class, "params", \%options)
ABCresult
0XX0
1000
11X1
1011
429  
430 (ref $options{methods} eq 'HASH')
431 || __PACKAGE__->_throw_exception( CreateMOPClassTakesHashRefOfMethods => class => $class,
432 params => \%options
433 )
434 1520 ▶ if exists $options{methods};
Branch: 1 branch
BranchTrueFalseTotal
if exists $options{"methods"} 26 1494 1520
Condition80%4/5
exists $options{'methods'} && (ref $options{'methods'} eq 'HASH' || __PACKAGE__->_throw_exception('CreateMOPClassTakesHashRefOfMethods', 'class', $class, 'params', \%options))
!ll
149426
ref $options{"methods"} eq "HASH" or __PACKAGE__->_throw_exception("CreateMOPClassTakesHashRefOfMethods", "class", $class, "params", \%options)
l!l&&r!l&&!r
2510
MC/DC0%0/3
exists $options{'methods'} && (ref $options{'methods'} eq 'HASH' || __PACKAGE__->_throw_exception('CreateMOPClassTakesHashRefOfMethods', 'class', $class, 'params', \%options))
exists $options{'methods'} ref $options{"methods"} eq "HASH" __PACKAGE__->_throw_exception("CreateMOPClassTakesHashRefOfMethods", "class", $class, "params", \%options)
Truth table0%0/4
A && B or C
A= exists $options{'methods'}B= ref $options{"methods"} eq "HASH"C= __PACKAGE__->_throw_exception("CreateMOPClassTakesHashRefOfMethods", "class", $class, "params", \%options)
ABCresult
0XX0
1000
11X1
1011
435  
436 1519 my $package = delete $options{package};
437 1519 my $superclasses = delete $options{superclasses};
438 1519 my $attributes = delete $options{attributes};
439 1519 my $methods = delete $options{methods};
440 my $meta_name = exists $options{meta_name}
441 ? delete $options{meta_name}
442 1519 ▶ : 'meta';
Branch: 1 branch
BranchTrueFalseTotal
exists $options{'meta_name'} ? : 2 1517 1519
443  
444 1519 my $meta = $class->SUPER::create($package => %options);
445  
446 1514 ▶ $meta->_add_meta_method($meta_name)
Branch: 1 branch
BranchTrueFalseTotal
if defined $meta_name 1513 1 1514
447 if defined $meta_name;
448  
449 1514 ▶ $meta->superclasses(@{$superclasses})
Branch: 1 branch
BranchTrueFalseTotal
if defined $superclasses 1429 85 1514
450 if defined $superclasses;
451 # NOTE:
452 # process attributes first, so that they can
453 # install accessors, but locally defined methods
454 # can then overwrite them. It is maybe a little odd, but
455 # I think this should be the order of things.
456 1503 ▶ if (defined $attributes) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $attributes) 23 1480 1503
457 23 foreach my $attr (@{$attributes}) {
458 25 $meta->add_attribute($attr);
459 }
460 }
461 1502 ▶ if (defined $methods) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $methods) 25 1477 1502
462 25 foreach my $method_name (keys %{$methods}) {
463 32 $meta->add_method($method_name, $methods->{$method_name});
464 }
465 }
466 1502 return $meta;
467 }
468  
469 # XXX: something more intelligent here?
470 67 ▶sub _anon_package_prefix { 'Class::MOP::Class::__ANON__::SERIAL::' }
Subroutine _anon_package_prefix: called CC=1 SCAR=0.0
471  
472 2774 ▶sub create_anon_class { shift->create_anon(@_) }
Subroutine create_anon_class: called CC=1 SCAR=0.0
Pod documented
473 7 ▶sub is_anon_class { shift->is_anon(@_) }
Subroutine is_anon_class: called CC=1 SCAR=0.0
Pod documented
474  
475 0 ▶sub _anon_cache_key {
Subroutine _anon_cache_key: not called CC=2 SCAR=17.9
476 0 my $class = shift;
477 0 my %options = @_;
478 # Makes something like Super::Class|Super::Class::2
479 return join '=' => (
480 0 ▶ join( '|', sort @{ $options{superclasses} || [] } ),
Condition0%0/2
$options{'superclasses'} || []
l!l
00
MC/DC0%0/1
$options{'superclasses'} || []
$options{'superclasses'}
Truth table0%0/2
A
A= $options{'superclasses'}
Aresult
11
00
481 );
482 }
483  
484 # Instance Construction & Cloning
485  
486 21157 ▶sub new_object {
Subroutine new_object: called CC=2 SCAR=6.9
Pod documented
487 21157 my $class = shift;
488  
489 # NOTE:
490 # we need to protect the integrity of the
491 # Class::MOP::Class singletons here, so we
492 # delegate this to &construct_class_instance
493 # which will deal with the singletons
494 21157 ▶ return $class->_construct_class_instance(@_)
Branch: 1 branch
BranchTrueFalseTotal
if $class->name->isa("Class::MOP::Class") 6 21151 21157
495 if $class->name->isa('Class::MOP::Class');
496 21151 return $class->_construct_instance(@_);
497 }
498  
499 23859 ▶sub _construct_instance {
Subroutine _construct_instance: called CC=7 SCAR=19.5
500 23859 my $class = shift;
501 23859 ▶ my $params = @_ == 1 ? $_[0] : {@_};
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 ? : 23266 593 23859
502 23859 my $meta_instance = $class->get_meta_instance();
503 # FIXME:
504 # the code below is almost certainly incorrect
505 # but this is foreign inheritance, so we might
506 # have to kludge it in the end.
507 23859 my $instance;
508 23859 ▶ if (my $instance_class = blessed($params->{__INSTANCE__})) {
Branch: 2 branches
BranchTrueFalseTotal
if (my $instance_class = builtin::blessed($params->{'__INSTANCE__'})) { } 9 23850 23859
elsif (exists $params->{'__INSTANCE__'}) { } 5 23845 23850
509 ($instance_class eq $class->name)
510 || $class->_throw_exception( InstanceBlessedIntoWrongClass => class_name => $class->name,
511 params => $params,
512 instance => $params->{__INSTANCE__}
513 9 ▶ );
Branch: 1 branch
BranchTrueFalseTotal
$instance_class eq $class->name or $class->_throw_exception("InstanceBlessedIntoWrongClass", "class_name", $class->name, "params", $params, "instance", $params->{"__INSTANCE__"}) 6 3 9
514 6 $instance = $params->{__INSTANCE__};
515 }
516 elsif (exists $params->{__INSTANCE__}) {
517 $class->_throw_exception( InstanceMustBeABlessedReference => class_name => $class->name,
518 params => $params,
519 instance => $params->{__INSTANCE__}
520 5 );
521 }
522 else {
523 23845 $instance = $meta_instance->create_instance();
524 }
525 23851 foreach my $attr ($class->get_all_attributes()) {
526 169981 $attr->initialize_instance_slot($meta_instance, $instance, $params);
527 }
528 22857 ▶ if (Class::MOP::metaclass_is_weak($class->name)) {
Branch: 1 branch
BranchTrueFalseTotal
if (Class::MOP::metaclass_is_weak($class->name)) 1683 21174 22857
529 1683 $meta_instance->_set_mop_slot($instance, $class);
530 }
531 22857 return $instance;
532 }
533  
534 12484 ▶sub _inline_new_object {
Subroutine _inline_new_object: called CC=1 SCAR=0.0
535 12484 my $self = shift;
536  
537 return (
538 12484 'my $class = shift;',
539 '$class = Scalar::Util::blessed($class) || $class;',
540 $self->_inline_fallback_constructor('$class'),
541 $self->_inline_params('$params', '$class'),
542 $self->_inline_generate_instance('$instance', '$class'),
543 $self->_inline_slot_initializers,
544 $self->_inline_preserve_weak_metaclasses,
545 $self->_inline_extra_init,
546 'return $instance',
547 );
548 }
549  
550 12484 ▶sub _inline_fallback_constructor {
Subroutine _inline_fallback_constructor: called CC=1 SCAR=0.0
551 12484 my $self = shift;
552 12484 my ($class) = @_;
553 return (
554 12484 'return ' . $self->_generate_fallback_constructor($class),
555 'if ' . $class . ' ne \'' . $self->name . '\';',
556 );
557 }
558  
559 11723 ▶sub _generate_fallback_constructor {
Subroutine _generate_fallback_constructor: called CC=1 SCAR=0.0
560 11723 my $self = shift;
561 11723 my ($class) = @_;
562 11723 return 'Class::MOP::Class->initialize(' . $class . ')->new_object(@_)',
563 }
564  
565 11723 ▶sub _inline_params {
Subroutine _inline_params: called CC=1 SCAR=0.0
566 11723 my $self = shift;
567 11723 my ($params, $class) = @_;
568 return (
569 11723 'my ' . $params . ' = @_ == 1 ? $_[0] : {@_};',
570 );
571 }
572  
573 12484 ▶sub _inline_generate_instance {
Subroutine _inline_generate_instance: called CC=1 SCAR=0.0
574 12484 my $self = shift;
575 12484 my ($inst, $class) = @_;
576 return (
577 12484 'my ' . $inst . ' = ' . $self->_inline_create_instance($class) . ';',
578 );
579 }
580  
581 12484 ▶sub _inline_create_instance {
Subroutine _inline_create_instance: called CC=1 SCAR=0.0
582 12484 my $self = shift;
583  
584 12484 return $self->get_meta_instance->inline_create_instance(@_);
585 }
586  
587 12484 ▶sub _inline_slot_initializers {
Subroutine _inline_slot_initializers: called CC=1 SCAR=0.0
588 12484 my $self = shift;
589  
590 12484 my $idx = 0;
591  
592 112706 return map { $self->_inline_slot_initializer($_, $idx++) }
593 12484 sort { $a->name cmp $b->name } $self->get_all_attributes;
594 }
595  
596 112706 ▶sub _inline_slot_initializer {
Subroutine _inline_slot_initializer: called CC=4 SCAR=13.9
597 112706 my $self = shift;
598 112706 my ($attr, $idx) = @_;
599  
600 112706 ▶ if (defined(my $init_arg = $attr->init_arg)) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined(my $init_arg = $attr->init_arg)) { } 108462 4244 112706
elsif (my(@default) = $self->_inline_init_attr_from_default($attr, $idx)) { } 3858 386 4244
601 108462 my @source = (
602 'if (exists $params->{\'' . $init_arg . '\'}) {',
603 $self->_inline_init_attr_from_constructor($attr, $idx),
604 '}',
605 );
606 108462 ▶ if (my @default = $self->_inline_init_attr_from_default($attr, $idx)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@default) = $self->_inline_init_attr_from_default($attr, $idx)) 34655 73807 108462
607 34655 push @source, (
608 'else {',
609 @default,
610 '}',
611 );
612 }
613 108462 return @source;
614 }
615 elsif (my @default = $self->_inline_init_attr_from_default($attr, $idx)) {
616 return (
617 3858 '{',
618 @default,
619 '}',
620 );
621 }
622 else {
623 386 return ();
624 }
625 }
626  
627 105975 ▶sub _inline_init_attr_from_constructor {
Subroutine _inline_init_attr_from_constructor: called CC=2 SCAR=7.0
628 105975 my $self = shift;
629 105975 my ($attr, $idx) = @_;
630  
631 105975 my @initial_value = $attr->_inline_set_value(
632 '$instance', '$params->{\'' . $attr->init_arg . '\'}',
633 );
634  
635 105975 ▶ push @initial_value, (
Branch: 1 branch
BranchTrueFalseTotal
if $attr->has_initializer 0 105975 105975
636 '$attrs->[' . $idx . ']->set_initial_value(',
637 '$instance,',
638 $attr->_inline_instance_get('$instance'),
639 ');',
640 ) if $attr->has_initializer;
641  
642 105975 return @initial_value;
643 }
644  
645 110210 ▶sub _inline_init_attr_from_default {
Subroutine _inline_init_attr_from_default: called CC=3 SCAR=11.0
646 110210 my $self = shift;
647 110210 my ($attr, $idx) = @_;
648  
649 110210 my $default = $self->_inline_default_value($attr, $idx);
650 110210 ▶ return unless $default;
Branch: 1 branch
BranchTrueFalseTotal
unless $default 71838 38372 110210
651  
652 38372 my @initial_value = $attr->_inline_set_value('$instance', $default);
653  
654 38372 ▶ push @initial_value, (
Branch: 1 branch
BranchTrueFalseTotal
if $attr->has_initializer 0 38372 38372
655 '$attrs->[' . $idx . ']->set_initial_value(',
656 '$instance,',
657 $attr->_inline_instance_get('$instance'),
658 ');',
659 ) if $attr->has_initializer;
660  
661 38372 return @initial_value;
662 }
663  
664 111431 ▶sub _inline_default_value {
Subroutine _inline_default_value: called CC=4 SCAR=13.9
665 111431 my $self = shift;
666 111431 my ($attr, $index) = @_;
667  
668 111431 ▶ if ($attr->has_default) {
Branch: 2 branches
BranchTrueFalseTotal
if ($attr->has_default) { } 38510 72921 111431
elsif ($attr->has_builder) { } 3 72918 72921
669 # NOTE:
670 # default values can either be CODE refs
671 # in which case we need to call them. Or
672 # they can be scalars (strings/numbers)
673 # in which case we can just deal with them
674 # in the code we eval.
675 38510 ▶ if ($attr->is_default_a_coderef) {
Branch: 1 branch
BranchTrueFalseTotal
if ($attr->is_default_a_coderef) { } 27219 11291 38510
676 27219 return '$defaults->[' . $index . ']->($instance)';
677 }
678 else {
679 11291 return '$defaults->[' . $index . ']';
680 }
681 }
682 elsif ($attr->has_builder) {
683 3 return '$instance->' . $attr->builder;
684 }
685 else {
686 72918 return;
687 }
688 }
689  
690 12484 ▶sub _inline_preserve_weak_metaclasses {
Subroutine _inline_preserve_weak_metaclasses: called CC=2 SCAR=6.9
691 12484 my $self = shift;
692 12484 ▶ if (Class::MOP::metaclass_is_weak($self->name)) {
Branch: 1 branch
BranchTrueFalseTotal
if (Class::MOP::metaclass_is_weak($self->name)) { } 21 12463 12484
693 return (
694 21 $self->_inline_set_mop_slot(
695 '$instance', 'Class::MOP::class_of($class)'
696 ) . ';'
697 );
698 }
699 else {
700 12463 return ();
701 }
702 }
703  
704 11693 ▶sub _inline_extra_init { }
Subroutine _inline_extra_init: called CC=1 SCAR=0.0
705  
706 12485 ▶sub _eval_environment {
Subroutine _eval_environment: called CC=1 SCAR=0.0
707 12485 my $self = shift;
708  
709 12485 my @attrs = sort { $a->name cmp $b->name } $self->get_all_attributes;
710  
711 12485 my $defaults = [map { $_->default } @attrs];
712  
713 return {
714 12485 '$defaults' => \$defaults,
715 };
716 }
717  
718  
719 142141 ▶sub get_meta_instance {
Subroutine get_meta_instance: called CC=2 SCAR=7.1
Pod documented
720 142141 my $self = shift;
721 142141 ▶ $self->{'_meta_instance'} ||= $self->_create_meta_instance();
Condition66%2/3
$self->{"_meta_instance"} ||= $self->_create_meta_instance
l!l&&r!l&&!r
120588215530
MC/DC0%0/2
$self->{"_meta_instance"} ||= $self->_create_meta_instance
$self->{"_meta_instance"} $self->_create_meta_instance
Truth table66%2/3
A ||= B
A= $self->{"_meta_instance"}B= $self->_create_meta_instance
ABresult
1X1
011
000
722 }
723  
724 21553 ▶sub _create_meta_instance {
Subroutine _create_meta_instance: called CC=2 SCAR=6.9
725 21553 my $self = shift;
726  
727 21553 my $instance = $self->instance_metaclass->new(
728 associated_metaclass => $self,
729 attributes => [ $self->get_all_attributes() ],
730 );
731  
732 21553 ▶ $self->add_meta_instance_dependencies()
Branch: 1 branch
BranchTrueFalseTotal
if $instance->is_dependent_on_superclasses 4 21549 21553
733 if $instance->is_dependent_on_superclasses();
734  
735 21553 return $instance;
736 }
737  
738 # TODO: this is actually not being used!
739 0 ▶sub _inline_rebless_instance {
Subroutine _inline_rebless_instance: not called CC=1 SCAR=6.9
740 0 my $self = shift;
741  
742 0 return $self->get_meta_instance->inline_rebless_instance_structure(@_);
743 }
744  
745 0 ▶sub _inline_get_mop_slot {
Subroutine _inline_get_mop_slot: not called CC=1 SCAR=6.9
746 0 my $self = shift;
747  
748 0 return $self->get_meta_instance->_inline_get_mop_slot(@_);
749 }
750  
751 21 ▶sub _inline_set_mop_slot {
Subroutine _inline_set_mop_slot: called CC=1 SCAR=0.0
752 21 my $self = shift;
753  
754 21 return $self->get_meta_instance->_inline_set_mop_slot(@_);
755 }
756  
757 0 ▶sub _inline_clear_mop_slot {
Subroutine _inline_clear_mop_slot: not called CC=1 SCAR=6.9
758 0 my $self = shift;
759  
760 0 return $self->get_meta_instance->_inline_clear_mop_slot(@_);
761 }
762  
763 27 ▶sub clone_object {
Subroutine clone_object: called CC=4 SCAR=13.9
Pod documented
764 27 my $class = shift;
765 27 my $instance = shift;
766 27 ▶ (blessed($instance) && $instance->isa($class->name))
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($instance) and $instance->isa($class->name) or $class->_throw_exception("CloneObjectExpectsAnInstanceOfMetaclass", "class_name", $class->name, "instance", $instance) 22 5 27
Condition100%3/3
builtin::blessed($instance) and $instance->isa($class->name)
!ll&&!rl&&r
4122
MC/DC100%2/2
builtin::blessed($instance) and $instance->isa($class->name)
builtin::blessed($instance) $instance->isa($class->name)
Truth table100%3/3
A and B
A= builtin::blessed($instance)B= $instance->isa($class->name)
ABresult
0X0
100
111
767 || $class->_throw_exception( CloneObjectExpectsAnInstanceOfMetaclass => class_name => $class->name,
768 instance => $instance,
769 );
770 # NOTE:
771 # we need to protect the integrity of the
772 # Class::MOP::Class singletons here, they
773 # should not be cloned.
774 22 ▶ return $instance if $instance->isa('Class::MOP::Class');
Branch: 1 branch
BranchTrueFalseTotal
if $instance->isa("Class::MOP::Class") 5 17 22
775 17 $class->_clone_instance($instance, @_);
776 }
777  
778 18 ▶sub _clone_instance {
Subroutine _clone_instance: called CC=5 SCAR=16.1
779 18 my ($class, $instance, %params) = @_;
780 18 ▶ (blessed($instance))
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($instance) or $class->_throw_exception("OnlyInstancesCanBeCloned", "class_name", $class->name, "instance", $instance, "params", \%params) 17 1 18
781 || $class->_throw_exception( OnlyInstancesCanBeCloned => class_name => $class->name,
782 instance => $instance,
783 params => \%params
784 );
785 17 my $meta_instance = $class->get_meta_instance();
786 17 my $clone = $meta_instance->clone_instance($instance);
787 17 foreach my $attr ($class->get_all_attributes()) {
788 172 ▶ if ( defined( my $init_arg = $attr->init_arg ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $init_arg = $attr->init_arg)) 163 9 172
789 163 ▶ if (exists $params{$init_arg}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $params{$init_arg}) 15 148 163
790 15 $attr->set_value($clone, $params{$init_arg});
791 }
792 }
793 }
794 17 return $clone;
795 }
796  
797 126 ▶sub _force_rebless_instance {
Subroutine _force_rebless_instance: called CC=4 SCAR=13.9
798 126 my ($self, $instance, %params) = @_;
799 126 my $old_metaclass = Class::MOP::class_of($instance);
800  
801 126 ▶ $old_metaclass->rebless_instance_away($instance, $self, %params)
Branch: 1 branch
BranchTrueFalseTotal
if $old_metaclass 126 0 126
802 if $old_metaclass;
803  
804 126 my $meta_instance = $self->get_meta_instance;
805  
806 126 ▶ if (Class::MOP::metaclass_is_weak($old_metaclass->name)) {
Branch: 1 branch
BranchTrueFalseTotal
if (Class::MOP::metaclass_is_weak($old_metaclass->name)) 1 125 126
807 1 $meta_instance->_clear_mop_slot($instance);
808 }
809  
810 # rebless!
811 # we use $_[1] here because of t/cmop/rebless_overload.t regressions
812 # on 5.8.8
813 126 $meta_instance->rebless_instance_structure($_[1], $self);
814  
815 126 $self->_fixup_attributes_after_rebless($instance, $old_metaclass, %params);
816  
817 122 ▶ if (Class::MOP::metaclass_is_weak($self->name)) {
Branch: 1 branch
BranchTrueFalseTotal
if (Class::MOP::metaclass_is_weak($self->name)) 4 118 122
818 4 $meta_instance->_set_mop_slot($instance, $self);
819 }
820 }
821  
822 45 ▶sub rebless_instance {
Subroutine rebless_instance: called CC=3 SCAR=11.0
Pod documented
823 45 my ($self, $instance, %params) = @_;
824 45 my $old_metaclass = Class::MOP::class_of($instance);
825  
826 45 ▶ my $old_class = $old_metaclass ? $old_metaclass->name : blessed($instance);
Branch: 1 branch
BranchTrueFalseTotal
$old_metaclass ? : 45 0 45
827 45 ▶ $self->name->isa($old_class)
Branch: 1 branch
BranchTrueFalseTotal
$self->name->isa($old_class) or $self->_throw_exception("CanReblessOnlyIntoASubclass", "class_name", $self->name, "instance", $instance, "instance_class", builtin::blessed($instance), "params", \%params) 42 3 45
828 || $self->_throw_exception( CanReblessOnlyIntoASubclass => class_name => $self->name,
829 instance => $instance,
830 instance_class => blessed( $instance ),
831 params => \%params,
832 );
833  
834 42 $self->_force_rebless_instance($_[1], %params);
835  
836 38 return $instance;
837 }
838  
839 9 ▶sub rebless_instance_back {
Subroutine rebless_instance_back: called CC=3 SCAR=11.0
Pod documented
840 9 my ( $self, $instance ) = @_;
841 9 my $old_metaclass = Class::MOP::class_of($instance);
842 9 ▶ my $old_class
Branch: 1 branch
BranchTrueFalseTotal
$old_metaclass ? : 9 0 9
843 = $old_metaclass ? $old_metaclass->name : blessed($instance);
844 9 ▶ $old_class->isa( $self->name )
Branch: 1 branch
BranchTrueFalseTotal
$old_class->isa($self->name) or $self->_throw_exception("CanReblessOnlyIntoASuperclass", "class_name", $self->name, "instance", $instance, "instance_class", builtin::blessed($instance)) 6 3 9
845 || $self->_throw_exception( CanReblessOnlyIntoASuperclass => class_name => $self->name,
846 instance => $instance,
847 instance_class => blessed( $instance ),
848 );
849  
850 6 $self->_force_rebless_instance($_[1]);
851  
852 6 return $instance;
853 }
854  
855 126 ▶sub rebless_instance_away {
Subroutine rebless_instance_away: called CC=1 SCAR=0.0
Pod undocumented
856 # this intentionally does nothing, it is just a hook
857 }
858  
859 126 ▶sub _fixup_attributes_after_rebless {
Subroutine _fixup_attributes_after_rebless: called CC=9 SCAR=22.0
860 126 my $self = shift;
861 126 my ($instance, $rebless_from, %params) = @_;
862 126 my $meta_instance = $self->get_meta_instance;
863  
864 126 for my $attr ( $rebless_from->get_all_attributes ) {
865 1337 ▶ next if $self->find_attribute_by_name( $attr->name );
Branch: 1 branch
BranchTrueFalseTotal
if $self->find_attribute_by_name($attr->name) 1334 3 1337
866 3 $meta_instance->deinitialize_slot( $instance, $_ ) for $attr->slots;
867 }
868  
869 126 foreach my $attr ( $self->get_all_attributes ) {
870 1404 ▶ if ( $attr->has_value($instance) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($attr->has_value($instance)) 1169 235 1404
871 1169 ▶ if ( defined( my $init_arg = $attr->init_arg ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $init_arg = $attr->init_arg)) { } 917 252 1169
872 $params{$init_arg} = $attr->get_value($instance)
873 917 ▶ unless exists $params{$init_arg};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $params{$init_arg} 914 3 917
874 }
875 else {
876 252 $attr->set_value($instance, $attr->get_value($instance));
877 }
878 }
879 }
880  
881 126 foreach my $attr ($self->get_all_attributes) {
882 1401 $attr->initialize_instance_slot($meta_instance, $instance, \%params);
883 }
884 }
885  
886 57885 ▶sub _attach_attribute {
Subroutine _attach_attribute: called CC=1 SCAR=0.0
887 57885 my ($self, $attribute) = @_;
888 57885 $attribute->attach_to_class($self);
889 }
890  
891 57885 ▶sub _post_add_attribute {
Subroutine _post_add_attribute: called CC=1 SCAR=0.0
892 57885 my ( $self, $attribute ) = @_;
893  
894 57885 $self->invalidate_meta_instances;
895  
896 # invalidate package flag here
897 57885 ▶ try {
Subroutine __ANON__: called CC=1 SCAR=0.0
898 57885 local $SIG{__DIE__};
899 57885 $attribute->install_accessors;
900 }
901 22 ▶ catch {
Subroutine __ANON__: called CC=1 SCAR=0.0
902 22 $self->remove_attribute( $attribute->name );
903 5 die $_;
904 57885 };
905 }
906  
907 47 ▶sub remove_attribute {
Subroutine remove_attribute: called CC=2 SCAR=6.9
Pod documented
908 47 my $self = shift;
909  
910 47 ▶ my $removed_attribute = $self->SUPER::remove_attribute(@_)
Branch: 1 branch
BranchTrueFalseTotal
my $removed_attribute = $self->SUPER::remove_attribute(@_) or return 43 1 44
911 or return;
912  
913 43 $self->invalidate_meta_instances;
914  
915 43 $removed_attribute->remove_accessors;
916 26 $removed_attribute->detach_from_class;
917  
918 26 return$removed_attribute;
919 }
920  
921 24622 ▶sub find_attribute_by_name {
Subroutine find_attribute_by_name: called CC=3 SCAR=11.0
Pod documented
922 24622 my ( $self, $attr_name ) = @_;
923  
924 24622 foreach my $class ( $self->linearized_isa ) {
925 # fetch the meta-class ...
926 30898 my $meta = Class::MOP::Class->initialize($class);
927 30898 ▶ return $meta->get_attribute($attr_name)
Branch: 1 branch
BranchTrueFalseTotal
if $meta->has_attribute($attr_name) 24549 6349 30898
928 if $meta->has_attribute($attr_name);
929 }
930  
931 73 return;
932 }
933  
934 64521 ▶sub get_all_attributes {
Subroutine get_all_attributes: called CC=1 SCAR=0.0
Pod documented
935 64521 my $self = shift;
936 64521 my %attrs = map { %{ Class::MOP::Class->initialize($_)->_attribute_map } }
937 reverse $self->linearized_isa;
938 64521 return values %attrs;
939 }
940  
941 # Inheritance
942  
943 46200 ▶sub superclasses {
Subroutine superclasses: called CC=2 SCAR=6.9
944 46200 my $self = shift;
945  
946 46200 my $isa = $self->get_or_add_package_symbol('@ISA');
947  
948 46199 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 3578 42621 46199
949 3578 my @supers = @_;
950 3578 @{$isa} = @supers;
951  
952 # NOTE:
953 # on 5.8 and below, we need to call
954 # a method to get Perl to detect
955 # a cycle in the class hierarchy
956 3576 my $class = $self->name;
957 3576 $class->isa($class);
958  
959 # NOTE:
960 # we need to check the metaclass
961 # compatibility here so that we can
962 # be sure that the superclass is
963 # not potentially creating an issues
964 # we don't know about
965  
966 3576 $self->_check_metaclass_compatibility();
967 3556 $self->_superclasses_updated();
968 }
969  
970 46177 return @{$isa};
971 }
972  
973 3556 ▶sub _superclasses_updated {
Subroutine _superclasses_updated: called CC=1 SCAR=0.0
974 3556 my $self = shift;
975 3556 $self->update_meta_instance_dependencies();
976 # keep strong references to all our parents, so they don't disappear if
977 # they are anon classes and don't have any direct instances
978 $self->_superclass_metas(
979 3556 map { Class::MOP::class_of($_) } $self->superclasses
980 );
981 }
982  
983 3556 ▶sub _superclass_metas {
Subroutine _superclass_metas: called CC=1 SCAR=0.0
984 3556 my $self = shift;
985 3556 $self->{_superclass_metas} = [@_];
986 }
987  
988 12 ▶sub subclasses {
Subroutine subclasses: called CC=1 SCAR=0.0
Pod documented
989 12 my $self = shift;
990 12 my $super_class = $self->name;
991  
992 12 return @{ $super_class->mro::get_isarev() };
993 }
994  
995 6 ▶sub direct_subclasses {
Subroutine direct_subclasses: called CC=1 SCAR=0.0
Pod documented
996 6 my $self = shift;
997 6 my $super_class = $self->name;
998  
999 return grep {
1000 6 grep {
1001 8 $_ eq $super_class
1002 } Class::MOP::Class->initialize($_)->superclasses
1003 } $self->subclasses;
1004 }
1005  
1006 138344 ▶sub linearized_isa {
Subroutine linearized_isa: called CC=1 SCAR=0.0
Pod documented
1007 138344 return @{ mro::get_linear_isa( (shift)->name ) };
1008 }
1009  
1010 3787 ▶sub class_precedence_list {
Subroutine class_precedence_list: called CC=4 SCAR=14.6
Pod documented
1011 3787 my $self = shift;
1012 3787 my $name = $self->name;
1013  
1014 3787 ▶ unless (Class::MOP::IS_RUNNING_ON_5_10()) {
Branch: 1 branch
BranchTrueFalseTotal
unless (&Class::MOP::IS_RUNNING_ON_5_10()) 0 3787 3787
1015 # NOTE:
1016 # We need to check for circular inheritance here
1017 # if we are not on 5.10, cause 5.8 detects it late.
1018 # This will do nothing if all is well, and blow up
1019 # otherwise. Yes, it's an ugly hack, better
1020 # suggestions are welcome.
1021 # - SL
1022 0 ▶ ($name || return)->isa('This is a test for circular inheritance')
Condition0%0/2
$name || (return)
l!l
00
MC/DC0%0/1
$name || (return)
$name
Truth table0%0/2
A
A= $name
Aresult
11
00
1023 }
1024  
1025 # if our mro is c3, we can
1026 # just grab the linear_isa
1027 3787 ▶ if (mro::get_mro($name) eq 'c3') {
Branch: 1 branch
BranchTrueFalseTotal
if (&mro::get_mro($name) eq 'c3') { } 1 3786 3787
1028 1 return @{ mro::get_linear_isa($name) }
1029 }
1030 else {
1031 # NOTE:
1032 # we can't grab the linear_isa for dfs
1033 # since it has all the duplicates
1034 # already removed.
1035 return (
1036 $name,
1037 map {
1038 3786 Class::MOP::Class->initialize($_)->class_precedence_list()
1039 } $self->superclasses()
1040 );
1041 }
1042 }
1043  
1044 70950 ▶sub _method_lookup_order {
Subroutine _method_lookup_order: called CC=1 SCAR=0.0
1045 70950 return (shift->linearized_isa, 'UNIVERSAL');
1046 }
1047  
1048 ## Methods
1049  
1050 {
1051 15861 ▶ my $fetch_and_prepare_method = sub {
Subroutine __ANON__: called CC=4 SCAR=13.9
1052 15861 my ($self, $method_name) = @_;
1053 15861 my $wrapped_metaclass = $self->wrapped_method_metaclass;
1054 # fetch it locally
1055 15861 my $method = $self->get_method($method_name);
1056 # if we don't have local ...
1057 15861 ▶ unless ($method) {
Branch: 1 branch
BranchTrueFalseTotal
if ($method) { } 231 15630 15861
1058 # try to find the next method
1059 15630 $method = $self->find_next_method_by_name($method_name);
1060 # die if it does not exist
1061 15630 ▶ (defined $method)
Branch: 1 branch
BranchTrueFalseTotal
defined $method or $self->_throw_exception("MethodNameNotFoundInInheritanceHierarchy", "class_name", $self->name, "method_name", $method_name) 15628 2 15630
1062 || $self->_throw_exception( MethodNameNotFoundInInheritanceHierarchy => class_name => $self->name,
1063 method_name => $method_name
1064 );
1065 # and now make sure to wrap it
1066 # even if it is already wrapped
1067 # because we need a new sub ref
1068 15628 $method = $wrapped_metaclass->wrap($method,
1069 package_name => $self->name,
1070 name => $method_name,
1071 );
1072 }
1073 else {
1074 # now make sure we wrap it properly
1075 231 ▶ $method = $wrapped_metaclass->wrap($method,
Branch: 1 branch
BranchTrueFalseTotal
unless $method->isa($wrapped_metaclass) 222 9 231
1076 package_name => $self->name,
1077 name => $method_name,
1078 ) unless $method->isa($wrapped_metaclass);
1079 }
1080 15859 $self->add_method($method_name => $method);
1081 15859 return $method;
1082 459 };
1083  
1084 159 ▶ sub add_before_method_modifier {
Subroutine add_before_method_modifier: called CC=3 SCAR=11.0
Pod documented
1085 159 my ($self, $method_name, $method_modifier) = @_;
1086 159 ▶ (defined $method_name && length $method_name)
Branch: 1 branch
BranchTrueFalseTotal
defined $method_name and length $method_name or $self->_throw_exception("MethodModifierNeedsMethodName", "class_name", $self->name) 158 1 159
Condition66%2/3
defined $method_name and length $method_name
!ll&&!rl&&r
10158
MC/DC50%1/2
defined $method_name and length $method_name
defined $method_name length $method_name
Truth table66%2/3
A and B
A= defined $method_nameB= length $method_name
ABresult
0X0
100
111
1087 || $self->_throw_exception( MethodModifierNeedsMethodName => class_name => $self->name );
1088 158 my $method = $fetch_and_prepare_method->($self, $method_name);
1089 156 $method->add_before_modifier(
1090 set_subname(':before' => $method_modifier)
1091 );
1092 }
1093  
1094 54 ▶ sub add_after_method_modifier {
Subroutine add_after_method_modifier: called CC=3 SCAR=11.0
Pod documented
1095 54 my ($self, $method_name, $method_modifier) = @_;
1096 54 ▶ (defined $method_name && length $method_name)
Branch: 1 branch
BranchTrueFalseTotal
defined $method_name and length $method_name or $self->_throw_exception("MethodModifierNeedsMethodName", "class_name", $self->name) 53 1 54
Condition66%2/3
defined $method_name and length $method_name
!ll&&!rl&&r
1053
MC/DC50%1/2
defined $method_name and length $method_name
defined $method_name length $method_name
Truth table66%2/3
A and B
A= defined $method_nameB= length $method_name
ABresult
0X0
100
111
1097 || $self->_throw_exception( MethodModifierNeedsMethodName => class_name => $self->name );
1098 53 my $method = $fetch_and_prepare_method->($self, $method_name);
1099 53 $method->add_after_modifier(
1100 set_subname(':after' => $method_modifier)
1101 );
1102 }
1103  
1104 15651 ▶ sub add_around_method_modifier {
Subroutine add_around_method_modifier: called CC=3 SCAR=11.0
Pod documented
1105 15651 my ($self, $method_name, $method_modifier) = @_;
1106 15651 ▶ (defined $method_name && length $method_name)
Branch: 1 branch
BranchTrueFalseTotal
defined $method_name and length $method_name or $self->_throw_exception("MethodModifierNeedsMethodName", "class_name", $self->name) 15650 1 15651
Condition66%2/3
defined $method_name and length $method_name
!ll&&!rl&&r
1015650
MC/DC50%1/2
defined $method_name and length $method_name
defined $method_name length $method_name
Truth table66%2/3
A and B
A= defined $method_nameB= length $method_name
ABresult
0X0
100
111
1107 || $self->_throw_exception( MethodModifierNeedsMethodName => class_name => $self->name );
1108 15650 my $method = $fetch_and_prepare_method->($self, $method_name);
1109 15650 $method->add_around_modifier(
1110 set_subname(':around' => $method_modifier)
1111 );
1112 }
1113  
1114 # NOTE:
1115 # the methods above used to be named like this:
1116 # ${pkg}::${method}:(before|after|around)
1117 # but this proved problematic when using one modifier
1118 # to wrap multiple methods (something which is likely
1119 # to happen pretty regularly IMO). So instead of naming
1120 # it like this, I have chosen to just name them purely
1121 # with their modifier names, like so:
1122 # :(before|after|around)
1123 # The fact is that in a stack trace, it will be fairly
1124 # evident from the context what method they are attached
1125 # to, and so don't need the fully qualified name.
1126 }
1127  
1128 459 ▶sub find_method_by_name {
Subroutine find_method_by_name: called CC=5 SCAR=16.1
Pod documented
1129 35916 my ($self, $method_name) = @_;
1130 35916 ▶ (defined $method_name && length $method_name)
Branch: 1 branch
BranchTrueFalseTotal
defined $method_name and length $method_name or $self->_throw_exception("MethodNameNotGiven", "class_name", $self->name) 35915 1 35916
Condition66%2/3
defined $method_name and length $method_name
!ll&&!rl&&r
1035915
MC/DC50%1/2
defined $method_name and length $method_name
defined $method_name length $method_name
Truth table66%2/3
A and B
A= defined $method_nameB= length $method_name
ABresult
0X0
100
111
1131 || $self->_throw_exception( MethodNameNotGiven => class_name => $self->name );
1132 35915 foreach my $class ($self->_method_lookup_order) {
1133 130501 my $method = Class::MOP::Class->initialize($class)->get_method($method_name);
1134 130501 ▶ return $method if defined $method;
Branch: 1 branch
BranchTrueFalseTotal
if defined $method 17482 113019 130501
1135 }
1136 18433 return;
1137 }
1138  
1139 2679 ▶sub get_all_methods {
Subroutine get_all_methods: called CC=3 SCAR=11.0
Pod documented
1140 2679 my $self = shift;
1141  
1142 2679 my %methods;
1143 2679 for my $class ( reverse $self->_method_lookup_order ) {
1144 5618 my $meta = Class::MOP::Class->initialize($class);
1145  
1146 5618 $methods{ $_->name } = $_ for $meta->_get_local_methods;
1147 }
1148  
1149 2679 return values %methods;
1150 }
1151  
1152 4 ▶sub get_all_method_names {
Subroutine get_all_method_names: called CC=1 SCAR=0.0
Pod documented
1153 4 my $self = shift;
1154 4 map { $_->name } $self->get_all_methods;
1155 }
1156  
1157 1559 ▶sub find_all_methods_by_name {
Subroutine find_all_methods_by_name: called CC=5 SCAR=16.1
Pod documented
1158 1559 my ($self, $method_name) = @_;
1159 1559 ▶ (defined $method_name && length $method_name)
Branch: 1 branch
BranchTrueFalseTotal
defined $method_name and length $method_name or $self->_throw_exception("MethodNameNotGiven", "class_name", $self->name) 1556 3 1559
Condition100%3/3
defined $method_name and length $method_name
!ll&&!rl&&r
211556
MC/DC100%2/2
defined $method_name and length $method_name
defined $method_name length $method_name
Truth table100%3/3
A and B
A= defined $method_nameB= length $method_name
ABresult
0X0
100
111
1160 || $self->_throw_exception( MethodNameNotGiven => class_name => $self->name );
1161 1556 my @methods;
1162 1556 foreach my $class ($self->_method_lookup_order) {
1163 # fetch the meta-class ...
1164 5608 my $meta = Class::MOP::Class->initialize($class);
1165 5608 ▶ push @methods => {
Branch: 1 branch
BranchTrueFalseTotal
if $meta->has_method($method_name) 690 4918 5608
1166 name => $method_name,
1167 class => $class,
1168 code => $meta->get_method($method_name)
1169 } if $meta->has_method($method_name);
1170 }
1171 1556 return @methods;
1172 }
1173  
1174 30801 ▶sub find_next_method_by_name {
Subroutine find_next_method_by_name: called CC=5 SCAR=16.1
Pod documented
1175 30801 my ($self, $method_name) = @_;
1176 30801 ▶ (defined $method_name && length $method_name)
Branch: 1 branch
BranchTrueFalseTotal
defined $method_name and length $method_name or $self->_throw_exception("MethodNameNotGiven", "class_name", $self->name) 30800 1 30801
Condition66%2/3
defined $method_name and length $method_name
!ll&&!rl&&r
1030800
MC/DC50%1/2
defined $method_name and length $method_name
defined $method_name length $method_name
Truth table66%2/3
A and B
A= defined $method_nameB= length $method_name
ABresult
0X0
100
111
1177 || $self->_throw_exception( MethodNameNotGiven => class_name => $self->name );
1178 30800 my @cpl = ($self->_method_lookup_order);
1179 30800 shift @cpl; # discard ourselves
1180 30800 foreach my $class (@cpl) {
1181 57006 my $method = Class::MOP::Class->initialize($class)->get_method($method_name);
1182 57006 ▶ return $method if defined $method;
Branch: 1 branch
BranchTrueFalseTotal
if defined $method 29979 27027 57006
1183 }
1184 821 return;
1185 }
1186  
1187 3556 ▶sub update_meta_instance_dependencies {
Subroutine update_meta_instance_dependencies: called CC=2 SCAR=8.1
Pod undocumented
1188 3556 my $self = shift;
1189  
1190 3556 ▶ if ( $self->{meta_instance_dependencies} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"meta_instance_dependencies"}) 0 3556 3556
1191 0 return $self->add_meta_instance_dependencies;
1192 }
1193 }
1194  
1195 4 ▶sub add_meta_instance_dependencies {
Subroutine add_meta_instance_dependencies: called CC=2 SCAR=6.9
Pod undocumented
1196 4 my $self = shift;
1197  
1198 4 $self->remove_meta_instance_dependencies;
1199  
1200 4 my @attrs = $self->get_all_attributes();
1201  
1202 4 my %seen;
1203 14 my @classes = grep { not $seen{ $_->name }++ }
1204 4 map { $_->associated_class } @attrs;
1205  
1206 4 foreach my $class (@classes) {
1207 9 $class->add_dependent_meta_instance($self);
1208 }
1209  
1210 4 $self->{meta_instance_dependencies} = \@classes;
1211 }
1212  
1213 4 ▶sub remove_meta_instance_dependencies {
Subroutine remove_meta_instance_dependencies: called CC=3 SCAR=11.0
Pod undocumented
1214 4 my $self = shift;
1215  
1216 4 ▶ if ( my $classes = delete $self->{meta_instance_dependencies} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $classes = delete $self->{"meta_instance_dependencies"}) 1 3 4
1217 1 foreach my $class (@$classes) {
1218 3 $class->remove_dependent_meta_instance($self);
1219 }
1220  
1221 1 return $classes;
1222 }
1223  
1224 3 return;
1225  
1226 }
1227  
1228 9 ▶sub add_dependent_meta_instance {
Subroutine add_dependent_meta_instance: called CC=1 SCAR=0.0
Pod undocumented
1229 9 my ( $self, $metaclass ) = @_;
1230 9 push @{ $self->{dependent_meta_instances} }, $metaclass;
1231 }
1232  
1233 3 ▶sub remove_dependent_meta_instance {
Subroutine remove_dependent_meta_instance: called CC=2 SCAR=6.9
Pod undocumented
1234 3 my ( $self, $metaclass ) = @_;
1235 3 my $name = $metaclass->name;
1236 6 @$_ = grep { $_->name ne $name } @$_
1237 3 for $self->{dependent_meta_instances};
1238 }
1239  
1240 57928 ▶sub invalidate_meta_instances {
Subroutine invalidate_meta_instances: called CC=2 SCAR=6.9
Pod undocumented
1241 57928 my $self = shift;
1242 $_->invalidate_meta_instance()
1243 57928 for $self, @{ $self->{dependent_meta_instances} };
1244 }
1245  
1246 57931 ▶sub invalidate_meta_instance {
Subroutine invalidate_meta_instance: called CC=1 SCAR=0.0
Pod undocumented
1247 57931 my $self = shift;
1248 57931 undef $self->{_meta_instance};
1249 }
1250  
1251 # check if we can reinitialize
1252 204 ▶sub is_pristine {
Subroutine is_pristine: called CC=4 SCAR=13.9
Pod documented
1253 204 my $self = shift;
1254  
1255 # if any local attr is defined
1256 204 ▶ return if $self->get_attribute_list;
Branch: 1 branch
BranchTrueFalseTotal
if $self->get_attribute_list 6 198 204
1257  
1258 # or any non-declared methods
1259 198 for my $method ( map { $self->get_method($_) } $self->get_method_list ) {
1260 119 ▶ return if $method->isa("Class::MOP::Method::Generated");
Branch: 1 branch
BranchTrueFalseTotal
if $method->isa("Class::MOP::Method::Generated") 0 119 119
1261 # FIXME do we need to enforce this too? return unless $method->isa( $self->method_metaclass );
1262 }
1263  
1264 198 return 1;
1265 }
1266  
1267 ## Class closing
1268  
1269 35900 ▶sub is_mutable { 1 }
Subroutine is_mutable: called CC=1 SCAR=0.0
Pod documented
1270 19848 ▶sub is_immutable { 0 }
Subroutine is_immutable: called CC=1 SCAR=0.0
Pod documented
1271  
1272 17 ▶sub immutable_options { %{ $_[0]{__immutable}{options} || {} } }
Subroutine immutable_options: called CC=2 SCAR=6.9
Pod documented
Condition100%2/2
$_[0]{'__immutable'}{'options'} || {}
l!l
161
MC/DC100%1/1
$_[0]{'__immutable'}{'options'} || {}
$_[0]{'__immutable'}{'options'}
Truth table100%2/2
A
A= $_[0]{'__immutable'}{'options'}
Aresult
11
00
1273  
1274 23063 ▶sub _immutable_options {
Subroutine _immutable_options: called CC=1 SCAR=0.0
1275 23063 my ( $self, @args ) = @_;
1276  
1277 return (
1278 23063 inline_accessors => 1,
1279 inline_constructor => 1,
1280 inline_destructor => 0,
1281 debug => 0,
1282 immutable_trait => $self->immutable_trait,
1283 constructor_name => $self->constructor_name,
1284 constructor_class => $self->constructor_class,
1285 destructor_class => $self->destructor_class,
1286 @args,
1287 );
1288 }
1289  
1290 23072 ▶sub make_immutable {
Subroutine make_immutable: called CC=2 SCAR=6.9
Pod documented
1291 23072 my ( $self, @args ) = @_;
1292  
1293 23072 ▶ return $self unless $self->is_mutable;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->is_mutable 9 23063 23072
1294  
1295 23063 my ($file, $line) = (caller)[1..2];
1296  
1297 23063 $self->_initialize_immutable(
1298 file => $file,
1299 line => $line,
1300 $self->_immutable_options(@args),
1301 );
1302 23061 $self->_rebless_as_immutable(@args);
1303  
1304 23060 return $self;
1305 }
1306  
1307 16 ▶sub make_mutable {
Subroutine make_mutable: called CC=2 SCAR=6.9
Pod documented
1308 16 my $self = shift;
1309  
1310 16 ▶ if ( $self->is_immutable ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->is_immutable) { } 14 2 16
1311 14 my @args = $self->immutable_options;
1312 14 $self->_rebless_as_mutable();
1313 14 $self->_remove_inlined_code(@args);
1314 14 delete $self->{__immutable};
1315 14 return $self;
1316 }
1317 else {
1318 2 return;
1319 }
1320 }
1321  
1322 23061 ▶sub _rebless_as_immutable {
Subroutine _rebless_as_immutable: called CC=1 SCAR=0.0
1323 23061 my ( $self, @args ) = @_;
1324  
1325 23061 $self->{__immutable}{original_class} = ref $self;
1326  
1327 23061 bless $self => $self->_immutable_metaclass(@args);
1328 }
1329  
1330 23061 ▶sub _immutable_metaclass {
Subroutine _immutable_metaclass: called CC=6 SCAR=18.1
1331 23061 my ( $self, %args ) = @_;
1332  
1333 23061 ▶ if ( my $class = $args{immutable_metaclass} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $class = $args{"immutable_metaclass"}) 0 23061 23061
1334 0 return $class;
1335 }
1336  
1337 23061 ▶ my $trait = $args{immutable_trait} = $self->immutable_trait
Condition66%2/3
$self->immutable_trait || $self->_throw_exception('NoImmutableTraitSpecifiedForClass', 'class_name', $self->name, 'params', \%args)
l!l&&r!l&&!r
2306010
MC/DC0%0/2
$self->immutable_trait || $self->_throw_exception('NoImmutableTraitSpecifiedForClass', 'class_name', $self->name, 'params', \%args)
$self->immutable_trait $self->_throw_exception('NoImmutableTraitSpecifiedForClass', 'class_name', $self->name, 'params', \%args)
Truth table66%2/3
A || B
A= $self->immutable_traitB= $self->_throw_exception('NoImmutableTraitSpecifiedForClass', 'class_name', $self->name, 'params', \%args)
ABresult
1X1
011
000
1338 || $self->_throw_exception( NoImmutableTraitSpecifiedForClass => class_name => $self->name,
1339 params => \%args
1340 );
1341  
1342 23060 my $meta = $self->meta;
1343 23060 my $meta_attr = $meta->find_attribute_by_name("immutable_trait");
1344  
1345 23060 my $class_name;
1346  
1347 23060 ▶ if ( $meta_attr and $trait eq $meta_attr->default ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($meta_attr and $trait eq $meta_attr->default) { } 23058 2 23060
Condition66%2/3
$meta_attr and $trait eq $meta_attr->default
!ll&&!rl&&r
0223058
MC/DC50%1/2
$meta_attr and $trait eq $meta_attr->default
$meta_attr $trait eq $meta_attr->default
Truth table66%2/3
A and B
A= $meta_attrB= $trait eq $meta_attr->default
ABresult
0X0
100
111
1348 # if the trait is the same as the default we try and pick a
1349 # predictable name for the immutable metaclass
1350 23058 $class_name = 'Class::MOP::Class::Immutable::' . ref($self);
1351 }
1352 else {
1353 2 $class_name = join '::', 'Class::MOP::Class::Immutable::CustomTrait',
1354 $trait, 'ForMetaClass', ref($self);
1355 }
1356  
1357 23060 ▶ return $class_name
Branch: 1 branch
BranchTrueFalseTotal
if Class::MOP::does_metaclass_exist($class_name) 22364 696 23060
1358 if Class::MOP::does_metaclass_exist($class_name);
1359  
1360 # If the metaclass is a subclass of CMOP::Class which has had
1361 # metaclass roles applied (via Moose), then we want to make sure
1362 # that we preserve that anonymous class (see Fey::ORM for an
1363 # example of where this matters).
1364 696 my $meta_name = $meta->_real_ref_name;
1365  
1366 696 my $immutable_meta = $meta_name->create(
1367 $class_name,
1368 superclasses => [ ref $self ],
1369 );
1370  
1371 696 Class::MOP::MiniTrait::apply( $immutable_meta, $trait );
1372  
1373 696 $immutable_meta->make_immutable(
1374 inline_constructor => 0,
1375 inline_accessors => 0,
1376 );
1377  
1378 696 return $class_name;
1379 }
1380  
1381 14 ▶sub _remove_inlined_code {
Subroutine _remove_inlined_code: called CC=2 SCAR=6.9
1382 14 my $self = shift;
1383  
1384 14 $self->remove_method( $_->name ) for $self->_inlined_methods;
1385  
1386 14 delete $self->{__immutable}{inlined_methods};
1387 }
1388  
1389 15 ▶sub _inlined_methods { @{ $_[0]{__immutable}{inlined_methods} || [] } }
Subroutine _inlined_methods: called CC=2 SCAR=7.2
Condition50%1/2
$_[0]{'__immutable'}{'inlined_methods'} || []
l!l
150
MC/DC0%0/1
$_[0]{'__immutable'}{'inlined_methods'} || []
$_[0]{'__immutable'}{'inlined_methods'}
Truth table50%1/2
A
A= $_[0]{'__immutable'}{'inlined_methods'}
Aresult
11
00
1390  
1391 13234 ▶sub _add_inlined_method {
Subroutine _add_inlined_method: called CC=2 SCAR=6.9
1392 13234 my ( $self, $method ) = @_;
1393  
1394 13234 ▶ push @{ $self->{__immutable}{inlined_methods} ||= [] }, $method;
Condition100%2/2
$self->{"__immutable"}{"inlined_methods"} ||= []
l!l
75212482
MC/DC100%1/1
$self->{"__immutable"}{"inlined_methods"} ||= []
$self->{"__immutable"}{"inlined_methods"}
Truth table100%2/2
A
A= $self->{"__immutable"}{"inlined_methods"}
Aresult
11
00
1395 }
1396  
1397 23063 ▶sub _initialize_immutable {
Subroutine _initialize_immutable: called CC=1 SCAR=0.0
1398 23063 my ( $self, %args ) = @_;
1399  
1400 23063 $self->{__immutable}{options} = \%args;
1401 23063 $self->_install_inlined_code(%args);
1402 }
1403  
1404 23063 ▶sub _install_inlined_code {
Subroutine _install_inlined_code: called CC=4 SCAR=13.9
1405 23063 my ( $self, %args ) = @_;
1406  
1407 # FIXME
1408 23063 ▶ $self->_inline_accessors(%args) if $args{inline_accessors};
Branch: 1 branch
BranchTrueFalseTotal
if $args{"inline_accessors"} 12879 10184 23063
1409 23063 ▶ $self->_inline_constructor(%args) if $args{inline_constructor};
Branch: 1 branch
BranchTrueFalseTotal
if $args{"inline_constructor"} 12487 10576 23063
1410 23062 ▶ $self->_inline_destructor(%args) if $args{inline_destructor};
Branch: 1 branch
BranchTrueFalseTotal
if $args{"inline_destructor"} 779 22283 23062
1411 }
1412  
1413 14 ▶sub _rebless_as_mutable {
Subroutine _rebless_as_mutable: called CC=1 SCAR=0.0
1414 14 my $self = shift;
1415  
1416 14 bless $self, $self->_get_mutable_metaclass_name;
1417  
1418 14 return $self;
1419 }
1420  
1421 12879 ▶sub _inline_accessors {
Subroutine _inline_accessors: called CC=2 SCAR=6.9
1422 12879 my $self = shift;
1423  
1424 12879 foreach my $attr_name ( $self->get_attribute_list ) {
1425 29969 $self->get_attribute($attr_name)->install_accessors(1);
1426 }
1427 }
1428  
1429 12487 ▶sub _inline_constructor {
Subroutine _inline_constructor: called CC=6 SCAR=17.9
1430 12487 my ( $self, %args ) = @_;
1431  
1432 12487 my $name = $args{constructor_name};
1433 # A class may not even have a constructor, and that's okay.
1434 12487 ▶ return unless defined $name;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $name 0 12487 12487
1435  
1436 12487 ▶ if ( $self->has_method($name) && !$args{replace_constructor} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_method($name) and not $args{"replace_constructor"}) 2 12485 12487
Condition66%2/3
$self->has_method($name) and not $args{"replace_constructor"}
!ll&&!rl&&r
1248502
MC/DC50%1/2
$self->has_method($name) and not $args{"replace_constructor"}
$self->has_method($name) not $args{"replace_constructor"}
Truth table66%2/3
A and not(B)
A= $self->has_method($name)B= not $args{"replace_constructor"}
ABresult
0X0
100
111
1437 2 my $class = $self->name;
1438 2 warn "Not inlining a constructor for $class since it defines"
1439 . " its own constructor.\n"
1440 . "If you are certain you don't need to inline your"
1441 . " constructor, specify inline_constructor => 0 in your"
1442 . " call to $class->meta->make_immutable\n";
1443 2 return;
1444 }
1445  
1446 12485 my $constructor_class = $args{constructor_class};
1447  
1448 {
1449 12485 local $@;
1450 12485 use_package_optimistically($constructor_class);
1451 }
1452  
1453 my $constructor = $constructor_class->new(
1454 options => \%args,
1455 metaclass => $self,
1456 is_inline => 1,
1457 package_name => $self->name,
1458 name => $name,
1459 definition_context => {
1460 description => "constructor " . $self->name . "::" . $name,
1461 file => $args{file},
1462 line => $args{line},
1463 },
1464 12485 );
1465  
1466 12484 ▶ if ( $args{replace_constructor} or $constructor->can_be_inlined ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"replace_constructor"} or $constructor->can_be_inlined) 12477 7 12484
Condition100%3/3
$args{"replace_constructor"} or $constructor->can_be_inlined
l!l&&r!l&&!r
2124757
MC/DC100%2/2
$args{"replace_constructor"} or $constructor->can_be_inlined
$args{"replace_constructor"} $constructor->can_be_inlined
Truth table100%3/3
A or B
A= $args{"replace_constructor"}B= $constructor->can_be_inlined
ABresult
1X1
011
000
1467 12477 $self->add_method( $name => $constructor );
1468 12477 $self->_add_inlined_method($constructor);
1469 }
1470 }
1471  
1472 779 ▶sub _inline_destructor {
Subroutine _inline_destructor: called CC=8 SCAR=20.8
1473 779 my ( $self, %args ) = @_;
1474  
1475 ( exists $args{destructor_class} && defined $args{destructor_class} )
1476 779 ▶ || $self->_throw_exception( NoDestructorClassSpecified => class_name => $self->name,
Branch: 1 branch
BranchTrueFalseTotal
exists $args{"destructor_class"} and defined $args{"destructor_class"} or $self->_throw_exception("NoDestructorClassSpecified", "class_name", $self->name, "params", \%args) 778 1 779
Condition66%2/3
exists $args{"destructor_class"} and defined $args{"destructor_class"}
!ll&&!rl&&r
01778
MC/DC50%1/2
exists $args{"destructor_class"} and defined $args{"destructor_class"}
exists $args{"destructor_class"} defined $args{"destructor_class"}
Truth table66%2/3
A and B
A= exists $args{"destructor_class"}B= defined $args{"destructor_class"}
ABresult
0X0
100
111
1477 params => \%args,
1478 );
1479  
1480 778 ▶ if ( $self->has_method('DESTROY') && ! $args{replace_destructor} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_method("DESTROY") and not $args{"replace_destructor"}) 2 776 778
Condition100%3/3
$self->has_method("DESTROY") and not $args{"replace_destructor"}
!ll&&!rl&&r
77512
MC/DC100%2/2
$self->has_method("DESTROY") and not $args{"replace_destructor"}
$self->has_method("DESTROY") not $args{"replace_destructor"}
Truth table100%3/3
A and not(B)
A= $self->has_method("DESTROY")B= not $args{"replace_destructor"}
ABresult
0X0
100
111
1481 2 my $class = $self->name;
1482 2 warn "Not inlining a destructor for $class since it defines"
1483 . " its own destructor.\n";
1484 2 return;
1485 }
1486  
1487 776 my $destructor_class = $args{destructor_class};
1488  
1489 {
1490 776 local $@;
1491 776 use_package_optimistically($destructor_class);
1492 }
1493  
1494 776 ▶ return unless $destructor_class->is_needed($self);
Branch: 1 branch
BranchTrueFalseTotal
unless $destructor_class->is_needed($self) 18 758 776
1495  
1496 my $destructor = $destructor_class->new(
1497 options => \%args,
1498 metaclass => $self,
1499 package_name => $self->name,
1500 name => 'DESTROY',
1501 definition_context => {
1502 description => "destructor " . $self->name . "::DESTROY",
1503 file => $args{file},
1504 line => $args{line},
1505 },
1506 758 );
1507  
1508 758 ▶ if ( $args{replace_destructor} or $destructor->can_be_inlined ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"replace_destructor"} or $destructor->can_be_inlined) 757 1 758
Condition100%3/3
$args{"replace_destructor"} or $destructor->can_be_inlined
l!l&&r!l&&!r
17561
MC/DC100%2/2
$args{"replace_destructor"} or $destructor->can_be_inlined
$args{"replace_destructor"} $destructor->can_be_inlined
Truth table100%3/3
A or B
A= $args{"replace_destructor"}B= $destructor->can_be_inlined
ABresult
1X1
011
000
1509 757 $self->add_method( 'DESTROY' => $destructor );
1510 757 $self->_add_inlined_method($destructor);
1511 }
1512 }
1513  
1514 459 1;
1515  
1516 # ABSTRACT: Class Meta Object
1517  
1518 __END__
« blib/lib/Moose/Meta/Attribute/Native/Trait.pm ↑ Summary blib/lib/Class/MOP/Method/Generated.pm »