blib/lib/Class/Mixer.pm

filter: Statementstmt 93.2% 179 / 192 Branchbran 75.0% 48 / 64 Conditioncond 54.5% 12 / 22 MC/DCmcdc 23.0% 3 / 13 Subroutinesub 100% 16 / 16 Podpod 0.0% 0 / 6 totaltotal 82.4% 258 / 313 CC 57 SCAR 24.0
CC
57
Cov
86%
mixdown
21.5
remix_class
9.7
import
9.1
CRAP 11.0
↑ Summary
1 package Class::Mixer;
2 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use Class::C3;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use base;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 our $VERSION = '0.54';
6  
7 sub new
8 11 ▶{
Subroutine new: called CC=4 SCAR=14.0
Pod undocumented
9 11 my $class = shift;
10 11 ▶ $class = ref $class if ref $class;
Branch: 1 branch
BranchTrueFalseTotal
if ref $class 0 11 11
11 11 ▶ $Class::Mixer::DEBUG ||= 0;
Condition50%1/2
$Class::Mixer::DEBUG ||= 0
l!l
011
MC/DC0%0/1
$Class::Mixer::DEBUG ||= 0
$Class::Mixer::DEBUG
Truth table50%1/2
A
A= $Class::Mixer::DEBUG
Aresult
11
00
12  
13 11 $class->remix_class;
14  
15 10 my $self = bless {},$class;
16 10 ▶ $self->init(@_) if $self->can('init');
Branch: 1 branch
BranchTrueFalseTotal
if $self->can("init") 7 3 10
17 10 return $self;
18 }
19  
20  
21 # this will remix the class the first time it is instantiated,
22 # after that, the class is considered closed.
23 sub remix_class
24 11 ▶{
Subroutine remix_class: called CC=7 SCAR=22.7
Pod undocumented
25 11 my $self = shift;
26 11 ▶ my $class = ref $self || $self;
Condition33%1/3
ref $self || $self
l!l&&r!l&&!r
0110
MC/DC0%0/2
ref $self || $self
ref $self $self
Truth table33%1/3
A || B
A= ref $selfB= $self
ABresult
1X1
011
000
27 1 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
28 11 ▶ return if ${"$class\::REMIX"};
Branch: 1 branch
BranchTrueFalseTotal
if ${"${class}::REMIX";} 0 11 11
29  
30 11 ${"$class\::REMIX"} = 1;
31 11 @{"$class\::WASA"} = @{"$class\::ISA"};
32  
33 11 ▶ if ($Class::Mixer::DEBUG) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Class::Mixer::DEBUG) 0 11 11
34 0 print "REMIXING $class...\n";
35 0 my @classes = Class::C3::calculateMRO($class);
36 0 print "before: @classes\n";
37 }
38  
39 11 my $con = {}; # constraints
40 11 $self->remix_collect($class,$con);
41  
42 11 ▶ if ($Class::Mixer::DEBUG > 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Class::Mixer::DEBUG > 2) 0 11 11
43 0 require Data::Dumper;
44 0 $Data::Dumper::Sortkeys = 1;
45 0 print Data::Dumper::Dumper($con);
46 }
47  
48 11 $self->mixdown($con);
49 0 ▶ print qq{\@$class\::ISA = @{"$class\::ISA"}\n} if $Class::Mixer::DEBUG > 1;
Branch: 1 branch
BranchTrueFalseTotal
if $Class::Mixer::DEBUG > 1 0 10 10
50  
51 10 Class::C3::reinitialize();
52 10 ▶ if ($Class::Mixer::DEBUG) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Class::Mixer::DEBUG) 0 10 10
53 0 my @classes = Class::C3::calculateMRO($class);
54 0 print "after: @classes\n";
55 }
56 }
57  
58 sub remix_collect
59 203 ▶{
Subroutine remix_collect: called CC=8 SCAR=20.8
Pod undocumented
60 203 my $self = shift;
61 203 ▶ my $class = ref $self || $self;
Condition33%1/3
ref $self || $self
l!l&&r!l&&!r
02030
MC/DC0%0/2
ref $self || $self
ref $self $self
Truth table33%1/3
A || B
A= ref $selfB= $self
ABresult
1X1
011
000
62 1 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
63  
64 203 my $subclass = shift;
65 203 my $con = shift;
66 203 ▶ return if exists $con->{$subclass};
Branch: 1 branch
BranchTrueFalseTotal
if exists $con->{$subclass} 113 90 203
67 90 $con->{$subclass} = {};
68  
69 90 my @ISA = @{"$subclass\::WASA"} ?
70 14 @{"$subclass\::WASA"} :
71 90 ▶ @{"$subclass\::ISA"};
Branch: 1 branch
BranchTrueFalseTotal
@{"${subclass}::WASA";} ? : 14 76 90
72 90 my @mixers = @{"$subclass\::MIXERS"};
73  
74 90 my $type = 'before';
75 90 for my $mixer (@ISA,@mixers) {
76 269 ▶ if ($mixer =~ m/^(before|after|isa|requires?|optional)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mixer =~ /^(before|after|isa|requires?|optional)$/) 77 192 269
77 77 $type = $mixer;
78 77 ▶ $type = 'requires' if $type eq 'require';
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "require" 0 77 77
79 77 next;
80 }
81  
82 192 ▶ $con->{$subclass}->{$type} ||= [];
Condition100%2/2
$con->{$subclass}{$type} ||= []
l!l
87105
MC/DC100%1/1
$con->{$subclass}{$type} ||= []
$con->{$subclass}{$type}
Truth table100%2/2
A
A= $con->{$subclass}{$type}
Aresult
11
00
83 192 push @{$con->{$subclass}->{$type}}, $mixer;
84  
85 192 remix_require($mixer);
86 192 $self->remix_collect($mixer,$con);
87 }
88 }
89  
90  
91 # "borrowed" from base.pm
92 sub remix_require
93 225 ▶{
Subroutine remix_require: called CC=8 SCAR=21.2
Pod undocumented
94 1 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
95 225 my $base = shift;
96  
97 225 my $vglob = ${$base.'::'}{VERSION};
98 225 ▶ if ($vglob && *$vglob{SCALAR}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($vglob and *$vglob{'SCALAR'}) { } 198 27 225
Condition100%2/2
$vglob and *$vglob{"SCALAR"}
!ll
27198
MC/DC100%1/1
$vglob and *$vglob{"SCALAR"}
$vglob
Truth table100%2/2
A
A= $vglob
Aresult
00
11
99 0 ${$base.'::VERSION'} = '-1, set by Class::Mixer'
100 198 ▶ unless defined ${$base.'::VERSION'};
Branch: 1 branch
BranchTrueFalseTotal
unless defined ${$base . '::VERSION';} 0 198 198
101 } else {
102 27 local $SIG{__DIE__};
103 27 eval "require $base";
104 # Only ignore "Can't locate" errors from our eval require.
105 # Other fatal errors (syntax etc) must be reported.
106 27 ▶ die $@ if $@ && $@ !~ /^Can't locate .*? at \(eval /;
Branch: 1 branch
BranchTrueFalseTotal
if $@ and not $@ =~ /^Can't locate .*? at \(eval / 0 27 27
Condition33%1/3
$@ and not $@ =~ /^Can't locate .*? at \(eval /
!ll&&!rl&&r
0270
MC/DC0%0/2
$@ and not $@ =~ /^Can't locate .*? at \(eval /
$@ not $@ =~ /^Can't locate .*? at \(eval /
Truth table33%1/3
A and not(B)
A= $@B= not $@ =~ /^Can't locate .*? at \(eval /
ABresult
0X0
100
111
107 27 ▶ unless (%{"$base\::"}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (%{"${base}::";}) 1 26 27
108 1 require Carp;
109 1 Carp::croak(<<ERROR);
110 Base class package "$base" is empty.
111 (Perhaps you need to 'use' the module which defines that package first.)
112 ERROR
113  
114 }
115 26 ${$base.'::VERSION'} = "-1, set by Class::Mixer"
116 26 ▶ unless defined ${$base.'::VERSION'};
Branch: 1 branch
BranchTrueFalseTotal
unless defined ${$base . '::VERSION';} 26 0 26
117 }
118 }
119  
120  
121 sub mixdown
122 11 ▶{
Subroutine mixdown: called CC=21 SCAR=30.7
Pod undocumented
123 11 my $self = shift;
124 11 ▶ my $class = ref $self || $self;
Condition33%1/3
ref $self || $self
l!l&&r!l&&!r
0110
MC/DC0%0/2
ref $self || $self
ref $self $self
Truth table33%1/3
A || B
A= ref $selfB= $self
ABresult
1X1
011
000
125 11 my $con = shift;
126 1 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
127  
128 11 my @classes = ($class, grep $_ ne $class, keys %$con);
129 11 my @BEA = @{"$class\::ISA"};
130  
131 # isa: when A isa B,
132 # substitute A for all B's
133 11 for my $subclass (@classes) {
134 90 ▶ next unless $con->{$subclass}->{isa};
Branch: 1 branch
BranchTrueFalseTotal
unless $con->{$subclass}{"isa"} 81 9 90
135 9 for my $isa (@{$con->{$subclass}->{isa}}) {
136 9 for my $sub2 (@classes) {
137 106 for my $k (keys %{$con->{$sub2}}) {
138 137 ▶ next if $k eq 'isa';
Branch: 1 branch
BranchTrueFalseTotal
if $k eq "isa" 17 120 137
139 120 for (@{$con->{$sub2}->{$k}}) {
140 233 ▶ $_ = $subclass if $_ eq $isa;
Branch: 1 branch
BranchTrueFalseTotal
if $_ eq $isa 10 223 233
141 }
142 }
143 }
144 }
145 }
146  
147 11 for my $subclass (@classes) {
148 90 ▶ next unless $con->{$subclass}->{optional};
Branch: 1 branch
BranchTrueFalseTotal
unless $con->{$subclass}{"optional"} 87 3 90
149 3 my @opt = @{$con->{$subclass}->{optional}};
150 3 $con->{$subclass}->{optional} = {};
151 3 for my $o (@opt) {
152 3 $con->{$subclass}->{optional}->{$o} = 1;
153 }
154 }
155 # after: A after B means B before A, A is optional
156 11 for my $subclass (@classes) {
157 90 ▶ next unless $con->{$subclass}->{after};
Branch: 1 branch
BranchTrueFalseTotal
unless $con->{$subclass}{"after"} 84 6 90
158 6 for my $mixer (@{$con->{$subclass}->{after}}) {
159 6 ▶ $con->{$mixer}->{before} ||= [];
Condition50%1/2
$con->{$mixer}{"before"} ||= []
l!l
60
MC/DC0%0/1
$con->{$mixer}{"before"} ||= []
$con->{$mixer}{"before"}
Truth table50%1/2
A
A= $con->{$mixer}{"before"}
Aresult
11
00
160 6 push @{$con->{$mixer}->{before}}, $subclass;
161 6 $con->{$mixer}->{optional}->{$subclass} = 1;
162 }
163 }
164 11 ▶ if ($Class::Mixer::DEBUG > 5) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Class::Mixer::DEBUG > 5) 0 11 11
165 0 print "AFTER isa and after substitutions\n";
166 0 print Data::Dumper::Dumper($con);
167 }
168  
169 # make a tree
170 11 for my $subclass (@classes) {
171 $con->{$subclass}->{node} = {
172 90 class=>$subclass,
173 isa=>[],
174 bef=>[],
175 req=>[],
176 };
177 }
178 11 for my $subclass (@classes) {
179 90 push @{$con->{$subclass}->{node}->{req}},
180 11 map { $con->{$_}->{node} }
181 90 @{$con->{$subclass}->{requires}};
182 90 push @{$con->{$subclass}->{node}->{bef}},
183 169 map { $con->{$_}->{node} }
184 90 @{$con->{$subclass}->{before}};
185 # isa should bind tightest
186 90 push @{$con->{$subclass}->{node}->{isa}},
187 9 map { $con->{$_}->{node} }
188 90 @{$con->{$subclass}->{isa}};
189 ;
190 }
191 11 ▶ if ($Class::Mixer::DEBUG > 4) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Class::Mixer::DEBUG > 4) 0 11 11
192 0 print Data::Dumper::Dumper($con->{$class}->{node});
193 }
194  
195 # reverse depth first traversal
196 11 @BEA = depth_first_traverse($con->{$class}->{node});
197 10 shift @BEA; # remove self
198  
199 10 @{"$class\::ISA"} = @BEA;
200 }
201  
202 sub depth_first_traverse
203 198 ▶{
Subroutine depth_first_traverse: called CC=6 SCAR=17.9
Pod undocumented
204 198 my $node = shift;
205 198 ▶ my $stem = shift || '';
Condition100%2/2
shift() || ''
l!l
18711
MC/DC100%1/1
shift() || ''
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
206 198 $stem = $stem.' '.$node->{class}.' ';
207 #print "$stem\n";
208  
209 # check for loops
210 198 for (@{$node->{bef}},@{$node->{isa}}) {
211 256 ▶ if ($stem =~ m/\s$$_{class}\s/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($stem =~ /\s$_->{"class"}\s/) 1 255 256
212 1 die("inconsistent hierarchy ($stem $$_{class})");
213 }
214 }
215  
216 197 ▶ return if $node->{visited};
Branch: 1 branch
BranchTrueFalseTotal
if $node->{"visited"} 108 89 197
217 89 $node->{visited} = 1;
218 89 my @r;
219  
220 89 for (@{$node->{req}}, @{$node->{bef}}, @{$node->{isa}}) {
221 187 unshift @r,depth_first_traverse($_,$stem);
222 }
223 #print $node->{class};
224 #print " ";
225 87 return $node->{class},@r;
226 }
227  
228  
229 # use Class::Mixer automatically adds Class::Mixer to ISA
230 # require all reference classes ala use base
231 # XXX test: do not require optional classes
232 # also force c3 semantics ala use Class::C3
233 sub import
234 27 ▶{
Subroutine import: called CC=9 SCAR=22.1
235 27 my $pkg = shift;
236 27 ▶ return unless $pkg eq 'Class::Mixer'; # not for inheritors
Branch: 1 branch
BranchTrueFalseTotal
unless $pkg eq "Class::Mixer" 0 27 27
237 27 my $class = caller(0);
238  
239 # save off classes -- real work done in new()
240 1 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
241 1 ▶ no warnings 'once';
Subroutine BEGIN: called CC=1 SCAR=0.0
242 27 my @mixers = @{"$class\::MIXERS"} = @_;
243  
244 # require references classes
245 27 my $type = 'before';
246 27 for my $mixer (@mixers) {
247 65 ▶ if ($mixer =~ m/^(before|after|isa|requires?|optional)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mixer =~ /^(before|after|isa|requires?|optional)$/) 31 34 65
248 31 $type = $mixer;
249 31 ▶ $type = 'requires' if $type eq 'require';
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "require" 0 31 31
250 31 next;
251 }
252  
253 34 ▶ remix_require($mixer) unless $type eq 'optional';
Branch: 1 branch
BranchTrueFalseTotal
unless $type eq "optional" 33 1 34
254 }
255  
256 # force Class::Mixer into ISA, so our new() will be invoked
257 26 push @{"$class\::ISA"}, $pkg;
258  
259 # from Class::C3::import
260 26 ▶ if ($class ne 'main') {
Branch: 1 branch
BranchTrueFalseTotal
if ($class ne "main") 25 1 26
261 25 ▶ mro::set_mro($class, 'c3') if $Class::C3::C3_IN_CORE;
Branch: 1 branch
BranchTrueFalseTotal
if $Class::C3::C3_IN_CORE 25 0 25
262 25 ▶ $Class::C3::MRO{$class} = undef unless exists $Class::C3::MRO{$class};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $Class::C3::MRO{$class} 25 0 25
263 }
264 }
265  
266 1 1;
267  
268 __END__
↑ Summary