Class/Implements.pm

filter: Statementstmt 98.8% 167 / 169 Branchbran 80.0% 32 / 40 Conditioncond 80.0% 12 / 15 MC/DCmcdc 66.6% 6 / 9 Subroutinesub 100% 24 / 24 Podpod 100% 3 / 3 totaltotal 93.8% 244 / 260 CC 38 SCAR 14.1
CC
38
Cov
94%
__ANON__
7.0
_check_implementations
7.0
__ANON__
6.0
CRAP 4.1
« Class/AttributesHandler.pm ↑ Summary Class/Tree.pm »
1 package Fukurama::Class::Implements;
2 4 ▶use Fukurama::Class::Version(0.02);
Subroutine BEGIN: called CC=1 SCAR=0.0
3 4 ▶use Fukurama::Class::Rigid;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 4 ▶use Fukurama::Class::Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 4 ▶use Fukurama::Class::Tree();
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 4 our $LEVEL_DISABLE = 0;
8 4 our $LEVEL_CHECK_NONE = 1;
9 4 our $LEVEL_CHECK_ALL = 2;
10  
11 4 our $CHECK_LEVEL = $LEVEL_CHECK_ALL;
12  
13 4 my $ERRORS = {};
14 4 my $ISA_ALREADY_DECORATED;
15 4 my $REGISTER = {};
16  
17 =head1 NAME
18  
19 Fukurama::Class::Implements - Pragma to provide interfaces
20  
21 =head1 VERSION
22  
23 Version 0.02 (beta)
24  
25 =head1 SYNOPSIS
26  
27 package MyClass;
28 use Fukurama::Class::Implements('MyParent');
29  
30 =head1 DESCRIPTION
31  
32 This pragma-like module enables te possibility to use interfaces (like in java). The implementation
33 of all subroutines (except perls speacials) will be checked at compiletime. Your package won't inherit
34 from this interface but every isa() will say that it is. Use Fukurama::Class instead, to get all the
35 features for OO.
36  
37 =head1 CONFIG
38  
39 You can define the check-level which describes how the module will check implementations.
40 The following levels are allowed:
41  
42 =over 4
43  
44 =item DISABLE (0)
45  
46 There is no check and no change in UNIVERSAL. If you use this level, it's like you remove this module.
47 There are no side effects. You should only use this, if you never use the isa() method to check for interfaces.
48  
49 =item CHECK_NONE (1)
50  
51 All Registration-Processes are executed and UNIVERSAL::isa would be decorated, but there would be no check.
52 This level is recommended for production.
53  
54 =item CHECK_ALL (2)
55  
56 All Classes would checked for implementation. This is the default behavior when you does'n change the
57 check-level.
58  
59 =back
60  
61 =head1 EXPORT
62  
63 =over 4
64  
65 =item UNIVERSAL::isa
66  
67 would be decorated
68  
69 =back
70  
71 =head1 METHODS
72  
73 =over 4
74  
75 =item implements( child_class:STRING, interface_class:STRING ) return:VOID
76  
77 Helper-method, which would executed by every pragma usage.
78  
79 =item run_check() return:VOID
80  
81 Helper method for static perl (see Fukurama::Class > BUGS)
82  
83 =item register_class_tree() return:VOID
84  
85 Helper method to register needed handler in Fukurama::Class::Tree
86  
87 =back
88  
89 =head1 AUTHOR, BUGS, SUPPORT, ACKNOWLEDGEMENTS, COPYRIGHT & LICENSE
90  
91 see perldoc of L<Fukurama::Class>
92  
93 =cut
94  
95 # void
96 2654 ▶my $BUILD_HANDLER = sub {
Subroutine __ANON__: called CC=6 SCAR=17.9
97 2654 my $classname = $_[0];
98 2654 my $classdef = $_[1];
99
100 2654 my $interface_def = $REGISTER->{$classname};
101 2654 ▶ return if(!$interface_def);
Branch: 1 branch
BranchTrueFalseTotal
unless $interface_def 2639 15 2654
102
103 15 my $inheritation_paths = [];
104 15 foreach my $interface (keys(%$interface_def)) {
105 28 my $interface_inheritation_paths = Fukurama::Class::Tree->get_inheritation_path($interface);
106 28 foreach my $path (@$interface_inheritation_paths) {
107 8 unshift(@$path, $interface);
108 8 push(@$inheritation_paths, $path);
109 }
110 28 ▶ push(@$inheritation_paths, [$interface]) if(!scalar(@$interface_inheritation_paths));
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @$interface_inheritation_paths 20 8 28
111 }
112 15 ▶ $classdef->{'implements'} = $inheritation_paths if(scalar(@$inheritation_paths));
Branch: 1 branch
BranchTrueFalseTotal
if scalar @$inheritation_paths 15 0 15
113 15 return;
114 4 };
115 # void
116 2520 ▶my $CHECK_HANDLER = sub {
Subroutine __ANON__: called CC=7 SCAR=19.5
117 2520 my $classname = $_[0];
118 2520 my $classdef = $_[1];
119
120 2520 ▶ return if($CHECK_LEVEL <= $LEVEL_CHECK_NONE);
Branch: 1 branch
BranchTrueFalseTotal
if $Fukurama::Class::Implements::CHECK_LEVEL <= $Fukurama::Class::Implements::LEVEL_CHECK_NONE 0 2520 2520
121 2520 my $paths = $classdef->{'implements'};
122 2520 ▶ return if(ref($paths) ne 'ARRAY');
Branch: 1 branch
BranchTrueFalseTotal
if ref $paths ne "ARRAY" 2506 14 2520
123
124 14 my $interface_list = {};
125 14 foreach my $path (@$paths) {
126 26 my $level = 0;
127 26 foreach my $class (@$path) {
128 38 ++$level;
129 38 ▶ $interface_list->{$class} ||= ($level == 1 ? 1 : 0);
Branch: 1 branch
BranchTrueFalseTotal
$level == 1 ? : 26 12 38
Condition66%2/3
$interface_list->{$class} ||= $level == 1 ? 1 : 0
l!l&&r!l&&!r
02612
MC/DC50%1/2
$interface_list->{$class} ||= $level == 1 ? 1 : 0
$interface_list->{$class} $level == 1 ? 1 : 0
Truth table66%2/3
A ||= B
A= $interface_list->{$class}B= $level == 1 ? 1 : 0
ABresult
1X1
011
000
130 }
131 }
132 14 __PACKAGE__->_check_implementations($classname, $interface_list);
133 12 return;
134 4 };
135 # AUTOMAGIC void
136 9 ▶sub import {
Subroutine import: called CC=2 SCAR=7.0
137 9 my $class = $_[0];
138 9 my $interface = $_[1];
139 9 my $import_depth = $_[2];
140
141 9 ▶ $import_depth ||= 0;
Condition50%1/2
$import_depth ||= 0
l!l
09
MC/DC0%0/1
$import_depth ||= 0
$import_depth
Truth table50%1/2
A
A= $import_depth
Aresult
11
00
142 9 my $child = [caller($import_depth)]->[0];
143 9 $class->implements($child, $interface);
144 8 return undef;
145 }
146 # void
147 9 ▶sub implements {
Subroutine implements: called CC=5 SCAR=16.3
Pod documented
148 9 my $class = $_[0];
149 9 my $child = $_[1];
150 9 my $interface = $_[2];
151
152 9 ▶ return if($CHECK_LEVEL == $LEVEL_DISABLE);
Branch: 1 branch
BranchTrueFalseTotal
if $Fukurama::Class::Implements::CHECK_LEVEL == $Fukurama::Class::Implements::LEVEL_DISABLE 0 9 9
153  
154 4 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
155  
156 9 $class->_decorate_isa();
157 0 ▶ if(!%{"$interface\::"} && !eval("use $interface();return 1")) {
Subroutine BEGIN: called CC=1 SCAR=0.3
Branch: 1 branch
BranchTrueFalseTotal
unless (%{"${interface}::";} or eval "use $interface();return 1") 1 8 9
Condition66%2/3
%{"${interface}::";} or eval "use $interface();return 1"
l!l&&r!l&&!r
801
MC/DC50%1/2
%{"${interface}::";} or eval "use $interface();return 1"
%{"${interface}::";} eval "use $interface();return 1"
Truth table66%2/3
A or B
A= %{"${interface}::";}B= eval "use $interface();return 1"
ABresult
1X1
011
000
158 1 _croak($@);
159 }
160 8 ▶ $REGISTER->{$child} ||= {};
Condition100%2/2
$REGISTER->{$child} ||= {}
l!l
35
MC/DC100%1/1
$REGISTER->{$child} ||= {}
$REGISTER->{$child}
Truth table100%2/2
A
A= $REGISTER->{$child}
Aresult
11
00
161 8 $REGISTER->{$child}->{$interface} = undef;
162 8 $class->register_class_tree();
163 8 return;
164 }
165 # void
166 10 ▶sub register_class_tree {
Subroutine register_class_tree: called CC=1 SCAR=0.0
Pod documented
167 10 my $class = $_[0];
168
169 10 Fukurama::Class::Tree->register_build_handler($BUILD_HANDLER);
170 10 Fukurama::Class::Tree->register_check_handler($CHECK_HANDLER);
171 10 return;
172 }
173 # void
174 11 ▶sub run_check {
Subroutine run_check: called CC=4 SCAR=14.3
Pod documented
175 11 my $class = $_[0];
176 11 my $type = $_[1];
177
178 11 ▶ return if($CHECK_LEVEL <= $LEVEL_CHECK_NONE);
Branch: 1 branch
BranchTrueFalseTotal
if $Fukurama::Class::Implements::CHECK_LEVEL <= $Fukurama::Class::Implements::LEVEL_CHECK_NONE 0 11 11
179 11 ▶ $type = 'MANUAL' if(!defined($type));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $type 0 11 11
180
181 11 ▶ if($CHECK_LEVEL == $LEVEL_CHECK_ALL) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Fukurama::Class::Implements::CHECK_LEVEL == $Fukurama::Class::Implements::LEVEL_CHECK_ALL) 11 0 11
182 11 Fukurama::Class::Tree->run_check($type);
183 }
184 9 return;
185 }
186 # void
187 14 ▶sub _check_implementations {
Subroutine _check_implementations: called CC=7 SCAR=19.5
188 14 my $class = $_[0];
189 14 my $checked_class = $_[1];
190 14 my $checked_class_interfaces = $_[2];
191
192 14 my $error_list = [];
193 14 my $interface_defs = [];
194 14 my @interfaces = keys(%$checked_class_interfaces);
195 14 foreach my $interface (@interfaces) {
196 38 push(@$interface_defs, {
197 class => $interface,
198 subs => [Fukurama::Class::Tree->get_class_subs($interface)],
199 });
200 }
201 14 my $class_def = {};
202 14 foreach my $sub (Fukurama::Class::Tree->get_class_subs($checked_class)) {
203 22 $class_def->{$sub} = undef;
204 }
205 14 $class->_check_class_def($checked_class, $class_def, $interface_defs, $error_list);
206
207 14 ▶ if(@$error_list) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$error_list) 6 8 14
208 6 my $errors = '';
209 6 foreach my $e (@$error_list) {
210 14 my $key = $e->{'class'} . '-' . $e->{'method'};
211 14 ▶ next if($ERRORS->{$key});
Branch: 1 branch
BranchTrueFalseTotal
if $ERRORS->{$key} 10 4 14
212 $errors .= "\n > You doesn't implement method '$e->{method}' in class '$e->{class}' which is defined in interface(es): " .
213 4 join(', ', @{$e->{interfaces}});
214 4 $ERRORS->{$key} = 1;
215 }
216 6 ▶ _croak(scalar(@$error_list) . " Interface-Error(s):$errors\n", 1) if($errors);
Branch: 1 branch
BranchTrueFalseTotal
if $errors 2 4 6
217 }
218 12 return;
219 }
220 # void
221 14 ▶sub _check_class_def {
Subroutine _check_class_def: called CC=2 SCAR=6.9
222 14 my $class = $_[0];
223 14 my $obj_class = $_[1];
224 14 my $class_def = $_[2];
225 14 my $interface_defs = $_[3];
226 14 my $errorlist = $_[4];
227
228 14 my $interface_methods = $class->_merge_interface_methods($interface_defs);
229 14 foreach my $method (keys %$interface_methods) {
230 30 $class->_check_method_implementation($obj_class, $method, exists($class_def->{$method}), $interface_methods->{$method}, $errorlist);
231 }
232 14 return;
233 }
234 # void
235 30 ▶sub _check_method_implementation {
Subroutine _check_method_implementation: called CC=3 SCAR=11.0
236 30 my $class = $_[0];
237 30 my $obj_class = $_[1];
238 30 my $method = $_[2];
239 30 my $class_method_exist = $_[3];
240 30 my $interface_method_list = $_[4];
241 30 my $error_list = $_[5];
242
243 30 ▶ if(!$class_method_exist) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($class_method_exist) 14 16 30
244 14 my $definitions = [];
245 14 foreach my $interface (@$interface_method_list) {
246 22 push(@$definitions, $interface);
247 }
248 14 push(@$error_list, {
249 class => $obj_class,
250 method => $method,
251 interfaces => $definitions,
252 });
253 }
254 30 return;
255 }
256 # hash[]
257 14 ▶sub _merge_interface_methods {
Subroutine _merge_interface_methods: called CC=4 SCAR=13.9
258 14 my $class = $_[0];
259 14 my $interface_defs = $_[1];
260
261 14 my $methodnames = {};
262 14 foreach my $def (@$interface_defs) {
263 38 foreach my $method (@{$def->{'subs'}}) {
264 54 ▶ $methodnames->{$method} ||= [];
Condition100%2/2
$methodnames->{$method} ||= []
l!l
2430
MC/DC100%1/1
$methodnames->{$method} ||= []
$methodnames->{$method}
Truth table100%2/2
A
A= $methodnames->{$method}
Aresult
11
00
265 54 push(@{$methodnames->{$method}}, $def->{'class'});
266 }
267 }
268 14 return $methodnames;
269 }
270 # string{}
271 42811 ▶sub _has_interface {
Subroutine _has_interface: called CC=4 SCAR=13.9
272 42811 my $class = $_[0];
273 42811 my $obj_class = $_[1];
274 42811 my $interface_class = $_[2];
275
276 42811 ▶ return 0 if(!defined($obj_class));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $obj_class 0 42811 42811
277 42811 my $interfaces = $REGISTER->{$obj_class};
278 42811 ▶ return 0 if(!$interfaces || !exists($interfaces->{$interface_class}));
Branch: 1 branch
BranchTrueFalseTotal
unless $interfaces and exists $interfaces->{$interface_class} 42805 6 42811
Condition100%3/3
$interfaces and exists $interfaces->{$interface_class}
!ll&&!rl&&r
42765406
MC/DC100%2/2
$interfaces and exists $interfaces->{$interface_class}
$interfaces exists $interfaces->{$interface_class}
Truth table100%3/3
A and B
A= $interfacesB= exists $interfaces->{$interface_class}
ABresult
0X0
100
111
279 6 return 1;
280 }
281 # void
282 9 ▶sub _decorate_isa {
Subroutine _decorate_isa: called CC=3 SCAR=11.0
283 9 my $class = $_[0];
284
285 4 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
286 4 ▶ no warnings 'redefine';
Subroutine BEGIN: called CC=1 SCAR=0.0
287
288 9 ▶ return if($ISA_ALREADY_DECORATED);
Branch: 1 branch
BranchTrueFalseTotal
if $ISA_ALREADY_DECORATED 7 2 9
289
290 2 my $identifier = 'UNIVERSAL::isa';
291 2 my $old = *{$identifier}{'CODE'};
292 2 ▶ die("Unable to decorate non existing sub $identifier") if(!$old);
Branch: 1 branch
BranchTrueFalseTotal
unless $old 0 2 2
293
294 2 ▶ *{$identifier} = sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
295 42811 my $obj_class = $_[0];
296 42811 my $type = $_[1];
297
298 42811 ▶ return 1 if($class->_has_interface($obj_class, $type));
Branch: 1 branch
BranchTrueFalseTotal
if $class->_has_interface($obj_class, $type) 6 42805 42811
299
300 42805 goto &$old;
301 2 };
302 2 $ISA_ALREADY_DECORATED = 1;
303 2 return;
304 }
305  
306 4 ▶no warnings 'void'; # avoid 'Too late to run CHECK/INIT block'
Subroutine BEGIN: called CC=1 SCAR=0.0
307  
308 # AUTOMAGIC void
309 4 ▶sub CHECK {
Subroutine CHECK: called CC=1 SCAR=0.0
310 4 __PACKAGE__->run_check('CHECK');
311 }
312 # AUTOMAGIC void
313 4 ▶sub END {
Subroutine END: called CC=1 SCAR=0.0
314 4 __PACKAGE__->run_check('END');
315 }
316 4 1;
« Class/AttributesHandler.pm ↑ Summary Class/Tree.pm »