Class/Extends.pm

filter: Statementstmt 99.2% 135 / 136 Branchbran 67.6% 23 / 34 Conditioncond 58.8% 10 / 17 MC/DCmcdc 36.3% 4 / 11 Subroutinesub 100% 25 / 25 Podpod 100% 3 / 3 totaltotal 88.5% 200 / 226 CC 31 SCAR 16.5
CC
31
Cov
90%
_check_this_class
9.7
__ANON__
9.0
extends
8.2
CRAP 5.2
« Class/Rigid.pm ↑ Summary Class/Attributes.pm »
1 package Fukurama::Class::Extends;
2 4 ▶use Fukurama::Class::Version(0.01);
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_REGISTERED = 2;
10 4 our $LEVEL_CHECK_CHILDS = 3;
11 4 our $LEVEL_CHECK_ALL = 4;
12  
13 4 our $CHECK_LEVEL = $LEVEL_CHECK_CHILDS;
14  
15 4 my $REGISTER = {};
16 4 my $ERRORS = {};
17  
18 =head1 NAME
19  
20 Fukurama::Class::Extends - Pragma to extend class inheritation
21  
22 =head1 VERSION
23  
24 Version 0.01 (beta)
25  
26 =head1 SYNOPSIS
27  
28 package MyClass;
29 use Fukurama::Class::Extends('MyParent');
30  
31 =head1 DESCRIPTION
32  
33 This pragma-like module provides some extra check features for inheritation at compiletime.
34 It would check that your parent Module is loaded and that in multi-inheritation there is no
35 subroutine-conflict. Use Fukurama::Class instead, to get all the features for OO.
36  
37 =head1 CONFIG
38  
39 You can define the check-level which describes how the module will check inheritations.
40 The following levels are allowed:
41  
42 =over 4
43  
44 =item $Fukurama::Class::Extends::CHECK_LEVEL = $Fukurama::Class::Extends::LEVEL_DISABLE
45  
46 There is no check. If you use this level, it's like you use B<use base qw(...)>. There are no side effects.
47 This level is recommended for production.
48  
49 =item $Fukurama::Class::Extends::CHECK_LEVEL = $Fukurama::Class::Extends::LEVEL_CHECK_NONE
50  
51 All registration processes are executed, but there would be no check.
52  
53 =item $Fukurama::Class::Extends::CHECK_LEVEL = $Fukurama::Class::Extends::LEVEL_CHECK_REGISTERED
54  
55 All classes, which use this module would checked for Multi-inheritation-conflicts.
56  
57 =item $Fukurama::Class::Extends::CHECK_LEVEL = $Fukurama::Class::Extends::LEVEL_CHECK_CHILDS
58  
59 All classes, which use this module AND all childs of these classes would checked for Multi-inheritation-conflicts.
60 This is the default behavior when you does'n change the check-level.
61  
62 =item $Fukurama::Class::Extends::CHECK_LEVEL = $Fukurama::Class::Extends::LEVEL_CHECK_ALL
63  
64 All classes would checked for Multi-inheritation-conflicts. This means really ALL classes. Even all perl-internals.
65 This level is only for the sake of completeness.
66  
67 =back
68  
69 =head1 EXPORT
70  
71 -
72  
73 =head1 METHODS
74  
75 =over 4
76  
77 =item extends( child_class:STRING, childs_parent_class:STRING ) return:VOID
78  
79 Helper-method, which would executed by every pragma usage.
80  
81 =item run_check() return:VOID
82  
83 Helper method for static perl (see Fukurama::Class > BUGS)
84  
85 =item register_class_tree() return:VOID
86  
87 Helper method to register needed handler in Fukurama::Class::Tree
88  
89 =back
90  
91 =head1 AUTHOR, BUGS, SUPPORT, ACKNOWLEDGEMENTS, COPYRIGHT & LICENSE
92  
93 see perldoc of L<Fukurama::Class>
94  
95 =cut
96  
97 # void
98 1767 ▶my $BUILD_HANDLER = sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
99 1767 my $classname = $_[0];
100 1767 my $classdef = $_[1];
101
102 1767 my $inheritation_paths = Fukurama::Class::Tree->get_inheritation_path($classname);
103 1767 ▶ $classdef->{'extends'} = $inheritation_paths if(scalar(@$inheritation_paths));
Branch: 1 branch
BranchTrueFalseTotal
if scalar @$inheritation_paths 654 1113 1767
104 1767 return;
105 4 };
106 # void
107 1767 ▶my $CHECK_HANDLER = sub {
Subroutine __ANON__: called CC=9 SCAR=22.0
108 1767 my $classname = $_[0];
109 1767 my $classdef = $_[1];
110
111 1767 my $paths = $classdef->{'extends'};
112 1767 ▶ return if(ref($paths) ne 'ARRAY' || !__PACKAGE__->_check_this_class($classname, $paths));
Branch: 1 branch
BranchTrueFalseTotal
if ref $paths ne "ARRAY" or not __PACKAGE__->_check_this_class($classname, $paths) 1759 8 1767
Condition100%3/3
ref $paths ne "ARRAY" or not __PACKAGE__->_check_this_class($classname, $paths)
l!l&&r!l&&!r
11136468
MC/DC100%2/2
ref $paths ne "ARRAY" or not __PACKAGE__->_check_this_class($classname, $paths)
ref $paths ne "ARRAY" not __PACKAGE__->_check_this_class($classname, $paths)
Truth table100%3/3
A or not(B)
A= ref $paths ne "ARRAY"B= not __PACKAGE__->_check_this_class($classname, $paths)
ABresult
1X1
011
000
113
114 8 my $parent_path_subs = [];
115 8 foreach my $path (@$paths) {
116 10 my $path_subs = __PACKAGE__->_get_all_subs_for_classpath($path);
117 10 push(@$parent_path_subs, {
118 subs => $path_subs,
119 path => $path,
120 });
121 }
122 8 my $all_subs = {};
123 8 foreach my $entry (@$parent_path_subs) {
124 10 foreach my $subname (keys(%{$entry->{'subs'}})) {
125 10 ▶ if($all_subs->{$subname} && !__PACKAGE__->_is_same_sub($subname, $entry->{'subs'}->{$subname}, $all_subs->{$subname}->{'subs'}->{$subname})) {
Branch: 1 branch
BranchTrueFalseTotal
if ($all_subs->{$subname} and not __PACKAGE__->_is_same_sub($subname, $entry->{"subs"}{$subname}, $all_subs->{$subname}{"subs"}{$subname})) 2 8 10
Condition66%2/3
$all_subs->{$subname} and not __PACKAGE__->_is_same_sub($subname, $entry->{"subs"}{$subname}, $all_subs->{$subname}{"subs"}{$subname})
!ll&&!rl&&r
802
MC/DC50%1/2
$all_subs->{$subname} and not __PACKAGE__->_is_same_sub($subname, $entry->{"subs"}{$subname}, $all_subs->{$subname}{"subs"}{$subname})
$all_subs->{$subname} not __PACKAGE__->_is_same_sub($subname, $entry->{"subs"}{$subname}, $all_subs->{$subname}{"subs"}{$subname})
Truth table66%2/3
A and not(B)
A= $all_subs->{$subname}B= not __PACKAGE__->_is_same_sub($subname, $entry->{"subs"}{$subname}, $all_subs->{$subname}{"subs"}{$subname})
ABresult
0X0
100
111
126 2 my $other_entry = $all_subs->{$subname};
127 2 my $ident = "$classname\::$entry->{'subs'}->{$subname}";
128 2 ▶ next if($ERRORS->{$ident});
Branch: 1 branch
BranchTrueFalseTotal
if $ERRORS->{$ident} 1 1 2
129 _carp("Multi-inheritation-warning for class '$classname':\n" .
130 " > sub '$subname' is defined twice in parent-classes\n" .
131 " > '$entry->{'subs'}->{$subname}' and '$other_entry->{'subs'}->{$subname}'\n" .
132 " > inheritation-path for '$entry->{'subs'}->{$subname}':\n" .
133 1 " $classname > " . join(' > ', @{$entry->{'path'}}) . "\n" .
134 " > inheritation-path for '$other_entry->{'subs'}->{$subname}':\n" .
135 1 " $classname > " . join(' > ', @{$other_entry->{'path'}}) . "\n", 1);
136 1 $ERRORS->{$ident} = 1;
137 }
138 9 $all_subs->{$subname} = $entry;
139 }
140 }
141 8 return;
142 4 };
143 # AUTOMAGIC void
144 6 ▶sub import {
Subroutine import: called CC=1 SCAR=0.0
145 6 my $class = $_[0];
146 6 my $parent = $_[1];
147
148 6 my ($child) = caller(0);
149 6 local $Carp::CarpLevel = $Carp::CarpLevel + 1;
150 6 $class->extends($child, $parent);
151 5 return undef;
152 }
153 # void
154 6 ▶sub extends {
Subroutine extends: called CC=8 SCAR=21.1
Pod documented
155 6 my $class = $_[0];
156 6 my $child = $_[1];
157 6 my $parent = $_[2];
158 6 ▶ my $import_depth = $_[3] || 0;
Condition50%1/2
$_[3] || 0
l!l
06
MC/DC0%0/1
$_[3] || 0
$_[3]
Truth table50%1/2
A
A= $_[3]
Aresult
11
00
159
160 6 ▶ local $SIG{'__DIE__'} = sub {};
Subroutine __ANON__: called CC=1 SCAR=0.0
161
162 4 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
163
164 6 ▶ if($CHECK_LEVEL > $LEVEL_DISABLE) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Fukurama::Class::Extends::CHECK_LEVEL > $Fukurama::Class::Extends::LEVEL_DISABLE) 6 0 6
165 6 ▶ if(!%{"$child\::"} && eval("use $parent();return 1")) {
Branch: 1 branch
BranchTrueFalseTotal
if (not %{"${child}::";} and eval "use $parent();return 1") 0 6 6
Condition33%1/3
not %{"${child}::";} and eval "use $parent();return 1"
!ll&&!rl&&r
600
MC/DC0%0/2
not %{"${child}::";} and eval "use $parent();return 1"
not %{"${child}::";} eval "use $parent();return 1"
Truth table33%1/3
not(A) and B
A= not %{"${child}::";}B= eval "use $parent();return 1"
ABresult
0X0
100
111
166 0 _croak($@, $import_depth);
167 }
168 }
169 6 ▶ if(!eval("package $child;use base qw($parent);return 1") || $@) {
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
if (not eval "package $child;use base qw($parent);return 1" or $@) 1 5 6
Condition66%2/3
not eval "package $child;use base qw($parent);return 1" or $@
l!l&&r!l&&!r
105
MC/DC50%1/2
not eval "package $child;use base qw($parent);return 1" or $@
not eval "package $child;use base qw($parent);return 1" $@
Truth table66%2/3
not(A) or B
A= not eval "package $child;use base qw($parent);return 1"B= $@
ABresult
1X1
011
000
170 1 _croak("Can't extend class '$parent' in child class '$child':\n$@", $import_depth);
171 }
172
173 5 ▶ return if($CHECK_LEVEL == $LEVEL_DISABLE);
Branch: 1 branch
BranchTrueFalseTotal
if $Fukurama::Class::Extends::CHECK_LEVEL == $Fukurama::Class::Extends::LEVEL_DISABLE 0 5 5
174 5 $REGISTER->{$child} = 1;
175 5 $class->register_class_tree();
176 5 return;
177 }
178 # STATIC void
179 7 ▶sub register_class_tree {
Subroutine register_class_tree: called CC=1 SCAR=0.0
Pod documented
180 7 my $class = $_[0];
181
182 7 Fukurama::Class::Tree->register_build_handler($BUILD_HANDLER);
183 7 Fukurama::Class::Tree->register_check_handler($CHECK_HANDLER);
184 7 return;
185 }
186 # STATIC boolean
187 654 ▶sub _check_this_class {
Subroutine _check_this_class: called CC=9 SCAR=22.8
188 654 my $class = $_[0];
189 654 my $classname = $_[1];
190 654 my $paths = $_[2];
191
192 654 ▶ return 1 if($CHECK_LEVEL == $LEVEL_CHECK_ALL);
Branch: 1 branch
BranchTrueFalseTotal
if $Fukurama::Class::Extends::CHECK_LEVEL == $Fukurama::Class::Extends::LEVEL_CHECK_ALL 0 654 654
193 654 ▶ return 0 if($CHECK_LEVEL == $LEVEL_CHECK_NONE);
Branch: 1 branch
BranchTrueFalseTotal
if $Fukurama::Class::Extends::CHECK_LEVEL == $Fukurama::Class::Extends::LEVEL_CHECK_NONE 0 654 654
194
195 654 ▶ return 1 if($REGISTER->{$classname});
Branch: 1 branch
BranchTrueFalseTotal
if $REGISTER->{$classname} 8 646 654
196 646 ▶ return 0 if($CHECK_LEVEL == $LEVEL_CHECK_REGISTERED);
Branch: 1 branch
BranchTrueFalseTotal
if $Fukurama::Class::Extends::CHECK_LEVEL == $Fukurama::Class::Extends::LEVEL_CHECK_REGISTERED 0 646 646
197
198 646 ▶ if($CHECK_LEVEL == $LEVEL_CHECK_CHILDS) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Fukurama::Class::Extends::CHECK_LEVEL == $Fukurama::Class::Extends::LEVEL_CHECK_CHILDS) 646 0 646
199 646 foreach my $path (@$paths) {
200 814 foreach my $path_class (@$path) {
201 2174 ▶ return 1 if($REGISTER->{$path_class});
Branch: 1 branch
BranchTrueFalseTotal
if $REGISTER->{$path_class} 0 2174 2174
202 }
203 }
204 }
205 646 return 0;
206 }
207 # boolean
208 2 ▶sub _is_same_sub {
Subroutine _is_same_sub: called CC=2 SCAR=6.9
209 2 my $class = $_[0];
210 2 my $subname = $_[1];
211 2 my $first_class = $_[2];
212 2 my $second_class = $_[3];
213
214 4 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
215
216 2 ▶ return 1 if(*{$first_class . '::' . $subname}{'CODE'} == *{$second_class . '::' . $subname}{'CODE'});
Branch: 1 branch
BranchTrueFalseTotal
if *{$first_class . '::' . $subname;}{"CODE"} == *{$second_class . '::' . $subname;}{"CODE"} 0 2 2
217 2 return 0;
218 }
219 # hashref
220 10 ▶sub _get_all_subs_for_classpath {
Subroutine _get_all_subs_for_classpath: called CC=4 SCAR=14.2
221 10 my $class = $_[0];
222 10 my $path = $_[1];
223
224 10 my $path_subs = {};
225 10 foreach my $parent (@$path) {
226 10 foreach my $subname (Fukurama::Class::Tree->get_class_subs($parent)) {
227 10 ▶ $path_subs->{$subname} ||= $parent;
Condition33%1/3
$path_subs->{$subname} ||= $parent
l!l&&r!l&&!r
0100
MC/DC0%0/2
$path_subs->{$subname} ||= $parent
$path_subs->{$subname} $parent
Truth table33%1/3
A ||= B
A= $path_subs->{$subname}B= $parent
ABresult
1X1
011
000
228 }
229 }
230 10 return $path_subs;
231 }
232 # void
233 8 ▶sub run_check {
Subroutine run_check: called CC=3 SCAR=11.3
Pod documented
234 8 my $class = $_[0];
235 8 my $type = $_[1];
236
237 8 ▶ $type = 'MANUAL' if(!defined($type));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $type 0 8 8
238 8 ▶ Fukurama::Class::Tree->run_check('CHECK') if($CHECK_LEVEL > $LEVEL_DISABLE);
Branch: 1 branch
BranchTrueFalseTotal
if $Fukurama::Class::Extends::CHECK_LEVEL > $Fukurama::Class::Extends::LEVEL_DISABLE 8 0 8
239 8 return;
240 }
241  
242 4 ▶no warnings 'void'; # avoid 'Too late to run CHECK/INIT block'
Subroutine BEGIN: called CC=1 SCAR=0.0
243  
244 # AUTOMAGIC void
245 4 ▶CHECK {
Subroutine CHECK: called CC=1 SCAR=0.0
246 4 __PACKAGE__->run_check('CHECK');
247 }
248 # AUTOMAGIC void
249 4 ▶END {
Subroutine END: called CC=1 SCAR=0.0
250 4 __PACKAGE__->run_check('END');
251 }
252 4 1;
« Class/Rigid.pm ↑ Summary Class/Attributes.pm »