Phylo/Util/MOP.pm

filter: Statementstmt 64.3% 146 / 227 Branchbran 40.0% 12 / 30 Conditioncond 25.0% 3 / 12 MC/DCmcdc 0.0% 0 / 7 Subroutinesub 67.2% 41 / 61 Podpod 0.0% 0 / 27 totaltotal 55.4% 202 / 364 CC 25 SCAR 16.7
CC
25
Cov
60%
get_methods
42.0
_recurse_isa
6.4
__ANON__
6.0
CRAP 5.3
« Phylo/Unparsers/Mrp.pm ↑ Summary Phylo/Listable.pm »
1 package Bio::Phylo::Util::MOP;
2 57 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 57 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 57 ▶use attributes;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 57 ▶use Attribute::Handlers;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 57 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 57 ▶use Bio::Phylo::Util::Exceptions 'throw';
Subroutine BEGIN: called CC=1 SCAR=0.0
8 57 ▶use Bio::Phylo::Util::Logger ':levels';
Subroutine BEGIN: called CC=1 SCAR=0.0
9 57 ▶use Scalar::Util qw( refaddr );
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 =head1 NAME
12  
13 Bio::Phylo::Util::MOP - Meta-object programming, no serviceable parts inside
14  
15 =cut
16  
17 # this will be populated when the attribute handlers are triggered
18 57 my %methods;
19  
20 # this will progressively store/memoize all superclasses for given classes
21 my %classes;
22  
23 # this will progressively store/memoize the methods for given classes
24 57 my %class_methods;
25  
26 # this might be used to check the interface of alien subclasses
27 235 ▶sub import {
Subroutine import: called CC=1 SCAR=0.0
28
29 }
30  
31 # my %sims = $mop->get_symtable('Bio::Phylo');
32 0 ▶sub get_symtable {
Subroutine get_symtable: not called CC=1 SCAR=1.2
Pod undocumented
33 0 my ( $self, $package ) = @_;
34 0 my %symtable;
35 {
36 57 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
37 0 %symtable = %{"${package}::"};
38 57 ▶ use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
39 }
40 0 return \%symtable;
41 }
42  
43 # $mop->get_method('Bio::Phylo::new')->()
44 1852 ▶sub get_method {
Subroutine get_method: called CC=1 SCAR=0.0
Pod undocumented
45 1852 my ( $self, $fqn ) = @_;
46 1852 my $coderef;
47 eval {
48 57 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
49 1852 $coderef = \&{"${fqn}"};
50 57 ▶ use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
51 1852 };
52 1852 return $coderef;
53 }
54  
55 # @methods = @{ $mop->get_implementations( 'new', $obj || $package ) };
56 0 ▶sub get_implementations {
Subroutine get_implementations: not called CC=1 SCAR=6.9
Pod undocumented
57 0 my ( $self, $method, $obj ) = @_;
58 0 my @methods = grep { $_->{'name'} eq $method } @{ $self->get_methods($obj) };
59 0 return \@methods;
60 }
61  
62 # my @classes = @{ $mop->get_classes($obj) }
63 13250 ▶sub get_classes {
Subroutine get_classes: called CC=3 SCAR=11.1
Pod undocumented
64 13250 my ( $self, $obj, $all ) = @_;
65 13250 ▶ my $class = ref $obj || $obj;
Condition33%1/3
ref $obj || $obj
l!l&&r!l&&!r
1325000
MC/DC0%0/2
ref $obj || $obj
ref $obj $obj
Truth table33%1/3
A || B
A= ref $objB= $obj
ABresult
1X1
011
000
66
67 # return if already cached
68 13250 ▶ if ( $classes{$class} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($classes{$class}) { } 13027 223 13250
69 13027 return $classes{$class};
70 }
71
72 # compute, cache, return
73 else {
74 223 my ( $seen, $isa ) = ( {}, [] );
75 223 _recurse_isa($class, $isa, $seen, $all);
76 223 $classes{$class} = $isa;
77 223 return $isa;
78 }
79 }
80  
81 # starting from $class, push all superclasses (+$class) into @$isa,
82 # %$seen is just a helper to avoid getting stuck in cycles
83 1752 ▶sub _recurse_isa {
Subroutine _recurse_isa: called CC=6 SCAR=18.6
84 1752 my ( $class, $isa, $seen, $all ) = @_;
85 1752 ▶ if ( not $seen->{$class} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $seen->{$class}) 1752 0 1752
86 1752 $seen->{$class} = 1;
87 1752 ▶ if ( ( $class ne 'Exporter' and $class ne 'DynaLoader' ) or $all ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($class ne "Exporter" and $class ne "DynaLoader" or $all) 1752 0 1752
Condition33%2/6
$class ne "Exporter" and $class ne "DynaLoader" or $all
l!l&&r!l&&!r
175200
$class ne "Exporter" and $class ne "DynaLoader"
!ll&&!rl&&r
001752
MC/DC0%0/3
$class ne "Exporter" and $class ne "DynaLoader" or $all
$class ne "Exporter" $class ne "DynaLoader" $all
Truth table20%1/5
A and B or C
A= $class ne "Exporter"B= $class ne "DynaLoader"C= $all
ABCresult
11X1
0X11
1011
0X00
1000
88 1752 push @{$isa}, $class;
89 }
90 1752 my @isa;
91 {
92 57 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
93 1752 @isa = @{"${class}::ISA"};
94 57 ▶ use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
95 }
96 1752 _recurse_isa( $_, $isa, $seen, $all ) for @isa;
97 }
98 }
99  
100 # my @methods = @{ $mop->get_methods($obj) };
101 0 ▶sub get_methods {
Subroutine get_methods: not called CC=6 SCAR=37.4
Pod undocumented
102 0 my ( $self, $obj ) = @_;
103 0 ▶ my $class = ref $obj || $obj;
Condition0%0/3
ref $obj || $obj
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $obj || $obj
ref $obj $obj
Truth table0%0/3
A || B
A= ref $objB= $obj
ABresult
1X1
011
000
104
105 # return if already cached
106 0 ▶ if ( $class_methods{$class} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($class_methods{$class}) { } 0 0 0
107 0 return $class_methods{$class};
108 }
109
110 # compute, cache, return
111 else {
112 0 my $isa = $self->get_classes($obj);
113 0 my @methods;
114 0 for my $package ( @{ $isa } ) {
115  
116 0 my %symtable = %{ $self->get_symtable($package) };
117
118 # at this point we have lots of things, we just want methods
119 0 for my $entry ( keys %symtable ) {
120
121 # check if entry is a CODE reference
122 0 my $can = $package->can( $entry );
123 0 ▶ if ( ref $can eq 'CODE' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $can eq "CODE") 0 0 0
124 push @methods, {
125 'package' => $package,
126 'name' => $entry,
127 0 'glob' => $symtable{$entry},
128 'code' => $can,
129 };
130 }
131 }
132 }
133 0 $class_methods{$class} = \@methods;
134 0 return \@methods;
135 }
136 }
137  
138 326 ▶sub get_methods_by_attribute {
Subroutine get_methods_by_attribute: called CC=4 SCAR=13.9
Pod undocumented
139 326 my ( $self, $obj, $attribute ) = @_;
140 326 my $isa = $self->get_classes($obj);
141 326 my $methods = $methods{$attribute};
142 326 my @return;
143 326 for my $class ( @{ $isa } ) {
144 2438 ▶ if ( $methods->{$class} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($methods->{$class}) 807 1631 2438
145 807 for my $key ( keys %{ $methods->{$class} } ) {
146 push @return, {
147 'package' => $class,
148 'name' => $key,
149 2205 'code' => $methods->{$class}->{$key}
150 };
151 }
152 }
153 }
154 326 return \@return;
155 }
156  
157 0 ▶sub get_accessors {
Subroutine get_accessors: not called CC=1 SCAR=6.9
Pod undocumented
158 0 my ( $self, $obj ) = @_;
159 0 return $self->get_methods_by_attribute($obj,'Accessor');
160 }
161  
162 0 ▶sub get_mutators {
Subroutine get_mutators: not called CC=1 SCAR=6.9
Pod undocumented
163 0 my ( $self, $obj ) = @_;
164 0 return $self->get_methods_by_attribute($obj,'Mutator');
165 }
166  
167 0 ▶sub get_abstracts {
Subroutine get_abstracts: not called CC=1 SCAR=6.9
Pod undocumented
168 0 my ( $self, $obj ) = @_;
169 0 return $self->get_methods_by_attribute($obj,'Abstract');
170 }
171  
172 109 ▶sub get_constructors {
Subroutine get_constructors: called CC=1 SCAR=0.0
Pod undocumented
173 109 my ( $self, $obj ) = @_;
174 109 return $self->get_methods_by_attribute($obj,'Constructor');
175 }
176  
177 109 ▶sub get_clonables {
Subroutine get_clonables: called CC=1 SCAR=0.0
Pod undocumented
178 109 my ( $self, $obj ) = @_;
179 109 return $self->get_methods_by_attribute($obj,'Clonable');
180 }
181  
182 108 ▶sub get_deep_clonables {
Subroutine get_deep_clonables: called CC=1 SCAR=0.0
Pod undocumented
183 108 my ( $self, $obj ) = @_;
184 108 return $self->get_methods_by_attribute($obj,'DeepClonable');
185 }
186  
187 0 ▶sub get_destructors {
Subroutine get_destructors: not called CC=1 SCAR=6.9
Pod undocumented
188 0 my ( $self, $obj ) = @_;
189 0 return $self->get_methods_by_attribute($obj,'Destructor');
190 }
191  
192 0 ▶sub get_privates {
Subroutine get_privates: not called CC=1 SCAR=6.9
Pod undocumented
193 0 my ( $self, $obj ) = @_;
194 0 return $self->get_methods_by_attribute($obj,'Private');
195 }
196  
197 0 ▶sub get_statics {
Subroutine get_statics: not called CC=1 SCAR=6.9
Pod undocumented
198 0 my ( $self, $obj ) = @_;
199 0 return $self->get_methods_by_attribute($obj,'Static');
200 }
201  
202 0 ▶sub get_serializers {
Subroutine get_serializers: not called CC=1 SCAR=6.9
Pod undocumented
203 0 my ( $self, $obj ) = @_;
204 0 return $self->get_methods_by_attribute($obj,'Serializer');
205 }
206  
207 1886 ▶sub _handler {
Subroutine _handler: called CC=5 SCAR=16.3
208 1886 eval {
209 1886 my ($package, $symbol, $referent, $attr, $data) = @_;
210 1886 ▶ return if $symbol eq 'ANON';
Branch: 1 branch
BranchTrueFalseTotal
if $symbol eq "ANON" 0 1886 1886
211 1886 my $name = *$symbol;
212 1886 $name =~ s/.*://;
213 1886 ▶ $methods{$attr} = {} unless $methods{$attr};
Branch: 1 branch
BranchTrueFalseTotal
unless $methods{$attr} 218 1668 1886
214 1886 ▶ $methods{$attr}->{$package} = {} unless $methods{$attr}->{$package};
Branch: 1 branch
BranchTrueFalseTotal
unless $methods{$attr}{$package} 970 916 1886
215 1886 $methods{$attr}->{$package}->{$name} = $referent;
216 };
217 1886 ▶ if ( $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 1886 1886
218 0 throw 'API' => $@;
219 }
220 }
221  
222 0 ▶sub UNIVERSAL::Accessor : ATTR(CODE) {
Subroutine Accessor: not called CC=1 SCAR=6.9
Pod undocumented
223 0 my ($package, $symbol, $referent, $attr, $data) = @_;
224 0 _handler(@_);
225 57 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
226  
227 0 ▶sub UNIVERSAL::Private : ATTR(CODE) {
Subroutine Private: not called CC=2 SCAR=14.6
Pod undocumented
228 0 my ($package, $symbol, $referent, $attr, $data) = @_;
229 57 ▶ no warnings 'redefine';
Subroutine BEGIN: called CC=1 SCAR=0.0
230 0 ▶ return if $symbol eq 'ANON';
Branch: 1 branch
BranchTrueFalseTotal
if $symbol eq "ANON" 0 0 0
231 0 ▶ *$symbol = sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
232 0 my ($calling_package) = caller;
233 0 my $symname = *$symbol;
234 0 $symname =~ s/^\*//;
235 0 $symname =~ s/::[^:]+$//;
236 0 ▶ if ( $symname ne $package ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($symname ne $package) 0 0 0
237 0 throw 'API' => "Attempt to call Private method from outside package";
238 }
239 0 $referent->(@_);
240 0 };
241 0 _handler(@_);
242 57 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
243  
244 0 ▶sub UNIVERSAL::Protected : ATTR(CODE) {
Subroutine Protected: not called CC=2 SCAR=15.2
Pod undocumented
245 0 my ($package, $symbol, $referent, $attr, $data) = @_;
246 57 ▶ no warnings 'redefine';
Subroutine BEGIN: called CC=1 SCAR=0.0
247 0 ▶ return if $symbol eq 'ANON';
Branch: 1 branch
BranchTrueFalseTotal
if $symbol eq "ANON" 0 0 0
248 0 ▶ *$symbol = sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
249 0 my ($calling_package) = caller;
250 0 my $symname = *$symbol;
251 0 my $method = $symname;
252 0 $symname =~ s/^\*//;
253 0 $symname =~ s/::[^:]+$//;
254 0 my @package_names = split /::/, $package;
255 0 my @calling_names = split /::/, $calling_package;
256 0 my $seen_class = $package_names[0] eq $calling_names[0];
257 0 ▶ if ( not $seen_class ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($seen_class) 0 0 0
258 0 throw 'API' => "Attempt to call Protected method $method from outside of top-level namespace";
259 }
260 0 $referent->(@_);
261 0 };
262 0 _handler(@_);
263 57 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
264  
265 180 ▶sub UNIVERSAL::Constructor : ATTR(CODE) {
Subroutine Constructor: called CC=1 SCAR=0.0
Pod undocumented
266 180 my ($package, $symbol, $referent, $attr, $data) = @_;
267 180 _handler(@_);
268 57 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
269  
270 272 ▶sub UNIVERSAL::Destructor : ATTR(CODE) {
Subroutine Destructor: called CC=1 SCAR=0.0
Pod undocumented
271 272 my ($package, $symbol, $referent, $attr, $data) = @_;
272 272 _handler(@_);
273 57 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
274  
275 0 ▶sub UNIVERSAL::Static : ATTR(CODE) {
Subroutine Static: not called CC=1 SCAR=6.9
Pod undocumented
276 0 my ($package, $symbol, $referent, $attr, $data) = @_;
277 0 _handler(@_);
278 57 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
279  
280 0 ▶sub UNIVERSAL::Mutator : ATTR(CODE) {
Subroutine Mutator: not called CC=1 SCAR=6.9
Pod undocumented
281 0 my ($package, $symbol, $referent, $attr, $data) = @_;
282 0 _handler(@_);
283 57 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
284  
285 0 ▶sub UNIVERSAL::Abstract : ATTR(CODE) {
Subroutine Abstract: not called CC=2 SCAR=12.2
Pod undocumented
286 0 my ($package, $symbol, $referent, $attr, $data) = @_;
287 0 _handler(@_);
288 0 ▶ return if $symbol eq 'ANON';
Branch: 1 branch
BranchTrueFalseTotal
if $symbol eq "ANON" 0 0 0
289 57 ▶ no warnings 'redefine';
Subroutine BEGIN: called CC=1 SCAR=0.0
290 0 ▶ *$symbol = sub { throw 'NotImplemented' => "Abstract method, can't call $symbol" };
Subroutine __ANON__: not called CC=1 SCAR=6.9
291 57 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
292  
293 1316 ▶sub UNIVERSAL::Clonable : ATTR(CODE) {
Subroutine Clonable: called CC=1 SCAR=0.0
Pod undocumented
294 1316 my ($package, $symbol, $referent, $attr, $data) = @_;
295 1316 _handler(@_);
296 57 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
297  
298 118 ▶sub UNIVERSAL::DeepClonable : ATTR(CODE) {
Subroutine DeepClonable: called CC=1 SCAR=0.0
Pod undocumented
299 118 my ($package, $symbol, $referent, $attr, $data) = @_;
300 118 _handler(@_);
301 57 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
302  
303 0 ▶sub UNIVERSAL::Serializer : ATTR(CODE) {
Subroutine Serializer: not called CC=1 SCAR=6.9
Pod undocumented
304 0 my ($package, $symbol, $referent, $attr, $data) = @_;
305 0 _handler(@_);
306 57 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
307  
308 57 1;
309  
310  
« Phylo/Unparsers/Mrp.pm ↑ Summary Phylo/Listable.pm »