Class/Attributes.pm

filter: Statementstmt 91.4% 129 / 141 Branchbran 66.6% 12 / 18 Conditioncond 35.7% 5 / 14 MC/DCmcdc 25.0% 2 / 8 Subroutinesub 87.5% 21 / 24 Podpod 100% 4 / 4 totaltotal 82.7% 173 / 209 CC 25 SCAR 16.3
CC
25
Cov
84%
remove_attribute_handler
20.0
add_attribute_handler
12.0
_merge_class_definition
7.0
CRAP 5.1
« Class/Extends.pm ↑ Summary Class/AttributesHandler.pm »
1 package Fukurama::Class::Attributes;
2 3 ▶use Fukurama::Class::Version(0.01);
Subroutine BEGIN: called CC=1 SCAR=0.0
3 3 ▶use Fukurama::Class::Rigid;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use Fukurama::Class::Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
5
6 3 ▶use Fukurama::Class::Tree();
Subroutine BEGIN: called CC=1 SCAR=0.0
7 3 ▶use Fukurama::Class::AttributesHandler();
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶use Fukurama::Class::Attributes::OOStandard();
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 ▶use Fukurama::Class::Extends();
Subroutine BEGIN: called CC=1 SCAR=0.0
10 3 ▶use Fukurama::Class::Implements();
Subroutine BEGIN: called CC=1 SCAR=0.0
11
12 3 our $LEVEL_CHECK_NONE = 0;
13 3 our $LEVEL_CHECK_ALL = 1;
14
15 3 our $CHECK_LEVEL = $LEVEL_CHECK_ALL;
16
17 3 my $INIT;
18 my $REGISTERED_ATTRIBUTE_HANDLER;
19 3 my $REGISTERED_CLASSES;
20 3 my $INHERITATION_CHECK_SUBS;
21 3 my $INHERIT_METHOD_NAME;
22 3 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
23 3 $REGISTERED_ATTRIBUTE_HANDLER = {
24 'Fukurama::Class::Attributes::OOStandard' => 0,
25 };
26 3 $REGISTERED_CLASSES = {};
27 3 $INHERITATION_CHECK_SUBS = {};
28 3 $INHERIT_METHOD_NAME = 'check_inheritation';
29 }
30
31 =head1 NAME
32
33 Fukurama::Class::Attributes - Pragma like module to extend code attributes
34
35 =head1 VERSION
36
37 Version 0.01 (beta)
38
39 =head1 SYNOPSIS
40
41 package MyAttributeClass;
42 sub MyFirstCodeAttribute {
43 my $class = $_[0];
44 my $subroutine_data = $_[1];
45
46 ... do some checks or what you want ...
47
48 return;
49 }
50 sub check_inheritation {
51 my $class = $_[0];
52 my $method_name = $_[1];
53 my $parent_class = $_[2];
54 my $child_class = $_[3];
55 my $inheritation_type = $_[4];
56
57 return;
58 }
59
60 package MyClass;
61 BEGIN {
62 use Fukurama::Class::Attributes();
63 Fukurama::Class::Attributes->add_attribute_handler('MyAttributeClass');
64 }
65 use Fukurama::Class::Attributes;
66 sub my_method : Method(static|void|) {
67 return;
68 }
69 sub other_sub : MyFirstCodeAttribute() {
70 ...
71 }
72
73 =head1 DESCRIPTION
74
75 This pragma-like module provides functions to extend code attributes for yourself and check
76 the inheritation. It includes Fukurama::Attributes::OOStandard, which enables Method and Constructor
77 definitoins for subroutines. Use Fukurama::Class instead, to get all the features for OO.
78
79 =head1 CONFIG
80
81 You can disables all checks, which includes syntax and inheritation check by saying:
82
83 $Fukurama::Class::Attributes::CHECK_LEVEL = $Fukurama::Class::Attributes::LEVEL_CHECK_NONE;
84
85 =head1 EXPORT
86
87 -
88
89 =head1 METHODS
90
91 =over 4
92
93 =item add_attribute_handler( handler_class:CLASS ) return:BOOLEAN
94
95 Add all defined attribute-methods of the given class, so you can use their code attributes in
96 all of your subroutines. See section L<CREATE AN OWN ATTRIBUTE CLASS> for the rules.
97
98 =item remove_attribute_handler( handler_class:CLASS ) return:BOOLEAN
99
100 Remove all defined attribute-methods of the given class, so you can't use their anymore as code attributes.
101
102 =item register_class( export_to_class:CLASS ) return:BOOLEAN
103
104 Export all code attributes to the given class so you can use all registered code attributes in there.
105 Every child of the given class can even use this behavior.
106
107 =item run_check( ) return:VOID
108
109 Helper method for static perl (see Fukurama::Class > BUGS)
110 Its calls Fukurama::Class::AttributeHandler->run_check(), which check the correct syntax of all registered
111 code attributes and check some defined conventions.
112
113 E.g. for code attribute B<Method>: it will check that the access level of child class methods are the same or
114 stricter than the parent class method.
115
116 =back
117
118 =head1 CREATE AN OWN ATTRIBUTE CLASS
119
120 An "attribute class" describe one (or many) code attributes, which you can use like B<my_method : MyNewCodeAttribute(Foo) {...}>
121 There are the following rules for the class-methods:
122
123 -Every Attribute has to start with an uppercase letter
124
125 -Only on other method can be there, the subroutine B<check_inheritation()>
126
127 -All these methods have to be void
128
129 Attribute methods take on parameter, a hash reference, which contain informations about the method, which uses
130 the actual code-attribute. For every subroutine which contains this code attribute, the corresponding
131 method in your attribute class would be called. The parameter contain the following data:
132
133 resolved => BOOLEAN, # the subroutine is resolved (only internal use to avoid calls without the name of the subroutine)
134 data => STRING, # the attribute-data. If you say B<sub get : Method(public||)> it youd contain B<public||>
135 sub_name => STRING, # the name of the subroutine, which call this attribute
136 executed => BOOLEAN, # this attribute for this subroutine is allways called (only internal use to avoid double callings)
137 attribute => STRING, # the name of the attribute. Its the same like the name of your code-attribute-method.
138 handler => HASHREF, # Contain a reference to your code-attribute method and class. Only for internal use.
139 sub => CODEREF, # The code referense of the subroutine, which contain the code attribute.
140 class => STRING, # The class in which the subroutine is declared, which contain the code attribute.
141
142 There are many things which you can do with code attributes, e.g. the Method and Constructor definitions from
143 Fukurama::Class::Attributes::OOStandard or some simple things like in Catalyst. So, do what you need.
144
145 The check_inheritation() method is optional, check the code attribute inheritation for each class and take the following parameters:
146
147 $method_name : STRING # the methodname which is checked
148 $parent_class : STRING # the parent class of the actual checked class
149 $child_class : STRING # the actual checked class
150 $inheritation_type : STRING # the type of inheritation. extend is standard, implement even exists
151
152 With this method you can compare every subroutine, which contain a code attribute, with all parents. If you use multi
153 inheritation or interfaces there can be more than one parent. And it even compares in the same way all grandparents etc.
154
155 =head1 AUTHOR, BUGS, SUPPORT, ACKNOWLEDGEMENTS, COPYRIGHT & LICENSE
156
157 see perldoc of L<Fukurama::Class>
158
159 =cut
160
161 # STATIC boolean
162 0 ▶sub add_attribute_handler {
Subroutine add_attribute_handler: not called CC=3 SCAR=24.8
Pod documented
163 0 my $class = $_[0];
164 0 my $handler_class = $_[1];
165
166 0 ▶ $REGISTERED_ATTRIBUTE_HANDLER ||= {};
Condition0%0/2
$REGISTERED_ATTRIBUTE_HANDLER ||= {}
l!l
00
MC/DC0%0/1
$REGISTERED_ATTRIBUTE_HANDLER ||= {}
$REGISTERED_ATTRIBUTE_HANDLER
Truth table0%0/2
A
A= $REGISTERED_ATTRIBUTE_HANDLER
Aresult
11
00
167 0 ▶ return 0 if(defined($REGISTERED_ATTRIBUTE_HANDLER->{$handler_class}));
Branch: 1 branch
BranchTrueFalseTotal
if defined $REGISTERED_ATTRIBUTE_HANDLER->{$handler_class} 0 0 0
168 0 $REGISTERED_ATTRIBUTE_HANDLER->{$handler_class} = 1;
169 0 return 1;
170 }
171 # STATIC boolean
172 0 ▶sub remove_attribute_handler {
Subroutine remove_attribute_handler: not called CC=4 SCAR=30.0
Pod documented
173 0 my $class = $_[0];
174 0 my $handler_class = $_[1];
175
176 return 0 if(!$REGISTERED_ATTRIBUTE_HANDLER || !defined($REGISTERED_ATTRIBUTE_HANDLER->{$handler_class})
177 0 ▶ || $REGISTERED_ATTRIBUTE_HANDLER->{$handler_class});
Branch: 1 branch
BranchTrueFalseTotal
if not $REGISTERED_ATTRIBUTE_HANDLER && defined $REGISTERED_ATTRIBUTE_HANDLER->{$handler_class} or $REGISTERED_ATTRIBUTE_HANDLER->{$handler_class} 0 0 0
Condition0%0/6
not $REGISTERED_ATTRIBUTE_HANDLER && defined $REGISTERED_ATTRIBUTE_HANDLER->{$handler_class} or $REGISTERED_ATTRIBUTE_HANDLER->{$handler_class}
l!l&&r!l&&!r
000
$REGISTERED_ATTRIBUTE_HANDLER && defined $REGISTERED_ATTRIBUTE_HANDLER->{$handler_class}
!ll&&!rl&&r
000
MC/DC0%0/3
not $REGISTERED_ATTRIBUTE_HANDLER && defined $REGISTERED_ATTRIBUTE_HANDLER->{$handler_class} or $REGISTERED_ATTRIBUTE_HANDLER->{$handler_class}
$REGISTERED_ATTRIBUTE_HANDLER defined $REGISTERED_ATTRIBUTE_HANDLER->{$handler_class} $REGISTERED_ATTRIBUTE_HANDLER->{$handler_class}
Truth table0%0/4
not(A && B) or C
A= $REGISTERED_ATTRIBUTE_HANDLERB= defined $REGISTERED_ATTRIBUTE_HANDLER->{$handler_class}C= $REGISTERED_ATTRIBUTE_HANDLER->{$handler_class}
ABCresult
0XX1
10X1
1111
1100
178 0 delete($REGISTERED_ATTRIBUTE_HANDLER->{$handler_class});
179 0 return 1;
180 }
181 # AUTOMAGIC void
182 4 ▶sub import {
Subroutine import: called CC=1 SCAR=0.0
183
184 4 my $caller_class = caller();
185 4 __PACKAGE__->register_class($caller_class);
186 4 return;
187 }
188 # STATIC void
189 5 ▶sub register_class {
Subroutine register_class: called CC=4 SCAR=13.9
Pod documented
190 5 my $class = $_[0];
191 5 my $export_to_class = $_[1];
192
193 5 ▶ if(!$INIT) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($INIT) 2 3 5
194 2 $class->_init();
195 2 $INIT = 1;
196 }
197
198 5 foreach my $handler_class (keys(%$REGISTERED_ATTRIBUTE_HANDLER)) {
199 5 ▶ next if($REGISTERED_ATTRIBUTE_HANDLER->{$handler_class});
Branch: 1 branch
BranchTrueFalseTotal
if $REGISTERED_ATTRIBUTE_HANDLER->{$handler_class} 3 2 5
200 2 Fukurama::Class::AttributesHandler->register_attributes($handler_class);
201
202 3 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
203
204 2 my $inheritation_sub = *{$handler_class . '::' . $INHERIT_METHOD_NAME}{'CODE'};
205 2 $INHERITATION_CHECK_SUBS->{$handler_class} = $inheritation_sub;
206 2 $REGISTERED_ATTRIBUTE_HANDLER->{$handler_class} = 1;
207 }
208
209 5 $REGISTERED_CLASSES->{$export_to_class} = 1;
210 5 Fukurama::Class::AttributesHandler->export($export_to_class);
211 5 return;
212 }
213 # STATIC void
214 2 ▶sub _init {
Subroutine _init: called CC=2 SCAR=7.0
215 2 my $class = $_[0];
216
217 2 Fukurama::Class::AttributesHandler->register_helper_method($INHERIT_METHOD_NAME);
218 2 ▶ return if($CHECK_LEVEL == $LEVEL_CHECK_NONE);
Branch: 1 branch
BranchTrueFalseTotal
if $Fukurama::Class::Attributes::CHECK_LEVEL == $Fukurama::Class::Attributes::LEVEL_CHECK_NONE 0 2 2
219
220 1174 ▶ my $CHECK_HANDLER = sub {
Subroutine __ANON__: called CC=2 SCAR=7.0
221 1174 my $classname = $_[0];
222 1174 my $classdef = $_[1];
223
224 1174 ▶ return if($CHECK_LEVEL == $LEVEL_CHECK_NONE);
Branch: 1 branch
BranchTrueFalseTotal
if $Fukurama::Class::Attributes::CHECK_LEVEL == $Fukurama::Class::Attributes::LEVEL_CHECK_NONE 0 1174 1174
225 1174 __PACKAGE__->_check_inheritation($classname, $classdef);
226 1174 return;
227 2 };
228
229 2 Fukurama::Class::Extends->register_class_tree();
230 2 Fukurama::Class::Implements->register_class_tree();
231 2 Fukurama::Class::Tree->register_check_handler($CHECK_HANDLER);
232 2 return;
233 }
234 # STATIC void
235 1174 ▶sub _check_inheritation {
Subroutine _check_inheritation: called CC=4 SCAR=13.9
236 1174 my $class = $_[0];
237 1174 my $classname = $_[1];
238 1174 my $classdef= $_[2];
239
240 1174 foreach my $inherit_data (@{$class->_get_parent_classes($classname, $classdef)}) {
241 554 my $filtered_inherit_path = $class->_get_registered_parents($inherit_data->{'path'});
242 554 ▶ next if(!scalar(@$filtered_inherit_path));
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @$filtered_inherit_path 534 20 554
243 20 push(@$filtered_inherit_path, $classname);
244
245 20 my $parent_path = [];
246 20 ▶ while(scalar(@$filtered_inherit_path) > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @$filtered_inherit_path > 1) 42 20 62
247 42 my $parent = shift(@$filtered_inherit_path);
248 42 my $child = $filtered_inherit_path->[0];
249
250 42 $class->_merge_class_definition($parent, $child, $parent_path, $inherit_data->{'type'});
251 42 push(@$parent_path, $parent);
252 }
253 }
254 1174 return;
255 }
256 # STATIC void
257 42 ▶sub _merge_class_definition {
Subroutine _merge_class_definition: called CC=7 SCAR=19.5
258 42 my $class = $_[0];
259 42 my $parent = $_[1];
260 42 my $child = $_[2];
261 42 my $parent_path = $_[3];
262 42 my $inheritation_type = $_[4];
263
264 42 my $parent_methods = {};
265 42 foreach my $parent_class (@$parent_path, $parent) {
266 74 foreach my $parent_method (Fukurama::Class::Tree->get_all_subs($parent_class)) {
267 214 $parent_methods->{$parent_method} = $parent_class;
268 }
269 }
270
271 42 my $child_methods = {};
272 42 foreach my $child_method (Fukurama::Class::Tree->get_all_subs($child)) {
273 110 $child_methods->{$child_method} = $child;
274 }
275
276 42 my @all_methods = (keys(%$child_methods), keys(%$parent_methods));
277 42 my %unique_methods = ();
278 42 @unique_methods{@all_methods} = (1) x scalar(@all_methods);
279
280 42 foreach my $method (keys(%unique_methods)) {
281 152 ▶ my $parent_class = $parent_methods->{$method} || $parent;
Condition66%2/3
$parent_methods->{$method} || $parent
l!l&&r!l&&!r
138140
MC/DC0%0/2
$parent_methods->{$method} || $parent
$parent_methods->{$method} $parent
Truth table66%2/3
A || B
A= $parent_methods->{$method}B= $parent
ABresult
1X1
011
000
282 152 foreach my $handler_class (keys(%$INHERITATION_CHECK_SUBS)) {
283 152 &{$INHERITATION_CHECK_SUBS->{$handler_class}}($handler_class, $method, $parent_class, $child, $inheritation_type);
284 }
285 }
286 42 return;
287 }
288 # STATIC string[]
289 1174 ▶sub _get_parent_classes {
Subroutine _get_parent_classes: called CC=3 SCAR=11.0
290 1174 my $class = $_[0];
291 1174 my $check_class = $_[1];
292 1174 my $classdef = $_[2];
293
294 1174 my @all_paths = ();
295 1174 foreach my $type (keys(%$classdef)) {
296 440 foreach my $inherit_path (@{$classdef->{$type}}) {
297 554 push(@all_paths, {
298 type => $type,
299 path => $inherit_path,
300 });
301 }
302 }
303 1174 return \@all_paths;
304 }
305 # STATIC string[]
306 554 ▶sub _get_registered_parents {
Subroutine _get_registered_parents: called CC=4 SCAR=13.9
307 554 my $class = $_[0];
308 554 my $path = $_[1];
309
310 554 my @filtered_path = ();
311 554 my $was_registered = 0;
312 554 foreach my $parent (reverse(@$path)) {
313 1480 ▶ next if(!$was_registered && !$REGISTERED_CLASSES->{$parent});
Branch: 1 branch
BranchTrueFalseTotal
unless $was_registered or $REGISTERED_CLASSES->{$parent} 1438 42 1480
Condition100%3/3
$was_registered or $REGISTERED_CLASSES->{$parent}
l!l&&r!l&&!r
22201438
MC/DC100%2/2
$was_registered or $REGISTERED_CLASSES->{$parent}
$was_registered $REGISTERED_CLASSES->{$parent}
Truth table100%3/3
A or B
A= $was_registeredB= $REGISTERED_CLASSES->{$parent}
ABresult
1X1
011
000
314 42 $was_registered = 1;
315 42 push(@filtered_path, $parent);
316 }
317 554 return \@filtered_path;
318 }
319 # STATIC void
320 0 ▶sub run_check {
Subroutine run_check: not called CC=1 SCAR=6.9
Pod documented
321 0 Fukurama::Class::AttributesHandler->run_check();
322 }
323
324 3 ▶no warnings 'void'; # avoid 'Too late to run CHECK/INIT block'
Subroutine BEGIN: called CC=1 SCAR=0.0
325
326 # AUTOMAGIC
327 3 ▶CHECK {
Subroutine CHECK: called CC=1 SCAR=0.0
328 3 Fukurama::Class::AttributesHandler->run_check();
329 }
330 # AUTOMAGIC
331 3 ▶END {
Subroutine END: called CC=1 SCAR=0.0
332 3 Fukurama::Class::AttributesHandler->run_check();
333 }
334 3 1;
« Class/Extends.pm ↑ Summary Class/AttributesHandler.pm »