GX/Meta/Class.pm

filter: Statementstmt 84.3% 232 / 275 Branchbran 55.9% 47 / 84 Conditioncond 44.4% 24 / 54 MC/DCmcdc 16.6% 6 / 36 Subroutinesub 97.0% 65 / 67 Podpod 100% 36 / 36 totaltotal 74.2% 410 / 552 CC 71 SCAR 21.7
CC
71
Cov
73%
all_methods_with_code_attribute
33.0
methods_with_code_attribute
33.0
add_attribute
27.4
CRAP 8.8
« GX/Exception.pm ↑ Summary GX/HTTP/Body/File.pm »
1 # ----------------------------------------------------------------------------------------------------------------------
2 # GX Framework (c) 2009-2011 Jörg A. Uzarek <uzarek@runlevelnull.de>
3 # File: GX/Meta/Class.pm
4 # ----------------------------------------------------------------------------------------------------------------------
5  
6 269 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 269 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 package GX::Meta::Class;
10  
11 269 ▶BEGIN {
Subroutine BEGIN: called CC=2 SCAR=8.1
12  
13 269 ▶ if ( $] < 5.009_005 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($] < 5.009005) { } 0 269 269
14 # mro::* interface compatibility for older Perls
15 0 require MRO::Compat;
16 }
17 else {
18 269 require mro;
19 }
20  
21 }
22  
23 269 ▶use GX::Meta::Attribute;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 269 ▶use GX::Meta::Constants qw( REGEX_CLASS_NAME REGEX_IDENTIFIER REGEX_METHOD_NAME );
Subroutine BEGIN: called CC=1 SCAR=0.0
25 269 ▶use GX::Meta::Exception;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 269 ▶use GX::Meta::Method;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 269 ▶use GX::Meta::Module;
Subroutine BEGIN: called CC=1 SCAR=0.0
28 269 ▶use GX::Meta::Package;
Subroutine BEGIN: called CC=1 SCAR=0.0
29 269 ▶use GX::Meta::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
30  
31 269 ▶use Scalar::Util qw( blessed weaken );
Subroutine BEGIN: called CC=1 SCAR=0.0
32  
33  
34 # ----------------------------------------------------------------------------------------------------------------------
35 # Load Sub::Name if available
36 # ----------------------------------------------------------------------------------------------------------------------
37  
38 269 my $USE_SUB_NAME;
39  
40 269 ▶BEGIN { local $@; $USE_SUB_NAME = eval "require Sub::Name; 1;"; }
Subroutine BEGIN: called CC=1 SCAR=0.0
41  
42  
43 # ----------------------------------------------------------------------------------------------------------------------
44 # Overloading
45 # ----------------------------------------------------------------------------------------------------------------------
46  
47 use overload
48 14097 ▶ 'bool' => sub { 1 },
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
49 108 ▶ '0+' => sub { $_[0] },
Subroutine __ANON__: called CC=1 SCAR=0.0
50 231 ▶ '""' => sub { $_[0]->name },
Subroutine __ANON__: called CC=1 SCAR=0.0
51 269 'fallback' => 1;
52  
53  
54 # ----------------------------------------------------------------------------------------------------------------------
55 # Constructor
56 # ----------------------------------------------------------------------------------------------------------------------
57  
58 15120 ▶sub new {
Subroutine new: called CC=5 SCAR=16.8
Pod documented
59  
60 15120 my $class = shift;
61 15120 ▶ my %args = ( @_ == 1 ) ? ( 'name' => $_[0] ) : @_;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 ? : 15120 0 15120
62  
63 15120 my $class_name = $args{'name'};
64  
65 15120 ▶ if ( ! defined $class_name ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $class_name) 0 15120 15120
66 0 complain "Missing argument (\"name\")";
67 }
68  
69 15120 my $self = GX::Meta::Class::Registry->get( $class_name );
70  
71 15120 ▶ if ( ! $self ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self) 1704 13416 15120
72  
73 1704 ▶ if ( $class_name !~ REGEX_CLASS_NAME ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($class_name =~ /^[a-zA-Z_]\w*(?:::[a-zA-Z_]\w*)*$/) 0 1704 1704
74 0 complain "Invalid argument (\"name\")";
75 }
76  
77 1704 $self = bless {
78 'attributes' => [],
79 'attributes_index' => {},
80 'code_attribute_store' => {},
81 'code_attributes' => [],
82 'methods' => {},
83 'methods_pkg_gen' => -1,
84 'name' => $class_name,
85 'package' => undef
86 }, $class;
87  
88 1704 GX::Meta::Class::Registry->set( $class_name => $self );
89  
90 }
91  
92 15120 return $self;
93  
94 }
95  
96  
97 # ----------------------------------------------------------------------------------------------------------------------
98 # Public methods
99 # ----------------------------------------------------------------------------------------------------------------------
100  
101 3360 ▶sub add_attribute {
Subroutine add_attribute: called CC=10 SCAR=33.1
Pod documented
102  
103 3360 my $self = shift;
104  
105 3360 my $attribute;
106  
107 3360 ▶ if ( @_ == 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 1) { } 0 3360 3360
108  
109 0 $attribute = $_[0];
110  
111 0 ▶ if ( ! blessed $attribute || ! $attribute->isa( 'GX::Meta::Attribute' ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (builtin::blessed($attribute) and $attribute->isa("GX::Meta::Attribute")) 0 0 0
Condition0%0/3
builtin::blessed($attribute) and $attribute->isa("GX::Meta::Attribute")
!ll&&!rl&&r
000
MC/DC0%0/2
builtin::blessed($attribute) and $attribute->isa("GX::Meta::Attribute")
builtin::blessed($attribute) $attribute->isa("GX::Meta::Attribute")
Truth table0%0/3
A and B
A= builtin::blessed($attribute)B= $attribute->isa("GX::Meta::Attribute")
ABresult
0X0
100
111
112 0 complain "Invalid argument";
113 }
114  
115 0 ▶ if ( $attribute->class && $attribute->class != $self ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($attribute->class and $attribute->class != $self) 0 0 0
Condition0%0/3
$attribute->class and $attribute->class != $self
!ll&&!rl&&r
000
MC/DC0%0/2
$attribute->class and $attribute->class != $self
$attribute->class $attribute->class != $self
Truth table0%0/3
A and B
A= $attribute->classB= $attribute->class != $self
ABresult
0X0
100
111
116 0 complain "Cannot add the attribute because it is already associated with another class"
117 }
118  
119 }
120 else {
121  
122 3360 ▶ if ( @_ % 2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ % 2) 0 3360 3360
123 0 complain "Invalid number of arguments";
124 }
125  
126 3360 $attribute = eval { $self->_create_attribute( @_ ) };
127  
128 3360 ▶ if ( ! $attribute ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($attribute) 0 3360 3360
129 0 GX::Exception->complain(
130 message => "Cannot create the attribute metaobject",
131 subexception => $@
132 );
133 }
134  
135 }
136  
137 3360 my $attribute_name = $attribute->name;
138  
139 3360 ▶ if ( ! defined $attribute_name ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $attribute_name) 0 3360 3360
140 0 complain "Cannot add the attribute (undefined attribute name)";
141 }
142  
143 3360 ▶ if ( $self->_get_attributes_index->{$attribute_name} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_get_attributes_index->{$attribute_name}) 0 3360 3360
144 0 complain "Cannot add the attribute (duplicate attribute name: \"$attribute_name\")";
145 }
146  
147 3360 $self->_get_attributes_index->{$attribute_name} = $attribute;
148  
149 3360 push @{$self->_get_attributes}, $attribute;
150  
151 3360 return $attribute;
152  
153 }
154  
155 175 ▶sub add_code_attribute {
Subroutine add_code_attribute: called CC=4 SCAR=15.8
Pod documented
156  
157 175 my $self = shift;
158 175 my $attribute = shift;
159  
160 175 ▶ if ( @_ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 175 175
161 0 complain "Invalid number of arguments";
162 }
163  
164 175 ▶ if ( ! defined $attribute || $attribute !~ REGEX_IDENTIFIER ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $attribute and $attribute =~ /^[a-zA-Z_]\w*$/) 0 175 175
Condition33%1/3
defined $attribute and $attribute =~ /^[a-zA-Z_]\w*$/
!ll&&!rl&&r
00175
MC/DC0%0/2
defined $attribute and $attribute =~ /^[a-zA-Z_]\w*$/
defined $attribute $attribute =~ /^[a-zA-Z_]\w*$/
Truth table33%1/3
A and B
A= defined $attributeB= $attribute =~ /^[a-zA-Z_]\w*$/
ABresult
0X0
100
111
165 0 complain "Invalid argument";
166 }
167  
168 175 push @{$self->_get_code_attributes}, $attribute;
169  
170 175 return;
171  
172 }
173  
174 13183 ▶sub add_method {
Subroutine add_method: called CC=6 SCAR=21.5
Pod documented
175  
176 13183 my $self = shift;
177 13183 my $method_name = shift;
178 13183 my $code_reference = shift;
179  
180 13183 ▶ if ( @_ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 13183 13183
181 0 complain "Invalid number of arguments";
182 }
183  
184 13183 ▶ if ( ! defined $method_name || $method_name !~ REGEX_METHOD_NAME ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $method_name and $method_name =~ /^[a-zA-Z_]\w*$/) 0 13183 13183
Condition33%1/3
defined $method_name and $method_name =~ /^[a-zA-Z_]\w*$/
!ll&&!rl&&r
0013183
MC/DC0%0/2
defined $method_name and $method_name =~ /^[a-zA-Z_]\w*$/
defined $method_name $method_name =~ /^[a-zA-Z_]\w*$/
Truth table33%1/3
A and B
A= defined $method_nameB= $method_name =~ /^[a-zA-Z_]\w*$/
ABresult
0X0
100
111
185 0 complain "Invalid argument";
186 }
187  
188 13183 ▶ if ( ref $code_reference ne 'CODE' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $code_reference ne "CODE") 0 13183 13183
189 0 complain "Invalid argument";
190 }
191  
192 13183 $self->package->assign_to_typeglob( $method_name, $code_reference );
193  
194 13183 ▶ if ( $USE_SUB_NAME ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($USE_SUB_NAME) 0 13183 13183
195 0 Sub::Name::subname( "${self}::${method_name}", $code_reference );
196 }
197  
198 13183 return;
199  
200 }
201  
202 2059 ▶sub all_attributes {
Subroutine all_attributes: called CC=4 SCAR=13.9
Pod documented
203  
204 2059 my $self = shift;
205  
206 2059 my @attributes;
207 my %seen_attributes;
208  
209 2059 for my $class ( $self->linearized_isa_classes ) {
210  
211 7458 for my $attribute ( $class->attributes ) {
212 8954 ▶ push @attributes, $attribute unless $seen_attributes{$attribute->name}++;
Branch: 1 branch
BranchTrueFalseTotal
unless $seen_attributes{$attribute->name}++ 8732 222 8954
213 }
214  
215 }
216  
217 2059 return @attributes;
218  
219 }
220  
221 4 ▶sub all_class_attributes {
Subroutine all_class_attributes: called CC=1 SCAR=0.0
Pod documented
222  
223 4 return grep { $_->is_static } $_[0]->all_attributes;
224  
225 }
226  
227 62 ▶sub all_code_attributes {
Subroutine all_code_attributes: called CC=1 SCAR=0.0
Pod documented
228  
229 62 return map { $_->code_attributes } $_[0]->linearized_isa_classes;
230  
231 }
232  
233 2040 ▶sub all_instance_attributes {
Subroutine all_instance_attributes: called CC=1 SCAR=0.0
Pod documented
234  
235 2040 return grep { ! $_->is_static } $_[0]->all_attributes;
236  
237 }
238  
239 28 ▶sub all_methods {
Subroutine all_methods: called CC=1 SCAR=0.0
Pod documented
240  
241 28 my $self = shift;
242  
243 28 my %methods = map { %{$_->_get_methods} } reverse $self->linearized_isa_classes;
244  
245 28 return values %methods;
246  
247 }
248  
249 2 ▶sub all_methods_with_code_attribute {
Subroutine all_methods_with_code_attribute: called CC=8 SCAR=35.0
Pod documented
250  
251 2 my $self = shift;
252  
253 2 my @result;
254  
255 2 ▶ if ( @_ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) { } 0 2 2
256  
257 0 my $code_attribute = shift;
258  
259 0 ▶ if ( @_ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
260 0 complain "Invalid number of arguments";
261 }
262  
263 0 ▶ if ( ! defined $code_attribute || $code_attribute !~ REGEX_IDENTIFIER ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $code_attribute and $code_attribute =~ /^[a-zA-Z_]\w*$/) 0 0 0
Condition0%0/3
defined $code_attribute and $code_attribute =~ /^[a-zA-Z_]\w*$/
!ll&&!rl&&r
000
MC/DC0%0/2
defined $code_attribute and $code_attribute =~ /^[a-zA-Z_]\w*$/
defined $code_attribute $code_attribute =~ /^[a-zA-Z_]\w*$/
Truth table0%0/3
A and B
A= defined $code_attributeB= $code_attribute =~ /^[a-zA-Z_]\w*$/
ABresult
0X0
100
111
264 0 complain "Invalid argument";
265 }
266  
267 0 my $code_attribute_regex = qr/^$code_attribute(?:\(.*\))?$/;
268  
269 METHOD:
270 0 for my $method ( $self->all_methods ) {
271  
272 0 for ( $method->code_attributes ) {
273 0 ▶ push( @result, $method ), next METHOD if /$code_attribute_regex/;
Branch: 1 branch
BranchTrueFalseTotal
if /$code_attribute_regex/ 0 0 0
274 }
275  
276 }
277  
278 }
279 else {
280 2 @result = grep { $_->code_attributes } $self->all_methods;
281 }
282  
283 2 return @result;
284  
285 }
286  
287 40 ▶sub all_subclasses {
Subroutine all_subclasses: called CC=5 SCAR=16.1
Pod documented
288  
289 40 my $self = shift;
290  
291 40 my $class_name = $self->name;
292  
293 40 my @subclass_names;
294  
295 40 my @package_names = grep { $_ ne 'main' } GX::Meta::Util::subpackage_names( 'main' );
296  
297 40 ▶ while ( @package_names ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@package_names) 12600 40 12640
298  
299 12600 my $package_name = shift @package_names;
300  
301 12600 local $@;
302  
303 12600 ▶ if ( eval { $package_name->isa( $class_name ) } && $package_name ne $class_name ) {
Branch: 1 branch
BranchTrueFalseTotal
if (eval { do { $package_name->isa($class_name) } } and $package_name ne $class_name) 48 12552 12600
Condition100%3/3
eval { do { $package_name->isa($class_name) } } and $package_name ne $class_name
!ll&&!rl&&r
125124048
MC/DC100%2/2
eval { do { $package_name->isa($class_name) } } and $package_name ne $class_name
eval { do { $package_name->isa($class_name) } } $package_name ne $class_name
Truth table100%3/3
A and B
A= eval { do { $package_name->isa($class_name) } }B= $package_name ne $class_name
ABresult
0X0
100
111
304 48 push @subclass_names, $package_name;
305 }
306  
307 12600 push @package_names, map { "${package_name}::$_" } GX::Meta::Util::subpackage_names( $package_name );
308  
309 }
310  
311 40 ▶ return map { GX::Meta::Class::Registry->get( $_ ) // __PACKAGE__->new( $_ ) } @subclass_names;
Condition66%2/3
"GX::Meta::Class::Registry"->get($_) // __PACKAGE__->new($_)
l!l&&r!l&&!r
4440
MC/DC0%0/2
"GX::Meta::Class::Registry"->get($_) // __PACKAGE__->new($_)
"GX::Meta::Class::Registry"->get($_) __PACKAGE__->new($_)
Truth table66%2/3
A // B
A= "GX::Meta::Class::Registry"->get($_)B= __PACKAGE__->new($_)
ABresult
1X1
011
000
312  
313 }
314  
315 20 ▶sub all_superclasses {
Subroutine all_superclasses: called CC=1 SCAR=0.0
Pod documented
316  
317 20 my $self = shift;
318  
319 20 my @classes = $self->linearized_isa_classes;
320  
321 20 shift @classes;
322  
323 20 return @classes;
324  
325 }
326  
327 90 ▶sub attribute {
Subroutine attribute: called CC=1 SCAR=0.0
Pod documented
328  
329 90 return $_[0]->_get_attributes_index->{$_[1]};
330  
331 }
332  
333 7473 ▶sub attributes {
Subroutine attributes: called CC=1 SCAR=0.0
Pod documented
334  
335 7473 return @{$_[0]->_get_attributes};
336  
337 }
338  
339 4 ▶sub class_attributes {
Subroutine class_attributes: called CC=1 SCAR=0.0
Pod documented
340  
341 4 return grep { $_->is_static } @{$_[0]->_get_attributes};
342  
343 }
344  
345 496 ▶sub class_data {
Subroutine class_data: called CC=1 SCAR=0.0
Pod documented
346  
347 269 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
348  
349 496 return \%{ $_[0]->name . '::' . $_[0]->class_data_identifier };
350  
351 }
352  
353 1027 ▶sub class_data_identifier {
Subroutine class_data_identifier: called CC=1 SCAR=0.0
Pod documented
354  
355 1027 return 'CLASS_DATA';
356  
357 }
358  
359 352 ▶sub code_attributes {
Subroutine code_attributes: called CC=1 SCAR=0.0
Pod documented
360  
361 352 return @{$_[0]->_get_code_attributes};
362  
363 }
364  
365 0 ▶sub destroy {
Subroutine destroy: not called CC=1 SCAR=6.9
Pod documented
366  
367 0 return GX::Meta::Class::Registry->remove( $_[0]->name );
368  
369 }
370  
371 6 ▶sub has_attribute {
Subroutine has_attribute: called CC=2 SCAR=6.9
Pod documented
372  
373 6 ▶ return $_[0]->_get_attributes_index->{$_[1]} ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$_[0]->_get_attributes_index->{$_[1]} ? : 4 2 6
374  
375 }
376  
377 9428 ▶sub has_method {
Subroutine has_method: called CC=2 SCAR=6.9
Pod documented
378  
379 9428 ▶ return $_[0]->_get_methods->{$_[1]} ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$_[0]->_get_methods->{$_[1]} ? : 5441 3987 9428
380  
381 }
382  
383 1628 ▶sub inherit_from {
Subroutine inherit_from: called CC=6 SCAR=19.0
Pod documented
384  
385 1628 my $self = shift;
386  
387 1628 my @class_names;
388  
389 1628 for ( @_ ) {
390  
391 1692 ▶ if ( blessed $_ && $_->isa( __PACKAGE__ ) ) {
Branch: 2 branches
BranchTrueFalseTotal
if (builtin::blessed($_) and $_->isa(__PACKAGE__)) { } 3 1689 1692
elsif (defined $_ and $_ =~ /^[a-zA-Z_]\w*(?:::[a-zA-Z_]\w*)*$/) { } 1689 0 1689
Condition66%2/3
builtin::blessed($_) and $_->isa(__PACKAGE__)
!ll&&!rl&&r
168903
MC/DC50%1/2
builtin::blessed($_) and $_->isa(__PACKAGE__)
builtin::blessed($_) $_->isa(__PACKAGE__)
Truth table66%2/3
A and B
A= builtin::blessed($_)B= $_->isa(__PACKAGE__)
ABresult
0X0
100
111
392 3 push @class_names, $_->name;
393 }
394 ▶ elsif ( defined $_ && $_ =~ REGEX_CLASS_NAME ) {
Condition33%1/3
defined $_ and $_ =~ /^[a-zA-Z_]\w*(?:::[a-zA-Z_]\w*)*$/
!ll&&!rl&&r
001689
MC/DC0%0/2
defined $_ and $_ =~ /^[a-zA-Z_]\w*(?:::[a-zA-Z_]\w*)*$/
defined $_ $_ =~ /^[a-zA-Z_]\w*(?:::[a-zA-Z_]\w*)*$/
Truth table33%1/3
A and B
A= defined $_B= $_ =~ /^[a-zA-Z_]\w*(?:::[a-zA-Z_]\w*)*$/
ABresult
0X0
100
111
395 1689 push @class_names, $_;
396 }
397 else {
398 0 complain "Invalid argument";
399 }
400  
401 }
402  
403 1628 $self->_set_isa( @class_names, $self->_get_isa );
404  
405 1628 return;
406  
407 }
408  
409 7 ▶sub inherited_methods {
Subroutine inherited_methods: called CC=1 SCAR=0.0
Pod documented
410  
411 7 my $self = shift;
412  
413 7 return grep { $_->class != $self } $self->all_methods;
414  
415 }
416  
417 4 ▶sub instance_attributes {
Subroutine instance_attributes: called CC=1 SCAR=0.0
Pod documented
418  
419 4 return grep { ! $_->is_static } @{$_[0]->_get_attributes};
420  
421 }
422  
423 3232 ▶sub linearized_isa {
Subroutine linearized_isa: called CC=1 SCAR=0.0
Pod documented
424  
425 3232 return @{ mro::get_linear_isa( $_[0]->name ) };
426  
427 }
428  
429 2185 ▶sub linearized_isa_classes {
Subroutine linearized_isa_classes: called CC=2 SCAR=7.1
Pod documented
430  
431 return map {
432 2185 ▶ GX::Meta::Class::Registry->get( $_ ) // __PACKAGE__->new( $_ )
Condition66%2/3
"GX::Meta::Class::Registry"->get($_) // __PACKAGE__->new($_)
l!l&&r!l&&!r
75114760
MC/DC0%0/2
"GX::Meta::Class::Registry"->get($_) // __PACKAGE__->new($_)
"GX::Meta::Class::Registry"->get($_) __PACKAGE__->new($_)
Truth table66%2/3
A // B
A= "GX::Meta::Class::Registry"->get($_)B= __PACKAGE__->new($_)
ABresult
1X1
011
000
433 } $_[0]->linearized_isa;
434  
435 }
436  
437 9866 ▶sub method {
Subroutine method: called CC=1 SCAR=0.0
Pod documented
438  
439 9866 return $_[0]->_get_methods->{$_[1]}
440  
441 }
442  
443 3 ▶sub method_names {
Subroutine method_names: called CC=1 SCAR=0.0
Pod documented
444  
445 3 return keys %{$_[0]->_get_methods};
446  
447 }
448  
449 14 ▶sub methods {
Subroutine methods: called CC=1 SCAR=0.0
Pod documented
450  
451 14 return values %{$_[0]->_get_methods};
452  
453 }
454  
455 2 ▶sub methods_with_code_attribute {
Subroutine methods_with_code_attribute: called CC=8 SCAR=35.0
Pod documented
456  
457 2 my $self = shift;
458  
459 2 my @result;
460  
461 2 ▶ if ( @_ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) { } 0 2 2
462  
463 0 my $code_attribute = shift;
464  
465 0 ▶ if ( @_ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
466 0 complain "Invalid number of arguments";
467 }
468  
469 0 ▶ if ( ! defined $code_attribute || $code_attribute !~ REGEX_IDENTIFIER ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $code_attribute and $code_attribute =~ /^[a-zA-Z_]\w*$/) 0 0 0
Condition0%0/3
defined $code_attribute and $code_attribute =~ /^[a-zA-Z_]\w*$/
!ll&&!rl&&r
000
MC/DC0%0/2
defined $code_attribute and $code_attribute =~ /^[a-zA-Z_]\w*$/
defined $code_attribute $code_attribute =~ /^[a-zA-Z_]\w*$/
Truth table0%0/3
A and B
A= defined $code_attributeB= $code_attribute =~ /^[a-zA-Z_]\w*$/
ABresult
0X0
100
111
470 0 complain "Invalid argument";
471 }
472  
473 0 my $code_attribute_regex = qr/^$code_attribute(?:\(.*\))?$/;
474  
475 METHOD:
476 0 for my $method ( $self->methods ) {
477  
478 0 for ( $method->code_attributes ) {
479 0 ▶ push( @result, $method ), next METHOD if /$code_attribute_regex/;
Branch: 1 branch
BranchTrueFalseTotal
if /$code_attribute_regex/ 0 0 0
480 }
481  
482 }
483  
484 }
485 else {
486 2 @result = grep { $_->code_attributes } $self->methods;
487 }
488  
489 2 return @result;
490  
491 }
492  
493 70149 ▶sub name {
Subroutine name: called CC=1 SCAR=0.0
Pod documented
494  
495 70149 return $_[0]->{'name'};
496  
497 }
498  
499 29867 ▶sub package {
Subroutine package: called CC=2 SCAR=7.2
Pod documented
500  
501 29867 ▶ return $_[0]->{'package'} //= GX::Meta::Package->new( $_[0]->name );
Condition66%2/3
$_[0]{"package"} //= "GX::Meta::Package"->new($_[0]->name)
l!l&&r!l&&!r
2865412130
MC/DC0%0/2
$_[0]{"package"} //= "GX::Meta::Package"->new($_[0]->name)
$_[0]{"package"} "GX::Meta::Package"->new($_[0]->name)
Truth table66%2/3
A //= B
A= $_[0]{"package"}B= "GX::Meta::Package"->new($_[0]->name)
ABresult
1X1
011
000
502  
503 }
504  
505 5227 ▶sub remove_method {
Subroutine remove_method: called CC=5 SCAR=17.8
Pod documented
506  
507 5227 my $self = shift;
508 5227 my $method_name = shift;
509  
510 5227 ▶ if ( @_ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 5227 5227
511 0 complain "Invalid number of arguments";
512 }
513  
514 5227 ▶ if ( ! defined $method_name || $method_name !~ REGEX_METHOD_NAME ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $method_name and $method_name =~ /^[a-zA-Z_]\w*$/) 0 5227 5227
Condition33%1/3
defined $method_name and $method_name =~ /^[a-zA-Z_]\w*$/
!ll&&!rl&&r
005227
MC/DC0%0/2
defined $method_name and $method_name =~ /^[a-zA-Z_]\w*$/
defined $method_name $method_name =~ /^[a-zA-Z_]\w*$/
Truth table33%1/3
A and B
A= defined $method_nameB= $method_name =~ /^[a-zA-Z_]\w*$/
ABresult
0X0
100
111
515 0 complain "Invalid argument";
516 }
517  
518 5227 ▶ return unless $self->has_method( $method_name );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->has_method($method_name) 3 5224 5227
519  
520 5224 $self->package->clear_typeglob_slot( $method_name, 'CODE' );
521  
522 5224 return 1;
523  
524 }
525  
526 20 ▶sub subclasses {
Subroutine subclasses: called CC=4 SCAR=13.9
Pod documented
527  
528 20 my $self = shift;
529  
530 20 my @subclasses;
531  
532 SUBCLASS:
533 20 for my $subclass ( $self->all_subclasses ) {
534  
535 24 for my $superclass ( $subclass->superclasses ) {
536 36 ▶ push( @subclasses, $subclass ), next SUBCLASS if $superclass == $self;
Branch: 1 branch
BranchTrueFalseTotal
if $superclass == $self 18 18 36
537 }
538  
539 }
540  
541 20 return @subclasses;
542  
543 }
544  
545 53 ▶sub superclasses {
Subroutine superclasses: called CC=9 SCAR=23.2
Pod documented
546  
547 53 my $self = shift;
548  
549 53 ▶ if ( @_ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 6 47 53
550  
551 6 my @class_names;
552  
553 6 for ( @_ ) {
554  
555 8 ▶ if ( blessed $_ && $_->isa( __PACKAGE__ ) ) {
Branch: 2 branches
BranchTrueFalseTotal
if (builtin::blessed($_) and $_->isa(__PACKAGE__)) { } 4 4 8
elsif (defined $_ and $_ =~ /^[a-zA-Z_]\w*(?:::[a-zA-Z_]\w*)*$/) { } 4 0 4
Condition66%2/3
builtin::blessed($_) and $_->isa(__PACKAGE__)
!ll&&!rl&&r
404
MC/DC50%1/2
builtin::blessed($_) and $_->isa(__PACKAGE__)
builtin::blessed($_) $_->isa(__PACKAGE__)
Truth table66%2/3
A and B
A= builtin::blessed($_)B= $_->isa(__PACKAGE__)
ABresult
0X0
100
111
556 4 push @class_names, $_->name;
557 }
558 ▶ elsif ( defined $_ && $_ =~ REGEX_CLASS_NAME ) {
Condition33%1/3
defined $_ and $_ =~ /^[a-zA-Z_]\w*(?:::[a-zA-Z_]\w*)*$/
!ll&&!rl&&r
004
MC/DC0%0/2
defined $_ and $_ =~ /^[a-zA-Z_]\w*(?:::[a-zA-Z_]\w*)*$/
defined $_ $_ =~ /^[a-zA-Z_]\w*(?:::[a-zA-Z_]\w*)*$/
Truth table33%1/3
A and B
A= defined $_B= $_ =~ /^[a-zA-Z_]\w*(?:::[a-zA-Z_]\w*)*$/
ABresult
0X0
100
111
559 4 push @class_names, $_;
560 }
561 else {
562 0 complain "Invalid argument";
563 }
564  
565 }
566  
567 6 $self->_set_isa( @class_names );
568  
569 6 ▶ return unless defined wantarray;
Branch: 1 branch
BranchTrueFalseTotal
unless defined wantarray 1 5 6
570  
571 }
572  
573 return map {
574 52 ▶ GX::Meta::Class::Registry->get( $_ ) // __PACKAGE__->new( $_ )
Condition33%1/3
"GX::Meta::Class::Registry"->get($_) // __PACKAGE__->new($_)
l!l&&r!l&&!r
7400
MC/DC0%0/2
"GX::Meta::Class::Registry"->get($_) // __PACKAGE__->new($_)
"GX::Meta::Class::Registry"->get($_) __PACKAGE__->new($_)
Truth table33%1/3
A // B
A= "GX::Meta::Class::Registry"->get($_)B= __PACKAGE__->new($_)
ABresult
1X1
011
000
575 } $self->_get_isa;
576  
577 }
578  
579  
580 # ----------------------------------------------------------------------------------------------------------------------
581 # Internal methods
582 # ----------------------------------------------------------------------------------------------------------------------
583  
584 728 ▶sub code_attribute_store {
Subroutine code_attribute_store: called CC=1 SCAR=0.0
Pod documented
585  
586 728 return $_[0]->{'code_attribute_store'};
587  
588 }
589  
590  
591 # ----------------------------------------------------------------------------------------------------------------------
592 # Private methods
593 # ----------------------------------------------------------------------------------------------------------------------
594  
595 3360 ▶sub _create_attribute {
Subroutine _create_attribute: called CC=1 SCAR=0.0
596  
597 3360 my $self = shift;
598  
599 3360 return GX::Meta::Attribute->new( @_, class => $self );
600  
601 }
602  
603 26290 ▶sub _create_method {
Subroutine _create_method: called CC=1 SCAR=0.0
604  
605 26290 my $self = shift;
606  
607 26290 return GX::Meta::Method->new( @_, class => $self );
608  
609 }
610  
611 10841 ▶sub _get_attributes {
Subroutine _get_attributes: called CC=1 SCAR=0.0
612  
613 10841 return $_[0]->{'attributes'};
614  
615 }
616  
617 6816 ▶sub _get_attributes_index {
Subroutine _get_attributes_index: called CC=1 SCAR=0.0
618  
619 6816 return $_[0]->{'attributes_index'};
620  
621 }
622  
623 527 ▶sub _get_code_attributes {
Subroutine _get_code_attributes: called CC=1 SCAR=0.0
624  
625 527 return $_[0]->{'code_attributes'};
626  
627 }
628  
629 1680 ▶sub _get_isa {
Subroutine _get_isa: called CC=1 SCAR=0.0
630  
631 269 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
632  
633 1680 return @{ $_[0]->name . '::ISA' };
634  
635 }
636  
637 19406 ▶sub _get_methods {
Subroutine _get_methods: called CC=4 SCAR=13.9
638  
639 19406 my $self = shift;
640  
641 19406 my $pkg_gen = mro::get_pkg_gen( $self->name );
642  
643 19406 ▶ return $self->{'methods'} if $pkg_gen == $self->{'methods_pkg_gen'};
Branch: 1 branch
BranchTrueFalseTotal
if $pkg_gen == $self->{"methods_pkg_gen"} 8081 11325 19406
644  
645 11325 $self->{'methods_pkg_gen'} = $pkg_gen;
646  
647 11325 my $methods = $self->{'methods'};
648 11325 my $class_name = $self->name;
649  
650 %$methods = map {
651 315453 ▶ ( $_ => $methods->{$_} // $self->_create_method( class => $self, name => $_ ) )
Condition66%2/3
$methods->{$_} // $self->_create_method("class", $self, "name", $_)
l!l&&r!l&&!r
289163262900
MC/DC0%0/2
$methods->{$_} // $self->_create_method("class", $self, "name", $_)
$methods->{$_} $self->_create_method("class", $self, "name", $_)
Truth table66%2/3
A // B
A= $methods->{$_}B= $self->_create_method("class", $self, "name", $_)
ABresult
1X1
011
000
652 } grep {
653 415629 ▶ $_ =~ REGEX_METHOD_NAME && defined &{"$class_name\::$_"}
Condition100%3/3
$_ =~ /^[a-zA-Z_]\w*$/ && defined &{"${class_name}::$_";}
!ll&&!rl&&r
1383986337315453
MC/DC100%2/2
$_ =~ /^[a-zA-Z_]\w*$/ && defined &{"${class_name}::$_";}
$_ =~ /^[a-zA-Z_]\w*$/ defined &{"${class_name}::$_";}
Truth table100%3/3
A && B
A= $_ =~ /^[a-zA-Z_]\w*$/B= defined &{"${class_name}::$_";}
ABresult
0X0
100
111
654 11325 } keys %{$self->package->symbol_table};
655  
656 11325 return $methods;
657  
658 }
659  
660 1634 ▶sub _set_isa {
Subroutine _set_isa: called CC=1 SCAR=0.0
661  
662 1634 my $self = shift;
663  
664 269 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
665  
666 1634 @{ $self->name . '::ISA' } = @_;
667  
668 1634 return;
669  
670 }
671  
672  
673 # ----------------------------------------------------------------------------------------------------------------------
674 # Object registry
675 # ----------------------------------------------------------------------------------------------------------------------
676  
677 package GX::Meta::Class::Registry;
678  
679 {
680  
681 269 my %OBJECTS;
682  
683 0 ▶ sub clear {
Subroutine clear: not called CC=1 SCAR=6.9
684 0 %OBJECTS = ();
685 0 return;
686 }
687  
688 23229 ▶ sub get {
Subroutine get: called CC=1 SCAR=0.0
689 23229 return $OBJECTS{$_[1]};
690 }
691  
692 6 ▶ sub remove {
Subroutine remove: called CC=1 SCAR=0.0
693 6 return delete $OBJECTS{$_[1]};
694 }
695  
696 1705 ▶ sub set {
Subroutine set: called CC=1 SCAR=0.0
697 1705 return $OBJECTS{$_[1]} = $_[2];
698 }
699  
700 }
701  
702  
703 269 1;
704  
705 __END__
« GX/Exception.pm ↑ Summary GX/HTTP/Body/File.pm »