Class/Tree.pm

filter: Statementstmt 100% 116 / 116 Branchbran 80.7% 21 / 26 Conditioncond 66.6% 6 / 9 MC/DCmcdc 60.0% 3 / 5 Subroutinesub 100% 21 / 21 Podpod 100% 7 / 7 totaltotal 94.5% 174 / 184 CC 24 SCAR 11.9
CC
24
Cov
95%
_read_class
6.0
get_all_subs
5.1
_get_inheritation_path
5.0
CRAP 3.3
« Class/Implements.pm ↑ Summary Class/Abstract.pm »
1 package Fukurama::Class::Tree;
2 7 ▶use Fukurama::Class::Version(0.03);
Subroutine BEGIN: called CC=1 SCAR=0.0
3 7 ▶use Fukurama::Class::Rigid;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 7 ▶use Fukurama::Class::Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 7 my $CHECK = {};
7 7 my $BUILD = {};
8 7 my $IS_BUILD = 0;
9 7 my $EXEC_ONCE = {};
10 7 my $CLASSTREE = {};
11  
12 7 my $FORBID_SUB_TYPES = ['system', 'tie', 'thread'];
13 7 my $FORBIDDEN_SUBS = {
14 system => {
15 import => 1,
16 unimport => 1,
17 can => 1,
18 isa => 1,
19 VERSION => 1,
20 BEGIN => 1,
21 UNITCHECK => 1,
22 CHECK => 1,
23 INIT => 1,
24 END => 1,
25 DESTROY => 1,
26 AUTOLOAD => 1,
27 MODIFY_CODE_ATTRIBUTES => 1,
28 MODIFY_SCALAR_ATTRIBUTES => 1,
29 MODIFY_ARRAY_ATTRIBUTES => 1,
30 MODIFY_HASH_ATTRIBUTES => 1,
31 MODIFY_GLOB_ATTRIBUTES => 1,
32 FETCH_CODE_ATTRIBUTES => 1,
33 FETCH_SCALAR_ATTRIBUTES => 1,
34 FETCH_ARRAY_ATTRIBUTES => 1,
35 FETCH_HASH_ATTRIBUTES => 1,
36 FETCH_GLOB_ATTRIBUTES => 1,
37 },
38 thread => {
39 CLONE => 1,
40 CLONE_SKIP => 1,
41 },
42 tie => {
43 TIESCALAR => 1,
44 FETCH => 1,
45 STORE => 1,
46 UNTIE => 1,
47 TIEARRAY => 1,
48 FETCHSIZE => 1,
49 STORESIZE => 1,
50 EXTEND => 1,
51 EXISTS => 1,
52 DELETE => 1,
53 CLEAR => 1,
54 PUSH => 1,
55 POP => 1,
56 SHIFT => 1,
57 UNSHIFT => 1,
58 SPLICE => 1,
59 TIEHASH => 1,
60 FIRSTKEY => 1,
61 NEXTKEY => 1,
62 SCALAR => 1,
63 TIEHANDLE => 1,
64 WRITE => 1,
65 PRINT => 1,
66 PRINTF => 1,
67 READ => 1,
68 READLINE => 1,
69 GETC => 1,
70 CLOSE => 1,
71 },
72 };
73 =head1 NAME
74  
75 Fukurama::Class::Tree - Helper-class to register class-handler
76  
77 =head1 VERSION
78  
79 Version 0.03 (beta)
80  
81 =head1 SYNOPSIS
82  
83 my $BUILD_HANDLER = sub {
84 my $classname = $_[0];
85 my $classdef = $_[1];
86
87 no strict 'refs';
88
89 $classdef->{'implements'} = \@{$classname . '::INTERFACES'};
90 return;
91 };
92 my $CHECK_HANDLER = sub {
93 my $classname = $_[0];
94 my $classdef = $_[1];
95
96 my $paths = $classdef->{'implements'};
97 return if(ref($paths) ne 'ARRAY');
98 # Do what ever you want (for interfaces, see Fukurama::Class::Implements)
99 # ...
100 return;
101 };
102 Fukurama::Class::Tree->register_build_handler($BUILD_HANDLER);
103 Fukurama::Class::Tree->register_check_handler($CHECK_HANDLER);
104  
105 =head1 DESCRIPTION
106  
107 This module register class-definitions, read the inheritation-trees and execute checks to the registered class-defintions.
108 You can register handler to create you own class defintions and handler to check something at this classes.
109 It's a central helper class for most of Fukurama::Class - modules.
110  
111 =head1 CONFIG
112  
113 -
114
115 =head1 EXPORT
116  
117 -
118  
119 =head1 METHODS
120  
121 =over 4
122  
123 =item get_all_subs( class:STRING ) return:STRING()
124  
125 Get all methods from the given class.
126  
127 =item get_class_subs( class:STRING ) return:STRING[]
128  
129 Get all methods for the given class. It omit all special-methods. See is_special_sub().
130  
131 =item get_inheritation_path( class:STRING ) return:[ STRING[] ]
132  
133 Return all inheritation class-paths for the given class.
134 For example, if a class B<MyClass> (multiple-)inherit from B<ParentA> and B<ParentB>, it will return these two inheritation-class-paths.
135 If the given class doesn't use any multi inheritation, you will get an arrayref with one classpath and these classpath will be
136 an array of all parents and grandparents etc. the given class.
137  
138 =item is_special_sub( subname:STRING ) return:BOOLEAN
139  
140 Check, if the given subroutine(-name) is from an special method which is used perl "magically".
141 For example it returns true for I<import()>, I<unimport()>, I<DESTROY()> etc.
142  
143 =item register_build_handler( handler:CODE ) return:VOID
144  
145 Register a handler subroutine to build your own class-defintion. For example you can implement an own syntax to define
146 interface-implementations. The build-handler takes two parameters: the name and the definition-hash (which you can extend)
147 for each loaded class.
148  
149 =item register_check_handler( handler:CODE ) return:VOID
150  
151 Register a handler subroutine to check the classes. For example you can check an self-defined interface syntax. The check-handler
152 takes two parameters: the name and the definition-hash, which was build via bild-handler, for each loaded class.
153  
154 =item run_check() return:VOID
155  
156 Helper method for static perl (see Fukurama::Class > BUGS)
157 This method will find all loades classes, run all registered build-handler for each loaded class and, when this is finished,
158 it runs all registered check-handler (even for each loaded class).
159  
160 =back
161  
162 =head1 AUTHOR, BUGS, SUPPORT, ACKNOWLEDGEMENTS, COPYRIGHT & LICENSE
163  
164 see perldoc of L<Fukurama::Class>
165  
166 =cut
167  
168 # void
169 35 ▶sub run_check {
Subroutine run_check: called CC=3 SCAR=11.0
Pod documented
170 35 my $class = $_[0];
171 35 my $type = $_[1];
172
173 35 ▶ $type = 'MANUAL' if(!defined($type));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $type 2 33 35
174
175 35 ▶ return if($EXEC_ONCE->{$type});
Branch: 1 branch
BranchTrueFalseTotal
if $EXEC_ONCE->{$type} 17 18 35
176 18 $class->_build();
177 18 $class->_check();
178 16 $EXEC_ONCE->{$type} = 1;
179 16 return;
180 }
181 # void
182 18 ▶sub register_build_handler {
Subroutine register_build_handler: called CC=2 SCAR=7.0
Pod documented
183 18 my $class = $_[0];
184 18 my $handler = $_[1];
185
186 18 ▶ _croak("Can only register subrefs as handler, not '$handler'") if(ref($handler) ne 'CODE');
Branch: 1 branch
BranchTrueFalseTotal
if ref $handler ne "CODE" 0 18 18
187 18 $BUILD->{int($handler)} = $handler;
188 18 return;
189 }
190 # void
191 20 ▶sub register_check_handler {
Subroutine register_check_handler: called CC=2 SCAR=7.0
Pod documented
192 20 my $class = $_[0];
193 20 my $handler = $_[1];
194
195 20 ▶ _croak("Can only register subrefs as handler, not '$handler'") if(ref($handler) ne 'CODE');
Branch: 1 branch
BranchTrueFalseTotal
if ref $handler ne "CODE" 0 20 20
196 20 $CHECK->{int($handler)} = $handler;
197 20 return;
198 }
199 # void
200 18 ▶sub _build {
Subroutine _build: called CC=1 SCAR=0.0
201 18 my $class = $_[0];
202
203 7 ▶ no warnings 'recursion';
Subroutine BEGIN: called CC=1 SCAR=0.0
204
205 18 $CLASSTREE = {};
206 18 $class->_read_class('', $CLASSTREE);
207 18 $IS_BUILD = 1;
208
209 18 return;
210 }
211 # void
212 5286 ▶sub _read_class {
Subroutine _read_class: called CC=6 SCAR=17.9
213 5286 my $class = $_[0];
214 5286 my $parent_class = $_[1];
215 5286 my $classtree = $_[2];
216
217 7 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
218
219 5286 foreach my $child_class (keys %{$parent_class . '::'}) {
220 107818 my $classname = ($parent_class . '::' . $child_class);
221 107818 $classname =~ s/^(?:::)(?:main|)//;
222 107818 $classname =~ s/::$//;
223 107818 ▶ next if(!UNIVERSAL::isa($classname, $classname) || $classname =~ m/[^a-zA-Z0-9_:]/);
Branch: 1 branch
BranchTrueFalseTotal
if not UNIVERSAL::isa($classname, $classname) or $classname =~ /[^a-zA-Z0-9_:]/ 102537 5281 107818
Condition100%3/3
not UNIVERSAL::isa($classname, $classname) or $classname =~ /[^a-zA-Z0-9_:]/
l!l&&r!l&&!r
10253525281
MC/DC100%2/2
not UNIVERSAL::isa($classname, $classname) or $classname =~ /[^a-zA-Z0-9_:]/
not UNIVERSAL::isa($classname, $classname) $classname =~ /[^a-zA-Z0-9_:]/
Truth table100%3/3
not(A) or B
A= not UNIVERSAL::isa($classname, $classname)B= $classname =~ /[^a-zA-Z0-9_:]/
ABresult
1X1
011
000
224 5281 ▶ next if($classtree->{$classname});
Branch: 1 branch
BranchTrueFalseTotal
if $classtree->{$classname} 13 5268 5281
225
226 5268 $classtree->{$classname} = {};
227 5268 foreach my $build_handler (values(%$BUILD)) {
228 5293 &$build_handler($classname, $classtree->{$classname});
229 }
230 5268 $class->_read_class($classname, $classtree);
231 }
232 5286 return;
233 }
234 # void
235 18 ▶sub _check {
Subroutine _check: called CC=4 SCAR=13.9
236 18 my $class = $_[0];
237
238 18 ▶ _croak("Can't check classtree without build!") if(!$IS_BUILD);
Branch: 1 branch
BranchTrueFalseTotal
unless $IS_BUILD 0 18 18
239 18 foreach my $class (keys(%$CLASSTREE)) {
240 5134 foreach my $check_handler (values(%$CHECK)) {
241 6333 &$check_handler($class, $CLASSTREE->{$class});
242 }
243 }
244 16 return;
245 }
246 # string()
247 63 ▶sub get_class_subs {
Subroutine get_class_subs: called CC=1 SCAR=0.0
Pod documented
248 63 my $class = $_[0];
249 63 my $used_class = $_[1];
250
251 63 return grep { !$class->is_special_sub($_) } $class->get_all_subs($used_class);
252 }
253 # string ()
254 179 ▶sub get_all_subs {
Subroutine get_all_subs: called CC=5 SCAR=16.3
Pod documented
255 179 my $class = $_[0];
256 179 my $used_class = $_[1];
257
258 7 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
259
260 179 my $subs = {};
261 179 foreach my $glob (%{$used_class . '::'}) {
262 2070 ▶ next if((ref($glob) && ref($glob) ne 'GLOB') || !*$glob{'CODE'});
Branch: 1 branch
BranchTrueFalseTotal
if ref $glob and ref $glob ne "GLOB" or not *$glob{"CODE"} 1647 423 2070
Condition50%3/6
ref $glob and ref $glob ne "GLOB" or not *$glob{"CODE"}
l!l&&r!l&&!r
01647423
ref $glob and ref $glob ne "GLOB"
!ll&&!rl&&r
207000
MC/DC33%1/3
ref $glob and ref $glob ne "GLOB" or not *$glob{"CODE"}
ref $glob ref $glob ne "GLOB" not *$glob{"CODE"}
Truth table40%2/5
A and B or not(C)
A= ref $globB= ref $glob ne "GLOB"C= not *$glob{"CODE"}
ABCresult
11X1
0X11
1011
0X00
1000
263 423 $subs->{*$glob{'NAME'}} = undef;
264 }
265 179 return keys(%$subs);
266 }
267 # boolean
268 257 ▶sub is_special_sub {
Subroutine is_special_sub: called CC=3 SCAR=11.0
Pod documented
269 257 my $class = $_[0];
270 257 my $subname = $_[1];
271
272 257 foreach my $type (@$FORBID_SUB_TYPES) {
273 659 ▶ return 1 if($FORBIDDEN_SUBS->{$type}->{$subname});
Branch: 1 branch
BranchTrueFalseTotal
if $FORBIDDEN_SUBS->{$type}{$subname} 56 603 659
274 }
275 201 return 0;
276 }
277 # void
278 3634 ▶sub _get_inheritation_path {
Subroutine _get_inheritation_path: called CC=5 SCAR=16.1
279 3634 my $class = $_[0];
280 3634 my $child = $_[1];
281 3634 my $child_path = $_[2];
282 3634 my $all_path_routes = $_[3];
283  
284 7 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
285
286 3634 my $parents = \@{$child . '::ISA'};
287 3634 ▶ if(!scalar(@$parents)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (scalar @$parents) 1968 1666 3634
288 1968 ▶ push(@$all_path_routes, [@$child_path]) if(scalar(@$child_path));
Branch: 1 branch
BranchTrueFalseTotal
if scalar @$child_path 835 1133 1968
289 1968 return;
290 }
291
292 1666 foreach my $parent (@$parents) {
293 1838 my $class_allways_in_path = grep({ $_ eq $parent } @$child_path);
294 1838 ▶ next if($class_allways_in_path);
Branch: 1 branch
BranchTrueFalseTotal
if $class_allways_in_path 0 1838 1838
295 1838 $class->_get_inheritation_path($parent, [@$child_path, $parent], $all_path_routes);
296 }
297 1666 return;
298 }
299 # array[]
300 1796 ▶sub get_inheritation_path {
Subroutine get_inheritation_path: called CC=2 SCAR=7.0
Pod documented
301 1796 my $class = $_[0];
302 1796 my $child_class = $_[1];
303
304 1796 ▶ return [] if(!$child_class);
Branch: 1 branch
BranchTrueFalseTotal
unless $child_class 0 1796 1796
305 1796 my $all_path_routes = [];
306 1796 $class->_get_inheritation_path($child_class, [], $all_path_routes);
307 1796 return $all_path_routes;
308 }
309  
310 7 ▶no warnings 'void'; # avoid 'Too late to run CHECK/INIT block'
Subroutine BEGIN: called CC=1 SCAR=0.0
311  
312 # AUTOMAGIC void
313 7 ▶CHECK {
Subroutine CHECK: called CC=1 SCAR=0.0
314 7 __PACKAGE__->run_check('CHECK');
315 }
316 # AUTOMAGIC void
317 7 ▶END {
Subroutine END: called CC=1 SCAR=0.0
318 7 __PACKAGE__->run_check('END');
319 }
320 7 1;
« Class/Implements.pm ↑ Summary Class/Abstract.pm »