Traits/Util.pm

filter: Statementstmt 96.8% 62 / 64 Branchbran 80.5% 29 / 36 Conditioncond 100% 3 / 3 MC/DCmcdc 100% 2 / 2 Subroutinesub 100% 11 / 11 Podpod 100% 3 / 3 totaltotal 92.4% 110 / 119 CC 20 SCAR 14.7
CC
20
Cov
91%
__ANON__
7.1
resolve_traits
6.0
__ANON__
4.0
CRAP 4.3
↑ Summary Traits.pm »
1 8 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 8 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 8 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 0 ▶BEGIN { if ($] < 5.010000) { require UNIVERSAL::DOES } };
Subroutine BEGIN: called CC=2 SCAR=9.2
Branch: 1 branch
BranchTrueFalseTotal
if ($] < "5.01") 0 8 8
6  
7 package MooX::Traits::Util;
8  
9 8 our $AUTHORITY = 'cpan:TOBYINK';
10 8 our $VERSION = '0.005';
11  
12 8 ▶use Exporter::Shiny qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
13 new_class_with_traits
14 new_class_with_traits_one_by_one
15 8 );
16  
17 8 my @keepsies;
18 my $parameterize_role = sub
19 8 ▶{
Subroutine __ANON__: called CC=7 SCAR=19.6
20 8 my ($class, $trait, $params) = @_;
21 8 ▶ return $trait unless @_ == 3;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ == 3 0 8 8
22
23 8 require Module::Runtime;
24 8 Module::Runtime::use_package_optimistically($trait);
25
26 8 ▶ if ( $INC{'MooseX/Role/Parameterized.pm'} )
Branch: 1 branch
BranchTrueFalseTotal
if ($INC{"MooseX/Role/Parameterized.pm"}) 4 4 8
27 {
28 4 require Moose::Util;
29 4 my $meta = Moose::Util::find_meta($trait);
30 4 ▶ if ($meta->can('generate_role'))
Branch: 1 branch
BranchTrueFalseTotal
if ($meta->can("generate_role")) 3 1 4
31 {
32 3 my $generated = $meta->generate_role(parameters => $params);
33 3 push @keepsies, $generated; # prevent cleanup
34 3 return $generated->name;
35 }
36 }
37
38 5 ▶ if ( $trait->can("make_variant") )
Branch: 1 branch
BranchTrueFalseTotal
if ($trait->can("make_variant")) 3 2 5
39 {
40 3 require Package::Variant;
41 3 ▶ return "Package::Variant"->build_variant_of(
Branch: 2 branches
BranchTrueFalseTotal
ref $params eq 'ARRAY' ? : 0 3 3
ref $params eq 'HASH' ? : 3 0 3
42 $trait,
43 ref($params) eq q(ARRAY) ? @$params :
44 ref($params) eq q(HASH) ? %$params :
45 $$params,
46 );
47 }
48
49 2 return $trait;
50 8 };
51  
52 my $looks_like_params = sub
53 43 ▶{
Subroutine __ANON__: called CC=3 SCAR=11.0
54 43 my $thing = $_[0];
55 43 ▶ return !!0 if not ref $thing;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $thing 35 8 43
56 8 require Scalar::Util;
57 8 ▶ return !!0 if Scalar::Util::blessed($thing);
Branch: 1 branch
BranchTrueFalseTotal
if builtin::blessed($thing) 0 8 8
58 8 return !!1;
59 8 };
60  
61 sub resolve_traits
62 33 ▶{
Subroutine resolve_traits: called CC=6 SCAR=17.9
Pod documented
63 33 my ($class, @args) = @_;
64
65 33 ▶ my $ns = $class->DOES('MooX::Traits') ? $class->_trait_namespace : undef;
Branch: 1 branch
BranchTrueFalseTotal
$class->DOES('MooX::Traits') ? : 27 6 33
66 33 ▶ $ns = defined($ns) ? "$ns\::" : "";
Branch: 1 branch
BranchTrueFalseTotal
defined $ns ? : 10 23 33
67
68 33 my @traits;
69 33 ▶ while (@args)
Branch: 1 branch
BranchTrueFalseTotal
if (@args) 41 33 74
70 {
71 41 my $trait = shift(@args);
72 41 ▶ $trait = $trait =~ /\A\+(.+)\z/ ? $1 : "$ns$trait";
Branch: 1 branch
BranchTrueFalseTotal
$trait =~ /\A\+(.+)\z/ ? : 3 38 41
73
74 41 ▶ push @traits => (
Branch: 1 branch
BranchTrueFalseTotal
&$looks_like_params($args[0]) ? : 8 33 41
75 $looks_like_params->($args[0])
76 ? $parameterize_role->($class, $trait, shift(@args))
77 : $trait
78 );
79 }
80 33 return @traits;
81 }
82  
83 my $toolage = sub
84 33 ▶{
Subroutine __ANON__: called CC=4 SCAR=13.9
85 33 my $class = shift;
86
87 33 ▶ if ($INC{"Moo.pm"} and $Moo::MAKERS{$class}{is_class})
Branch: 1 branch
BranchTrueFalseTotal
if ($INC{"Moo.pm"} and $Moo::MAKERS{$class}{"is_class"}) 23 10 33
Condition100%3/3
$INC{"Moo.pm"} and $Moo::MAKERS{$class}{"is_class"}
!ll&&!rl&&r
8223
MC/DC100%2/2
$INC{"Moo.pm"} and $Moo::MAKERS{$class}{"is_class"}
$INC{"Moo.pm"} $Moo::MAKERS{$class}{"is_class"}
Truth table100%3/3
A and B
A= $INC{"Moo.pm"}B= $Moo::MAKERS{$class}{"is_class"}
ABresult
0X0
100
111
88 {
89 23 require Moo::Role;
90 23 return "Moo::Role";
91 }
92
93 10 ▶ if ($INC{"Moo/Role.pm"})
Branch: 1 branch
BranchTrueFalseTotal
if ($INC{"Moo/Role.pm"}) 2 8 10
94 {
95 2 return "Moo::Role";
96 }
97
98 8 "Role::Tiny";
99 8 };
100  
101 sub new_class_with_traits
102 33 ▶{
Subroutine new_class_with_traits: called CC=1 SCAR=0.0
Pod documented
103 33 my ($class, @traits) = @_;
104 33 $class->$toolage->create_class_with_roles(
105 $class,
106 resolve_traits($class, @traits),
107 );
108 }
109  
110 sub new_class_with_traits_one_by_one
111 2 ▶{
Subroutine new_class_with_traits_one_by_one: called CC=3 SCAR=11.8
Pod documented
112 2 my ($class, @traits) = @_;
113 2 ▶ while (@traits)
Branch: 1 branch
BranchTrueFalseTotal
if (@traits) 2 0 2
114 {
115 2 my @trait = shift(@traits);
116 2 ▶ push @trait, shift(@traits)
Branch: 1 branch
BranchTrueFalseTotal
if &$looks_like_params($traits[0]) 0 2 2
117 if $looks_like_params->($traits[0]);
118 2 $class = new_class_with_traits($class, @trait);
119 }
120 0 return $class;
121 }
122  
123 8 1;
124  
125 __END__
↑ Summary Traits.pm »