GX/Class.pm

filter: Statementstmt 86.3% 139 / 161 Branchbran 68.9% 51 / 74 Conditioncond 70.0% 14 / 20 MC/DCmcdc 46.1% 6 / 13 Subroutinesub 95.6% 22 / 23 Podpod 100% 1 / 1 totaltotal 79.7% 233 / 292 CC 53 SCAR 23.2
CC
53
Cov
80%
import
31.9
__ANON__
8.5
__ANON__
7.1
CRAP 10.2
« GX/Route/Static/Compiled.pm ↑ Summary GX/Meta/Attribute/Hash/Ordered.pm »
1 # ----------------------------------------------------------------------------------------------------------------------
2 # GX Framework (c) 2009-2011 Jörg A. Uzarek <uzarek@runlevelnull.de>
3 # File: GX/Class.pm
4 # ----------------------------------------------------------------------------------------------------------------------
5  
6 package GX::Class;
7  
8 345 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 338 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 292 ▶use GX::Class::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 285 ▶use GX::Exception ();
Subroutine BEGIN: called CC=1 SCAR=0.0
13 272 ▶use GX::Meta;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 264 ▶use Scalar::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17  
18 # ----------------------------------------------------------------------------------------------------------------------
19 # Export mechanism
20 # ----------------------------------------------------------------------------------------------------------------------
21  
22 my %KEYWORD_FACTORY = (
23  
24 1122 ▶ 'build' => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
25  
26 1122 my $class = shift;
27  
28 1076 ▶ return sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
29  
30 1078 ▶ if ( caller ne $class ) {
Branch: 1 branch
BranchTrueFalseTotal
if (caller ne $class) 0 1042 1042
31 34 GX::Exception->complain( "Package mismatch" );
32 }
33  
34 1076 ▶ $class->__build if $class->can( '__build' );
Branch: 1 branch
BranchTrueFalseTotal
if $class->can("__build") 992 50 1042
35  
36 247 ▶ eval "package $class; no GX::Class;";
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
37  
38 1076 return;
39  
40 1122 };
41  
42 },
43  
44 1120 ▶ 'extends' => sub {
Subroutine __ANON__: called CC=1 SCAR=0.2
45  
46 1120 my $class = shift;
47  
48 448 ▶ return sub {
Subroutine __ANON__: called CC=6 SCAR=19.6
49  
50 448 ▶ if ( caller ne $class ) {
Branch: 1 branch
BranchTrueFalseTotal
if (caller ne $class) 0 448 448
51 0 GX::Exception->complain( "Package mismatch" );
52 }
53  
54 448 my @superclasses = @_;
55  
56 448 eval {
57 448 $class->meta->inherit_from( @superclasses );
58 };
59  
60 448 ▶ if ( $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 448 448
61 0 GX::Exception->complain( $@ );
62 }
63  
64 448 for ( @superclasses ) {
65 505 ▶ GX::Class::Util::class_exists( $_ ) or GX::Meta::Util::load_module( $_ ) or next;
Branch: 1 branch
BranchTrueFalseTotal
GX::Class::Util::class_exists($_) or GX::Meta::Util::load_module($_) or next 505 0 505
Condition66%2/3
GX::Class::Util::class_exists($_) or GX::Meta::Util::load_module($_)
l!l&&r!l&&!r
1083970
MC/DC0%0/2
GX::Class::Util::class_exists($_) or GX::Meta::Util::load_module($_)
GX::Class::Util::class_exists($_) GX::Meta::Util::load_module($_)
Truth table66%2/3
A or B
A= GX::Class::Util::class_exists($_)B= GX::Meta::Util::load_module($_)
ABresult
1X1
011
000
66 }
67  
68 448 return;
69  
70 1120 };
71  
72 },
73  
74 1086 ▶ 'has' => sub {
Subroutine __ANON__: called CC=1 SCAR=0.1
75  
76 1086 my $class = shift;
77  
78 3356 ▶ return sub {
Subroutine __ANON__: called CC=8 SCAR=21.4
79  
80 3356 ▶ if ( caller ne $class ) {
Branch: 1 branch
BranchTrueFalseTotal
if (caller ne $class) 0 3356 3356
81 0 GX::Exception->complain( "Package mismatch" );
82 }
83  
84 3356 my $name = shift;
85  
86 3356 ▶ if ( @_ % 2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ % 2) 0 3356 3356
87 0 GX::Exception->complain( "Odd number of options" );
88 }
89  
90 3356 my %args = @_;
91  
92 3356 eval {
93  
94 3356 my $attribute = $class->meta->add_attribute( %args, name => $name );
95  
96 3356 ▶ if ( ! exists $args{'accessor'} && ! exists $args{'accessors'} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $args{"accessor"} or exists $args{"accessors"}) 1102 2254 3356
Condition100%3/3
exists $args{"accessor"} or exists $args{"accessors"}
l!l&&r!l&&!r
123510191102
MC/DC100%2/2
exists $args{"accessor"} or exists $args{"accessors"}
exists $args{"accessor"} exists $args{"accessors"}
Truth table100%3/3
A or B
A= exists $args{"accessor"}B= exists $args{"accessors"}
ABresult
1X1
011
000
97  
98 1102 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if ($name =~ /^[a-zA-Z_]\w*$/ and not $class->meta->has_method($name)) 976 126 1102
Condition100%3/3
$name =~ /^[a-zA-Z_]\w*$/ and not $class->meta->has_method($name)
!ll&&!rl&&r
1125976
MC/DC100%2/2
$name =~ /^[a-zA-Z_]\w*$/ and not $class->meta->has_method($name)
$name =~ /^[a-zA-Z_]\w*$/ not $class->meta->has_method($name)
Truth table100%3/3
A and not(B)
A= $name =~ /^[a-zA-Z_]\w*$/B= not $class->meta->has_method($name)
ABresult
0X0
100
111
99 $name =~ GX::Meta::Constants::REGEX_METHOD_NAME &&
100 ! $class->meta->has_method( $name )
101 ) {
102 976 $attribute->add_accessor( name => $name );
103 }
104  
105 }
106  
107 3356 $attribute->install;
108  
109 };
110  
111 3356 ▶ if ( $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 3356 3356
112 0 GX::Exception->complain( $@ );
113 }
114  
115 3356 return;
116  
117 1086 };
118  
119 },
120  
121 1086 ▶ 'static' => sub {
Subroutine __ANON__: called CC=1 SCAR=0.5
122  
123 1086 my $class = shift;
124  
125 240 ▶ return sub {
Subroutine __ANON__: called CC=3 SCAR=13.3
126  
127 240 ▶ if ( caller ne $class ) {
Branch: 1 branch
BranchTrueFalseTotal
if (caller ne $class) 0 240 240
128 0 GX::Exception->complain( "Package mismatch" );
129 }
130  
131 240 ▶ if ( ! defined wantarray ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined wantarray) 0 240 240
132 0 GX::Exception->complain( "Useless use of keyword \"static\" in void context" );
133 }
134  
135 240 return ( @_, static => 1 );
136  
137 1086 };
138  
139 },
140  
141 1086 ▶ 'with' => sub {
Subroutine __ANON__: called CC=1 SCAR=0.3
142  
143 1086 my $class = shift;
144  
145 2 ▶ return sub {
Subroutine __ANON__: called CC=3 SCAR=12.3
146  
147 2 ▶ if ( caller ne $class ) {
Branch: 1 branch
BranchTrueFalseTotal
if (caller ne $class) 0 2 2
148 0 GX::Exception->complain( "Package mismatch" );
149 }
150  
151 2 eval {
152 2 GX::Class::Util::mixin( $class, @_ );
153 };
154  
155 2 ▶ if ( $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 2 2
156 0 GX::Exception->complain( $@ );
157 }
158  
159 2 return;
160  
161 1086 };
162  
163 }
164  
165 217 );
166  
167 1087 ▶sub import {
Subroutine import: called CC=26 SCAR=34.6
168  
169 1087 my $package = shift;
170  
171 1087 ▶ return unless $package eq __PACKAGE__;
Branch: 1 branch
BranchTrueFalseTotal
unless $package eq __PACKAGE__ 0 1087 1087
172  
173 1087 my $class = caller();
174  
175 1087 ▶ if ( $class eq 'main' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($class eq "main") 0 1087 1087
176 0 GX::Exception->complain( "Cannot import \"$package\" into package \"main\"" );
177 }
178  
179 1087 ▶ if ( @_ % 2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ % 2) 0 1087 1087
180 0 GX::Exception->complain( "Invalid number of arguments" );
181 }
182  
183 1087 my %args = @_;
184  
185 1087 for my $pragma ( qw( strict warnings ) ) {
186 2174 $pragma->import;
187 }
188  
189 1087 my $meta;
190  
191 1087 ▶ if ( exists $args{'meta'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args{'meta'}) { } 1 1086 1087
192  
193 1 $meta = delete $args{'meta'};
194  
195 1 ▶ if ( ! Scalar::Util::blessed( $meta ) || ! $meta->isa( 'GX::Meta::Class' ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (builtin::blessed($meta) and $meta->isa("GX::Meta::Class")) 0 1 1
Condition33%1/3
builtin::blessed($meta) and $meta->isa("GX::Meta::Class")
!ll&&!rl&&r
001
MC/DC0%0/2
builtin::blessed($meta) and $meta->isa("GX::Meta::Class")
builtin::blessed($meta) $meta->isa("GX::Meta::Class")
Truth table33%1/3
A and B
A= builtin::blessed($meta)B= $meta->isa("GX::Meta::Class")
ABresult
0X0
100
111
196 0 GX::Exception->complain( "Invalid value for option \"meta\"" );
197 }
198  
199 1 ▶ if ( $meta->name ne $class ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($meta->name ne $class) 0 1 1
200 0 GX::Exception->complain( "Metaobject mismatch" );
201 }
202  
203 1 GX::Meta::Class::Registry->set( $class => $meta );
204  
205 }
206 else {
207 1086 $meta = GX::Meta::Class->new( $class );
208 }
209  
210 1087 ▶ if ( exists $args{'superclass'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args{'superclass'}) { } 575 512 1087
211  
212 575 eval {
213 575 $meta->inherit_from( $args{'superclass'} );
214 };
215  
216 575 ▶ if ( $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 575 575
217 0 GX::Exception->complain(
218 message => "Cannot add superclass",
219 subexception => $@,
220 verbosity => 1
221 );
222 }
223  
224 575 delete $args{'superclass'};
225  
226 }
227 else {
228 512 $meta->inherit_from( $package );
229 }
230  
231 1087 ▶ if ( exists $args{'extends'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args{"extends"}) 16 1071 1087
232  
233 16 ▶ my @superclasses = ref $args{'extends'} eq 'ARRAY' ? @{$args{'extends'}} : $args{'extends'};
Branch: 1 branch
BranchTrueFalseTotal
ref $args{'extends'} eq 'ARRAY' ? : 6 10 16
234  
235 16 eval {
236 16 $meta->inherit_from( @superclasses );
237 };
238  
239 16 ▶ if ( $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 16 16
240 0 GX::Exception->complain(
241 message => "Cannot add superclasses",
242 subexception => $@,
243 verbosity => 1
244 );
245 }
246  
247 16 for ( @superclasses ) {
248 22 ▶ GX::Class::Util::class_exists( $_ ) or GX::Meta::Util::load_module( $_ ) or next;
Branch: 1 branch
BranchTrueFalseTotal
GX::Class::Util::class_exists($_) or GX::Meta::Util::load_module($_) or next 19 3 22
Condition100%3/3
GX::Class::Util::class_exists($_) or GX::Meta::Util::load_module($_)
l!l&&r!l&&!r
1453
MC/DC100%2/2
GX::Class::Util::class_exists($_) or GX::Meta::Util::load_module($_)
GX::Class::Util::class_exists($_) GX::Meta::Util::load_module($_)
Truth table100%3/3
A or B
A= GX::Class::Util::class_exists($_)B= GX::Meta::Util::load_module($_)
ABresult
1X1
011
000
249 }
250  
251 16 delete $args{'extends'};
252  
253 }
254  
255 1087 ▶ if ( exists $args{'code_attributes'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args{"code_attributes"}) 107 980 1087
256  
257 107 ▶ if ( ref $args{'code_attributes'} eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $args{'code_attributes'} eq 'ARRAY') { } 107 0 107
258  
259 107 for my $code_attribute ( @{$args{'code_attributes'}} ) {
260  
261 172 eval {
262 172 $meta->add_code_attribute( $code_attribute );
263 };
264  
265 172 ▶ if ( $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 172 172
266 0 GX::Exception->complain(
267 message => "Cannot add code attribute",
268 subexception => $@,
269 verbosity => 1
270 );
271 }
272  
273 }
274  
275 }
276 else {
277 0 GX::Exception->complain( "Invalid argument (\"code_attributes\" must be an array reference)" );
278 }
279  
280 107 delete $args{'code_attributes'};
281  
282 }
283  
284 1087 ▶ if ( exists $args{'with'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args{"with"}) 3 1084 1087
285  
286 3 eval {
287 3 ▶ GX::Class::Util::mixin( $class, ref $args{'with'} eq 'ARRAY' ? @{$args{'with'}} : $args{'with'} );
Branch: 1 branch
BranchTrueFalseTotal
ref $args{'with'} eq 'ARRAY' ? : 1 2 3
288 };
289  
290 3 ▶ if ( $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 1 2 3
291 1 GX::Exception->complain(
292 message => "Mixin failed",
293 subexception => $@,
294 verbosity => 1
295 );
296 }
297  
298 2 delete $args{'with'};
299  
300 }
301  
302 1086 ▶ if ( keys %args ) {
Branch: 1 branch
BranchTrueFalseTotal
if (keys %args) 0 1086 1086
303 0 GX::Exception->complain(
304 sprintf( "Unknown option (\"%s\")", ( sort keys %args )[0] )
305 );
306 }
307  
308 1086 for my $keyword ( keys %KEYWORD_FACTORY ) {
309 5430 $meta->add_method( $keyword => $KEYWORD_FACTORY{$keyword}->( $class ) );
310 }
311  
312 1086 return;
313  
314 }
315  
316 1044 ▶sub unimport {
Subroutine unimport: called CC=4 SCAR=14.2
317  
318 1044 my $package = shift;
319  
320 1044 ▶ return unless $package eq __PACKAGE__;
Branch: 1 branch
BranchTrueFalseTotal
unless $package eq __PACKAGE__ 0 1044 1044
321  
322 1044 my $meta = caller->meta;
323  
324 1044 for my $keyword ( keys %KEYWORD_FACTORY ) {
325 5220 ▶ $meta->remove_method( $keyword ) if $meta->method( $keyword );
Branch: 1 branch
BranchTrueFalseTotal
if $meta->method($keyword) 5220 0 5220
326 }
327  
328 1044 return;
329  
330 }
331  
332  
333 # ----------------------------------------------------------------------------------------------------------------------
334 # Public methods
335 # ----------------------------------------------------------------------------------------------------------------------
336  
337 13409 ▶sub meta {
Subroutine meta: called CC=2 SCAR=7.2
Pod documented
338  
339 13409 ▶ return GX::Meta::Class->new( ref $_[0] || $_[0] );
Condition66%2/3
ref $_[0] || $_[0]
l!l&&r!l&&!r
59133500
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
340  
341 }
342  
343  
344 # ----------------------------------------------------------------------------------------------------------------------
345 # Attribute magic
346 # ----------------------------------------------------------------------------------------------------------------------
347  
348 0 ▶sub FETCH_CODE_ATTRIBUTES {
Subroutine FETCH_CODE_ATTRIBUTES: not called CC=2 SCAR=17.9
349  
350 0 my $class = shift;
351 0 my $code_reference = shift;
352  
353 0 ▶ return @{ $class->meta->code_attribute_store->{$code_reference} || return };
Condition0%0/2
$class->meta->code_attribute_store->{$code_reference} || (return)
l!l
00
MC/DC0%0/1
$class->meta->code_attribute_store->{$code_reference} || (return)
$class->meta->code_attribute_store->{$code_reference}
Truth table0%0/2
A
A= $class->meta->code_attribute_store->{$code_reference}
Aresult
11
00
354  
355 }
356  
357 56 ▶sub MODIFY_CODE_ATTRIBUTES {
Subroutine MODIFY_CODE_ATTRIBUTES: called CC=5 SCAR=16.1
358  
359 56 my $class = shift;
360 56 my $code_reference = shift;
361 56 my @code_attributes = @_;
362  
363 56 for my $allowed_code_attribute ( $class->meta->all_code_attributes ) {
364  
365 69 my $code_attribute_regex = qr/^$allowed_code_attribute(?:\(.*\))?$/;
366  
367 69 my @unmatched_code_attributes;
368  
369 69 for my $code_attribute ( @code_attributes ) {
370  
371 69 ▶ if ( $code_attribute =~ $code_attribute_regex ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($code_attribute =~ /$code_attribute_regex/) { } 56 13 69
372 56 push @{$class->meta->code_attribute_store->{$code_reference}}, $code_attribute;
373 }
374 else {
375 13 push @unmatched_code_attributes, $code_attribute;
376 }
377  
378 }
379  
380 69 @code_attributes = @unmatched_code_attributes;
381  
382 69 ▶ last if ! @code_attributes;
Branch: 1 branch
BranchTrueFalseTotal
unless @code_attributes 56 13 69
383  
384 }
385  
386 56 return @code_attributes;
387  
388 }
389  
390  
391 217 1;
392  
393 __END__
« GX/Route/Static/Compiled.pm ↑ Summary GX/Meta/Attribute/Hash/Ordered.pm »