blib/lib/App/perlimports/Include.pm

filter: Statementstmt 92.2% 308 / 334 Branchbran 80.5% 137 / 170 Conditioncond 78.4% 102 / 130 MC/DCmcdc 62.8% 44 / 70 Subroutinesub 94.5% 35 / 37 Podpod 0.0% 0 / 1 totaltotal 84.3% 626 / 742 CC 143 SCAR 38.2
CC
143
Cov
86%
_build_imports
105.6
_build_formatted_ppi_statement
31.1
_maybe_get_new_include
15.1
CRAP 45.6
« blib/lib/App/perlimports/CLI.pm ↑ Summary blib/lib/App/perlimports/Document.pm »
1 package App::perlimports::Include;
2  
3 107 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 107 our $VERSION = '0.000067';
6  
7 ## no critic (Bangs::ProhibitDebuggingModules)
8  
9 107 ▶use Data::Dumper qw( Dumper );
Subroutine BEGIN: called CC=1 SCAR=0.0
10 107 ▶use List::Util qw( any none uniq );
Subroutine BEGIN: called CC=1 SCAR=0.0
11 107 ▶use Memoize qw( flush_cache memoize );
Subroutine BEGIN: called CC=1 SCAR=0.0
12 107 ▶use MooX::StrictConstructor;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 107 ▶use PPI::Document ();
Subroutine BEGIN: called CC=1 SCAR=0.0
14 107 ▶use PPIx::Utils::Classification qw( is_function_call is_perl_builtin );
Subroutine BEGIN: called CC=1 SCAR=0.0
15 107 ▶use Ref::Util qw( is_plain_arrayref is_plain_hashref );
Subroutine BEGIN: called CC=1 SCAR=0.0
16 107 ▶use Sub::HandlesVia;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 107 ▶use Try::Tiny qw( catch try );
Subroutine BEGIN: called CC=1 SCAR=0.0
18 107 ▶use Types::Standard qw(ArrayRef Bool HashRef InstanceOf Int Maybe Object Str);
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 107 with 'App::perlimports::Role::Logger';
21  
22 107 memoize('is_function_call');
23  
24 160 ▶sub BUILD {
Subroutine BUILD: called CC=1 SCAR=0.0
Pod undocumented
25 160 flush_cache('is_function_call');
26 }
27  
28 # this is a catalog of the @EXPORT's of the target module,
29 # or @EXPORT_OK, or else whatever comes from "use Module;"
30 107 has _explicit_exports => (
31 is => 'ro',
32 isa => HashRef,
33 handles_via => 'Hash',
34 handles => {
35 _delete_export => 'delete',
36 _explicit_export_count => 'count',
37 _has_explicit_exports => 'count',
38 _import_name => 'get',
39 _is_importable => 'exists',
40 },
41 lazy => 1,
42 builder => '_build_explicit_exports',
43 );
44  
45 107 has _document => (
46 is => 'ro',
47 isa => InstanceOf ['App::perlimports::Document'],
48 required => 1,
49 init_arg => 'document',
50 );
51  
52 107 has _export_inspector => (
53 is => 'ro',
54 isa => InstanceOf ['App::perlimports::ExportInspector'],
55 predicate => '_has_export_inspector', # used in test
56 lazy => 1,
57 builder => '_build_export_inspector',
58 );
59  
60 107 has formatted_ppi_statement => (
61 is => 'ro',
62 isa => InstanceOf ['PPI::Statement::Include'],
63 lazy => 1,
64 builder => '_build_formatted_ppi_statement',
65 );
66  
67 107 has _ignored_modules => (
68 is => 'ro',
69 isa => ArrayRef,
70 init_arg => 'ignored_modules',
71 predicate => '_has_ignored_modules',
72 );
73  
74 107 has _imports => (
75 is => 'ro',
76 isa => ArrayRef,
77 lazy => 1,
78 builder => '_build_imports',
79 );
80  
81 107 has _include => (
82 is => 'ro',
83 isa => InstanceOf ['PPI::Statement::Include'],
84 init_arg => 'include',
85 required => 1,
86 );
87  
88 107 has _is_ignored => (
89 is => 'ro',
90 isa => Bool,
91 lazy => 1,
92 builder => '_build_is_ignored',
93 );
94  
95 has _isa_test_builder_module => (
96 is => 'ro',
97 isa => Bool,
98 lazy => 1,
99 98 ▶ default => sub { shift->_export_inspector->isa_test_builder },
Subroutine __ANON__: called CC=1 SCAR=0.0
100 107 );
101  
102 107 has _is_translatable => (
103 is => 'ro',
104 isa => Bool,
105 lazy => 1,
106 builder => '_build_is_translatable',
107 documentation => 'Is this a require which can be converted to a use?',
108 );
109  
110 has module_name => (
111 is => 'ro',
112 isa => Maybe [Str],
113 lazy => 1,
114 146 ▶ default => sub { shift->_include->module },
Subroutine __ANON__: called CC=1 SCAR=0.0
115 107 );
116  
117 107 has _found_imports => (
118 is => 'ro',
119 isa => Maybe [ArrayRef],
120 init_arg => 'found_imports',
121 );
122  
123 107 has _indent => (
124 is => 'ro',
125 isa => Int,
126 init_arg => 'indent',
127 default => 4,
128 );
129  
130 107 has _pad_brackets => (
131 is => 'ro',
132 isa => Bool,
133 init_arg => 'pad_brackets',
134 default => 0,
135 );
136  
137 107 has _pad_imports => (
138 is => 'ro',
139 isa => Bool,
140 init_arg => 'pad_imports',
141 default => 1,
142 );
143  
144 107 has _preserve_require => (
145 is => 'ro',
146 isa => Bool,
147 init_arg => 'preserve_require',
148 default => 1,
149 );
150  
151 107 has _tidy_whitespace => (
152 is => 'ro',
153 isa => Bool,
154 init_arg => 'tidy_whitespace',
155 lazy => 1,
156 default => 1,
157 );
158  
159 has _will_never_export => (
160 is => 'ro',
161 isa => Bool,
162 lazy => 1,
163 142 ▶ default => sub {
Subroutine __ANON__: called CC=2 SCAR=7.1
164 142 my $self = shift;
165 142 ▶ return exists $self->_document->never_exports->{ $self->module_name }
Condition66%2/3
exists $self->_document->never_exports->{$self->module_name} || $self->_export_inspector->is_oo_class
l!l&&r!l&&!r
70135
MC/DC50%1/2
exists $self->_document->never_exports->{$self->module_name} || $self->_export_inspector->is_oo_class
exists $self->_document->never_exports->{$self->module_name} $self->_export_inspector->is_oo_class
Truth table66%2/3
A || B
A= exists $self->_document->never_exports->{$self->module_name}B= $self->_export_inspector->is_oo_class
ABresult
1X1
011
000
166 || $self->_export_inspector->is_oo_class;
167 },
168 107 );
169  
170 138 ▶sub _build_export_inspector {
Subroutine _build_export_inspector: called CC=1 SCAR=0.0
171 138 my $self = shift;
172 138 return $self->_document->inspector_for( $self->module_name );
173 }
174  
175 # If we have implicit (but not explicit) exports, we will make a best guess at
176 # what gets exported by using the implicit list.
177 134 ▶sub _build_explicit_exports {
Subroutine _build_explicit_exports: called CC=3 SCAR=11.0
178 134 my $self = shift;
179  
180 # Env is a special case. It ties environment variables to Perl variables,
181 # so there is no @EXPORT for the ExportInspector to discover -- whatever a
182 # given "use Env" statement imports is exactly what it provides. Treat the
183 # statement's own arguments as its exportable symbols so that unused ones
184 # can be pruned. See t/env.t and GH #23.
185 134 ▶ if ( $self->_is_env ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_is_env) 8 126 134
186 8 return $self->_env_exports;
187 }
188  
189 126 ▶ return $self->_export_inspector->has_explicit_exports
Branch: 1 branch
BranchTrueFalseTotal
$self->_export_inspector->has_explicit_exports ? : 119 7 126
190 ? $self->_export_inspector->explicit_exports
191 : $self->_export_inspector->implicit_exports;
192 }
193  
194 277 ▶sub _is_env {
Subroutine _is_env: called CC=2 SCAR=7.0
195 277 my $self = shift;
196 277 my $name = $self->module_name;
197 277 ▶ return defined $name && $name eq 'Env';
Condition66%2/3
defined $name && $name eq 'Env'
!ll&&!rl&&r
026017
MC/DC50%1/2
defined $name && $name eq 'Env'
defined $name $name eq 'Env'
Truth table66%2/3
A && B
A= defined $nameB= $name eq 'Env'
ABresult
0X0
100
111
198 }
199  
200 8 ▶sub _env_exports {
Subroutine _env_exports: called CC=6 SCAR=18.0
201 8 my $self = shift;
202  
203 8 my %exports;
204 8 ▶ for my $arg ( @{ $self->_found_imports // [] } ) {
Condition50%1/2
$self->_found_imports // []
l!l
80
MC/DC0%0/1
$self->_found_imports // []
$self->_found_imports
Truth table50%1/2
A
A= $self->_found_imports
Aresult
11
00
205  
206 # A bareword (FOO) and the scalar form ($FOO) both tie the scalar
207 # $FOO; only @FOO and %FOO tie the array or hash. Key on the sigil'd
208 # symbol we'll actually search for in the code, but keep the literal
209 # the user wrote as the value so we preserve their original style.
210 11 my $sigil = substr( $arg, 0, 1 );
211 11 ▶ my $key
Branch: 1 branch
BranchTrueFalseTotal
$sigil eq '$' || $sigil eq '@' || $sigil eq '%' ? : 8 3 11
Condition83%5/6
$sigil eq "\$" or $sigil eq "\@" or $sigil eq "%"
l!l&&r!l&&!r
803
$sigil eq "\$" or $sigil eq "\@"
l!l&&r!l&&!r
263
MC/DC66%2/3
$sigil eq "\$" or $sigil eq "\@" or $sigil eq "%"
$sigil eq "\$" $sigil eq "\@" $sigil eq "%"
Truth table75%3/4
A or B or C
A= $sigil eq "\$"B= $sigil eq "\@"C= $sigil eq "%"
ABCresult
1XX1
01X1
0011
0000
212 = ( $sigil eq '$' || $sigil eq '@' || $sigil eq '%' )
213 ? $arg
214 : '$' . $arg;
215 11 $exports{$key} = $arg;
216 }
217 8 return \%exports;
218 }
219  
220 ## no critic (Subroutines::ProhibitExcessComplexity)
221 134 ▶sub _build_imports {
Subroutine _build_imports: called CC=63 SCAR=46.6
222 134 my $self = shift;
223  
224 # This is not a real symbol, so we should never be looking for it to appear
225 # in the code.
226 134 ▶ $self->_delete_export('verbose') if $self->module_name eq 'Carp';
Branch: 1 branch
BranchTrueFalseTotal
if $self->module_name eq "Carp" 23 111 134
227  
228 134 my %found;
229  
230 # Stolen from Perl::Critic::Policy::TooMuchCode::ProhibitUnfoundImport
231 134 for my $word ( $self->_document->possibly_imported_tokens ) {
232 865 ▶ next if exists $found{"$word"};
Branch: 1 branch
BranchTrueFalseTotal
if exists $found{"$word"} 1 864 865
233  
234 # stop if we've found everything that can be imported (every
235 # possible exported symbol is being used)
236 864 ▶ last unless $self->_imports_remain( \%found );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_imports_remain(\%found) 9 855 864
237  
238 # We don't want (for instance) pragma names to be confused with
239 # functions.
240 #
241 # ie:
242 # use warnings;
243 # use Test::Warnings; # exports warnings()
244 #
245 # However, we also want to catch function calls in use statements, like
246 # "use lib catfile( 't', 'lib');"
247 #
248 # or
249 #
250 # use Mojo::File qw( curfile );
251 # use lib curfile->sibling('lib')->to_string;
252 855 my $is_function_call = is_function_call($word);
253 855 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if ($word->parent and $word->parent->isa("PPI::Statement::Include") and not $is_function_call || $word->snext_sibling && $word->snext_sibling eq "->") 1 854 855
Condition66%8/12
$word->parent and $word->parent->isa("PPI::Statement::Include") and not $is_function_call || $word->snext_sibling && $word->snext_sibling eq "->"
!ll&&!rl&&r
84951
$word->parent and $word->parent->isa("PPI::Statement::Include")
!ll&&!rl&&r
08496
$is_function_call || $word->snext_sibling && $word->snext_sibling eq '->'
l!l&&r!l&&!r
501
$word->snext_sibling and $word->snext_sibling eq "->"
!ll&&!rl&&r
010
MC/DC40%2/5
$word->parent and $word->parent->isa("PPI::Statement::Include") and not $is_function_call || $word->snext_sibling && $word->snext_sibling eq "->"
$word->parent $word->parent->isa("PPI::Statement::Include") $is_function_call $word->snext_sibling $word->snext_sibling eq "->"
Truth table50%3/6
A and B and not(C || D and E)
A= $word->parentB= $word->parent->isa("PPI::Statement::Include")C= $is_function_callD= $word->snext_siblingE= $word->snext_sibling eq "->"
ABCDEresult
0XXXX0
10XXX0
111XX0
110110
1100X1
110101
254 $word->parent
255 && $word->parent->isa('PPI::Statement::Include')
256 && ( !$is_function_call
257 && !( $word->snext_sibling && $word->snext_sibling eq '->' ) )
258 ) {
259 1 next;
260 }
261  
262 # Don't turn "use POSIX ();" into "use POSIX qw( sprintf );"
263 # If it's a function and it's a builtin function and it's either not
264 # included in found_imports or original imports are not implicit
265 # then skip this.
266 854 ▶ if ( defined $self->_found_imports
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->_found_imports and &none(sub { $_ eq $word; } , @{$self->_found_imports;}) and $is_function_call and is_perl_builtin($word)) 123 731 854
Condition100%9/9
defined $self->_found_imports and &none(sub { $_ eq $word; } , @{$self->_found_imports;}) and $is_function_call and is_perl_builtin($word)
!ll&&!rl&&r
503228123
defined $self->_found_imports and &none(sub { $_ eq $word; } , @{$self->_found_imports;}) and $is_function_call
!ll&&!rl&&r
45251351
defined $self->_found_imports and &none(sub { $_ eq $word; } , @{$self->_found_imports;})
!ll&&!rl&&r
342110402
MC/DC100%4/4
defined $self->_found_imports and &none(sub { $_ eq $word; } , @{$self->_found_imports;}) and $is_function_call and is_perl_builtin($word)
defined $self->_found_imports &none(sub { $_ eq $word; } , @{$self->_found_imports;}) $is_function_call is_perl_builtin($word)
Truth table100%5/5
A and B and C and D
A= defined $self->_found_importsB= &none(sub { $_ eq $word; } , @{$self->_found_imports;})C= $is_function_callD= is_perl_builtin($word)
ABCDresult
0XXX0
10XX0
110X0
11100
11111
267 512 ▶ && ( none { $_ eq $word } @{ $self->_found_imports } )
Subroutine __ANON__: called CC=1 SCAR=0.0
268 && $is_function_call
269 && is_perl_builtin($word) ) {
270 123 next;
271 }
272  
273 731 my @found_import;
274 731 my $isa_symbol = $word->isa('PPI::Token::Symbol');
275  
276 # Don't confuse my @Foo with a use of @Foo which is exported by a module.
277 731 ▶ if ( $isa_symbol && $word->content =~ m{\A(@|%|$)} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($isa_symbol and $word->content =~ /\A(@|%|$)/) 58 673 731
Condition100%3/3
$isa_symbol and $word->content =~ /\A(@|%|$)/
!ll&&!rl&&r
44622758
MC/DC100%2/2
$isa_symbol and $word->content =~ /\A(@|%|$)/
$isa_symbol $word->content =~ /\A(@|%|$)/
Truth table100%3/3
A and B
A= $isa_symbolB= $word->content =~ /\A(@|%|$)/
ABresult
0X0
100
111
278 58 my $previous_sibling = $word->sprevious_sibling;
279 58 ▶ if ( $previous_sibling && $previous_sibling->content eq 'my' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($previous_sibling and $previous_sibling->content eq "my") 24 34 58
Condition100%3/3
$previous_sibling and $previous_sibling->content eq "my"
!ll&&!rl&&r
33124
MC/DC100%2/2
$previous_sibling and $previous_sibling->content eq "my"
$previous_sibling $previous_sibling->content eq "my"
Truth table100%3/3
A and B
A= $previous_siblingB= $previous_sibling->content eq "my"
ABresult
0X0
100
111
280 24 next;
281 }
282 }
283  
284 # If a module exports %foo and we find $foo{bar}, $word->canonical
285 # returns $foo and $word->symbol returns %foo
286 707 ▶ if ( $isa_symbol
Branch: 7 branches
BranchTrueFalseTotal
if ($isa_symbol and $self->_is_importable($word->symbol)) { } 16 691 707
elsif ($isa_symbol and $word->symbol_type eq '&' and $self->_is_importable(substr($word->symbol, 1))) { } 0 691 691
elsif ($self->_is_importable("$word") and not $word =~ /^\w/ && $word->previous_token && $word->previous_token eq '{' && $word->previous_token->previous_token && $word->previous_token->previous_token eq '$') { } 180 511 691
elsif ($is_function_call and $self->_is_importable('&' . $word)) { } 2 509 511
elsif ($is_function_call and $word =~ /^::\w+/ and $self->_is_importable(substr($word, 2))) { } 2 507 509
elsif ($word->isa('PPI::Token::Label')) { } 0 507 507
elsif ($word->isa('PPI::Token::Prototype')) { } 0 507 507
Condition100%3/3
$isa_symbol and $self->_is_importable($word->symbol)
!ll&&!rl&&r
44624516
MC/DC100%2/2
$isa_symbol and $self->_is_importable($word->symbol)
$isa_symbol $self->_is_importable($word->symbol)
Truth table100%3/3
A and B
A= $isa_symbolB= $self->_is_importable($word->symbol)
ABresult
0X0
100
111
287 && $self->_is_importable( $word->symbol ) ) {
288 16 @found_import = ( $word->symbol );
289 }
290  
291 # Match on \&is_Str as is_Str
292 ▶ elsif ($isa_symbol
Condition83%5/6
$isa_symbol and $word->symbol_type eq "&" and $self->_is_importable(substr($word->symbol, 1))
!ll&&!rl&&r
68920
$isa_symbol and $word->symbol_type eq "&"
!ll&&!rl&&r
4462432
MC/DC0%0/3
$isa_symbol and $word->symbol_type eq "&" and $self->_is_importable(substr($word->symbol, 1))
$isa_symbol $word->symbol_type eq "&" $self->_is_importable(substr($word->symbol, 1))
Truth table75%3/4
A and B and C
A= $isa_symbolB= $word->symbol_type eq "&"C= $self->_is_importable(substr($word->symbol, 1))
ABCresult
0XX0
10X0
1100
1111
293 && $word->symbol_type eq '&'
294 && $self->_is_importable( substr( $word->symbol, 1 ) ) ) {
295 0 @found_import = ( substr( $word->symbol, 1 ) );
296 }
297  
298 # Don't catch ${foo} here and mistake it for "foo". We deal with that
299 # elsewhere. Don't catch @{ split_header $str }.
300 ▶ elsif (
Condition66%10/15
$self->_is_importable("$word") and not $word =~ /^\w/ && $word->previous_token && $word->previous_token eq "{" && $word->previous_token->previous_token && $word->previous_token->previous_token eq "\$"
!ll&&!rl&&r
5101180
$word =~ /^\w/ && $word->previous_token && $word->previous_token eq '{' && $word->previous_token->previous_token && $word->previous_token->previous_token eq '$'
!ll&&!rl&&r
18001
$word =~ /^\w/ and $word->previous_token and $word->previous_token eq "{" and $word->previous_token->previous_token
!ll&&!rl&&r
18001
$word =~ /^\w/ and $word->previous_token and $word->previous_token eq "{"
!ll&&!rl&&r
01801
$word =~ /^\w/ and $word->previous_token
!ll&&!rl&&r
00181
MC/DC33%2/6
$self->_is_importable("$word") and not $word =~ /^\w/ && $word->previous_token && $word->previous_token eq "{" && $word->previous_token->previous_token && $word->previous_token->previous_token eq "\$"
$self->_is_importable("$word") $word =~ /^\w/ $word->previous_token $word->previous_token eq "{" $word->previous_token->previous_token $word->previous_token->previous_token eq '$'
Truth table42%3/7
A and not(B and C and D and E && F)
A= $self->_is_importable("$word")B= $word =~ /^\w/C= $word->previous_tokenD= $word->previous_token eq "{"E= $word->previous_token->previous_tokenF= $word->previous_token->previous_token eq '$'
ABCDEFresult
0XXXXX0
1111110
10XXXX1
110XXX1
1110XX1
11110X1
1111101
301 $self->_is_importable("$word")
302 && !(
303 $word =~ m{^\w}
304 && $word->previous_token
305 && $word->previous_token eq '{'
306 && $word->previous_token->previous_token
307 && $word->previous_token->previous_token eq '$'
308 )
309 ) {
310 180 @found_import = ("$word");
311 }
312  
313 # Maybe a subroutine ref has been exported. For instance,
314 # Getopt::Long exports &GetOptions
315 ▶ elsif ($is_function_call
Condition100%3/3
$is_function_call and $self->_is_importable('&' . $word)
!ll&&!rl&&r
844252
MC/DC100%2/2
$is_function_call and $self->_is_importable('&' . $word)
$is_function_call $self->_is_importable('&' . $word)
Truth table100%3/3
A and B
A= $is_function_callB= $self->_is_importable('&' . $word)
ABresult
0X0
100
111
316 && $self->_is_importable( '&' . $word ) ) {
317 2 @found_import = ( '&' . "$word" );
318 }
319  
320 # Maybe this is an inner package referencing a function in main. We
321 # don't really deal with inner packages otherwise, so this could break
322 # some things.
323 ▶ elsif ($is_function_call
Condition83%5/6
$is_function_call and $word =~ /^::\w+/ and $self->_is_importable(substr($word, 2))
!ll&&!rl&&r
50702
$is_function_call and $word =~ /^::\w+/
!ll&&!rl&&r
844232
MC/DC66%2/3
$is_function_call and $word =~ /^::\w+/ and $self->_is_importable(substr($word, 2))
$is_function_call $word =~ /^::\w+/ $self->_is_importable(substr($word, 2))
Truth table75%3/4
A and B and C
A= $is_function_callB= $word =~ /^::\w+/C= $self->_is_importable(substr($word, 2))
ABCresult
0XX0
10X0
1100
1111
324 && $word =~ m{^::\w+}
325 && $self->_is_importable( substr( $word, 2 ) ) ) {
326 2 @found_import = ( substr( $word, 2 ) );
327 }
328  
329 # PPI can think that an imported function in a ternary is a label
330 # my $foo = $enabled ? GEOIP_MEMORY_CACHE : 0;
331 # The content of the $word will be "GEOIP_MEMORY_CACHE :"
332 elsif ( $word->isa('PPI::Token::Label') ) {
333 0 ▶ if ( $word->content =~ m{^(\w+)} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($word->content =~ /^(\w+)/) 0 0 0
334 0 my $label = $1;
335 0 ▶ if ( $self->_is_importable($label) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_is_importable($label)) 0 0 0
336 0 @found_import = ($label);
337 0 $found{$label}++;
338 }
339 }
340 }
341  
342 # Sometimes an import is only used to set a default value for a
343 # variable in a signature. Without treating a prototype as a signature,
344 # we would miss the import entirely. I'm not particularly proud of
345 # this, but since PPI doesn't yet support signatures, this will at
346 # least help us cover some cases. If the prototype is actually a
347 # prototype, then this just shouldn't find anything.
348 elsif ( $word->isa('PPI::Token::Prototype') ) {
349 0 my $prototype = $word->prototype;
350  
351 # sometimes closing parens don't get included by PPI.
352 0 ▶ if ( substr( $prototype, -1, 1 ) eq '(' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (substr($prototype, -1, 1) eq "(") 0 0 0
353 0 $prototype .= ')';
354 }
355 0 $prototype =~ s{,}{;}g;
356  
357 0 $prototype .= ';'; # Won't hurt if there's an extra ";"
358 0 my $new = PPI::Document->new( \$prototype );
359 my $words = $new->find(
360 0 ▶ sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
361 0 ▶ $_[1]->isa('PPI::Token::Word')
Branch: 1 branch
BranchTrueFalseTotal
$_[1]->isa("PPI::Token::Word") or $_[1]->isa("PPI::Token::Symbol") 0 0 0
362 || $_[1]->isa('PPI::Token::Symbol');
363 }
364 0 ▶ ) || [];
Condition0%0/2
$new->find(sub { $_[1]->isa('PPI::Token::Word') or $_[1]->isa('PPI::Token::Symbol'); } ) || []
l!l
00
MC/DC0%0/1
$new->find(sub { $_[1]->isa('PPI::Token::Word') or $_[1]->isa('PPI::Token::Symbol'); } ) || []
$new->find(sub { $_[1]->isa('PPI::Token::Word') or $_[1]->isa('PPI::Token::Symbol'); } )
Truth table0%0/2
A
A= $new->find(sub { $_[1]->isa('PPI::Token::Word') or $_[1]->isa('PPI::Token::Symbol'); } )
Aresult
11
00
365 0 for my $word ( @{$words} ) {
366 0 ▶ if ( $self->_is_importable("$word") ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_is_importable("$word")) 0 0 0
367 0 push @found_import, "$word";
368 }
369 }
370 }
371  
372 707 for my $found (@found_import) {
373  
374 # could have been imported by another Include
375 200 ▶ if ( !$self->_is_already_imported($found) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->_is_already_imported($found)) 184 16 200
376 184 $found{$found}++;
377 }
378 }
379 }
380  
381 # A used import might be a variable interpolated into quotes.
382 134 ▶ if ( $self->_imports_remain( \%found ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_imports_remain(\%found)) 114 20 134
383 114 for my $var ( keys %{ $self->_document->interpolated_symbols } ) {
384 11 ▶ if ( $self->_is_importable($var) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_is_importable($var)) 8 3 11
385 8 $found{$var} = 1;
386 }
387 }
388 }
389  
390 # A used import might be just be a symbol that just gets exported. ie. If
391 # it appears as @EXPORT = ( 'SOME_SYMBOL') we don't want to miss it.
392 134 ▶ if ( $self->_imports_remain( \%found )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_imports_remain(\%found) and $self->_document->my_own_inspector and $self->_document->my_own_inspector->is_exporter) 1 133 134
Condition100%6/6
$self->_imports_remain(\%found) and $self->_document->my_own_inspector and $self->_document->my_own_inspector->is_exporter
!ll&&!rl&&r
12941
$self->_imports_remain(\%found) and $self->_document->my_own_inspector
!ll&&!rl&&r
211085
MC/DC100%3/3
$self->_imports_remain(\%found) and $self->_document->my_own_inspector and $self->_document->my_own_inspector->is_exporter
$self->_imports_remain(\%found) $self->_document->my_own_inspector $self->_document->my_own_inspector->is_exporter
Truth table100%4/4
A and B and C
A= $self->_imports_remain(\%found)B= $self->_document->my_own_inspectorC= $self->_document->my_own_inspector->is_exporter
ABCresult
0XX0
10X0
1100
1111
393 && $self->_document->my_own_inspector
394 && $self->_document->my_own_inspector->is_exporter ) {
395 1 for my $symbol (
396 uniq(
397 $self->_document->my_own_inspector->implicit_export_names,
398 $self->_document->my_own_inspector->explicit_export_names
399 )
400 ) {
401 3 ▶ if ( $self->_is_importable($symbol)
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_is_importable($symbol) and not $self->_document->is_sub_name("$symbol")) 3 0 3
Condition33%1/3
$self->_is_importable($symbol) and not $self->_document->is_sub_name("$symbol")
!ll&&!rl&&r
003
MC/DC0%0/2
$self->_is_importable($symbol) and not $self->_document->is_sub_name("$symbol")
$self->_is_importable($symbol) not $self->_document->is_sub_name("$symbol")
Truth table33%1/3
A and not(B)
A= $self->_is_importable($symbol)B= not $self->_document->is_sub_name("$symbol")
ABresult
0X0
100
111
402 && !$self->_document->is_sub_name("$symbol") ) {
403 3 $found{$symbol} = 1;
404 }
405 }
406 }
407  
408 # A used import might just be something that gets re-exported by
409 # Sub::Exporter
410 134 ▶ if ( $self->_imports_remain( \%found ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_imports_remain(\%found)) 113 21 134
411 113 for my $func ( $self->_document->sub_exporter_export_list ) {
412 2 ▶ if ( $self->_is_importable($func) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_is_importable($func)) 1 1 2
413 1 $found{$func}++;
414 }
415 }
416 }
417  
418 # When a module exports a typeglob (e.g. English exports *PROGRAM_NAME),
419 # _import_name normalizes an imported slot like $PROGRAM_NAME to the
420 # typeglob form *PROGRAM_NAME. But if the user explicitly imported a valid
421 # slot form, that is correct and more minimal, so preserve what they wrote
422 # rather than expanding it to the typeglob.
423 my %found_imports
424 134 ▶ = map { $_ => 1 } @{ $self->_found_imports // [] };
Condition100%2/2
$self->_found_imports // []
l!l
8450
MC/DC100%1/1
$self->_found_imports // []
$self->_found_imports
Truth table100%2/2
A
A= $self->_found_imports
Aresult
11
00
425 my @found
426 134 ▶ = map { exists $found_imports{$_} ? $_ : $self->_import_name($_); }
Branch: 1 branch
BranchTrueFalseTotal
exists $found_imports{$_} ? : 102 94 196
427 keys %found;
428  
429 # Some modules have imports which are basically flags, rather than names of
430 # symbols to export. So if a flag is already in the import, we need to
431 # preserve it, rather than risk altering the behaviour of the module.
432 134 ▶ if ( $self->_export_inspector->has_import_flags ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_export_inspector->has_import_flags) 24 110 134
433 24 for my $arg ( @{ $self->_export_inspector->import_flags } ) {
434 24 ▶ if ( defined $self->_found_imports
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->_found_imports and &any(sub { $_ eq $arg; } , @{$self->_found_imports;})) 6 18 24
Condition100%3/3
defined $self->_found_imports and &any(sub { $_ eq $arg; } , @{$self->_found_imports;})
!ll&&!rl&&r
4146
MC/DC100%2/2
defined $self->_found_imports and &any(sub { $_ eq $arg; } , @{$self->_found_imports;})
defined $self->_found_imports &any(sub { $_ eq $arg; } , @{$self->_found_imports;})
Truth table100%3/3
A and B
A= defined $self->_found_importsB= &any(sub { $_ eq $arg; } , @{$self->_found_imports;})
ABresult
0X0
100
111
435 20 ▶ && ( any { $_ eq $arg } @{ $self->_found_imports } ) ) {
Subroutine __ANON__: called CC=1 SCAR=0.0
436 6 push @found, $arg;
437 }
438 }
439 }
440  
441 134 @found = uniq _sort_symbols(@found);
442 134 ▶ if ( $self->_found_imports ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_found_imports) 84 50 134
443 84 my @preserved = grep { m{\A[!_]} } @{ $self->_found_imports };
444 84 @found = uniq( @preserved, @found );
445 }
446 134 return \@found;
447 }
448  
449 ## use critic
450  
451 154 ▶sub _build_is_ignored {
Subroutine _build_is_ignored: called CC=14 SCAR=26.6
452 154 my $self = shift;
453  
454 154 ▶ if ( $self->_include->type eq 'require' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_include->type eq "require") 15 139 154
455 15 ▶ return 1 if !$self->_is_translatable;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_is_translatable 11 4 15
456 }
457  
458 # A bare "use Env;" imports every environment variable. There are no
459 # explicit arguments to prune, so leave the statement untouched rather than
460 # collapsing it to "use Env ();". See GH #23.
461 143 ▶ return 1 if $self->_is_env && !@{ $self->_found_imports // [] };
Branch: 1 branch
BranchTrueFalseTotal
if $self->_is_env and not @{$self->_found_imports // [];} 1 142 143
Condition100%5/5
$self->_is_env and not @{$self->_found_imports // [];}
!ll&&!rl&&r
13481
$self->_found_imports // []
l!l
81
MC/DC100%2/2
$self->_is_env and not @{$self->_found_imports // [];}
$self->_is_env not @{$self->_found_imports // [];}
MC/DC100%1/1
$self->_found_imports // []
$self->_found_imports
Truth table100%3/3
A and not(B)
A= $self->_is_envB= not @{$self->_found_imports // [];}
ABresult
0X0
100
111
Truth table100%2/2
A
A= $self->_found_imports
Aresult
11
00
462  
463 # This will be rewritten as "use Foo ();"
464 142 ▶ return 0 if $self->_will_never_export;
Branch: 1 branch
BranchTrueFalseTotal
if $self->_will_never_export 7 135 142
465  
466 135 ▶ return 1 if $self->_export_inspector->has_fatal_error;
Branch: 1 branch
BranchTrueFalseTotal
if $self->_export_inspector->has_fatal_error 0 135 135
467  
468 135 ▶ return 0 if $self->_export_inspector->is_oo_class;
Branch: 1 branch
BranchTrueFalseTotal
if $self->_export_inspector->is_oo_class 0 135 135
469  
470 # A MooseX::Types type library can have a Moose metaclass (as with
471 # MooseX::Types 0.50), which would otherwise trip the uses_moose check
472 # below. See GH#202.
473 135 ▶ return 0 if $self->_export_inspector->is_moose_type_class;
Branch: 1 branch
BranchTrueFalseTotal
if $self->_export_inspector->is_moose_type_class 0 135 135
474  
475 135 ▶ return 1 if $self->_export_inspector->is_moose_class;
Branch: 1 branch
BranchTrueFalseTotal
if $self->_export_inspector->is_moose_class 1 134 135
476  
477 134 ▶ return 1 if $self->_export_inspector->uses_moose;
Branch: 1 branch
BranchTrueFalseTotal
if $self->_export_inspector->uses_moose 0 134 134
478  
479 134 ▶ return 1 if $self->_export_inspector->is_moo_class;
Branch: 1 branch
BranchTrueFalseTotal
if $self->_export_inspector->is_moo_class 0 134 134
480  
481 return 1
482 134 ▶ if any { $_ eq 'Moo::Object' } @{ $self->_export_inspector->pkg_isa };
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
if &any(sub { $_ eq "Moo::Object"; } , @{$self->_export_inspector->pkg_isa;}) 1 133 134
483  
484 133 return 0;
485 }
486  
487 146 ▶sub _build_is_translatable {
Subroutine _build_is_translatable: called CC=8 SCAR=20.9
488 146 my $self = shift;
489  
490 146 ▶ return 0 if !$self->_include->type;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_include->type 0 146 146
491 146 ▶ return 0 if $self->_include->type ne 'require';
Branch: 1 branch
BranchTrueFalseTotal
if $self->_include->type ne "require" 131 15 146
492  
493 # A "require Foo;" is functionally distinct from "use Foo ();" -- require
494 # loads at runtime, use at compile time -- and is often deliberate. By
495 # default we leave requires untouched. Disable preserve_require to restore
496 # the old behaviour of translating them to "use Foo ();". See GH #76.
497 15 ▶ return 0 if $self->_preserve_require;
Branch: 1 branch
BranchTrueFalseTotal
if $self->_preserve_require 8 7 15
498  
499 7 ▶ return 0 if $self->module_name eq 'Exporter';
Branch: 1 branch
BranchTrueFalseTotal
if $self->module_name eq "Exporter" 0 7 7
500  
501 # We can deal with a top level require.
502 # require Foo; can be changed to use Foo ();
503 # We don't want to touch requires which are inside any kind of a condition.
504  
505 # If there is no parent, then it's likely just a single snippet
506 # provided by a text editor. We can process the snippet. If it's part
507 # of a larger document and the parent is not a PPI::Document, this
508 # would appear not to be a top level require.
509 7 ▶ if ( $self->_include->parent
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_include->parent and not $self->_include->parent->isa("PPI::Document")) 1 6 7
Condition66%2/3
$self->_include->parent and not $self->_include->parent->isa("PPI::Document")
!ll&&!rl&&r
061
MC/DC50%1/2
$self->_include->parent and not $self->_include->parent->isa("PPI::Document")
$self->_include->parent not $self->_include->parent->isa("PPI::Document")
Truth table66%2/3
A and not(B)
A= $self->_include->parentB= not $self->_include->parent->isa("PPI::Document")
ABresult
0X0
100
111
510 && !$self->_include->parent->isa('PPI::Document') ) {
511 1 return 0;
512 }
513  
514 # Postfix conditions are a bit harder to find. If the significant
515 # children amount to more than "require Module;", we'll just move on.
516 6 my @children = $self->_include->schildren;
517  
518 6 my $statement = join q{ }, @children[ 0 .. 2 ];
519 6 ▶ if ( $statement ne 'require ' . $self->module_name . ' ;' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($statement ne 'require ' . $self->module_name . ' ;') 2 4 6
520 2 return 0;
521 }
522  
523 # Any other case of "require Foo;" should be translated to "use Foo ();"
524 # as those are functionally equivalent.
525 4 return 1;
526 }
527  
528 ## no critic (Subroutines::ProhibitExcessComplexity)
529 154 ▶sub _build_formatted_ppi_statement {
Subroutine _build_formatted_ppi_statement: called CC=28 SCAR=34.4
530 154 my $self = shift;
531  
532 # The following steps may seem a bit out of order, but we're trying to
533 # short circuit if at all possible. That means not building an
534 # ExportInspector object unless we really need to.
535  
536 # Nothing to do here. Preserve the original statement.
537 154 ▶ return $self->_include if $self->_is_ignored;
Branch: 1 branch
BranchTrueFalseTotal
if $self->_is_ignored 14 140 154
538  
539 140 ▶ my $maybe_module_version
Branch: 1 branch
BranchTrueFalseTotal
$self->_include->module_version ? : 6 134 140
540 = $self->_include->module_version
541 ? q{ } . $self->_include->module_version
542 : q{};
543  
544 # In this case we either have a module which we know will never export
545 # symbols or a module which can export but for which we haven't found any
546 # imported symbols. In both cases we'll want to rewrite with an empty list
547 # of imports.
548 140 ▶ if ( $self->_will_never_export
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_will_never_export or $self->_is_translatable or not @{$self->_imports;}) 42 98 140
Condition100%6/6
$self->_will_never_export or $self->_is_translatable or not @{$self->_imports;}
l!l&&r!l&&!r
93398
$self->_will_never_export or $self->_is_translatable
l!l&&r!l&&!r
72131
MC/DC100%3/3
$self->_will_never_export or $self->_is_translatable or not @{$self->_imports;}
$self->_will_never_export $self->_is_translatable not @{$self->_imports;}
Truth table100%4/4
A or B or not(C)
A= $self->_will_never_exportB= $self->_is_translatableC= not @{$self->_imports;}
ABCresult
1XX1
01X1
0011
0000
549 || $self->_is_translatable
550 131 || !@{ $self->_imports } ) {
551 42 return $self->_maybe_get_new_include(
552 sprintf(
553 'use %s%s ();', $self->module_name, $maybe_module_version
554 )
555 );
556 }
557  
558 98 my $statement;
559  
560 98 my @args = $self->_include->arguments;
561  
562 # Don't touch a do { } block.
563 98 ▶ if ( $self->_isa_test_builder_module && @args && $args[0] eq 'do' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_isa_test_builder_module and @args and $args[0] eq "do") 1 97 98
Condition100%6/6
$self->_isa_test_builder_module and @args and $args[0] eq "do"
!ll&&!rl&&r
9431
$self->_isa_test_builder_module and @args
!ll&&!rl&&r
9044
MC/DC100%3/3
$self->_isa_test_builder_module and @args and $args[0] eq "do"
$self->_isa_test_builder_module @args $args[0] eq "do"
Truth table100%4/4
A and B and C
A= $self->_isa_test_builder_moduleB= @argsC= $args[0] eq "do"
ABCresult
0XX0
10X0
1100
1111
564 1 return $self->_include;
565 }
566  
567 # Do some contortions to turn PPI objects back into a data structure so
568 # that we can add or replace an import hash key and then end up with a new
569 # list which is sorted on hash keys. This makes the assumption that the
570 # same key won't get passed twice. This is pretty gross, but I was too lazy
571 # to try to figure out how to do this with PPI and I think it should
572 # *mostly* work. I don't like the formatting that Data::Dumper comes up
573 # with, so we'll run it through perltidy.
574  
575 97 ▶ if ( $self->_isa_test_builder_module
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_isa_test_builder_module and @args) { } 3 94 97
Condition100%3/3
$self->_isa_test_builder_module and @args
!ll&&!rl&&r
9043
MC/DC100%2/2
$self->_isa_test_builder_module and @args
$self->_isa_test_builder_module @args
Truth table100%3/3
A and B
A= $self->_isa_test_builder_moduleB= @args
ABresult
0X0
100
111
576 && @args ) {
577 3 my $all;
578  
579 3 ▶ if ( $args[0]->isa('PPI::Token::Word') ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($args[0]->isa('PPI::Token::Word')) { } 3 0 3
elsif ($args[0]->isa('PPI::Structure::List') and $args[0]->braces eq '()') { } 0 0 0
580 3 $all = join q{ }, map { "$_" } @args;
581 }
582  
583 ▶ elsif ($args[0]->isa('PPI::Structure::List')
Condition0%0/3
$args[0]->isa("PPI::Structure::List") and $args[0]->braces eq "()"
!ll&&!rl&&r
000
MC/DC0%0/2
$args[0]->isa("PPI::Structure::List") and $args[0]->braces eq "()"
$args[0]->isa("PPI::Structure::List") $args[0]->braces eq "()"
Truth table0%0/3
A and B
A= $args[0]->isa("PPI::Structure::List")B= $args[0]->braces eq "()"
ABresult
0X0
100
111
584 && $args[0]->braces eq '()' ) {
585 0 for my $child ( $args[0]->children ) {
586 0 $all .= "$child";
587 }
588 }
589  
590 ## no critic (BuiltinFunctions::ProhibitStringyEval)
591 3 my $args;
592 my $error;
593 3 ▶ try {
Subroutine __ANON__: called CC=1 SCAR=0.0
594 3 $args = eval( '{' . $all . '}' );
595 }
596 0 ▶ catch {
Subroutine __ANON__: not called CC=1 SCAR=6.9
597 0 $self->logger->info($_);
598 0 $error = 1;
599 3 };
600 ## use critic
601  
602 3 ▶ if ( !$error && !is_plain_hashref($args) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($error or is_plain_hashref($args)) 0 3 3
Condition33%1/3
$error or is_plain_hashref($args)
l!l&&r!l&&!r
030
MC/DC0%0/2
$error or is_plain_hashref($args)
$error is_plain_hashref($args)
Truth table33%1/3
A or B
A= $errorB= is_plain_hashref($args)
ABresult
1X1
011
000
603 0 $self->logger->info( 'Not a hashref: ' . Dumper($args) );
604 0 $error = 1;
605 }
606  
607 # We will replace these with our own parsed imports.
608 3 delete $args->{import};
609  
610 # Ignore this line if we can't parse it. This will happen if the arg to
611 # test is a do block, for example.
612 3 ▶ return $self->_include if $error;
Branch: 1 branch
BranchTrueFalseTotal
if $error 0 3 3
613  
614 3 local $Data::Dumper::Terse = 1;
615 3 local $Data::Dumper::Indent = 0;
616 3 local $Data::Dumper::Sortkeys = 1;
617 3 local $Data::Dumper::Quotekeys = 0;
618 3 local $Data::Dumper::Useqq = 0;
619 3 local $Data::Dumper::Trailingcomma = 1;
620 3 local $Data::Dumper::Deparse = 1;
621  
622 3 my $dumped = Dumper($args);
623 3 my $non_import_args;
624 my $import_arg;
625  
626 3 ▶ if ( $dumped =~ m/^{(.*)}$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dumped =~ /^{(.*)}$/) 3 0 3
627 3 $non_import_args = $1;
628 }
629  
630 3 ▶ if ( $self->_imports ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_imports) 3 0 3
631  
632 # Always emit tight brackets here; the -sbt flag passed to
633 # Perl::Tidy below controls whether padding is added.
634 $import_arg = sprintf(
635 'import => [qw( %s )]',
636 3 join( q{ }, @{ $self->_imports } )
637 );
638 }
639  
640 my $all_args = join ', ',
641 3 ▶ grep { $_ && $_ =~ m{\w} } ( $import_arg, $non_import_args );
Condition66%2/3
$_ && $_ =~ /\w/
!ll&&!rl&&r
303
MC/DC50%1/2
$_ && $_ =~ /\w/
$_ $_ =~ /\w/
Truth table66%2/3
A && B
A= $_B= $_ =~ /\w/
ABresult
0X0
100
111
642  
643 3 $statement = sprintf(
644 'use %s%s %s;',
645 $self->module_name,
646 $maybe_module_version,
647 $all_args
648 );
649  
650 # save ~60ms in cases where we don't need Perl::Tidy
651 3 require Perl::Tidy; ## no perlimports
652 3 ▶ my $sbt = $self->_pad_brackets ? 0 : 1;
Branch: 1 branch
BranchTrueFalseTotal
$self->_pad_brackets ? : 1 2 3
653 3 my $indent = $self->_indent;
654 3 Perl::Tidy::perltidy(
655 argv => "-npro -sbt=$sbt -i=$indent",
656 source => \$statement,
657 destination => \$statement
658 );
659 }
660  
661 else {
662 94 ▶ my $padding = $self->_pad_imports ? q{ } : q{};
Branch: 1 branch
BranchTrueFalseTotal
$self->_pad_imports ? : 93 1 94
663 94 ▶ my $bp = $self->_pad_brackets ? q{ } : q{};
Branch: 1 branch
BranchTrueFalseTotal
$self->_pad_brackets ? : 1 93 94
664 94 ▶ my $template
Branch: 1 branch
BranchTrueFalseTotal
$self->_isa_test_builder_module ? : 4 90 94
665 = $self->_isa_test_builder_module
666 ? "use %s%s import => [${bp}qw(%s%s%s)${bp}];"
667 : 'use %s%s qw(%s%s%s);';
668  
669 $statement = sprintf(
670 $template,
671 $self->module_name,
672 $maybe_module_version,
673 $padding,
674 join(
675 q{ },
676 94 @{ $self->_imports }
677 ),
678 $padding,
679 );
680 }
681  
682 # Don't deal with Test::Builder classes here to keep it simple for now
683 97 ▶ if ( length($statement) > 78 && !$self->_isa_test_builder_module ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $statement > 78 and not $self->_isa_test_builder_module) 6 91 97
Condition100%3/3
length $statement > 78 and not $self->_isa_test_builder_module
!ll&&!rl&&r
9016
MC/DC100%2/2
length $statement > 78 and not $self->_isa_test_builder_module
length $statement > 78 not $self->_isa_test_builder_module
Truth table100%3/3
A and not(B)
A= length $statement > 78B= not $self->_isa_test_builder_module
ABresult
0X0
100
111
684 6 $statement = sprintf(
685 "use %s%s qw(\n",
686 $self->module_name,
687 $maybe_module_version,
688 );
689 6 my $indent = q{ } x $self->_indent;
690 6 for ( @{ $self->_imports } ) {
691 46 $statement .= "$indent$_\n";
692 }
693 6 $statement .= ');';
694 }
695  
696 97 return $self->_maybe_get_new_include($statement);
697 }
698  
699 ## use critic
700  
701 1266 ▶sub _imports_remain {
Subroutine _imports_remain: called CC=1 SCAR=0.0
702 1266 my $self = shift;
703 1266 my $found = shift;
704 1266 return keys %{$found} < $self->_explicit_export_count;
705 }
706  
707 # Comments which live *inside* the original include statement (e.g. a
708 # "## no critic (...)" annotation between the module name and the import list,
709 # or a comment nested inside an explicit "( ... )" import list) are
710 # descendants of the PPI::Statement::Include and always appear before its
711 # terminating ";". Because we rebuild the statement from scratch, they would
712 # otherwise be silently dropped (see #50), so we collect them here, in source
713 # order, to be re-attached as a trailing side comment. Only the comment
714 # *content* is preserved, not its original position: every embedded comment is
715 # moved to the end of the rewritten statement. A comment which already trails
716 # the ";" is a sibling of the statement rather than a descendant, so it is left
717 # in place by the document and is intentionally not collected here.
718 169 ▶sub _original_side_comments {
Subroutine _original_side_comments: called CC=2 SCAR=6.9
719 169 my $self = shift;
720 my $comments
721 169 ▶ = $self->_include->find( sub { $_[1]->isa('PPI::Token::Comment') } );
Subroutine __ANON__: called CC=1 SCAR=0.0
722 169 ▶ return q{} unless $comments;
Branch: 1 branch
BranchTrueFalseTotal
unless $comments 165 4 169
723 4 return join q{ }, map { $_->content =~ s{\s+\z}{}r } @{$comments};
724 }
725  
726 # Takes a string 'use SomeModule ...', returns a PPI:Statement:Include.
727 # The returned obj could be one made from the string, if its different from
728 # the existing one, else it is just the original.
729 169 ▶sub _maybe_get_new_include {
Subroutine _maybe_get_new_include: called CC=15 SCAR=27.1
730 169 my $self = shift;
731 169 my $statement = shift;
732 169 my $orig = $self->_include;
733  
734 # Re-attach any comments which were embedded in the original statement as a
735 # trailing side comment so they survive the rewrite (#50). Four spaces of
736 # separation matches perltidy's default --minimum-space-to-comment, so the
737 # result is stable under a subsequent perltidy run. We only pipe through
738 # Perl::Tidy for single-line statements: running it over an already-wrapped
739 # (multi-line) import list would re-flow the indentation and fight our own
740 # --indent handling.
741 #
742 # Known limitation: whenever a rewrite ends up with more than one comment on
743 # the single collapsed line -- multiple comments embedded in the original
744 # statement (collected here), or one embedded plus one already trailing the
745 # ";" (kept in place by the document) -- their text is preserved, but
746 # Perl::Critic honours only the first "## no critic" on a physical line, so a
747 # second such annotation stops suppressing. These cases are rare and we
748 # deliberately keep every comment rather than drop one; a human can merge the
749 # annotations if needed.
750 169 my $comments = $self->_original_side_comments;
751 169 ▶ if ( length $comments ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $comments) 4 165 169
752 4 $statement .= q{ } . $comments;
753 4 ▶ if ( $statement !~ m{\n} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($statement =~ /\n/) 3 1 4
754 3 require Perl::Tidy; ## no perlimports
755 3 ▶ my $sbt = $self->_pad_brackets ? 0 : 1;
Branch: 1 branch
BranchTrueFalseTotal
$self->_pad_brackets ? : 0 3 3
756 3 my $indent = $self->_indent;
757 3 my $tidied;
758 3 Perl::Tidy::perltidy(
759 argv => "-npro -sbt=$sbt -i=$indent",
760 source => \$statement,
761 destination => \$tidied,
762 );
763 3 $tidied =~ s{\s+\z}{};
764 3 $statement = $tidied;
765 }
766 }
767  
768 169 ▶ return $orig if $statement eq $orig; # quick exit
Branch: 1 branch
BranchTrueFalseTotal
if $statement eq $orig 35 134 169
769  
770 # Prefix newlines to reproduce original's location
771 ## no critic (BuiltinFunctions::ProhibitLvalueSubstr)
772 134 my $doc = do {
773 134 ▶ my $line = $orig->line_number || 1;
Condition100%2/2
$orig->line_number || 1
l!l
1322
MC/DC100%1/1
$orig->line_number || 1
$orig->line_number
Truth table100%2/2
A
A= $orig->line_number
Aresult
11
00
774 134 my $text = "\n" x $line;
775 134 substr( $text, -1 ) = $statement;
776 134 PPI::Document->new( \$text, filename => $orig->logical_filename );
777 };
778 ## use critic
779  
780 # Cloning is necessary because the tokens in the found statement belong
781 # to the document. When the document is destroyed, the tokens go with it.
782 # With the clone, the duplicated tokens are independent of the doc.
783 134 my $rewrite = do {
784 134 $doc->index_locations;
785 134 my $includes = $doc->find('Statement::Include');
786 134 my $found = $includes->[0];
787 134 my $clone = $found->clone;
788  
789 # A re-attached side comment sits *after* the terminating ";", so PPI
790 # parses it as a sibling of the include rather than a child. Pull those
791 # trailing comment tokens into the clone so they are not lost when the
792 # document is destroyed.
793 134 ▶ if ( length $comments ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $comments) 4 130 134
794 4 my @trailing;
795 4 my $sibling = $found;
796 4 ▶ while ( $sibling = $sibling->next_sibling ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sibling = $sibling->next_sibling) 8 4 12
797 8 ▶ last if $sibling->isa('PPI::Statement');
Branch: 1 branch
BranchTrueFalseTotal
if $sibling->isa("PPI::Statement") 0 8 8
798 8 push @trailing, $sibling;
799 }
800 pop @trailing
801 4 ▶ while @trailing
Branch: 1 branch
BranchTrueFalseTotal
@trailing and not $trailing[-1]->isa("PPI::Token::Comment") and do { pop @trailing } 0 4 4
Condition33%1/3
@trailing and not $trailing[-1]->isa("PPI::Token::Comment")
!ll&&!rl&&r
040
MC/DC0%0/2
@trailing and not $trailing[-1]->isa("PPI::Token::Comment")
@trailing not $trailing[-1]->isa("PPI::Token::Comment")
Truth table33%1/3
A and not(B)
A= @trailingB= not $trailing[-1]->isa("PPI::Token::Comment")
ABresult
0X0
100
111
802 && !$trailing[-1]->isa('PPI::Token::Comment');
803 4 $clone->add_element( $_->clone ) for @trailing;
804 }
805 134 $clone;
806 };
807  
808 # If the -only- difference is some whitespace before the symbol list, we
809 # keep the original statement. This is because perltidy often adds space
810 # to align successive import lists, and we don't want to fight. So:
811 # use AAAAAAA qw( thing );
812 # use Foo qw( bar ); <== leave this spacing alone
813  
814 # strip away extra spaces before the symbol list..
815 134 my $untidied_include = do {
816 134 my $string = "$orig";
817 134 $string =~ s{\s+(qw|\()}{ $1};
818 134 $string;
819 };
820 134 ▶ if ( "$rewrite" eq $untidied_include ) {
Branch: 1 branch
BranchTrueFalseTotal
if ("$rewrite" eq $untidied_include) 9 125 134
821 9 return $orig;
822 }
823  
824 125 ▶ return $rewrite if $self->_tidy_whitespace;
Branch: 1 branch
BranchTrueFalseTotal
if $self->_tidy_whitespace 109 16 125
825  
826 # We will return the rewritten include if a newline has been added or
827 # removed. This is a formatting change that we *probably* want.
828  
829 16 ( my $a = $orig . q{} ) =~ s{\s+}{}g;
830 16 ( my $b = $rewrite . q{} ) =~ s{\s+}{}g;
831  
832 16 ▶ return ( $a eq $b ) ? $orig : $rewrite;
Branch: 1 branch
BranchTrueFalseTotal
$a eq $b ? : 6 10 16
833 }
834  
835 # If there's a different module in this document which has already imported
836 # a symbol of the same name in its original imports, the we should make
837 # sure we don't accidentally create a duplicate import here. For example,
838 # Path::Tiny and Test::TempDir::Tiny both export a tempdir() function.
839 # Without this check we'd add a "tempdir" to both modules if we find it
840 # being used in the document.
841  
842 200 ▶sub _is_already_imported {
Subroutine _is_already_imported: called CC=6 SCAR=18.0
843 200 my $self = shift;
844 200 my $symbol = shift; # a string, not an object
845 200 my $duplicate = 0;
846  
847 200 ▶ if ( $self->_document->is_constant_name($symbol) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_document->is_constant_name($symbol)) 0 200 200
848 0 $self->logger->debug("$symbol is defined as a constant");
849 0 return 1;
850 }
851  
852 200 foreach my $module (
853 313 grep { $_ ne $self->module_name }
854 200 keys %{ $self->_document->found_imports }
855 ) {
856 113 $self->logger->debug(
857 "checking $module for previous imports of $symbol");
858 113 my @imports;
859 113 ▶ if ( is_plain_arrayref( $self->_document->found_imports->{$module} ) )
Branch: 1 branch
BranchTrueFalseTotal
if (is_plain_arrayref($self->_document->found_imports->{$module})) { } 90 23 113
860 {
861 90 @imports = @{ $self->_document->found_imports->{$module} };
862 90 $self->logger->debug(
863 'Explicit imports found: ' . Dumper( [ sort @imports ] ) );
864 }
865 else {
866 23 ▶ if ( my $inspector = $self->_document->inspector_for($module) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $inspector = $self->_document->inspector_for($module)) 23 0 23
867 23 @imports = $inspector->implicit_export_names;
868 23 $self->logger->debug( 'Implicit imports found: '
869 . Dumper( [ sort @imports ] ) );
870 }
871 }
872  
873 113 ▶ if ( any { $_ eq $symbol } @imports ) {
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
if (&any(sub { $_ eq $symbol; } , @imports)) 16 97 113
874 16 $duplicate = 1;
875 16 $self->logger->debug("$symbol already imported via $module");
876 16 last;
877 }
878 }
879  
880 200 return $duplicate;
881 }
882  
883 134 ▶sub _sort_symbols {
Subroutine _sort_symbols: called CC=1 SCAR=0.0
884 134 my @list = @_;
885  
886 ## no critic (BuiltinFunctions::RequireSimpleSortBlock)
887 my @sorted = sort {
888 134 my $A = _transform_before_cmp($a);
889 191 my $B = _transform_before_cmp($b);
890 191 "\L$A" cmp "\L$B";
891 } @list;
892 134 return @sorted;
893 }
894  
895 # This looks a little weird, but basically we want to maintain a stable sort
896 # order with lists that look like (foo, $foo, @foo, %foo). We use "-" to begin
897 # the suffix because it comes earliest in a sorted list of letters and digits.
898 382 ▶sub _transform_before_cmp {
Subroutine _transform_before_cmp: called CC=4 SCAR=13.9
899 382 my $thing = shift;
900 382 ▶ if ( $thing =~ m{\A[\$]} ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($thing =~ /\A[\$]/) { } 11 371 382
elsif ($thing =~ /\A[\@]/) { } 9 362 371
elsif ($thing =~ /\A[%]/) { } 4 358 362
901 11 $thing = substr( $thing, 1 ) . '-0';
902 }
903 elsif ( $thing =~ m{\A[@]} ) {
904 9 $thing = substr( $thing, 1 ) . '-1';
905 }
906 elsif ( $thing =~ m{\A[%]} ) {
907 4 $thing = substr( $thing, 1 ) . '-2';
908 }
909 382 return $thing;
910 }
911  
912 107 1;
913  
914 # ABSTRACT: Encapsulate one use statement in a document
915  
916 __END__
« blib/lib/App/perlimports/CLI.pm ↑ Summary blib/lib/App/perlimports/Document.pm »