blib/lib/Mooish/Base.pm

filter: Statementstmt 84.8% 73 / 86 Branchbran 71.4% 20 / 28 Conditioncond 47.6% 10 / 21 MC/DCmcdc 20.0% 2 / 10 Subroutinesub 100% 12 / 12 Podpod n/a totaltotal 74.5% 117 / 157 CC 24 SCAR 22.1
CC
24
Cov
76%
import
17.1
_uses_extra_module
6.9
BEGIN
3.0
CRAP 9.1
↑ Summary
1 package Mooish::Base;
2 5 $Mooish::Base::VERSION = '1.005';
3 9 ▶use v5.10;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 9 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 9 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 9 ▶use Import::Into;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 5 require Mooish::AttributeBuilder;
9 5 require Type::Tiny;
10 5 require namespace::autoclean;
11  
12 9 ▶use constant FLAVOUR => $ENV{MOOISH_BASE_FLAVOUR} // 'Moo';
Subroutine BEGIN: called CC=2 SCAR=6.9
Condition100%2/2
$ENV{"MOOISH_BASE_FLAVOUR"} // "Moo"
l!l
45
MC/DC100%1/1
$ENV{"MOOISH_BASE_FLAVOUR"} // "Moo"
$ENV{"MOOISH_BASE_FLAVOUR"}
Truth table100%2/2
A
A= $ENV{"MOOISH_BASE_FLAVOUR"}
Aresult
11
00
13 9 ▶use constant ROLE_FLAVOUR => $ENV{MOOISH_BASE_ROLE_FLAVOUR} // (FLAVOUR . '::Role');
Subroutine BEGIN: called CC=2 SCAR=7.1
Condition50%1/2
$ENV{"MOOISH_BASE_ROLE_FLAVOUR"} // "Moo::Role"
l!l
09
MC/DC0%0/1
$ENV{"MOOISH_BASE_ROLE_FLAVOUR"} // "Moo::Role"
$ENV{"MOOISH_BASE_ROLE_FLAVOUR"}
Truth table50%1/2
A
A= $ENV{"MOOISH_BASE_ROLE_FLAVOUR"}
Aresult
11
00
14  
15 9 ▶use constant EXTRA_MODULES => $ENV{MOOISH_BASE_EXTRA_MODULES} // join ';', qw(
Subroutine BEGIN: called CC=2 SCAR=6.9
Condition100%2/2
$ENV{"MOOISH_BASE_EXTRA_MODULES"} // "Hook::AfterRuntime;MooX::TypeTiny;MooseX::XSAccessor"
l!l
81
MC/DC100%1/1
$ENV{"MOOISH_BASE_EXTRA_MODULES"} // "Hook::AfterRuntime;MooX::TypeTiny;MooseX::XSAccessor"
$ENV{"MOOISH_BASE_EXTRA_MODULES"}
Truth table100%2/2
A
A= $ENV{"MOOISH_BASE_EXTRA_MODULES"}
Aresult
11
00
16 Hook::AfterRuntime
17 MooX::TypeTiny
18 MooseX::XSAccessor
19 9 );
20  
21 9 ▶use constant EXTRA_MODULES_AVAILABLE => {
Subroutine BEGIN: called CC=1 SCAR=1.6
22 0 'Hook::AfterRuntime' => !!eval { require Hook::AfterRuntime; Hook::AfterRuntime->VERSION('0.003'); 1 },
23 0 'MooX::TypeTiny' => !!eval { require MooX::TypeTiny; MooX::TypeTiny->VERSION('0.002002'); 1 },
24 0 'MooX::XSConstructor' => !!eval { require MooX::XSConstructor; MooX::XSConstructor->VERSION('0.003002'); 1 },
25 'MooseX::XSConstructor' => !!
26 0 eval { require MooseX::XSConstructor; MooseX::XSConstructor->VERSION('0.001002'); 1 },
27 0 'MooseX::XSAccessor' => !!eval { require MooseX::XSAccessor; MooseX::XSAccessor->VERSION('0.010'); 1 },
28 9 };
29  
30 9 ▶use constant EXTRA_MODULES_RULES => {
Subroutine BEGIN: called CC=1 SCAR=0.0
31 'Hook::AfterRuntime' => {type => 'Moose'},
32 'MooX::TypeTiny' => {type => 'Moo'},
33 'MooX::XSConstructor' => {type => 'Moo'},
34 'MooseX::XSConstructor' => {type => 'Moose'},
35 'MooseX::XSAccessor' => {type => 'Moose', role => !!1},
36 9 };
37  
38 9 ▶BEGIN {
Subroutine BEGIN: called CC=3 SCAR=11.1
39 9 ▶ eval 'require ' . FLAVOUR or die $@;
Branch: 1 branch
BranchTrueFalseTotal
eval "require Moo" or die $@ 5 4 9
40 5 ▶ eval 'require ' . ROLE_FLAVOUR or die $@;
Branch: 1 branch
BranchTrueFalseTotal
eval "require Moo::Role" or die $@ 5 0 5
41 }
42  
43 5 our $DEBUG;
44  
45 sub _uses_extra_module
46 30 ▶{
Subroutine _uses_extra_module: called CC=5 SCAR=19.3
47 30 my ($module, $class_type, $role) = @_;
48  
49 state $wanted_modules = {
50 5 map { $_ => 1 }
51 30 map { s{^\s+}{}; s{\s+$}{}; $_ }
52 split ';', EXTRA_MODULES
53 };
54  
55 return $wanted_modules->{$module}
56 && EXTRA_MODULES_AVAILABLE->{$module}
57 && EXTRA_MODULES_RULES->{$module}{type} eq $class_type
58 && (!$role || EXTRA_MODULES_RULES->{$module}{role})
59 30 ▶ ;
Condition33%4/12
$wanted_modules->{$module} && {Hook::AfterRuntime => 0, MooX::TypeTiny => 0, MooX::XSConstructor => 0, MooseX::XSAccessor => 0, MooseX::XSConstructor => 0}->{$module} && {Hook::AfterRuntime => {type => 'Moose'}, MooX::TypeTiny => {type => 'Moo'}, MooX::XSConstructor => {type => 'Moo'}, MooseX::XSAccessor => {role => 1, type => 'Moose'}, MooseX::XSConstructor => {type => 'Moose'}}->{$module}{'type'} eq $class_type && (!$role || {Hook::AfterRuntime => {type => 'Moose'}, MooX::TypeTiny => {type => 'Moo'}, MooX::XSConstructor => {type => 'Moo'}, MooseX::XSAccessor => {role => 1, type => 'Moose'}, MooseX::XSConstructor => {type => 'Moose'}}->{$module}{'role'})
!ll&&!rl&&r
3000
$wanted_modules->{$module} and {"Hook::AfterRuntime" => !!0,"MooX::TypeTiny" => !!0,"MooX::XSConstructor" => !!0,"MooseX::XSAccessor" => !!0,"MooseX::XSConstructor" => !!0}->{$module} and {"Hook::AfterRuntime" => {"type" => "Moose"},"MooX::TypeTiny" => {"type" => "Moo"},"MooX::XSConstructor" => {"type" => "Moo"},"MooseX::XSAccessor" => {"role" => !!1,"type" => "Moose"},"MooseX::XSConstructor" => {"type" => "Moose"}}->{$module}{"type"} eq $class_type
!ll&&!rl&&r
3000
$wanted_modules->{$module} and {"Hook::AfterRuntime" => !!0,"MooX::TypeTiny" => !!0,"MooX::XSConstructor" => !!0,"MooseX::XSAccessor" => !!0,"MooseX::XSConstructor" => !!0}->{$module}
!ll&&!rl&&r
14160
not $role or {"Hook::AfterRuntime" => {"type" => "Moose"},"MooX::TypeTiny" => {"type" => "Moo"},"MooX::XSConstructor" => {"type" => "Moo"},"MooseX::XSAccessor" => {"role" => !!1,"type" => "Moose"},"MooseX::XSConstructor" => {"type" => "Moose"}}->{$module}{"role"}
l!l&&r!l&&!r
000
MC/DC0%0/5
$wanted_modules->{$module} && {Hook::AfterRuntime => 0, MooX::TypeTiny => 0, MooX::XSConstructor => 0, MooseX::XSAccessor => 0, MooseX::XSConstructor => 0}->{$module} && {Hook::AfterRuntime => {type => 'Moose'}, MooX::TypeTiny => {type => 'Moo'}, MooX::XSConstructor => {type => 'Moo'}, MooseX::XSAccessor => {role => 1, type => 'Moose'}, MooseX::XSConstructor => {type => 'Moose'}}->{$module}{'type'} eq $class_type && (!$role || {Hook::AfterRuntime => {type => 'Moose'}, MooX::TypeTiny => {type => 'Moo'}, MooX::XSConstructor => {type => 'Moo'}, MooseX::XSAccessor => {role => 1, type => 'Moose'}, MooseX::XSConstructor => {type => 'Moose'}}->{$module}{'role'})
$wanted_modules->{$module} {"Hook::AfterRuntime" => !!0,"MooX::TypeTiny" => !!0,"MooX::XSConstructor" => !!0,"MooseX::XSAccessor" => !!0,"MooseX::XSConstructor" => !!0}->{$module} {"Hook::AfterRuntime" => {"type" => "Moose"},"MooX::TypeTiny" => {"type" => "Moo"},"MooX::XSConstructor" => {"type" => "Moo"},"MooseX::XSAccessor" => {"role" => !!1,"type" => "Moose"},"MooseX::XSConstructor" => {"type" => "Moose"}}->{$module}{"type"} eq $class_type not $role {"Hook::AfterRuntime" => {"type" => "Moose"},"MooX::TypeTiny" => {"type" => "Moo"},"MooX::XSConstructor" => {"type" => "Moo"},"MooseX::XSAccessor" => {"role" => !!1,"type" => "Moose"},"MooseX::XSConstructor" => {"type" => "Moose"}}->{$module}{"role"}
Truth table33%2/6
A and B and C && not(D) or E
A= $wanted_modules->{$module}B= {"Hook::AfterRuntime" => !!0,"MooX::TypeTiny" => !!0,"MooX::XSConstructor" => !!0,"MooseX::XSAccessor" => !!0,"MooseX::XSConstructor" => !!0}->{$module}C= {"Hook::AfterRuntime" => {"type" => "Moose"},"MooX::TypeTiny" => {"type" => "Moo"},"MooX::XSConstructor" => {"type" => "Moo"},"MooseX::XSAccessor" => {"role" => !!1,"type" => "Moose"},"MooseX::XSConstructor" => {"type" => "Moose"}}->{$module}{"type"} eq $class_typeD= not $roleE= {"Hook::AfterRuntime" => {"type" => "Moose"},"MooX::TypeTiny" => {"type" => "Moo"},"MooX::XSConstructor" => {"type" => "Moo"},"MooseX::XSAccessor" => {"role" => !!1,"type" => "Moose"},"MooseX::XSConstructor" => {"type" => "Moose"}}->{$module}{"role"}
ABCDEresult
0XXXX0
10XXX0
110XX0
111000
1111X1
111011
60 }
61  
62 sub import
63 6 ▶{
Subroutine import: called CC=15 SCAR=28.4
64 6 my $me = shift;
65 6 my $pkg = caller;
66  
67 6 my $class_type = FLAVOUR;
68 6 my $role_type = ROLE_FLAVOUR;
69 6 my $role = !!0;
70 6 my $standard = !!0;
71  
72 6 foreach my $arg (@_) {
73 5 ▶ if ($arg eq -role) {
Branch: 2 branches
BranchTrueFalseTotal
if ($arg eq '-role') { } 3 2 5
elsif ($arg eq '-standard') { } 2 0 2
74 3 $role = !!1;
75 }
76 elsif ($arg eq -standard) {
77 2 $standard = !!1;
78 }
79 }
80  
81 # Moo is the best choice for module development
82 6 ▶ if ($standard) {
Branch: 1 branch
BranchTrueFalseTotal
if ($standard) 2 4 6
83 2 $class_type = 'Moo';
84 2 $role_type = 'Moo::Role';
85 }
86  
87 6 ▶ my $engine = $role ? $role_type : $class_type;
Branch: 1 branch
BranchTrueFalseTotal
$role ? : 3 3 6
88 6 $engine->import::into($pkg);
89 6 ▶ Mooish::AttributeBuilder->import::into($pkg, ($standard ? (-standard) : ()));
Branch: 1 branch
BranchTrueFalseTotal
$standard ? : 2 4 6
90 6 Types::Common->import::into($pkg, -types);
91 6 namespace::autoclean->import(-cleanee => $pkg);
92  
93 # install extra modules
94  
95 30 my %extra_modules = map { $_ => _uses_extra_module($_, $class_type, $role) }
96 6 keys %{(EXTRA_MODULES_RULES)};
97  
98 MooX::TypeTiny->import::into($pkg)
99 6 ▶ if $extra_modules{'MooX::TypeTiny'};
Branch: 1 branch
BranchTrueFalseTotal
if $extra_modules{"MooX::TypeTiny"} 0 6 6
100  
101 MooX::XSConstructor->import::into($pkg)
102 6 ▶ if $extra_modules{'MooX::XSConstructor'};
Branch: 1 branch
BranchTrueFalseTotal
if $extra_modules{"MooX::XSConstructor"} 0 6 6
103  
104 MooseX::XSConstructor->import::into($pkg)
105 6 ▶ if $extra_modules{'MooseX::XSConstructor'};
Branch: 1 branch
BranchTrueFalseTotal
if $extra_modules{"MooseX::XSConstructor"} 0 6 6
106  
107 MooseX::XSAccessor->import::into($pkg)
108 6 ▶ if $extra_modules{'MooseX::XSAccessor'};
Branch: 1 branch
BranchTrueFalseTotal
if $extra_modules{"MooseX::XSAccessor"} 0 6 6
109  
110 # special handling for Hook::AfterRuntime - warn if it can't be used on Moose
111 6 ▶ if ($extra_modules{'Hook::AfterRuntime'}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($extra_modules{'Hook::AfterRuntime'}) { } 0 6 6
elsif ($class_type eq 'Moose' and not $role) { } 0 12 12
112 0 Hook::AfterRuntime::after_runtime { $pkg->meta->make_immutable };
113 }
114 ▶ elsif ($class_type eq 'Moose' && !$role) {
Condition33%1/3
$class_type eq "Moose" and not $role
!ll&&!rl&&r
600
MC/DC0%0/2
$class_type eq "Moose" and not $role
$class_type eq "Moose" not $role
Truth table33%1/3
A and not(B)
A= $class_type eq "Moose"B= not $role
ABresult
0X0
100
111
115 0 warn "Mooish::Base can't make $pkg Moose class immutable - please install Hook::AfterRuntime module";
116 }
117  
118 # put debug information if requested
119 6 ▶ $DEBUG->{$pkg} = {
Branch: 1 branch
BranchTrueFalseTotal
if $Mooish::Base::DEBUG 2 4 6
120 class_type => $class_type,
121 role_type => $role_type,
122 role => $role,
123 standard => $standard,
124 extra_modules => \%extra_modules,
125 } if $DEBUG;
126 }
127  
128 5 1;
129  
130 __END__
↑ Summary