blib/lib/With/Roles.pm

filter: Statementstmt 88.4% 92 / 104 Branchbran 71.4% 30 / 42 Conditioncond 48.0% 24 / 50 MC/DCmcdc 17.2% 5 / 29 Subroutinesub 92.8% 13 / 14 Podpod n/a totaltotal 68.6% 164 / 239 CC 44 SCAR 34.4
CC
44
Cov
74%
_detect_type
76.1
roles
11.0
_composite_name
6.4
CRAP 31.1
↑ Summary
1 package With::Roles;
2 10 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 10 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 7 our $VERSION = '0.001002';
6 7 $VERSION =~ tr/_//d;
7  
8 8 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 7 my %COMPOSITE_NAME;
11 my %COMPOSITE_KEY;
12  
13 7 my $role_suffix = 'A000';
14 18 ▶sub _composite_name {
Subroutine _composite_name: called CC=6 SCAR=18.6
15 18 my ($base, $role_base, @roles) = @_;
16 18 my $key = join('+', $base, map join('|', @$_), @roles);
17 return $COMPOSITE_NAME{$key}
18 18 ▶ if exists $COMPOSITE_NAME{$key};
Branch: 1 branch
BranchTrueFalseTotal
if exists $COMPOSITE_NAME{$key} 2 16 18
19  
20 16 my ($cut) = map qr/$_/, join '|', map quotemeta, @$role_base, $base;
21  
22 16 my $new_name = $base;
23 16 for my $roles (@roles) {
24 # this creates the potential for ambiguity, but it's unlikely to happen and
25 # we will keep the resulting composite
26 18 my @short_names = @$roles;
27 18 for (@short_names) {
28 19 s/\A${cut}::/::/;
29 $_ = join '::',
30 19 map { s/\W/_/g; $_ }
31 split /::/;
32 }
33 18 $new_name .= '__WITH__' . join '__AND__', @short_names;
34 }
35  
36 16 ▶ if ($COMPOSITE_KEY{$new_name} || length($new_name) > 252) {
Branch: 1 branch
BranchTrueFalseTotal
if ($COMPOSITE_KEY{$new_name} or length $new_name > 252) 0 16 16
Condition33%1/3
$COMPOSITE_KEY{$new_name} or length $new_name > 252
l!l&&r!l&&!r
0016
MC/DC0%0/2
$COMPOSITE_KEY{$new_name} or length $new_name > 252
$COMPOSITE_KEY{$new_name} length $new_name > 252
Truth table33%1/3
A or B
A= $COMPOSITE_KEY{$new_name}B= length $new_name > 252
ABresult
1X1
011
000
37 0 my $abbrev = substr $new_name, 0, 250 - length $role_suffix;
38 0 $abbrev =~ s/(?<!:):$//;
39 0 $new_name = $abbrev.'__'.$role_suffix++;
40 }
41  
42 16 $COMPOSITE_KEY{$new_name} = $key;
43  
44 16 return $COMPOSITE_NAME{$key} = $new_name;
45 }
46  
47 12 ▶sub _gen {
Subroutine _gen: called CC=5 SCAR=16.2
48 12 my ($pack, $type, @ops) = @_;
49 12 my $e;
50 {
51 12 local $@;
52 7 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
53 7 ▶ no warnings 'once';
Subroutine BEGIN: called CC=1 SCAR=0.0
54 84 local *{"${pack}::${_}"}
55 12 for qw(with extends requires has around after before);
56  
57 12 ▶ my $code = join('',
Branch: 1 branch
BranchTrueFalseTotal
defined $type ? : 12 0 12
58 "package $pack;\n",
59 (defined $type ? "use $type;\n" : ()),
60 (
61 map "$ops[$_-1](\@{\$ops[$_]});\n",
62 map $_*2+1,
63 0 .. (@ops/2-1)
64 ),
65 "1;\n",
66 );
67  
68 12 ▶ eval $code or $e = $@;
Subroutine BEGIN: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
eval $code or $e = $@ 12 0 12
69 }
70 12 ▶ die $e if defined $e;
Branch: 1 branch
BranchTrueFalseTotal
if defined $e 0 12 12
71 }
72  
73 0 ▶sub _require {
Subroutine _require: not called CC=1 SCAR=6.9
74 0 my $package = shift;
75 0 (my $module = "$package.pm") =~ s{::|'}{/}g;
76 0 require $module;
77 }
78  
79 7 ▶sub _extends {
Subroutine _extends: called CC=1 SCAR=0.0
80 7 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
81 7 my $caller = caller;
82 7 @{"${caller}::ISA"} = (@_);
83 7 _copy_mro($_[0], $caller);
84 }
85  
86 10 ▶sub _copy_mro {
Subroutine _copy_mro: called CC=3 SCAR=11.4
87 10 my $source = shift;
88 10 ▶ my $target = shift || caller;
Condition66%2/3
shift() || caller
l!l&&r!l&&!r
730
MC/DC0%0/2
shift() || caller
shift() caller
Truth table66%2/3
A || B
A= shift()B= caller
ABresult
1X1
011
000
89 10 ▶ mro::set_mro($target, mro::get_mro($source))
Branch: 1 branch
BranchTrueFalseTotal
if defined &mro::set_mro 10 0 10
90 if defined &mro::set_mro;
91 }
92  
93 12 ▶sub _detect_type {
Subroutine _detect_type: called CC=23 SCAR=43.3
94 12 my ($base, @roles) = @_;
95 12 my $meta;
96 12 ▶ if (
Branch: 7 branches
BranchTrueFalseTotal
if ($INC{'Moo/Role.pm'} and 'Moo::Role'->is_role($base)) { } 1 11 12
elsif ($INC{'Moo.pm'} and 'Moo'->_accessor_maker_for($base)) { } 3 8 11
elsif ($INC{'Class/MOP.pm'} and $meta = Class::MOP::class_of($base) and $meta->isa('Moose::Meta::Role')) { } 0 8 8
elsif ($INC{'Class/MOP.pm'} and $meta = Class::MOP::class_of($base) and $meta->isa('Class::MOP::Class')) { } 0 8 8
elsif (defined &Mouse::Util::find_meta and $meta = Mouse::Util::find_meta($base) and $meta->isa('Mouse::Meta::Role')) { } 0 8 8
elsif (defined &Mouse::Util::find_meta and $meta = Mouse::Util::find_meta($base) and $meta->isa('Mouse::Meta::Class')) { } 0 8 8
elsif ($INC{'Role/Tiny.pm'} and 'Role::Tiny'->is_role($base)) { } 1 7 8
Condition100%3/3
$INC{"Moo/Role.pm"} and "Moo::Role"->is_role($base)
!ll&&!rl&&r
831
MC/DC100%2/2
$INC{"Moo/Role.pm"} and "Moo::Role"->is_role($base)
$INC{"Moo/Role.pm"} "Moo::Role"->is_role($base)
Truth table100%3/3
A and B
A= $INC{"Moo/Role.pm"}B= "Moo::Role"->is_role($base)
ABresult
0X0
100
111
97 $INC{'Moo/Role.pm'}
98 and Moo::Role->is_role($base)
99 ) {
100 1 return 'Moo::Role';
101 }
102 ▶ elsif (
Condition66%2/3
$INC{"Moo.pm"} and "Moo"->_accessor_maker_for($base)
!ll&&!rl&&r
803
MC/DC50%1/2
$INC{"Moo.pm"} and "Moo"->_accessor_maker_for($base)
$INC{"Moo.pm"} "Moo"->_accessor_maker_for($base)
Truth table66%2/3
A and B
A= $INC{"Moo.pm"}B= "Moo"->_accessor_maker_for($base)
ABresult
0X0
100
111
103 $INC{'Moo.pm'}
104 and Moo->_accessor_maker_for($base)
105 ) {
106 3 return 'Moo';
107 }
108 ▶ elsif (
Condition33%2/6
$INC{"Class/MOP.pm"} and $meta = Class::MOP::class_of($base) and $meta->isa("Moose::Meta::Role")
!ll&&!rl&&r
800
$INC{"Class/MOP.pm"} and $meta = Class::MOP::class_of($base)
!ll&&!rl&&r
800
MC/DC0%0/3
$INC{"Class/MOP.pm"} and $meta = Class::MOP::class_of($base) and $meta->isa("Moose::Meta::Role")
$INC{"Class/MOP.pm"} $meta = Class::MOP::class_of($base) $meta->isa("Moose::Meta::Role")
Truth table25%1/4
A and B and C
A= $INC{"Class/MOP.pm"}B= $meta = Class::MOP::class_of($base)C= $meta->isa("Moose::Meta::Role")
ABCresult
0XX0
10X0
1100
1111
109 $INC{'Class/MOP.pm'}
110 and $meta = Class::MOP::class_of($base)
111 and $meta->isa('Moose::Meta::Role')
112 ) {
113 0 return 'Moose::Role';
114 }
115 ▶ elsif (
Condition33%2/6
$INC{"Class/MOP.pm"} and $meta = Class::MOP::class_of($base) and $meta->isa("Class::MOP::Class")
!ll&&!rl&&r
800
$INC{"Class/MOP.pm"} and $meta = Class::MOP::class_of($base)
!ll&&!rl&&r
800
MC/DC0%0/3
$INC{"Class/MOP.pm"} and $meta = Class::MOP::class_of($base) and $meta->isa("Class::MOP::Class")
$INC{"Class/MOP.pm"} $meta = Class::MOP::class_of($base) $meta->isa("Class::MOP::Class")
Truth table25%1/4
A and B and C
A= $INC{"Class/MOP.pm"}B= $meta = Class::MOP::class_of($base)C= $meta->isa("Class::MOP::Class")
ABCresult
0XX0
10X0
1100
1111
116 $INC{'Class/MOP.pm'}
117 and $meta = Class::MOP::class_of($base)
118 and $meta->isa('Class::MOP::Class')
119 ) {
120 0 return 'Moose';
121 }
122 ▶ elsif (
Condition33%2/6
defined &Mouse::Util::find_meta and $meta = Mouse::Util::find_meta($base) and $meta->isa("Mouse::Meta::Role")
!ll&&!rl&&r
800
defined &Mouse::Util::find_meta and $meta = Mouse::Util::find_meta($base)
!ll&&!rl&&r
800
MC/DC0%0/3
defined &Mouse::Util::find_meta and $meta = Mouse::Util::find_meta($base) and $meta->isa("Mouse::Meta::Role")
defined &Mouse::Util::find_meta $meta = Mouse::Util::find_meta($base) $meta->isa("Mouse::Meta::Role")
Truth table25%1/4
A and B and C
A= defined &Mouse::Util::find_metaB= $meta = Mouse::Util::find_meta($base)C= $meta->isa("Mouse::Meta::Role")
ABCresult
0XX0
10X0
1100
1111
123 defined &Mouse::Util::find_meta
124 and $meta = Mouse::Util::find_meta($base)
125 and $meta->isa('Mouse::Meta::Role')
126 ) {
127 0 return 'Mouse::Role';
128 }
129 ▶ elsif (
Condition33%2/6
defined &Mouse::Util::find_meta and $meta = Mouse::Util::find_meta($base) and $meta->isa("Mouse::Meta::Class")
!ll&&!rl&&r
800
defined &Mouse::Util::find_meta and $meta = Mouse::Util::find_meta($base)
!ll&&!rl&&r
800
MC/DC0%0/3
defined &Mouse::Util::find_meta and $meta = Mouse::Util::find_meta($base) and $meta->isa("Mouse::Meta::Class")
defined &Mouse::Util::find_meta $meta = Mouse::Util::find_meta($base) $meta->isa("Mouse::Meta::Class")
Truth table25%1/4
A and B and C
A= defined &Mouse::Util::find_metaB= $meta = Mouse::Util::find_meta($base)C= $meta->isa("Mouse::Meta::Class")
ABCresult
0XX0
10X0
1100
1111
130 defined &Mouse::Util::find_meta
131 and $meta = Mouse::Util::find_meta($base)
132 and $meta->isa('Mouse::Meta::Class')
133 ) {
134 0 return 'Mouse';
135 }
136 ▶ elsif (
Condition66%2/3
$INC{"Role/Tiny.pm"} and "Role::Tiny"->is_role($base)
!ll&&!rl&&r
071
MC/DC50%1/2
$INC{"Role/Tiny.pm"} and "Role::Tiny"->is_role($base)
$INC{"Role/Tiny.pm"} "Role::Tiny"->is_role($base)
Truth table66%2/3
A and B
A= $INC{"Role/Tiny.pm"}B= "Role::Tiny"->is_role($base)
ABresult
0X0
100
111
137 $INC{'Role/Tiny.pm'}
138 and Role::Tiny->is_role($base)
139 ) {
140 1 return 'Role::Tiny';
141 }
142 else {
143 7 local $@;
144 0 eval { _require($_) }
145 7 ▶ for grep !($INC{'Role/Tiny.pm'} && Role::Tiny->is_role($_)), @roles;
Condition33%1/3
$INC{'Role/Tiny.pm'} && 'Role::Tiny'->is_role($_)
!ll&&!rl&&r
007
MC/DC0%0/2
$INC{'Role/Tiny.pm'} && 'Role::Tiny'->is_role($_)
$INC{'Role/Tiny.pm'} 'Role::Tiny'->is_role($_)
Truth table33%1/3
A && B
A= $INC{'Role/Tiny.pm'}B= 'Role::Tiny'->is_role($_)
ABresult
0X0
100
111
146 7 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if ($INC{'Role/Tiny.pm'} and not grep((!'Role::Tiny'->is_role($_)), @roles)) { } 7 0 7
Condition33%1/3
$INC{"Role/Tiny.pm"} and not grep((!"Role::Tiny"->is_role($_)), @roles)
!ll&&!rl&&r
007
MC/DC0%0/2
$INC{"Role/Tiny.pm"} and not grep((!"Role::Tiny"->is_role($_)), @roles)
$INC{"Role/Tiny.pm"} not grep((!"Role::Tiny"->is_role($_)), @roles)
Truth table33%1/3
A and not(B)
A= $INC{"Role/Tiny.pm"}B= not grep((!"Role::Tiny"->is_role($_)), @roles)
ABresult
0X0
100
111
147 $INC{'Role/Tiny.pm'}
148 and !grep !Role::Tiny->is_role($_), @roles
149 ) {
150 7 return 'Role::Tiny::With';
151 }
152 else {
153 0 return undef;
154 }
155 }
156 }
157  
158 7 my %BASE;
159 14 ▶sub with::roles {
Subroutine roles: called CC=11 SCAR=24.0
160 14 my ($self, @roles) = @_;
161 14 ▶ return $self
Branch: 1 branch
BranchTrueFalseTotal
unless @roles 0 14 14
162 if !@roles;
163  
164 14 ▶ my $base = ref $self || $self;
Condition66%2/3
ref $self || $self
l!l&&r!l&&!r
2120
MC/DC0%0/2
ref $self || $self
ref $self $self
Truth table66%2/3
A || B
A= ref $selfB= $self
ABresult
1X1
011
000
165  
166 14 ▶ my ($orig_base, @base_roles) = @{ $BASE{$base} || [$base] };
Condition100%2/2
$BASE{$base} || [$base]
l!l
113
MC/DC100%1/1
$BASE{$base} || [$base]
$BASE{$base}
Truth table100%2/2
A
A= $BASE{$base}
Aresult
11
00
167  
168 14 ▶ my $role_base = $self->can('ROLE_BASE') ? $self->ROLE_BASE : $orig_base.'::Role';
Branch: 1 branch
BranchTrueFalseTotal
$self->can('ROLE_BASE') ? : 1 13 14
169  
170 14 s/\A\+/${role_base}::/ for @roles;
171  
172 14 my @all_roles = (@base_roles, [ @roles ]);
173  
174 14 my $new = _composite_name($orig_base, [ $role_base ], @all_roles);
175  
176 14 ▶ if (!exists $BASE{$new}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $BASE{$new}) 12 2 14
177 12 ▶ my $type = _detect_type($base, @roles)
Branch: 1 branch
BranchTrueFalseTotal
my $type = _detect_type($base, @roles) or croak("Can't determine class or role type of $base or @roles!") 12 0 12
178 or croak "Can't determine class or role type of $base or @roles!";
179  
180 12 my @ops;
181  
182 12 ▶ if ($type eq 'Role::Tiny::With') {
Branch: 2 branches
BranchTrueFalseTotal
if ($type eq 'Role::Tiny::With') { } 7 5 12
elsif ($type =~ /Role/) { } 2 3 5
183 7 push @ops, __PACKAGE__.'::_extends', [ $base ];
184 }
185 elsif ($type =~ /Role/) {
186 2 push @ops, with => [ $base ];
187 }
188 else {
189 3 push @ops, extends => [ $base ];
190 3 push @ops, __PACKAGE__.'::_copy_mro' => [ $base ];
191 }
192  
193 12 push @ops, with => [ @roles ];
194  
195 12 _gen($new, $type, @ops);
196 }
197  
198 14 $BASE{$new} = [$orig_base, @all_roles];
199  
200 14 ▶ if (ref $self) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self) 2 12 14
201 # using $_[0] rather than $self, to work around how overload magic is
202 # applied on perl 5.8
203 2 return bless $_[0], $new;
204 }
205  
206 12 return $new;
207 }
208  
209 7 1;
210 __END__
↑ Summary