blib/lib/Mouse/Meta/Module.pm

filter: Statementstmt 91.5% 141 / 154 Branchbran 81.7% 67 / 82 Conditioncond 79.0% 34 / 43 MC/DCmcdc 50.0% 14 / 28 Subroutinesub 85.1% 23 / 27 Podpod 0.0% 0 / 12 totaltotal 80.6% 279 / 346 CC 59 SCAR 26.2
CC
59
Cov
87%
create
31.5
DESTROY
7.2
_collect_methods
6.3
CRAP 13.8
« blib/lib/Mouse/Meta/Method/Destructor.pm ↑ Summary blib/lib/Mouse/Exporter.pm »
1 package Mouse::Meta::Module;
2 287 ▶use Mouse::Util qw/:meta/; # enables strict and warnings
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 287 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
5 287 ▶use Scalar::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 287 my %METAS;
8  
9 287 if(Mouse::Util::MOUSE_XS){
10 # register meta storage for performance
11 287 Mouse::Util::__register_metaclass_storage(\%METAS, 0);
12  
13 # ensure thread safety
14 0 ▶ *CLONE = sub { Mouse::Util::__register_metaclass_storage(\%METAS, 1) };
Subroutine __ANON__: not called CC=1 SCAR=1.2
15 }
16  
17 2846 ▶sub initialize {
Subroutine initialize: called CC=4 SCAR=15.6
Pod undocumented
18 2846 my($class, $package_name, @args) = @_;
19  
20 2846 ▶ ($package_name && !ref($package_name))
Branch: 1 branch
BranchTrueFalseTotal
$package_name and not ref $package_name or $class->throw_error("You must pass a package name and it cannot be blessed") 2846 0 2846
Condition33%1/3
$package_name and not ref $package_name
!ll&&!rl&&r
002846
MC/DC0%0/2
$package_name and not ref $package_name
$package_name not ref $package_name
Truth table33%1/3
A and not(B)
A= $package_nameB= not ref $package_name
ABresult
0X0
100
111
21 || $class->throw_error("You must pass a package name and it cannot be blessed");
22  
23 2846 ▶ return $METAS{$package_name}
Condition66%2/3
$METAS{$package_name} ||= $class->_construct_meta("package", $package_name, @args)
l!l&&r!l&&!r
182610200
MC/DC0%0/2
$METAS{$package_name} ||= $class->_construct_meta("package", $package_name, @args)
$METAS{$package_name} $class->_construct_meta("package", $package_name, @args)
Truth table66%2/3
A ||= B
A= $METAS{$package_name}B= $class->_construct_meta("package", $package_name, @args)
ABresult
1X1
011
000
24 ||= $class->_construct_meta(package => $package_name, @args);
25 }
26  
27 31 ▶sub reinitialize {
Subroutine reinitialize: called CC=5 SCAR=16.7
Pod undocumented
28 31 my($class, $package_name, @args) = @_;
29  
30 31 ▶ $package_name = $package_name->name if ref $package_name;
Branch: 1 branch
BranchTrueFalseTotal
if ref $package_name 30 1 31
31  
32 31 ▶ ($package_name && !ref($package_name))
Branch: 1 branch
BranchTrueFalseTotal
$package_name and not ref $package_name or $class->throw_error("You must pass a package name and it cannot be blessed") 31 0 31
Condition33%1/3
$package_name and not ref $package_name
!ll&&!rl&&r
0031
MC/DC0%0/2
$package_name and not ref $package_name
$package_name not ref $package_name
Truth table33%1/3
A and not(B)
A= $package_nameB= not ref $package_name
ABresult
0X0
100
111
33 || $class->throw_error("You must pass a package name and it cannot be blessed");
34  
35 31 ▶ if(exists $METAS{$package_name}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $METAS{$package_name}) 31 0 31
36 31 unshift @args, %{ $METAS{$package_name} };
37 }
38 31 delete $METAS{$package_name};
39 31 return $class->initialize($package_name, @args);
40 }
41  
42 225 ▶sub _class_of{
Subroutine _class_of: called CC=3 SCAR=11.0
43 225 my($class_or_instance) = @_;
44 225 ▶ return undef unless defined $class_or_instance;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $class_or_instance 2 223 225
45 223 ▶ return $METAS{ ref($class_or_instance) || $class_or_instance };
Condition66%2/3
ref $class_or_instance || $class_or_instance
l!l&&r!l&&!r
132910
MC/DC0%0/2
ref $class_or_instance || $class_or_instance
ref $class_or_instance $class_or_instance
Truth table66%2/3
A || B
A= ref $class_or_instanceB= $class_or_instance
ABresult
1X1
011
000
46 }
47  
48 # Means of accessing all the metaclasses that have
49 # been initialized thus far.
50 # The public versions are aliased into Mouse::Util::*.
51 #sub _get_all_metaclasses { %METAS }
52 0 ▶sub _get_all_metaclass_instances { values %METAS }
Subroutine _get_all_metaclass_instances: not called CC=1 SCAR=6.9
53 0 ▶sub _get_all_metaclass_names { keys %METAS }
Subroutine _get_all_metaclass_names: not called CC=1 SCAR=6.9
54 3207 ▶sub _get_metaclass_by_name { $METAS{$_[0]} }
Subroutine _get_metaclass_by_name: called CC=1 SCAR=0.0
55 #sub _store_metaclass_by_name { $METAS{$_[0]} = $_[1] }
56 #sub _weaken_metaclass { weaken($METAS{$_[0]}) }
57 #sub _does_metaclass_exist { defined $METAS{$_[0]} }
58 #sub _remove_metaclass_by_name { delete $METAS{$_[0]} }
59  
60 sub name;
61  
62 sub namespace;
63  
64 # add_attribute is an abstract method
65  
66 0 ▶sub get_attribute_map { # DEPRECATED
Subroutine get_attribute_map: not called CC=1 SCAR=6.9
Pod undocumented
67 0 Carp::cluck('get_attribute_map() has been deprecated. Use get_attribute_list() and get_attribute() instead');
68 0 return $_[0]->{attributes};
69 }
70  
71 140 ▶sub has_attribute { exists $_[0]->{attributes}->{$_[1]} }
Subroutine has_attribute: called CC=1 SCAR=0.0
Pod undocumented
72 216 ▶sub get_attribute { $_[0]->{attributes}->{$_[1]} }
Subroutine get_attribute: called CC=1 SCAR=0.0
Pod undocumented
73 1 ▶sub remove_attribute { delete $_[0]->{attributes}->{$_[1]} }
Subroutine remove_attribute: called CC=1 SCAR=0.0
Pod undocumented
74  
75 355 ▶sub get_attribute_list{ keys %{$_[0]->{attributes}} }
Subroutine get_attribute_list: called CC=1 SCAR=0.0
Pod undocumented
76  
77 # XXX: not completely compatible with Moose
78 287 my %foreign = map{ $_ => undef } qw(
79 Mouse Mouse::Role Mouse::Util Mouse::Util::TypeConstraints
80 Carp Scalar::Util List::Util
81 builtin
82 );
83 5382 ▶sub _get_method_body {
Subroutine _get_method_body: called CC=3 SCAR=11.0
84 5382 my($self, $method_name) = @_;
85 5382 my $code = Mouse::Util::get_code_ref($self->{package}, $method_name);
86 5382 ▶ return $code && !exists $foreign{ Mouse::Util::get_code_package($code) }
Branch: 1 branch
BranchTrueFalseTotal
$code && !exists($foreign{Mouse::Util::get_code_package($code)}) ? : 395 4987 5382
Condition100%3/3
$code and not exists $foreign{Mouse::Util::get_code_package($code)}
!ll&&!rl&&r
9264061395
MC/DC100%2/2
$code and not exists $foreign{Mouse::Util::get_code_package($code)}
$code not exists $foreign{Mouse::Util::get_code_package($code)}
Truth table100%3/3
A and not(B)
A= $codeB= not exists $foreign{Mouse::Util::get_code_package($code)}
ABresult
0X0
100
111
87 ? $code
88 : undef;
89 }
90  
91 sub add_method;
92  
93 5728 ▶sub has_method {
Subroutine has_method: called CC=3 SCAR=11.0
Pod undocumented
94 5728 my($self, $method_name) = @_;
95 5728 ▶ defined($method_name)
Branch: 1 branch
BranchTrueFalseTotal
defined $method_name or $self->throw_error("You must define a method name") 5727 1 5728
96 or $self->throw_error('You must define a method name');
97  
98 5727 ▶ return defined( $self->{methods}{$method_name} )
Condition100%3/3
defined $self->{'methods'}{$method_name} || defined $self->_get_method_body($method_name)
l!l&&r!l&&!r
5442354948
MC/DC100%2/2
defined $self->{'methods'}{$method_name} || defined $self->_get_method_body($method_name)
defined $self->{'methods'}{$method_name} defined $self->_get_method_body($method_name)
Truth table100%3/3
A || B
A= defined $self->{'methods'}{$method_name}B= defined $self->_get_method_body($method_name)
ABresult
1X1
011
000
99 || defined( $self->_get_method_body($method_name) );
100 }
101  
102 369 ▶sub get_method_body {
Subroutine get_method_body: called CC=3 SCAR=11.0
Pod undocumented
103 369 my($self, $method_name) = @_;
104 369 ▶ defined($method_name)
Branch: 1 branch
BranchTrueFalseTotal
defined $method_name or $self->throw_error("You must define a method name") 369 0 369
105 or $self->throw_error('You must define a method name');
106  
107 369 ▶ return $self->{methods}{$method_name}
Condition100%3/3
$self->{"methods"}{$method_name} ||= $self->_get_method_body($method_name)
l!l&&r!l&&!r
17016039
MC/DC100%2/2
$self->{"methods"}{$method_name} ||= $self->_get_method_body($method_name)
$self->{"methods"}{$method_name} $self->_get_method_body($method_name)
Truth table100%3/3
A ||= B
A= $self->{"methods"}{$method_name}B= $self->_get_method_body($method_name)
ABresult
1X1
011
000
108 ||= $self->_get_method_body($method_name);
109 }
110  
111 62 ▶sub get_method {
Subroutine get_method: called CC=2 SCAR=6.9
Pod undocumented
112 62 my($self, $method_name) = @_;
113  
114 62 ▶ if(my $code = $self->get_method_body($method_name)){
Branch: 1 branch
BranchTrueFalseTotal
if (my $code = $self->get_method_body($method_name)) 44 18 62
115 44 return Mouse::Util::load_class($self->method_metaclass)->wrap(
116 body => $code,
117 name => $method_name,
118 package => $self->name,
119 associated_metaclass => $self,
120 );
121 }
122  
123 18 return undef;
124 }
125  
126 322 ▶sub get_method_list {
Subroutine get_method_list: called CC=1 SCAR=0.0
Pod undocumented
127 322 my($self) = @_;
128  
129 322 return grep { $self->has_method($_) } keys %{ $self->namespace };
130 }
131  
132 67 ▶sub _collect_methods { # Mouse specific, used for method modifiers
Subroutine _collect_methods: called CC=5 SCAR=18.3
133 67 my($meta, @args) = @_;
134  
135 67 my @methods;
136 67 foreach my $arg(@args){
137 69 ▶ if(my $type = ref $arg){
Branch: 1 branch
BranchTrueFalseTotal
if (my $type = ref $arg) { } 6 63 69
138 6 ▶ if($type eq 'Regexp'){
Branch: 2 branches
BranchTrueFalseTotal
if ($type eq 'Regexp') { } 6 0 6
elsif ($type eq 'ARRAY') { } 0 0 0
139 6 push @methods, grep { $_ =~ $arg } $meta->get_all_method_names;
140 }
141 elsif($type eq 'ARRAY'){
142 0 push @methods, @{$arg};
143 }
144 else{
145 0 my $subname = ( caller(1) )[3];
146 0 $meta->throw_error(
147 sprintf(
148 'Methods passed to %s must be provided as a list,'
149 . ' ArrayRef or regular expression, not %s',
150 $subname,
151 $type,
152 )
153 );
154 }
155 }
156 else{
157 63 push @methods, $arg;
158 }
159 }
160 64 return @methods;
161 }
162  
163 287 my $ANON_SERIAL = 0; # anonymous class/role id
164 286 my %IMMORTALS; # immortal anonymous classes
165  
166 178 ▶sub create {
Subroutine create: called CC=31 SCAR=34.5
Pod undocumented
167 178 my($self, $package_name, %options) = @_;
168  
169 178 ▶ my $class = ref($self) || $self;
Condition66%2/3
ref $self || $self
l!l&&r!l&&!r
221560
MC/DC0%0/2
ref $self || $self
ref $self $self
Truth table66%2/3
A || B
A= ref $selfB= $self
ABresult
1X1
011
000
170 178 ▶ $self->throw_error('You must pass a package name') if @_ < 2;
Branch: 1 branch
BranchTrueFalseTotal
if @_ < 2 0 178 178
171  
172 178 my $superclasses;
173 178 ▶ if(exists $options{superclasses}){
Branch: 1 branch
BranchTrueFalseTotal
if (exists $options{"superclasses"}) 89 89 178
174 89 ▶ if(Mouse::Util::is_a_metarole($self)){
Branch: 1 branch
BranchTrueFalseTotal
if (Mouse::Util::is_a_metarole($self)) { } 0 89 89
175 0 delete $options{superclasses};
176 }
177 else{
178 89 $superclasses = delete $options{superclasses};
179 89 ▶ (ref $superclasses eq 'ARRAY')
Branch: 1 branch
BranchTrueFalseTotal
ref $superclasses eq "ARRAY" or $self->throw_error("You must pass an ARRAY ref of superclasses") 88 1 89
180 || $self->throw_error("You must pass an ARRAY ref of superclasses");
181 }
182 }
183  
184 177 my $attributes = delete $options{attributes};
185 177 ▶ if(defined $attributes){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $attributes) 6 171 177
186 6 ▶ (ref $attributes eq 'ARRAY' || ref $attributes eq 'HASH')
Branch: 1 branch
BranchTrueFalseTotal
ref $attributes eq "ARRAY" or ref $attributes eq "HASH" or $self->throw_error("You must pass an ARRAY ref of attributes") 5 1 6
Condition100%3/3
ref $attributes eq "ARRAY" or ref $attributes eq "HASH"
l!l&&r!l&&!r
321
MC/DC100%2/2
ref $attributes eq "ARRAY" or ref $attributes eq "HASH"
ref $attributes eq "ARRAY" ref $attributes eq "HASH"
Truth table100%3/3
A or B
A= ref $attributes eq "ARRAY"B= ref $attributes eq "HASH"
ABresult
1X1
011
000
187 || $self->throw_error("You must pass an ARRAY ref of attributes");
188 }
189 176 my $methods = delete $options{methods};
190 176 ▶ if(defined $methods){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $methods) 8 168 176
191 8 ▶ (ref $methods eq 'HASH')
Branch: 1 branch
BranchTrueFalseTotal
ref $methods eq "HASH" or $self->throw_error("You must pass a HASH ref of methods") 7 1 8
192 || $self->throw_error("You must pass a HASH ref of methods");
193 }
194 175 my $roles = delete $options{roles};
195 175 ▶ if(defined $roles){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $roles) 90 85 175
196 90 ▶ (ref $roles eq 'ARRAY')
Branch: 1 branch
BranchTrueFalseTotal
ref $roles eq "ARRAY" or $self->throw_error("You must pass an ARRAY ref of roles") 89 1 90
197 || $self->throw_error("You must pass an ARRAY ref of roles");
198 }
199 174 my $mortal;
200 my $cache_key;
201  
202 174 ▶ if(!defined $package_name){ # anonymous
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $package_name) 165 9 174
203 165 $mortal = !$options{cache};
204  
205 # anonymous but immortal
206 165 ▶ if(!$mortal){
Branch: 1 branch
BranchTrueFalseTotal
unless ($mortal) 51 114 165
207 # something like Super::Class|Super::Class::2=Role|Role::1
208 $cache_key = join '=' => (
209 51 ▶ join('|', @{$superclasses || []}),
Condition100%2/2
$superclasses || []
l!l
501
MC/DC100%1/1
$superclasses || []
$superclasses
Truth table100%2/2
A
A= $superclasses
Aresult
11
00
210 51 ▶ join('|', sort @{$roles || []}),
Condition100%2/2
$roles || []
l!l
483
MC/DC100%1/1
$roles || []
$roles
Truth table100%2/2
A
A= $roles
Aresult
11
00
211 );
212 51 ▶ return $IMMORTALS{$cache_key} if exists $IMMORTALS{$cache_key};
Branch: 1 branch
BranchTrueFalseTotal
if exists $IMMORTALS{$cache_key} 9 42 51
213 }
214 156 $options{anon_serial_id} = ++$ANON_SERIAL;
215 156 $package_name = $class . '::__ANON__::' . $ANON_SERIAL;
216 }
217  
218  
219 # instantiate a module
220 {
221 287 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
222 0 ▶ ${ $package_name . '::VERSION' } = delete $options{version} if exists $options{version};
Branch: 1 branch
BranchTrueFalseTotal
if exists $options{"version"} 0 165 165
223 0 ▶ ${ $package_name . '::AUTHORITY' } = delete $options{authority} if exists $options{authority};
Branch: 1 branch
BranchTrueFalseTotal
if exists $options{"authority"} 0 165 165
224 }
225  
226 165 my $meta = $self->initialize( $package_name, %options);
227  
228 165 ▶ Scalar::Util::weaken($METAS{$package_name})
Branch: 1 branch
BranchTrueFalseTotal
if $mortal 114 51 165
229 if $mortal;
230  
231 42 ▶ $meta->add_method(meta => sub {
Subroutine __ANON__: called CC=2 SCAR=7.2
Subroutine meta: called CC=2 SCAR=7.2
232 42 ▶ $self->initialize(ref($_[0]) || $_[0]);
Condition66%4/6
ref $_[0] || $_[0]
l!l&&r!l&&!r
27150
ref $_[0] || $_[0]
l!l&&r!l&&!r
24300
MC/DC0%0/2
ref $_[0] || $_[0]
ref $_[0] $_[0]
MC/DC0%0/2
ref $_[0] || $_[0]
ref $_[0] $_[0]
Truth table66%2/3
A || B
A= ref $_[0]B= $_[0]
ABresult
1X1
011
000
Truth table66%2/3
A || B
A= ref $_[0]B= $_[0]
ABresult
1X1
011
000
233 165 });
234  
235 165 ▶ $meta->superclasses(@{$superclasses})
Branch: 1 branch
BranchTrueFalseTotal
if defined $superclasses 78 87 165
236 if defined $superclasses;
237  
238 # NOTE:
239 # process attributes first, so that they can
240 # install accessors, but locally defined methods
241 # can then overwrite them. It is maybe a little odd, but
242 # I think this should be the order of things.
243 164 ▶ if (defined $attributes) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $attributes) 5 159 164
244 5 ▶ if(ref($attributes) eq 'ARRAY'){
Branch: 1 branch
BranchTrueFalseTotal
if (ref $attributes eq 'ARRAY') { } 3 2 5
245 # array of Mouse::Meta::Attribute
246 3 foreach my $attr (@{$attributes}) {
247 3 $meta->add_attribute($attr);
248 }
249 }
250 else{
251 # hash map of name and attribute spec pairs
252 2 ▶ while(my($name, $attr) = each %{$attributes}){
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$attributes;}) 2 2 4
253 2 $meta->add_attribute($name => $attr);
254 }
255 }
256 }
257 164 ▶ if (defined $methods) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $methods) 7 157 164
258 7 ▶ while(my($method_name, $method_body) = each %{$methods}){
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$methods;}) 11 7 18
259 11 $meta->add_method($method_name, $method_body);
260 }
261 }
262 164 ▶ if (defined $roles and !$options{in_application_to_instance}){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $roles and not $options{"in_application_to_instance"}) 56 108 164
Condition100%3/3
defined $roles and not $options{"in_application_to_instance"}
!ll&&!rl&&r
852356
MC/DC100%2/2
defined $roles and not $options{"in_application_to_instance"}
defined $roles not $options{"in_application_to_instance"}
Truth table100%3/3
A and not(B)
A= defined $rolesB= not $options{"in_application_to_instance"}
ABresult
0X0
100
111
263 56 Mouse::Util::apply_all_roles($package_name, @{$roles});
264 }
265  
266 163 ▶ if($cache_key){
Branch: 1 branch
BranchTrueFalseTotal
if ($cache_key) 42 121 163
267 42 $IMMORTALS{$cache_key} = $meta;
268 }
269  
270 163 return $meta;
271 }
272  
273 103 ▶sub DESTROY{
Subroutine DESTROY: called CC=7 SCAR=19.7
274 103 my($self) = @_;
275  
276 103 ▶ return if Mouse::Util::in_global_destruction();
Branch: 1 branch
BranchTrueFalseTotal
if Mouse::Util::in_global_destruction() 0 103 103
277  
278 103 my $serial_id = $self->{anon_serial_id};
279 103 ▶ return if !$serial_id;
Branch: 1 branch
BranchTrueFalseTotal
unless $serial_id 29 74 103
280  
281 # XXX: cleaning stash with threads causes panic/SEGV on legacy perls.
282 74 ▶ if(exists $INC{'threads.pm'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $INC{"threads.pm"}) 0 74 74
283 # (caller)[2] indicates the caller's line number,
284 # which is zero when the current thread is joining (destroying).
285 0 ▶ return if( (caller)[2] == 0);
Branch: 1 branch
BranchTrueFalseTotal
if (caller)[2] == 0 0 0 0
286 }
287  
288 # clean up mortal anonymous class stuff
289  
290 # @ISA is a magical variable, so we must clear it manually.
291 74 ▶ @{$self->{superclasses}} = () if exists $self->{superclasses} && scalar(@{$self->{superclasses}}) > 0;
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"superclasses"} and scalar @{$self->{"superclasses"};} > 0 30 44 74
Condition100%3/3
exists $self->{"superclasses"} and scalar @{$self->{"superclasses"};} > 0
!ll&&!rl&&r
271730
MC/DC100%2/2
exists $self->{"superclasses"} and scalar @{$self->{"superclasses"};} > 0
exists $self->{"superclasses"} scalar @{$self->{"superclasses"};} > 0
Truth table100%3/3
A and B
A= exists $self->{"superclasses"}B= scalar @{$self->{"superclasses"};} > 0
ABresult
0X0
100
111
292  
293 # Then, clear the symbol table hash
294 74 %{$self->namespace} = ();
295  
296 74 my $name = $self->name;
297 74 delete $METAS{$name};
298  
299 74 $name =~ s/ $serial_id \z//xms;
300 287 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
301 74 delete ${$name}{ $serial_id . '::' };
302 74 return;
303 }
304  
305  
306 286 1;
307 __END__
« blib/lib/Mouse/Meta/Method/Destructor.pm ↑ Summary blib/lib/Mouse/Exporter.pm »