blib/lib/Moose/Util.pm

filter: Statementstmt 93.3% 224 / 240 Branchbran 83.9% 94 / 112 Conditioncond 59.5% 28 / 47 MC/DCmcdc 36.0% 9 / 25 Subroutinesub 92.4% 49 / 53 Podpod 93.7% 15 / 16 totaltotal 84.9% 419 / 493 CC 83 SCAR 21.7
CC
83
Cov
87%
_is_role_only_subclass
31.9
_apply_all_roles
13.0
_role_differences
12.7
CRAP 8.8
« blib/lib/Moose/Meta/Attribute.pm ↑ Summary blib/lib/Class/MOP/Method/Inlined.pm »
1 package Moose::Util;
2 402 our $VERSION = '2.4001';
3  
4 402 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 402 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 402 ▶use Module::Runtime 0.014 'use_package_optimistically', 'module_notional_filename';
Subroutine BEGIN: called CC=1 SCAR=0.0
8 402 ▶use Data::OptList;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 402 ▶use Sub::Exporter;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 402 ▶use Scalar::Util 'blessed';
Subroutine BEGIN: called CC=1 SCAR=0.0
11 402 ▶use List::Util 1.33 qw(first any all);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 402 ▶use overload ();
Subroutine BEGIN: called CC=1 SCAR=0.0
13 402 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15  
16 402 my @exports = qw[
17 find_meta
18 is_role
19 does_role
20 search_class_by_role
21 ensure_all_roles
22 apply_all_roles
23 with_traits
24 get_all_init_args
25 get_all_attribute_values
26 resolve_metatrait_alias
27 resolve_metaclass_alias
28 add_method_modifier
29 english_list
30 meta_attribute_alias
31 meta_class_alias
32 throw_exception
33 ];
34  
35 402 Sub::Exporter::setup_exporter({
36 exports => \@exports,
37 groups => { all => \@exports }
38 });
39  
40 # Things that need to ->import from Moose::Util
41 # should be loaded after Moose::Util defines ->import
42 402 require Class::MOP;
43  
44 1327 ▶sub throw_exception {
Subroutine throw_exception: called CC=1 SCAR=0.0
Pod documented
45 1327 my ($class_name, @args_to_exception) = @_;
46 1327 my $class = "Moose::Exception::$class_name";
47 1327 _load_user_class( $class );
48 1327 die $class->new( @args_to_exception );
49 }
50  
51 ## some utils for the utils ...
52  
53 2314 ▶sub find_meta { Class::MOP::class_of(@_) }
Subroutine find_meta: called CC=1 SCAR=0.0
Pod documented
54  
55 ## the functions ...
56  
57 7 ▶sub is_role {
Subroutine is_role: called CC=2 SCAR=6.9
Pod documented
58 7 my $package_or_obj = shift;
59  
60 7 my $meta = find_meta($package_or_obj);
61 7 ▶ return if not $meta;
Branch: 1 branch
BranchTrueFalseTotal
unless $meta 2 5 7
62 5 return $meta->isa('Moose::Meta::Role');
63 }
64  
65 123 ▶sub does_role {
Subroutine does_role: called CC=5 SCAR=16.1
Pod documented
66 123 my ($class_or_obj, $role) = @_;
67  
68 123 ▶ if (try { $class_or_obj->isa('Moose::Object') }) {
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
if (&try(sub { $class_or_obj->isa("Moose::Object"); } )) 49 74 123
69 49 return $class_or_obj->does($role);
70 }
71  
72 74 my $meta = find_meta($class_or_obj);
73  
74 74 ▶ return unless defined $meta;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $meta 3 71 74
75 71 ▶ return unless $meta->can('does_role');
Branch: 1 branch
BranchTrueFalseTotal
unless $meta->can("does_role") 12 59 71
76 59 ▶ return 1 if $meta->does_role($role);
Branch: 1 branch
BranchTrueFalseTotal
if $meta->does_role($role) 53 6 59
77 6 return;
78 }
79  
80 8 ▶sub search_class_by_role {
Subroutine search_class_by_role: called CC=8 SCAR=21.3
Pod documented
81 8 my ($class_or_obj, $role) = @_;
82  
83 8 my $meta = find_meta($class_or_obj);
84  
85 8 ▶ return unless defined $meta;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $meta 0 8 8
86  
87 8 ▶ my $role_name = blessed $role ? $role->name : $role;
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($role) ? : 4 4 8
88  
89 8 foreach my $class ($meta->class_precedence_list) {
90  
91 12 my $_meta = find_meta($class);
92  
93 12 ▶ next unless defined $_meta;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $_meta 0 12 12
94  
95 12 ▶ foreach my $role (@{ $_meta->roles || [] }) {
Condition50%1/2
$_meta->roles || []
l!l
120
MC/DC0%0/1
$_meta->roles || []
$_meta->roles
Truth table50%1/2
A
A= $_meta->roles
Aresult
11
00
96 6 ▶ return $class if $role->name eq $role_name;
Branch: 1 branch
BranchTrueFalseTotal
if $role->name eq $role_name 6 0 6
97 }
98 }
99  
100 2 return;
101 }
102  
103 # this can possibly behave in unexpected ways because the roles being composed
104 # before being applied could differ from call to call; I'm not sure if or how
105 # to document this possible quirk.
106 2 ▶sub ensure_all_roles {
Subroutine ensure_all_roles: called CC=1 SCAR=0.0
Pod documented
107 2 my $applicant = shift;
108 2 ▶ _apply_all_roles($applicant, sub { !does_role($applicant, $_) }, @_);
Subroutine __ANON__: called CC=1 SCAR=0.0
109 }
110  
111 1647 ▶sub apply_all_roles {
Subroutine apply_all_roles: called CC=1 SCAR=0.0
Pod documented
112 1647 my $applicant = shift;
113 1647 _apply_all_roles($applicant, undef, @_);
114 }
115  
116 1649 ▶sub _apply_all_roles {
Subroutine _apply_all_roles: called CC=13 SCAR=25.7
117 1649 my $applicant = shift;
118 1649 my $role_filter = shift;
119  
120 1649 ▶ unless (@_) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@_) 3 1646 1649
121 3 require Moose;
122 3 throw_exception( MustSpecifyAtleastOneRoleToApplicant => applicant => $applicant );
123 }
124  
125 # If @_ contains role meta objects, mkopt will think that they're values,
126 # because they're references. In other words (roleobj1, roleobj2,
127 # roleobj3) will become [ [ roleobj1, roleobj2 ], [ roleobj3, undef ] ]
128 # -- this is no good. We'll preprocess @_ first to eliminate the potential
129 # bug.
130 # -- rjbs, 2011-04-08
131 my $roles = Data::OptList::mkopt( [@_], {
132 moniker => 'role',
133 318 ▶ name_test => sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
134 318 ▶ ! ref $_[0] or blessed($_[0]) && $_[0]->isa('Moose::Meta::Role')
Branch: 1 branch
BranchTrueFalseTotal
not ref $_[0] or builtin::blessed($_[0]) and $_[0]->isa("Moose::Meta::Role") 260 58 318
Condition83%5/6
!ref($_[0]) || builtin::blessed($_[0]) && $_[0]->isa('Moose::Meta::Role')
l!l&&r!l&&!r
260355
builtin::blessed($_[0]) and $_[0]->isa("Moose::Meta::Role")
!ll&&!rl&&r
5503
MC/DC66%2/3
!ref($_[0]) || builtin::blessed($_[0]) && $_[0]->isa('Moose::Meta::Role')
!ref($_[0]) builtin::blessed($_[0]) $_[0]->isa("Moose::Meta::Role")
Truth table75%3/4
not(A) || B and C
A= !ref($_[0])B= builtin::blessed($_[0])C= $_[0]->isa("Moose::Meta::Role")
ABCresult
1XX1
0111
00X0
0100
135 }
136 1646 });
137  
138 1646 my @role_metas;
139 1646 foreach my $role (@$roles) {
140 1929 my $meta;
141  
142 1929 ▶ if ( blessed $role->[0] ) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($role->[0])) { } 26 1903 1929
143 26 $meta = $role->[0];
144 }
145 else {
146 1903 _load_user_class( $role->[0] , $role->[1] );
147 1899 $meta = find_meta( $role->[0] );
148 }
149  
150 1925 ▶ unless ($meta && $meta->isa('Moose::Meta::Role') ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($meta and $meta->isa("Moose::Meta::Role")) 3 1922 1925
Condition100%3/3
$meta and $meta->isa("Moose::Meta::Role")
!ll&&!rl&&r
211922
MC/DC100%2/2
$meta and $meta->isa("Moose::Meta::Role")
$meta $meta->isa("Moose::Meta::Role")
Truth table100%3/3
A and B
A= $metaB= $meta->isa("Moose::Meta::Role")
ABresult
0X0
100
111
151 3 throw_exception( CanOnlyConsumeRole => role_name => $role->[0] );
152 }
153  
154 1922 push @role_metas, [ $meta, $role->[1] ];
155 }
156  
157 1639 ▶ if ( defined $role_filter ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $role_filter) 2 1637 1639
158 2 @role_metas = grep { local $_ = $_->[0]; $role_filter->() } @role_metas;
159 }
160  
161 1639 ▶ return unless @role_metas;
Branch: 1 branch
BranchTrueFalseTotal
unless @role_metas 2 1637 1639
162  
163 1637 ▶ _load_user_class($applicant)
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($applicant) or Class::MOP::class_of($applicant) 1 1636 1637
Condition100%3/3
builtin::blessed($applicant) or Class::MOP::class_of($applicant)
l!l&&r!l&&!r
163151
MC/DC100%2/2
builtin::blessed($applicant) or Class::MOP::class_of($applicant)
builtin::blessed($applicant) Class::MOP::class_of($applicant)
Truth table100%3/3
A or B
A= builtin::blessed($applicant)B= Class::MOP::class_of($applicant)
ABresult
1X1
011
000
164 unless blessed($applicant)
165 || Class::MOP::class_of($applicant);
166  
167 1637 ▶ my $meta = ( blessed $applicant ? $applicant : Moose::Meta::Class->initialize($applicant) );
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($applicant) ? : 1631 6 1637
168  
169 1637 ▶ if ( scalar @role_metas == 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @role_metas == 1) { } 1411 226 1637
170 1411 my ( $role, $params ) = @{ $role_metas[0] };
171 1411 ▶ $role->apply( $meta, ( defined $params ? %$params : () ) );
Branch: 1 branch
BranchTrueFalseTotal
defined $params ? : 31 1380 1411
172 }
173 else {
174 226 Moose::Meta::Role->combine(@role_metas)->apply($meta);
175 }
176 }
177  
178 5 ▶sub with_traits {
Subroutine with_traits: called CC=2 SCAR=6.9
Pod documented
179 5 my ($class, @roles) = @_;
180 5 ▶ return $class unless @roles;
Branch: 1 branch
BranchTrueFalseTotal
unless @roles 1 4 5
181 4 return Moose::Meta::Class->create_anon_class(
182 superclasses => [$class],
183 roles => \@roles,
184 cache => 1,
185 )->name;
186 }
187  
188 # instance deconstruction ...
189  
190 0 ▶sub get_all_attribute_values {
Subroutine get_all_attribute_values: not called CC=1 SCAR=6.9
Pod documented
191 0 my ($class, $instance) = @_;
192 return +{
193 0 map { $_->name => $_->get_value($instance) }
194 0 grep { $_->has_value($instance) }
195 $class->get_all_attributes
196 };
197 }
198  
199 0 ▶sub get_all_init_args {
Subroutine get_all_init_args: not called CC=1 SCAR=6.9
Pod documented
200 0 my ($class, $instance) = @_;
201 return +{
202 0 map { $_->init_arg => $_->get_value($instance) }
203 0 grep { $_->has_value($instance) }
204 0 grep { defined($_->init_arg) }
205 $class->get_all_attributes
206 };
207 }
208  
209 476 ▶sub resolve_metatrait_alias {
Subroutine resolve_metatrait_alias: called CC=1 SCAR=0.0
Pod documented
210 476 return resolve_metaclass_alias( @_, trait => 1 );
211 }
212  
213 216 ▶sub _build_alias_package_name {
Subroutine _build_alias_package_name: called CC=2 SCAR=6.9
214 216 my ($type, $name, $trait) = @_;
215 216 ▶ return 'Moose::Meta::'
Branch: 1 branch
BranchTrueFalseTotal
$trait ? : 189 27 216
216 . $type
217 . '::Custom::'
218 . ( $trait ? 'Trait::' : '' )
219 . $name;
220 }
221  
222 {
223 402 my %cache;
224  
225 503 ▶ sub resolve_metaclass_alias {
Subroutine resolve_metaclass_alias: called CC=6 SCAR=17.9
Pod documented
226 503 my ( $type, $metaclass_name, %options ) = @_;
227  
228 503 ▶ my $cache_key = $type . q{ } . ( $options{trait} ? '-Trait' : '' );
Branch: 1 branch
BranchTrueFalseTotal
$options{'trait'} ? : 476 27 503
229 return $cache{$cache_key}{$metaclass_name}
230 503 ▶ if $cache{$cache_key}{$metaclass_name};
Branch: 1 branch
BranchTrueFalseTotal
if $cache{$cache_key}{$metaclass_name} 297 206 503
231  
232 my $possible_full_name = _build_alias_package_name(
233 $type, $metaclass_name, $options{trait}
234 206 );
235  
236 206 my @possible = ($possible_full_name, $metaclass_name);
237 206 for my $package (@possible) {
238 331 use_package_optimistically($package);
239 331 ▶ if ($package->can('register_implementation')) {
Branch: 2 branches
BranchTrueFalseTotal
if ($package->can('register_implementation')) { } 80 251 331
elsif (find_meta($package)) { } 122 129 251
240 80 return $cache{$cache_key}{$metaclass_name} =
241 $package->register_implementation;
242 }
243 elsif (find_meta($package)) {
244 122 return $cache{$cache_key}{$metaclass_name} = $package;
245 }
246 }
247  
248 4 throw_exception( CannotLocatePackageInINC => possible_packages => _english_list_or(@possible),
249 INC => \@INC,
250 type => $type,
251 metaclass_name => $metaclass_name,
252 params => \%options
253 );
254 }
255 }
256  
257 402 ▶sub add_method_modifier {
Subroutine add_method_modifier: called CC=8 SCAR=20.8
Pod undocumented
258 417 my ( $class_or_obj, $modifier_name, $args ) = @_;
259 417 ▶ my $meta
Branch: 1 branch
BranchTrueFalseTotal
$class_or_obj->can('add_before_method_modifier') ? : 415 2 417
260 = $class_or_obj->can('add_before_method_modifier')
261 ? $class_or_obj
262 : find_meta($class_or_obj);
263 417 my $code = pop @{$args};
264 417 my $add_modifier_method = 'add_' . $modifier_name . '_method_modifier';
265 417 ▶ if ( my $method_modifier_type = ref( $args->[0] ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $method_modifier_type = ref $args->[0]) { } 7 410 417
266 7 ▶ if ( $method_modifier_type eq 'Regexp' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($method_modifier_type eq 'Regexp') { } 3 4 7
elsif ($method_modifier_type eq 'ARRAY') { } 2 2 4
267 3 my @all_methods = $meta->get_all_methods;
268 my @matched_methods
269 3 = grep { $_->name =~ $args->[0] } @all_methods;
270 $meta->$add_modifier_method( $_->name, $code )
271 3 for @matched_methods;
272 }
273 elsif ($method_modifier_type eq 'ARRAY') {
274 2 $meta->$add_modifier_method( $_, $code ) for @{$args->[0]};
275 }
276 else {
277 2 throw_exception( IllegalMethodTypeToAddMethodModifier => class_or_object => $class_or_obj,
278 modifier_name => $modifier_name,
279 params => $args
280 );
281 }
282 }
283 else {
284 410 $meta->$add_modifier_method( $_, $code ) for @{$args};
285 }
286 }
287  
288 63 ▶sub english_list {
Subroutine english_list: called CC=1 SCAR=0.0
Pod documented
289 63 _english_list_and(@_);
290 }
291  
292 63 ▶sub _english_list_and {
Subroutine _english_list_and: called CC=1 SCAR=0.0
293 63 _english_list('and', \@_);
294 }
295  
296 55 ▶sub _english_list_or {
Subroutine _english_list_or: called CC=1 SCAR=0.0
297 55 _english_list('or', \@_);
298 }
299  
300 118 ▶sub _english_list {
Subroutine _english_list: called CC=3 SCAR=11.0
301 118 my ($conjunction, $items) = @_;
302  
303 118 my @items = sort @$items;
304  
305 118 ▶ return $items[0] if @items == 1;
Branch: 1 branch
BranchTrueFalseTotal
if @items == 1 15 103 118
306 103 ▶ return "$items[0] $conjunction $items[1]" if @items == 2;
Branch: 1 branch
BranchTrueFalseTotal
if @items == 2 50 53 103
307  
308 53 my $tail = pop @items;
309 53 my $list = join ', ', @items;
310 53 $list .= ", $conjunction " . $tail;
311  
312 53 return $list;
313 }
314  
315 1920 ▶sub _caller_info {
Subroutine _caller_info: called CC=2 SCAR=7.0
316 1920 ▶ my $level = @_ ? ($_[0] + 1) : 2;
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 0 1920 1920
317 1920 my %info;
318 1920 @info{qw(package file line)} = caller($level);
319 1920 return %info;
320 }
321  
322 10 ▶sub _create_alias {
Subroutine _create_alias: called CC=1 SCAR=0.0
323 10 my ($type, $name, $trait, $for) = @_;
324 10 my $package = _build_alias_package_name($type, $name, $trait);
325 Class::MOP::Class->initialize($package)->add_method(
326 1 ▶ register_implementation => sub { $for }
Subroutine __ANON__: called CC=1 SCAR=0.0
Subroutine register_implementation: called CC=1 SCAR=0.0
327 10 );
328 }
329  
330 6 ▶sub meta_attribute_alias {
Subroutine meta_attribute_alias: called CC=2 SCAR=7.0
Pod documented
331 6 my ($to, $from) = @_;
332 6 ▶ $from ||= caller;
Condition66%2/3
$from ||= caller
l!l&&r!l&&!r
330
MC/DC0%0/2
$from ||= caller
$from caller
Truth table66%2/3
A ||= B
A= $fromB= caller
ABresult
1X1
011
000
333 6 my $meta = Class::MOP::class_of($from);
334 6 my $trait = $meta->isa('Moose::Meta::Role');
335 6 _create_alias('Attribute', $to, $trait, $from);
336 }
337  
338 4 ▶sub meta_class_alias {
Subroutine meta_class_alias: called CC=2 SCAR=7.0
Pod documented
339 4 my ($to, $from) = @_;
340 4 ▶ $from ||= caller;
Condition66%2/3
$from ||= caller
l!l&&r!l&&!r
220
MC/DC0%0/2
$from ||= caller
$from caller
Truth table66%2/3
A ||= B
A= $fromB= caller
ABresult
1X1
011
000
341 4 my $meta = Class::MOP::class_of($from);
342 4 my $trait = $meta->isa('Moose::Meta::Role');
343 4 _create_alias('Class', $to, $trait, $from);
344 }
345  
346 8254 ▶sub _load_user_class {
Subroutine _load_user_class: called CC=3 SCAR=11.0
347 8254 my ($class, $opts) = @_;
348 &use_package_optimistically(
349 $class,
350 $opts && $opts->{-version} ? $opts->{-version} : ()
351 8254 ▶ );
Branch: 1 branch
BranchTrueFalseTotal
$opts && $opts->{'-version'} ? : 4 8250 8254
Condition100%3/3
$opts and $opts->{"-version"}
!ll&&!rl&&r
8200504
MC/DC100%2/2
$opts and $opts->{"-version"}
$opts $opts->{"-version"}
Truth table100%3/3
A and B
A= $optsB= $opts->{"-version"}
ABresult
0X0
100
111
352 }
353  
354 # XXX - this should be added to Params::Util
355 408 ▶sub _STRINGLIKE0 ($) {
Subroutine _STRINGLIKE0: called CC=4 SCAR=13.9
356 408 ▶ return 0 if !defined $_[0];
Branch: 1 branch
BranchTrueFalseTotal
unless defined $_[0] 0 408 408
357 408 ▶ return 1 if !ref $_[0];
Branch: 1 branch
BranchTrueFalseTotal
unless ref $_[0] 187 221 408
358 221 ▶ return 1 if overload::OverloadedStringify($_[0]);
Branch: 1 branch
BranchTrueFalseTotal
if overload::OverloadedStringify($_[0]) 24 197 221
359 197 return 0;
360 }
361  
362 61 ▶sub _reconcile_roles_for_metaclass {
Subroutine _reconcile_roles_for_metaclass: called CC=2 SCAR=6.9
363 61 my ($class_meta_name, $super_meta_name) = @_;
364  
365 61 my @role_differences = _role_differences(
366 $class_meta_name, $super_meta_name,
367 );
368  
369 # handle the case where we need to fix compatibility between a class and
370 # its parent, but all roles in the class are already also done by the
371 # parent
372 # see t/metaclasses/metaclass_compat_no_fixing_bug.t
373 61 ▶ return $super_meta_name
Branch: 1 branch
BranchTrueFalseTotal
unless @role_differences 3 58 61
374 unless @role_differences;
375  
376 return Moose::Meta::Class->create_anon_class(
377 superclasses => [$super_meta_name],
378 58 roles => [map { $_->name } @role_differences],
379 cache => 1,
380 )->name;
381 }
382  
383 61 ▶sub _role_differences {
Subroutine _role_differences: called CC=9 SCAR=25.4
384 61 my ($class_meta_name, $super_meta_name) = @_;
385 my @super_role_metas = map {
386 $_->isa('Moose::Meta::Role::Composite')
387 0 ▶ ? (@{ $_->get_roles })
Branch: 3 branches
BranchTrueFalseTotal
$_->isa('Moose::Meta::Role::Composite') ? : 0 64 64
$super_meta_name->meta->can('_roles_with_inheritance') ? : 61 0 61
$super_meta_name->meta->can('roles') ? : 0 0 0
388 : ($_)
389 } $super_meta_name->meta->can('_roles_with_inheritance')
390 ? $super_meta_name->meta->_roles_with_inheritance
391 : $super_meta_name->meta->can('roles')
392 0 ? @{ $super_meta_name->meta->roles }
393 : ();
394 my @role_metas = map {
395 $_->isa('Moose::Meta::Role::Composite')
396 67 ▶ ? (@{ $_->get_roles })
Branch: 3 branches
BranchTrueFalseTotal
$_->isa('Moose::Meta::Role::Composite') ? : 3 64 67
$class_meta_name->meta->can('_roles_with_inheritance') ? : 61 0 61
$class_meta_name->meta->can('roles') ? : 0 0 0
397 : ($_)
398 } $class_meta_name->meta->can('_roles_with_inheritance')
399 ? $class_meta_name->meta->_roles_with_inheritance
400 : $class_meta_name->meta->can('roles')
401 0 ? @{ $class_meta_name->meta->roles }
402 : ();
403 61 my @differences;
404 61 for my $role_meta (@role_metas) {
405 push @differences, $role_meta
406 70 ▶ unless any { $_->name eq $role_meta->name } @super_role_metas;
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
unless &any(sub { $_->name eq $role_meta->name; } , @super_role_metas) 58 12 70
407 }
408 61 return @differences;
409 }
410  
411 63 ▶sub _classes_differ_by_roles_only {
Subroutine _classes_differ_by_roles_only: called CC=3 SCAR=11.0
412 63 my ( $self_meta_name, $super_meta_name ) = @_;
413  
414 63 my $common_base_name
415 = _find_common_base( $self_meta_name, $super_meta_name );
416  
417 63 ▶ return unless defined $common_base_name;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $common_base_name 0 63 63
418  
419 my @super_meta_name_ancestor_names
420 63 = _get_ancestors_until( $super_meta_name, $common_base_name );
421 my @class_meta_name_ancestor_names
422 63 = _get_ancestors_until( $self_meta_name, $common_base_name );
423  
424 return
425 133 ▶ unless all { _is_role_only_subclass($_) }
Subroutine __ANON__: called CC=1 SCAR=0.0
426 63 ▶ @super_meta_name_ancestor_names,
Branch: 1 branch
BranchTrueFalseTotal
unless &all(sub { _is_role_only_subclass($_); } , @super_meta_name_ancestor_names, @class_meta_name_ancestor_names) 2 61 63
427 @class_meta_name_ancestor_names;
428  
429 61 return 1;
430 }
431  
432 63 ▶sub _find_common_base {
Subroutine _find_common_base: called CC=3 SCAR=11.4
433 63 my ($meta1, $meta2) = map { Class::MOP::class_of($_) } @_;
434 63 ▶ return unless defined $meta1 && defined $meta2;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $meta1 and defined $meta2 0 63 63
Condition33%1/3
defined $meta1 and defined $meta2
!ll&&!rl&&r
0063
MC/DC0%0/2
defined $meta1 and defined $meta2
defined $meta1 defined $meta2
Truth table33%1/3
A and B
A= defined $meta1B= defined $meta2
ABresult
0X0
100
111
435  
436 # FIXME? This doesn't account for multiple inheritance (not sure
437 # if it needs to though). For example, if somewhere in $meta1's
438 # history it inherits from both ClassA and ClassB, and $meta2
439 # inherits from ClassB & ClassA, does it matter? And what crazy
440 # fool would do that anyway?
441  
442 63 my %meta1_parents = map { $_ => 1 } $meta1->linearized_isa;
443  
444 63 ▶ return first { $meta1_parents{$_} } $meta2->linearized_isa;
Subroutine __ANON__: called CC=1 SCAR=0.0
445 }
446  
447 126 ▶sub _get_ancestors_until {
Subroutine _get_ancestors_until: called CC=3 SCAR=11.0
448 126 my ($start_name, $until_name) = @_;
449  
450 126 my @ancestor_names;
451 126 for my $ancestor_name (Class::MOP::class_of($start_name)->linearized_isa) {
452 263 ▶ last if $ancestor_name eq $until_name;
Branch: 1 branch
BranchTrueFalseTotal
if $ancestor_name eq $until_name 126 137 263
453 137 push @ancestor_names, $ancestor_name;
454 }
455 126 return @ancestor_names;
456 }
457  
458 133 ▶sub _is_role_only_subclass {
Subroutine _is_role_only_subclass: called CC=18 SCAR=34.6
459 133 my ($meta_name) = @_;
460 133 my $meta = Class::MOP::Class->initialize($meta_name);
461 133 my @parent_names = $meta->superclasses;
462  
463 # XXX: don't feel like messing with multiple inheritance here... what would
464 # that even do?
465 133 ▶ return unless @parent_names == 1;
Branch: 1 branch
BranchTrueFalseTotal
unless @parent_names == 1 0 133 133
466 133 my ($parent_name) = @parent_names;
467 133 my $parent_meta = Class::MOP::Class->initialize($parent_name);
468  
469 # only get the roles attached to this particular class, don't look at
470 # superclasses
471 133 ▶ my @roles = $meta->can('calculate_all_roles')
Branch: 1 branch
BranchTrueFalseTotal
$meta->can('calculate_all_roles') ? : 133 0 133
472 ? $meta->calculate_all_roles
473 : ();
474  
475 # it's obviously not a role-only subclass if it doesn't do any roles
476 133 ▶ return unless @roles;
Branch: 1 branch
BranchTrueFalseTotal
unless @roles 2 131 133
477  
478 # loop over all methods that are a part of the current class
479 # (not inherited)
480 131 for my $method ( $meta->_get_local_methods ) {
481 # always ignore meta
482 169 ▶ next if $method->isa('Class::MOP::Method::Meta');
Branch: 1 branch
BranchTrueFalseTotal
if $method->isa("Class::MOP::Method::Meta") 131 38 169
483 # we'll deal with attributes below
484 38 ▶ next if $method->can('associated_attribute');
Branch: 1 branch
BranchTrueFalseTotal
if $method->can("associated_attribute") 26 12 38
485 # if the method comes from a role we consumed, ignore it
486 12 ▶ next if $meta->can('does_role')
Branch: 1 branch
BranchTrueFalseTotal
if $meta->can("does_role") and $meta->does_role($method->original_package_name) 9 3 12
Condition66%2/3
$meta->can("does_role") and $meta->does_role($method->original_package_name)
!ll&&!rl&&r
039
MC/DC50%1/2
$meta->can("does_role") and $meta->does_role($method->original_package_name)
$meta->can("does_role") $meta->does_role($method->original_package_name)
Truth table66%2/3
A and B
A= $meta->can("does_role")B= $meta->does_role($method->original_package_name)
ABresult
0X0
100
111
487 && $meta->does_role($method->original_package_name);
488 # FIXME - this really isn't right. Just because a modifier is
489 # defined in a role doesn't mean it isn't _also_ defined in the
490 # subclass.
491 next if $method->isa('Class::MOP::Method::Wrapped')
492 && (
493 (!scalar($method->around_modifiers)
494 3 ▶ || any { $_->has_around_method_modifiers($method->name) } @roles)
Subroutine __ANON__: called CC=1 SCAR=0.0
495 && (!scalar($method->before_modifiers)
496 0 ▶ || any { $_->has_before_method_modifiers($method->name) } @roles)
Subroutine __ANON__: not called CC=1 SCAR=6.9
497 && (!scalar($method->after_modifiers)
498 0 ▶ || any { $_->has_after_method_modifiers($method->name) } @roles)
Subroutine __ANON__: not called CC=1 SCAR=2.3
Branch: 1 branch
BranchTrueFalseTotal
if $method->isa("Class::MOP::Method::Wrapped") and (!scalar($method->around_modifiers) || &any(sub { $_->has_around_method_modifiers($method->name); } , @roles) and !scalar($method->before_modifiers) || &any(sub { $_->has_before_method_modifiers($method->name); } , @roles) and !scalar($method->after_modifiers) || &any(sub { $_->has_after_method_modifiers($method->name); } , @roles)) 3 0 3
Condition33%6/18
$method->isa("Class::MOP::Method::Wrapped") and (!scalar($method->around_modifiers) || &any(sub { $_->has_around_method_modifiers($method->name); } , @roles) and !scalar($method->before_modifiers) || &any(sub { $_->has_before_method_modifiers($method->name); } , @roles) and !scalar($method->after_modifiers) || &any(sub { $_->has_after_method_modifiers($method->name); } , @roles))
!ll&&!rl&&r
003
!scalar($method->around_modifiers) || &any(sub { $_->has_around_method_modifiers($method->name); } , @roles) and !scalar($method->before_modifiers) || &any(sub { $_->has_before_method_modifiers($method->name); } , @roles) and !scalar($method->after_modifiers) || &any(sub { $_->has_after_method_modifiers($method->name); } , @roles)
!ll&&!rl&&r
003
!scalar($method->around_modifiers) || &any(sub { $_->has_around_method_modifiers($method->name); } , @roles) and !scalar($method->before_modifiers) || &any(sub { $_->has_before_method_modifiers($method->name); } , @roles)
!ll&&!rl&&r
003
not scalar $method->around_modifiers or &any(sub { $_->has_around_method_modifiers($method->name); } , @roles)
l!l&&r!l&&!r
030
not scalar $method->before_modifiers or &any(sub { $_->has_before_method_modifiers($method->name); } , @roles)
l!l&&r!l&&!r
300
not scalar $method->after_modifiers or &any(sub { $_->has_after_method_modifiers($method->name); } , @roles)
l!l&&r!l&&!r
300
MC/DC0%0/7
$method->isa("Class::MOP::Method::Wrapped") and (!scalar($method->around_modifiers) || &any(sub { $_->has_around_method_modifiers($method->name); } , @roles) and !scalar($method->before_modifiers) || &any(sub { $_->has_before_method_modifiers($method->name); } , @roles) and !scalar($method->after_modifiers) || &any(sub { $_->has_after_method_modifiers($method->name); } , @roles))
$method->isa("Class::MOP::Method::Wrapped") not scalar $method->around_modifiers &any(sub { $_->has_around_method_modifiers($method->name); } , @roles) not scalar $method->before_modifiers &any(sub { $_->has_before_method_modifiers($method->name); } , @roles) not scalar $method->after_modifiers &any(sub { $_->has_after_method_modifiers($method->name); } , @roles)
Truth table6%1/16
A and not(B) or C and not(D) or E and not(F) or G
A= $method->isa("Class::MOP::Method::Wrapped")B= not scalar $method->around_modifiersC= &any(sub { $_->has_around_method_modifiers($method->name); } , @roles)D= not scalar $method->before_modifiersE= &any(sub { $_->has_before_method_modifiers($method->name); } , @roles)F= not scalar $method->after_modifiersG= &any(sub { $_->has_after_method_modifiers($method->name); } , @roles)
ABCDEFGresult
0XXXXXX0
100XXXX0
11X00XX0
10100XX0
11X1X000
11X01000
1011X000
10101000
11X1X1X1
11X1X011
11X011X1
11X01011
1011X1X1
1011X011
101011X1
10101011
499 );
500  
501 0 return 0;
502 }
503  
504 # loop over all attributes that are a part of the current class
505 # (not inherited)
506 # FIXME - this really isn't right. Just because an attribute is
507 # defined in a role doesn't mean it isn't _also_ defined in the
508 # subclass.
509 131 for my $attr (map { $meta->get_attribute($_) } $meta->get_attribute_list) {
510 26 ▶ next if any { $_->has_attribute($attr->name) } @roles;
Subroutine __ANON__: called CC=1 SCAR=0.2
Branch: 1 branch
BranchTrueFalseTotal
if &any(sub { $_->has_attribute($attr->name); } , @roles) 26 0 26
511  
512 0 return 0;
513 }
514  
515 131 return 1;
516 }
517  
518 23 ▶sub _is_package_loaded {
Subroutine _is_package_loaded: called CC=1 SCAR=0.0
519 23 my ($package) = @_;
520 23 defined $INC{module_notional_filename($package)};
521 }
522  
523 402 1;
524  
525 # ABSTRACT: Utilities for working with Moose classes
526  
527 __END__
« blib/lib/Moose/Meta/Attribute.pm ↑ Summary blib/lib/Class/MOP/Method/Inlined.pm »