blib/lib/Moose/Meta/Role/Application/RoleSummation.pm

filter: Statementstmt 96.1% 173 / 180 Branchbran 83.3% 45 / 54 Conditioncond 75.0% 9 / 12 MC/DCmcdc 62.5% 5 / 8 Subroutinesub 92.0% 23 / 25 Podpod 100% 13 / 13 totaltotal 91.7% 268 / 292 CC 55 SCAR 18.9
CC
55
Cov
92%
apply_overloading
14.1
is_method_excluded
12.0
apply_methods
7.0
CRAP 6.6
« blib/lib/Moose.pm ↑ Summary blib/lib/metaclass.pm »
1 package Moose::Meta::Role::Application::RoleSummation;
2 385 our $VERSION = '2.4001';
3  
4 385 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 385 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 385 ▶use metaclass;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 385 ▶use List::Util 1.33 qw( all );
Subroutine BEGIN: called CC=1 SCAR=0.0
9 385 ▶use Scalar::Util 'blessed';
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 385 ▶use Moose::Meta::Role::Composite;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 385 ▶use parent 'Moose::Meta::Role::Application';
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 385 ▶use Moose::Util 'throw_exception';
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 __PACKAGE__->meta->add_attribute('role_params' => (
18 reader => 'role_params',
19 24 ▶ default => sub { {} },
Subroutine __ANON__: called CC=1 SCAR=0.0
20 385 Class::MOP::_definition_context(),
21 ));
22  
23 535 ▶sub get_exclusions_for_role {
Subroutine get_exclusions_for_role: called CC=6 SCAR=18.0
Pod documented
24 535 my ($self, $role) = @_;
25 535 ▶ $role = $role->name if blessed $role;
Branch: 1 branch
BranchTrueFalseTotal
if builtin::blessed($role) 535 0 535
26 535 ▶ my $excludes_key = exists $self->role_params->{$role}->{'-excludes'} ?
Branch: 1 branch
BranchTrueFalseTotal
exists $self->role_params->{$role}{'-excludes'} ? : 21 514 535
27 '-excludes' : 'excludes';
28 535 ▶ if ($self->role_params->{$role} && defined $self->role_params->{$role}->{$excludes_key}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->role_params->{$role} and defined $self->role_params->{$role}{$excludes_key}) 21 514 535
Condition66%2/3
$self->role_params->{$role} and defined $self->role_params->{$role}{$excludes_key}
!ll&&!rl&&r
051421
MC/DC50%1/2
$self->role_params->{$role} and defined $self->role_params->{$role}{$excludes_key}
$self->role_params->{$role} defined $self->role_params->{$role}{$excludes_key}
Truth table66%2/3
A and B
A= $self->role_params->{$role}B= defined $self->role_params->{$role}{$excludes_key}
ABresult
0X0
100
111
29 21 ▶ if (ref $self->role_params->{$role}->{$excludes_key} eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self->role_params->{$role}{$excludes_key} eq "ARRAY") 6 15 21
30 6 return $self->role_params->{$role}->{$excludes_key};
31 }
32 15 return [ $self->role_params->{$role}->{$excludes_key} ];
33 }
34 514 return [];
35 }
36  
37 842 ▶sub get_method_aliases_for_role {
Subroutine get_method_aliases_for_role: called CC=5 SCAR=16.1
Pod documented
38 842 my ($self, $role) = @_;
39 842 ▶ $role = $role->name if blessed $role;
Branch: 1 branch
BranchTrueFalseTotal
if builtin::blessed($role) 535 307 842
40 842 ▶ my $alias_key = exists $self->role_params->{$role}->{'-alias'} ?
Branch: 1 branch
BranchTrueFalseTotal
exists $self->role_params->{$role}{'-alias'} ? : 11 831 842
41 '-alias' : 'alias';
42 842 ▶ if ($self->role_params->{$role} && defined $self->role_params->{$role}->{$alias_key}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->role_params->{$role} and defined $self->role_params->{$role}{$alias_key}) 11 831 842
Condition66%2/3
$self->role_params->{$role} and defined $self->role_params->{$role}{$alias_key}
!ll&&!rl&&r
083111
MC/DC50%1/2
$self->role_params->{$role} and defined $self->role_params->{$role}{$alias_key}
$self->role_params->{$role} defined $self->role_params->{$role}{$alias_key}
Truth table66%2/3
A and B
A= $self->role_params->{$role}B= defined $self->role_params->{$role}{$alias_key}
ABresult
0X0
100
111
43 11 return $self->role_params->{$role}->{$alias_key};
44 }
45 831 return {};
46 }
47  
48 0 ▶sub is_method_excluded {
Subroutine is_method_excluded: not called CC=3 SCAR=24.8
Pod documented
49 0 my ($self, $role, $method_name) = @_;
50 0 foreach ($self->get_exclusions_for_role($role->name)) {
51 0 ▶ return 1 if $_ eq $method_name;
Branch: 1 branch
BranchTrueFalseTotal
if $_ eq $method_name 0 0 0
52 }
53 0 return 0;
54 }
55  
56 0 ▶sub is_method_aliased {
Subroutine is_method_aliased: not called CC=2 SCAR=17.9
Pod documented
57 0 my ($self, $role, $method_name) = @_;
58 0 ▶ exists $self->get_method_aliases_for_role($role->name)->{$method_name} ? 1 : 0
Branch: 1 branch
BranchTrueFalseTotal
exists $self->get_method_aliases_for_role($role->name)->{$method_name} ? : 0 0 0
59 }
60  
61 307 ▶sub is_aliased_method {
Subroutine is_aliased_method: called CC=2 SCAR=7.0
Pod documented
62 307 my ($self, $role, $method_name) = @_;
63 307 my %aliased_names = reverse %{$self->get_method_aliases_for_role($role->name)};
64 307 ▶ exists $aliased_names{$method_name} ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
exists $aliased_names{$method_name} ? : 0 307 307
65 }
66  
67 255 ▶sub check_role_exclusions {
Subroutine check_role_exclusions: called CC=6 SCAR=17.9
Pod documented
68 255 my ($self, $c) = @_;
69  
70 255 my %excluded_roles;
71 255 for my $role (@{ $c->get_roles }) {
72 577 my $name = $role->name;
73  
74 577 for my $excluded ($role->get_excluded_roles_list) {
75 8 push @{ $excluded_roles{$excluded} }, $name;
76 }
77 }
78  
79 255 foreach my $role (@{$c->get_roles}) {
80 573 foreach my $excluded (keys %excluded_roles) {
81 11 ▶ next unless $role->does_role($excluded);
Branch: 1 branch
BranchTrueFalseTotal
unless $role->does_role($excluded) 5 6 11
82  
83 6 my @excluding = @{ $excluded_roles{$excluded} };
84  
85 6 throw_exception( RoleExclusionConflict => roles => \@excluding,
86 role_name => $excluded
87 );
88 }
89 }
90  
91 249 $c->add_excluded_roles(keys %excluded_roles);
92 }
93  
94 249 ▶sub check_required_methods {
Subroutine check_required_methods: called CC=5 SCAR=16.1
Pod documented
95 249 my ($self, $c) = @_;
96  
97 my %all_required_methods =
98 149 map { $_->name => $_ }
99 564 map { $_->get_required_method_list }
100 249 @{$c->get_roles};
101  
102 249 foreach my $role (@{$c->get_roles}) {
103 564 foreach my $required (keys %all_required_methods) {
104  
105 336 ▶ delete $all_required_methods{$required}
Branch: 1 branch
BranchTrueFalseTotal
if $role->has_method($required) or $self->is_aliased_method($role, $required) 29 307 336
Condition66%2/3
$role->has_method($required) or $self->is_aliased_method($role, $required)
l!l&&r!l&&!r
290307
MC/DC50%1/2
$role->has_method($required) or $self->is_aliased_method($role, $required)
$role->has_method($required) $self->is_aliased_method($role, $required)
Truth table66%2/3
A or B
A= $role->has_method($required)B= $self->is_aliased_method($role, $required)
ABresult
1X1
011
000
106 if $role->has_method($required)
107 || $self->is_aliased_method($role, $required);
108 }
109 }
110  
111 249 $c->add_required_methods(values %all_required_methods);
112 }
113  
114 249 ▶sub check_required_attributes {
Subroutine check_required_attributes: called CC=1 SCAR=0.0
Pod documented
115  
116 }
117  
118 243 ▶sub apply_attributes {
Subroutine apply_attributes: called CC=6 SCAR=17.9
Pod documented
119 243 my ($self, $c) = @_;
120  
121 243 my @all_attributes;
122  
123 243 for my $role ( @{ $c->get_roles } ) {
124 push @all_attributes,
125 549 map { $role->get_attribute($_) } $role->get_attribute_list;
126 }
127  
128 243 my %seen;
129 243 foreach my $attr (@all_attributes) {
130 220 my $name = $attr->name;
131  
132 220 ▶ if ( exists $seen{$name} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $seen{$name}) 8 212 220
133 8 ▶ next if $seen{$name}->is_same_as($attr);
Branch: 1 branch
BranchTrueFalseTotal
if $seen{$name}->is_same_as($attr) 2 6 8
134  
135 6 my $role1 = $seen{$name}->associated_role->name;
136 6 my $role2 = $attr->associated_role->name;
137  
138 6 throw_exception( AttributeConflictInSummation => attribute_name => $name,
139 role_name => $role1,
140 second_role_name => $role2,
141 );
142 }
143  
144 212 $seen{$name} = $attr;
145 }
146  
147 237 foreach my $attr (@all_attributes) {
148 207 $c->add_attribute( $attr->clone );
149 }
150 }
151  
152 237 ▶sub apply_methods {
Subroutine apply_methods: called CC=7 SCAR=19.5
Pod documented
153 237 my ($self, $c) = @_;
154  
155 my @all_methods = map {
156 535 my $role = $_;
157 535 my $aliases = $self->get_method_aliases_for_role($role);
158 535 my %excludes = map { $_ => undef } @{ $self->get_exclusions_for_role($role) };
159 (
160 (map {
161 1921 ▶ exists $excludes{$_} ? () :
Branch: 1 branch
BranchTrueFalseTotal
exists $excludes{$_} ? : 29 1892 1921
162 +{
163 role => $role,
164 name => $_,
165 method => $role->get_method($_),
166 }
167 1921 } map { $_->name }
168 2454 grep { !$_->isa('Class::MOP::Method::Meta') }
169 $role->_get_local_methods),
170 (map {
171 535 +{
172 role => $role,
173 12 name => $aliases->{$_},
174 method => $role->get_method($_),
175 }
176 } keys %$aliases)
177 );
178 237 } @{$c->get_roles};
179  
180 237 my (%seen, %conflicts, %method_map);
181 237 foreach my $method (@all_methods) {
182 1904 ▶ next if $conflicts{$method->{name}};
Branch: 1 branch
BranchTrueFalseTotal
if $conflicts{$method->{"name"}} 3 1901 1904
183 1901 my $seen = $seen{$method->{name}};
184  
185 1901 ▶ if ($seen) {
Branch: 1 branch
BranchTrueFalseTotal
if ($seen) 265 1636 1901
186 265 ▶ if ($seen->{method}->body != $method->{method}->body) {
Branch: 1 branch
BranchTrueFalseTotal
if ($seen->{"method"}->body != $method->{"method"}->body) 135 130 265
187 $c->add_conflicting_method(
188 name => $method->{name},
189 135 roles => [$method->{role}->name, $seen->{role}->name],
190 );
191  
192 135 delete $method_map{$method->{name}};
193 135 $conflicts{$method->{name}} = 1;
194 135 next;
195 }
196 }
197  
198 1766 $seen{$method->{name}} = $method;
199 1766 $method_map{$method->{name}} = $method->{method};
200 }
201  
202 237 $c->add_method($_ => $method_map{$_}) for keys %method_map;
203 }
204  
205 237 ▶sub apply_override_method_modifiers {
Subroutine apply_override_method_modifiers: called CC=6 SCAR=17.9
Pod documented
206 237 my ($self, $c) = @_;
207  
208 my @all_overrides = map {
209 535 my $role = $_;
210 map {
211 535 +{
212 255 name => $_,
213 method => $role->get_override_method_modifier($_),
214 }
215 } $role->get_method_modifier_list('override');
216 237 } @{$c->get_roles};
217  
218 237 my %seen;
219 237 foreach my $override (@all_overrides) {
220 254 my @role_names = map { $_->name } @{$c->get_roles};
221 254 ▶ if ( $c->has_method($override->{name}) ){
Branch: 1 branch
BranchTrueFalseTotal
if ($c->has_method($override->{"name"})) 3 251 254
222 throw_exception( OverrideConflictInSummation => role_names => \@role_names,
223 role_application => $self,
224 method_name => $override->{name}
225 3 );
226 }
227 251 ▶ if (exists $seen{$override->{name}}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $seen{$override->{"name"}}) 47 204 251
228 47 ▶ if ( $seen{$override->{name}} != $override->{method} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($seen{$override->{"name"}} != $override->{"method"}) 4 43 47
229 throw_exception( OverrideConflictInSummation => role_names => \@role_names,
230 role_application => $self,
231 method_name => $override->{name},
232 4 two_overrides_found => 1
233 );
234 }
235 }
236 247 $seen{$override->{name}} = $override->{method};
237 }
238  
239 $c->add_override_method_modifier(
240 $_->{name}, $_->{method}
241 230 ) for @all_overrides;
242  
243 }
244  
245 690 ▶sub apply_method_modifiers {
Subroutine apply_method_modifiers: called CC=4 SCAR=13.9
Pod documented
246 690 my ($self, $modifier_type, $c) = @_;
247 690 my $add = "add_${modifier_type}_method_modifier";
248 690 my $get = "get_${modifier_type}_method_modifiers";
249 690 foreach my $role (@{$c->get_roles}) {
250 1557 foreach my $method_name ($role->get_method_modifier_list($modifier_type)) {
251 $c->$add(
252 $method_name,
253 $_
254 236 ) foreach $role->$get($method_name);
255 }
256 }
257 }
258  
259 249 ▶sub apply_overloading {
Subroutine apply_overloading: called CC=14 SCAR=26.5
Pod documented
260 249 my ( $self, $c ) = @_;
261  
262 249 my @overloaded_roles = grep { $_->is_overloaded } @{ $c->get_roles };
263 249 ▶ return unless @overloaded_roles;
Branch: 1 branch
BranchTrueFalseTotal
unless @overloaded_roles 239 10 249
264  
265 10 my %fallback;
266 10 for my $role (@overloaded_roles) {
267 17 $fallback{ $role->name } = $role->get_overload_fallback_value;
268 }
269  
270 10 for my $role_name ( keys %fallback ) {
271 15 for my $other_role_name ( grep { $_ ne $role_name } keys %fallback ) {
272 12 my @fb_values = @fallback{ $role_name, $other_role_name };
273 12 ▶ if ( all {defined} @fb_values ) {
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
if (&all(sub { defined $_; } , @fb_values)) 2 10 12
274 2 ▶ next if $fallback{$role_name} eq $fallback{$other_role_name};
Branch: 1 branch
BranchTrueFalseTotal
if $fallback{$role_name} eq $fallback{$other_role_name} 0 2 2
275 2 throw_exception(
276 'OverloadConflictInSummation',
277 role_names => [ $role_name, $other_role_name ],
278 role_application => $self,
279 overloaded_op => 'fallback',
280 );
281 }
282  
283 10 ▶ next if all { !defined } @fb_values;
Subroutine __ANON__: called CC=1 SCAR=0.2
Branch: 1 branch
BranchTrueFalseTotal
if &all(sub { not defined $_; } , @fb_values) 10 0 10
284 0 throw_exception(
285 'OverloadConflictInSummation',
286 role_names => [ $role_name, $other_role_name ],
287 role_application => $self,
288 overloaded_op => 'fallback',
289 );
290 }
291 }
292  
293 8 ▶ if ( keys %fallback ) {
Branch: 1 branch
BranchTrueFalseTotal
if (keys %fallback) 8 0 8
294 8 $c->set_overload_fallback_value( ( values %fallback )[0] );
295 }
296  
297 8 my %overload_map;
298 8 for my $role (@overloaded_roles) {
299 13 for my $overload ( $role->get_all_overloaded_operators ) {
300 13 $overload_map{ $overload->operator }{ $role->name } = $overload;
301 }
302 }
303  
304 8 for my $op_name ( keys %overload_map ) {
305 8 my @roles = keys %{ $overload_map{$op_name} };
306 8 my $overload = $overload_map{$op_name}{ $roles[0] };
307  
308 8 ▶ if ( @roles > 1 && !all { $overload->_is_equal_to($_) }
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
if (@roles > 1 and not &all(sub { $overload->_is_equal_to($_); } , values %{$overload_map{$op_name};})) 4 4 8
Condition100%3/3
@roles > 1 and not &all(sub { $overload->_is_equal_to($_); } , values %{$overload_map{$op_name};})
!ll&&!rl&&r
314
MC/DC100%2/2
@roles > 1 and not &all(sub { $overload->_is_equal_to($_); } , values %{$overload_map{$op_name};})
@roles > 1 not &all(sub { $overload->_is_equal_to($_); } , values %{$overload_map{$op_name};})
Truth table100%3/3
A and not(B)
A= @roles > 1B= not &all(sub { $overload->_is_equal_to($_); } , values %{$overload_map{$op_name};})
ABresult
0X0
100
111
309 5 values %{ $overload_map{$op_name} } ) {
310  
311 4 throw_exception(
312 'OverloadConflictInSummation',
313 role_names => [ @roles[ 0, 1 ] ],
314 role_application => $self,
315 overloaded_op => $op_name,
316 );
317 }
318  
319 $c->add_overloaded_operator(
320 4 $op_name => $overload_map{$op_name}{ $roles[0] } );
321 }
322 }
323  
324 385 1;
325  
326 # ABSTRACT: Combine two or more roles
327  
328 __END__
« blib/lib/Moose.pm ↑ Summary blib/lib/metaclass.pm »