blib/lib/Moose/Util/MetaRole.pm

filter: Statementstmt 100% 62 / 62 Branchbran 77.7% 14 / 18 Conditioncond 76.4% 13 / 17 MC/DCmcdc 44.4% 4 / 9 Subroutinesub 100% 13 / 13 Podpod 100% 2 / 2 totaltotal 89.2% 108 / 121 CC 17 SCAR 13.0
CC
17
Cov
92%
_metathing_for
6.0
_make_new_class
5.1
apply_base_class_roles
4.2
CRAP 3.7
« blib/lib/Moose/Meta/TypeConstraint/Registry.pm ↑ Summary blib/lib/Moose/Meta/TypeConstraint.pm »
1 package Moose::Util::MetaRole;
2 399 our $VERSION = '2.4001';
3  
4 399 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 399 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 399 ▶use Scalar::Util 'blessed';
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 399 ▶use List::Util 1.33 qw( first all );
Subroutine BEGIN: called CC=1 SCAR=0.0
9 399 ▶use Moose::Deprecated;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 399 ▶use Moose::Util 'throw_exception';
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 118 ▶sub apply_metaroles {
Subroutine apply_metaroles: called CC=2 SCAR=6.9
Pod documented
13 118 my %args = @_;
14  
15 118 my $for = _metathing_for( $args{for} );
16  
17 113 ▶ if ( $for->isa('Moose::Meta::Role') ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($for->isa('Moose::Meta::Role')) { } 18 95 113
18 18 return _make_new_metaclass( $for, $args{role_metaroles}, 'role' );
19 }
20 else {
21 95 return _make_new_metaclass( $for, $args{class_metaroles}, 'class' );
22 }
23 }
24  
25 125 ▶sub _metathing_for {
Subroutine _metathing_for: called CC=6 SCAR=17.9
26 125 my $passed = shift;
27  
28 125 ▶ my $found
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($passed) ? : 23 102 125
29 = blessed $passed
30 ? $passed
31 : Class::MOP::class_of($passed);
32  
33 125 ▶ return $found
Branch: 1 branch
BranchTrueFalseTotal
if defined $found and builtin::blessed($found) and $found->isa("Moose::Meta::Role") || $found->isa("Moose::Meta::Class") 119 6 125
Condition88%8/9
defined $found and builtin::blessed($found) and $found->isa("Moose::Meta::Role") || $found->isa("Moose::Meta::Class")
!ll&&!rl&&r
33119
defined $found and builtin::blessed($found)
!ll&&!rl&&r
30122
$found->isa("Moose::Meta::Role") or $found->isa("Moose::Meta::Class")
l!l&&r!l&&!r
20993
MC/DC75%3/4
defined $found and builtin::blessed($found) and $found->isa("Moose::Meta::Role") || $found->isa("Moose::Meta::Class")
defined $found builtin::blessed($found) $found->isa("Moose::Meta::Role") $found->isa("Moose::Meta::Class")
Truth table80%4/5
A and B and C or D
A= defined $foundB= builtin::blessed($found)C= $found->isa("Moose::Meta::Role")D= $found->isa("Moose::Meta::Class")
ABCDresult
0XXX0
10XX0
11000
111X1
11011
34 if defined $found
35 && blessed $found
36 && ( $found->isa('Moose::Meta::Role')
37 || $found->isa('Moose::Meta::Class') );
38  
39 6 local $Carp::CarpLevel = $Carp::CarpLevel + 1;
40  
41 6 throw_exception( InvalidArgPassedToMooseUtilMetaRole => argument => $passed );
42 }
43  
44 113 ▶sub _make_new_metaclass {
Subroutine _make_new_metaclass: called CC=4 SCAR=13.9
45 113 my $for = shift;
46 113 my $roles = shift;
47 113 my $primary = shift;
48  
49 113 ▶ return $for unless keys %{$roles};
Branch: 1 branch
BranchTrueFalseTotal
unless keys %{$roles;} 0 113 113
50  
51 my $new_metaclass
52 = exists $roles->{$primary}
53 113 ▶ ? _make_new_class( ref $for, $roles->{$primary} )
Branch: 1 branch
BranchTrueFalseTotal
exists $roles->{$primary} ? : 73 40 113
54 : blessed $for;
55  
56 113 my %classes;
57  
58 113 for my $key ( grep { $_ ne $primary } keys %{$roles} ) {
59 65 ▶ my $attr = first {$_}
Subroutine __ANON__: called CC=1 SCAR=0.0
60 65 map { $for->meta->find_attribute_by_name($_) } (
61 $key . '_metaclass',
62 $key . '_class'
63 );
64  
65 65 my $reader = $attr->get_read_method;
66  
67 $classes{ $attr->init_arg }
68 65 = _make_new_class( $for->$reader(), $roles->{$key} );
69 }
70  
71 113 my $new_meta = $new_metaclass->reinitialize( $for, %classes );
72  
73 112 return $new_meta;
74 }
75  
76 7 ▶sub apply_base_class_roles {
Subroutine apply_base_class_roles: called CC=4 SCAR=14.5
Pod documented
77 7 my %args = @_;
78  
79 7 ▶ my $meta = _metathing_for( $args{for} || $args{for_class} );
Condition33%1/3
$args{'for'} || $args{'for_class'}
l!l&&r!l&&!r
700
MC/DC0%0/2
$args{'for'} || $args{'for_class'}
$args{'for'} $args{'for_class'}
Truth table33%1/3
A || B
A= $args{'for'}B= $args{'for_class'}
ABresult
1X1
011
000
80 6 ▶ throw_exception( CannotApplyBaseClassRolesToRole => params => \%args,
Branch: 1 branch
BranchTrueFalseTotal
if $meta->isa("Moose::Meta::Role") 2 4 6
81 role_name => $meta->name,
82 )
83 if $meta->isa('Moose::Meta::Role');
84  
85 my $new_base = _make_new_class(
86 $meta->name,
87 $args{roles},
88 4 [ $meta->superclasses() ],
89 );
90  
91 4 ▶ $meta->superclasses($new_base)
Branch: 1 branch
BranchTrueFalseTotal
if $new_base ne $meta->name 4 0 4
92 if $new_base ne $meta->name();
93 }
94  
95 142 ▶sub _make_new_class {
Subroutine _make_new_class: called CC=5 SCAR=16.3
96 142 my $existing_class = shift;
97 142 my $roles = shift;
98 142 ▶ my $superclasses = shift || [$existing_class];
Condition100%2/2
shift() || [$existing_class]
l!l
4138
MC/DC100%1/1
shift() || [$existing_class]
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
99  
100 142 ▶ return $existing_class unless $roles;
Branch: 1 branch
BranchTrueFalseTotal
unless $roles 0 142 142
101  
102 142 my $meta = Class::MOP::Class->initialize($existing_class);
103  
104 return $existing_class
105 19 ▶ if $meta->can('does_role') && all { $meta->does_role($_) }
Subroutine __ANON__: called CC=1 SCAR=0.0
106 142 ▶ grep { !ref $_ } @{$roles};
Branch: 1 branch
BranchTrueFalseTotal
if $meta->can("does_role") and &all(sub { $meta->does_role($_); } , grep({not ref $_;} @{$roles;})) 0 142 142
Condition66%2/3
$meta->can("does_role") and &all(sub { $meta->does_role($_); } , grep({not ref $_;} @{$roles;}))
!ll&&!rl&&r
123190
MC/DC0%0/2
$meta->can("does_role") and &all(sub { $meta->does_role($_); } , grep({not ref $_;} @{$roles;}))
$meta->can("does_role") &all(sub { $meta->does_role($_); } , grep({not ref $_;} @{$roles;}))
Truth table66%2/3
A and B
A= $meta->can("does_role")B= &all(sub { $meta->does_role($_); } , grep({not ref $_;} @{$roles;}))
ABresult
0X0
100
111
107  
108 142 return Moose::Meta::Class->create_anon_class(
109 superclasses => $superclasses,
110 roles => $roles,
111 cache => 1,
112 )->name();
113 }
114  
115 399 1;
116  
117 # ABSTRACT: Apply roles to any metaclass, as well as the object base class
118  
119 __END__
« blib/lib/Moose/Meta/TypeConstraint/Registry.pm ↑ Summary blib/lib/Moose/Meta/TypeConstraint.pm »