lib/Mite/Package.pm

filter: Statementstmt 96.7% 119 / 123 Branchbran 72.2% 26 / 36 Conditioncond 50.0% 2 / 4 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 96.0% 24 / 25 Podpod 0.0% 0 / 7 totaltotal 86.8% 171 / 197 CC 24 SCAR 12.2
CC
24
Cov
90%
inject_mite_functions
8.0
__ANON__
4.8
autolax
4.1
CRAP 3.4
« lib/Mite/Trait/HasMethods.pm ↑ Summary lib/Mite/Trait/HasSuperclasses.pm »
1 117 ▶use 5.010001;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 117 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 117 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 package Mite::Package;
6 117 ▶use Mite::Miteception -all;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 117 our $AUTHORITY = 'cpan:TOBYINK';
9 117 our $VERSION = '0.013000';
10  
11 117 has name =>
12 is => ro,
13 isa => ValidClassName,
14 required => true;
15  
16 has shim_name =>
17 is => rw,
18 isa => ValidClassName,
19 lazy => true,
20 9 ▶ builder => sub {
Subroutine __ANON__: called CC=2 SCAR=7.1
21 9 my $self = shift;
22 9 ▶ eval { $self->project->config->data->{shim} } // 'Mite::Shim'
Condition50%1/2
eval { do { $self->project->config->data->{"shim"} } } // "Mite::Shim"
l!l
09
MC/DC0%0/1
eval { do { $self->project->config->data->{"shim"} } } // "Mite::Shim"
eval { do { $self->project->config->data->{"shim"} } }
Truth table50%1/2
A
A= eval { do { $self->project->config->data->{"shim"} } }
Aresult
11
00
23 117 };
24  
25 117 has source =>
26 is => rw,
27 isa => MiteSource,
28 # avoid a circular dep with Mite::Source
29 weak_ref => true;
30  
31 has imported_functions =>
32 is => ro,
33 isa => Map[ MethodName, Str ],
34 158 ▶ builder => sub { {} };
Subroutine __ANON__: called CC=1 SCAR=0.0
35  
36 has imported_keywords =>
37 is => ro,
38 isa => Map[ MethodName, Str ],
39 158 ▶ builder => sub { {} };
Subroutine __ANON__: called CC=1 SCAR=0.0
40  
41 117 has arg =>
42 is => rw,
43 default => {};
44  
45 0 ▶sub kind { 'package' }
Subroutine kind: not called CC=1 SCAR=6.9
Pod undocumented
46  
47 158 ▶sub BUILD {
Subroutine BUILD: called CC=2 SCAR=6.9
Pod undocumented
48 158 my $self = shift;
49  
50 158 require Type::Registry;
51 158 my $reg = 'Type::Registry'->for_class( $self->name );
52 158 $reg->add_types( 'Types::Standard' );
53 158 $reg->add_types( 'Types::Common::Numeric' );
54 158 $reg->add_types( 'Types::Common::String' );
55  
56 158 my $library = eval { $self->project->config->data->{types} };
57 158 ▶ $reg->add_types( $library ) if $library;
Branch: 1 branch
BranchTrueFalseTotal
if $library 0 158 158
58 }
59  
60 1131 ▶sub project {
Subroutine project: called CC=1 SCAR=0.0
Pod undocumented
61 1131 my $self = shift;
62  
63 1131 return $self->source->project;
64 }
65  
66 119 ▶sub inject_mite_functions {
Subroutine inject_mite_functions: called CC=8 SCAR=20.8
Pod undocumented
67 119 my ( $self, $file, $arg ) = ( shift, @_ );
68  
69 119 ▶ my $requested = sub { $arg->{$_[0]} ? 1 : $arg->{'!'.$_[0]} ? 0 : $arg->{'-all'} ? 1 : $_[1]; };
Subroutine __ANON__: called CC=4 SCAR=13.9
Branch: 3 branches
BranchTrueFalseTotal
$$arg{$_[0]} ? : 4 2019 2023
$$arg{'!' . $_[0]} ? : 0 2019 2019
$arg->{'-all'} ? : 170 1849 2019
70 119 my $shim = $self->shim_name;
71 119 my $package = $self->name;
72 119 my $ctxt = $shim->can( '_definition_context' );
73  
74 117 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
75 119 ${ $package .'::USES_MITE' } = ref( $self );
76 119 ${ $package .'::MITE_SHIM' } = $shim;
77  
78 119 my $want_bool = $requested->( '-bool', 0 );
79 119 my $want_is = $requested->( '-is', 0 );
80 119 for my $f ( qw/ true false / ) {
81 238 ▶ next unless $requested->( $f, $want_bool );
Branch: 1 branch
BranchTrueFalseTotal
unless &$requested($f, $want_bool) 214 24 238
82 24 *{"$package\::$f"} = \&{"$shim\::$f"};
83 24 $self->imported_functions->{$f} = "$shim\::$f";
84 }
85 119 for my $f ( qw/ ro rw rwp lazy bare / ) {
86 595 ▶ next unless $requested->( $f, $want_is );
Branch: 1 branch
BranchTrueFalseTotal
unless &$requested($f, $want_is) 540 55 595
87 55 *{"$package\::$f"} = \&{"$shim\::$f"};
88 55 $self->imported_functions->{$f} = "$shim\::$f";
89 }
90 119 for my $f ( qw/ carp croak confess guard STRICT lock unlock / ) {
91 833 ▶ next unless $requested->( $f, false );
Branch: 1 branch
BranchTrueFalseTotal
unless 762 71 833
92 71 *{"$package\::$f"} = \&{"$shim\::$f"};
93 71 $self->imported_functions->{$f} = "$shim\::$f";
94 }
95 119 ▶ if ( $requested->( blessed => false ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if () 10 109 119
96 10 require Scalar::Util;
97 10 *{"$package\::blessed"} = \&Scalar::Util::blessed;
98 10 $self->imported_functions->{blessed} = "Scalar::Util::blessed";
99 }
100 }
101  
102 210 ▶sub autolax {
Subroutine autolax: called CC=3 SCAR=14.2
Pod undocumented
103 210 my $self = shift;
104  
105 return undef
106 210 ▶ if not eval { $self->project->config->data->{autolax} };
Branch: 1 branch
BranchTrueFalseTotal
unless eval { do { $self->project->config->data->{"autolax"} } } 210 0 210
107  
108 return $self->imported_functions->{STRICT}
109 ? 'STRICT'
110 0 ▶ : sprintf( '%s::STRICT', $self->project->config->data->{shim} );
Branch: 1 branch
BranchTrueFalseTotal
$self->imported_functions->{'STRICT'} ? : 0 0 0
111 }
112  
113 117 for my $function ( qw/ carp croak confess / ) {
114 117 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
115 351 ▶ *{"_function_for_$function"} = sub {
Subroutine __ANON__: called CC=4 SCAR=15.6
116 295 my $self = shift;
117 return $function
118 295 ▶ if $self->imported_functions->{$function};
Branch: 1 branch
BranchTrueFalseTotal
if $self->imported_functions->{$function} 62 233 295
119 233 ▶ return sprintf '%s::%s', $self->shim_name, $function
Branch: 1 branch
BranchTrueFalseTotal
if $self->shim_name 233 0 233
120 if $self->shim_name;
121 0 ▶ $function eq 'carp' ? 'warn sprintf' : 'die sprintf';
Branch: 1 branch
BranchTrueFalseTotal
$function eq 'carp' ? : 0 0 0
122 351 };
123 }
124  
125 128 ▶sub compile {
Subroutine compile: called CC=1 SCAR=0.0
Pod undocumented
126 128 my $self = shift;
127  
128 128 my $code = join "\n",
129 '{',
130 map( $self->$_, $self->compilation_stages ),
131 '1;',
132 '}';
133  
134 #::diag $code if main->can('diag');
135 128 return $code;
136 }
137  
138 294 ▶sub _compile_meta {
Subroutine _compile_meta: called CC=1 SCAR=0.0
139 294 my ( $self, $classvar, $selfvar, $argvar, $metavar ) = @_;
140 294 return sprintf '( $Mite::META{%s} ||= %s->__META__ )',
141 $classvar, $classvar;
142 }
143  
144 128 ▶sub compilation_stages {
Subroutine compilation_stages: called CC=1 SCAR=0.0
Pod undocumented
145 128 return qw(
146 _compile_package
147 _compile_pragmas
148 _compile_uses_mite
149 _compile_imported_keywords
150 _compile_imported_functions
151 _compile_meta_method
152 );
153 }
154  
155 128 ▶sub _compile_package {
Subroutine _compile_package: called CC=1 SCAR=0.0
156 128 my $self = shift;
157  
158 128 return "package @{[ $self->name ]};";
159 }
160  
161 128 ▶sub _compile_pragmas {
Subroutine _compile_pragmas: called CC=1 SCAR=0.0
162 128 my $self = shift;
163  
164 128 return <<'CODE';
165 use strict;
166 use warnings;
167 no warnings qw( once void );
168 CODE
169 }
170  
171 128 ▶sub _compile_uses_mite {
Subroutine _compile_uses_mite: called CC=2 SCAR=7.0
172 128 my $self = shift;
173  
174 128 my @code = sprintf 'our $USES_MITE = %s;', B::perlstring( ref($self) );
175 128 ▶ if ( $self->shim_name ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->shim_name) 128 0 128
176 128 push @code, sprintf 'our $MITE_SHIM = %s;', B::perlstring( $self->shim_name );
177 }
178 128 push @code, sprintf 'our $MITE_VERSION = %s;', B::perlstring( $self->VERSION );
179 128 join "\n", @code;
180 }
181  
182 128 ▶sub _compile_imported_keywords {
Subroutine _compile_imported_keywords: called CC=3 SCAR=11.0
183 128 my $self = shift;
184  
185 128 ▶ my %func = %{ $self->imported_keywords or {} } or return;
Branch: 1 branch
BranchTrueFalseTotal
my(%func) = %{$self->imported_keywords or {};} or return 119 9 128
Condition50%1/2
$self->imported_keywords || {}
l!l
1280
MC/DC0%0/1
$self->imported_keywords || {}
$self->imported_keywords
Truth table50%1/2
A
A= $self->imported_keywords
Aresult
11
00
186 119 my @keywords = sort keys %func;
187 119 my $keyword_slots = join q{, }, map "*$_", @keywords;
188 119 my $coderefs = join "\n", map " $func{$_},", @keywords;
189  
190 119 return sprintf <<'CODE', B::perlstring( $self->shim_name ), B::perlstring( $self->name ), $keyword_slots, $self->shim_name, $coderefs;
191 # Mite keywords
192 BEGIN {
193 my ( $SHIM, $CALLER ) = ( %s, %s );
194 ( %s ) = do {
195 package %s;
196 no warnings 'redefine';
197 (
198 %s
199 );
200 };
201 };
202 CODE
203 }
204  
205 128 ▶sub _compile_imported_functions {
Subroutine _compile_imported_functions: called CC=3 SCAR=11.0
206 128 my $self = shift;
207 128 ▶ my %func = %{ $self->imported_functions } or return;
Branch: 1 branch
BranchTrueFalseTotal
my(%func) = %{$self->imported_functions;} or return 13 115 128
208  
209 return join "\n",
210 '# Mite imports',
211 'BEGIN {',
212 ( $func{blessed} ? ' require Scalar::Util;' : () ),
213 map(
214 13 ▶ sprintf( ' *%s = \&%s;', $_, $func{$_} ),
Branch: 1 branch
BranchTrueFalseTotal
$func{'blessed'} ? : 10 3 13
215 sort keys %func
216 ),
217 '};',
218 '';
219 }
220  
221 104 ▶sub _compile_meta_method {
Subroutine _compile_meta_method: called CC=2 SCAR=7.2
222 104 my $self = shift;
223  
224 104 my $code = <<'CODE';
225 # Gather metadata for constructor and destructor
226 sub __META__ {
227 no strict 'refs';
228 my $class = shift; $class = ref($class) || $class;
229 my $linear_isa = mro::get_linear_isa( $class );
230 return {
231 BUILD => [
232 map { ( *{$_}{CODE} ) ? ( *{$_}{CODE} ) : () }
233 map { "$_\::BUILD" } reverse @$linear_isa
234 ],
235 DEMOLISH => [
236 map { ( *{$_}{CODE} ) ? ( *{$_}{CODE} ) : () }
237 map { "$_\::DEMOLISH" } @$linear_isa
238 ],
239 HAS_BUILDARGS => $class->can('BUILDARGS'),
240 HAS_FOREIGNBUILDARGS => $class->can('FOREIGNBUILDARGS'),
241 };
242 }
243 CODE
244  
245 104 ▶ if ( eval { $self->project->config->data->{mop} } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (eval { do { $self->project->config->data->{"mop"} } }) 0 104 104
246 0 $code .= sprintf <<'CODE', $self->project->config->data->{mop};
247  
248 # Moose-compatibility method
249 sub meta {
250 require %s;
251 Moose::Util::find_meta( ref $_[0] or $_[0] );
252 }
253 CODE
254 }
255  
256 104 return $code;
257 }
258  
259 117 1;
« lib/Mite/Trait/HasMethods.pm ↑ Summary lib/Mite/Trait/HasSuperclasses.pm »