blib/lib/App/perlimports/Document.pm

filter: Statementstmt 90.3% 513 / 568 Branchbran 79.0% 196 / 248 Conditioncond 78.3% 192 / 245 MC/DCmcdc 59.5% 75 / 126 Subroutinesub 93.0% 80 / 86 Podpod 50.0% 3 / 6 totaltotal 82.8% 1059 / 1279 CC 232 SCAR 25.5
CC
232
Cov
85%
_parse_constant
39.9
_build_possible_imports
35.1
_maybe_cache_inspectors
20.4
CRAP 12.8
« blib/lib/App/perlimports/Include.pm ↑ Summary blib/lib/App/perlimports/Annotations.pm »
1 package App::perlimports::Document;
2  
3 107 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 107 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 107 our $VERSION = '0.000067';
7  
8 107 ▶use App::perlimports::Annotations ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9 107 ▶use App::perlimports::ExportInspector ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10 107 ▶use App::perlimports::Include ();
Subroutine BEGIN: called CC=1 SCAR=0.0
11 107 ▶use App::perlimports::Sandbox ();
Subroutine BEGIN: called CC=1 SCAR=0.0
12 107 ▶use App::perlimports::Sorter ();
Subroutine BEGIN: called CC=1 SCAR=0.0
13 107 ▶use File::Basename qw( fileparse );
Subroutine BEGIN: called CC=1 SCAR=0.0
14 107 ▶use List::Util qw( any uniq );
Subroutine BEGIN: called CC=1 SCAR=0.0
15 107 ▶use Module::Runtime qw( module_notional_filename );
Subroutine BEGIN: called CC=1 SCAR=0.0
16 107 ▶use MooX::StrictConstructor;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 107 ▶use Path::Tiny qw( path );
Subroutine BEGIN: called CC=1 SCAR=0.0
18 107 ▶use PPI::Document ();
Subroutine BEGIN: called CC=1 SCAR=0.0
19 107 ▶use PPIx::Utils::Classification qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
20 is_class_name
21 is_function_call
22 is_hash_key
23 is_method_call
24 is_package_declaration
25 107 );
26 107 ▶use PPIx::Utils::Traversal qw( split_nodes_on_comma );
Subroutine BEGIN: called CC=1 SCAR=0.0
27 107 ▶use Ref::Util qw( is_plain_arrayref is_plain_hashref );
Subroutine BEGIN: called CC=1 SCAR=0.0
28 107 ▶use Scalar::Util qw( refaddr );
Subroutine BEGIN: called CC=1 SCAR=0.0
29 107 ▶use Sub::HandlesVia;
Subroutine BEGIN: called CC=1 SCAR=0.0
30 107 ▶use Text::Diff ();
Subroutine BEGIN: called CC=1 SCAR=0.0
31 107 ▶use Try::Tiny qw( catch try );
Subroutine BEGIN: called CC=1 SCAR=0.0
32 use Types::Standard
33 107 ▶ qw( ArrayRef Bool HashRef InstanceOf Int Maybe Object Str );
Subroutine BEGIN: called CC=1 SCAR=0.0
34  
35 107 with 'App::perlimports::Role::Logger';
36  
37 has _annotations => (
38 is => 'ro',
39 isa => InstanceOf ['App::perlimports::Annotations'],
40 lazy => 1,
41 125 ▶ default => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
42 125 return App::perlimports::Annotations->new(
43 ppi_document => shift->ppi_document );
44 },
45 107 );
46  
47 107 has _cache => (
48 is => 'ro',
49 isa => Bool,
50 init_arg => 'cache',
51 lazy => 1,
52 default => 0,
53 );
54  
55 107 has _cache_dir => (
56 is => 'ro',
57 isa => InstanceOf ['Path::Tiny'],
58 lazy => 1,
59 builder => '_build_cache_dir',
60 );
61  
62 107 has _filename => (
63 is => 'ro',
64 isa => Str,
65 init_arg => 'filename',
66 required => 1,
67 );
68  
69 has _ignore_modules => (
70 is => 'ro',
71 isa => HashRef,
72 init_arg => 'ignore_modules',
73 141 ▶ default => sub { +{} },
Subroutine __ANON__: called CC=1 SCAR=0.0
74 107 );
75  
76 has _ignore_modules_pattern => (
77 is => 'ro',
78 isa => ArrayRef [Str],
79 init_arg => 'ignore_modules_pattern',
80 144 ▶ default => sub { [] },
Subroutine __ANON__: called CC=1 SCAR=0.0
81 107 );
82  
83 # list of PPI::Statement::Include (use, no, require)
84 # (excluding pragmas, ignored modules, and 'use VERSION')
85 107 has includes => (
86 is => 'ro',
87 isa => ArrayRef [Object], # PPI::Statement::Include
88 lazy => 1,
89 builder => '_build_includes',
90 );
91  
92 # Deliberately an explicit accessor rather than a Sub::HandlesVia 'elements'
93 # delegation. Under the Sub::HandlesVia::XS backend the array handler caches the
94 # Perl stack pointer, then invokes this lazy builder via call_sv; the builder
95 # grows (and reallocates) the stack, leaving that cached pointer dangling, and
96 # the handler's subsequent write corrupts the heap. See issue #171.
97 110 ▶sub all_includes { return @{ $_[0]->includes }; }
Subroutine all_includes: called CC=1 SCAR=0.0
Pod undocumented
98  
99 # constant subs defined using 'use constant'
100 107 has constants => (
101 is => 'ro',
102 isa => HashRef,
103 handles_via => 'Hash',
104 handles => {
105 is_constant_name => 'exists',
106 all_constants => 'keys',
107 _set_constants => 'set',
108 },
109 predicate => '_has_constants',
110 lazy => 1,
111 builder => '_build_constants',
112 );
113  
114 has _inspectors => (
115 is => 'ro',
116 isa => HashRef [ Maybe [Object] ],
117 handles_via => 'Hash',
118 handles => {
119 all_inspector_names => 'keys',
120 _get_inspector_for => 'get',
121 _has_inspector_for => 'exists',
122 _set_inspector_for => 'set',
123 },
124 lazy => 1,
125 126 ▶ default => sub { +{} },
Subroutine __ANON__: called CC=1 SCAR=0.0
126 107 );
127  
128 # catalog of variables seen in interpolated context (string, qr, et al)
129 107 has interpolated_symbols => (
130 is => 'ro',
131 isa => HashRef,
132 lazy => 1,
133 builder => '_build_interpolated_symbols',
134 );
135  
136 # (lint mode only) whether to output json (or else console text)
137 107 has json => (
138 is => 'ro',
139 isa => Bool,
140 lazy => 1,
141 default => 0,
142 );
143  
144 has _json_encoder => (
145 is => 'ro',
146 isa => InstanceOf ['Cpanel::JSON::XS'],
147 lazy => 1,
148 2 ▶ default => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
149 2 require Cpanel::JSON::XS;
150 2 return Cpanel::JSON::XS->new;
151 },
152 107 );
153  
154 # are we processing in lint mode ? (otherwise edit mode)
155 107 has lint => (
156 is => 'ro',
157 isa => Bool,
158 lazy => 1,
159 default => 0,
160 );
161  
162 107 has my_own_inspector => (
163 is => 'ro',
164 isa => Maybe [ InstanceOf ['App::perlimports::ExportInspector'] ],
165 lazy => 1,
166 builder => '_build_my_own_inspector',
167 );
168  
169 107 has never_exports => (
170 is => 'ro',
171 isa => HashRef,
172 lazy => 1,
173 builder => '_build_never_exports',
174 );
175  
176 107 has _never_export_modules => (
177 is => 'ro',
178 isa => ArrayRef [Str],
179 init_arg => 'never_export_modules',
180 predicate => '_has_never_export_modules',
181 );
182  
183 # catalog of symbols explicitly imported (by package), e.g.
184 # Carp => ['croak', ..], ...
185 # in edit mode, this will be altered after processing (tidied_document)
186 # to reflect what we think the import statement should be.
187 107 has found_imports => (
188 is => 'ro',
189 isa => HashRef,
190 handles_via => 'Hash',
191 handles => {
192 _reset_found_import => 'set',
193 },
194 lazy => 1,
195 builder => '_build_found_imports',
196 );
197  
198 107 has _indent => (
199 is => 'ro',
200 isa => Int,
201 init_arg => 'indent',
202 default => 4,
203 );
204  
205 107 has _pad_brackets => (
206 is => 'ro',
207 isa => Bool,
208 init_arg => 'pad_brackets',
209 default => 0,
210 );
211  
212 107 has _padding => (
213 is => 'ro',
214 isa => Bool,
215 init_arg => 'padding',
216 default => 1,
217 );
218  
219 107 has _sort => (
220 is => 'ro',
221 isa => Bool,
222 init_arg => 'sort',
223 default => 0,
224 );
225  
226 107 has ppi_document => (
227 is => 'ro',
228 isa => Object,
229 lazy => 1,
230 builder => '_build_ppi_document',
231 );
232  
233 # list of tokens in the document that -could- have come from an import
234 # (but most are keywords, built-ins, lexical vars, defined funcs, etc.)
235 107 has possible_imports => (
236 is => 'ro',
237 isa => ArrayRef [Object], # isa PPI:Token:Word, :Symbol, :Magic
238 lazy => 1,
239 builder => '_build_possible_imports',
240 );
241  
242 # Explicit accessor rather than a Sub::HandlesVia 'elements' delegation; see the
243 # note on all_includes above and issue #171.
244 135 ▶sub possibly_imported_tokens { return @{ $_[0]->possible_imports }; }
Subroutine possibly_imported_tokens: called CC=1 SCAR=0.0
Pod undocumented
245  
246 has _ppi_selection => (
247 is => 'ro',
248 isa => Object,
249 init_arg => 'ppi_selection',
250 lazy => 1,
251 120 ▶ default => sub { $_[0]->ppi_document },
Subroutine __ANON__: called CC=1 SCAR=0.0
252 107 );
253  
254 107 has _preserve_duplicates => (
255 is => 'ro',
256 isa => Bool,
257 init_arg => 'preserve_duplicates',
258 default => 1,
259 );
260  
261 107 has _preserve_require => (
262 is => 'ro',
263 isa => Bool,
264 init_arg => 'preserve_require',
265 default => 1,
266 );
267  
268 107 has _preserve_unused => (
269 is => 'ro',
270 isa => Bool,
271 init_arg => 'preserve_unused',
272 default => 1,
273 );
274  
275 107 has _sub_exporter_export_list => (
276 is => 'ro',
277 isa => ArrayRef,
278 lazy => 1,
279 builder => '_build_sub_exporter_export_list',
280 );
281  
282 # Explicit accessor rather than a Sub::HandlesVia 'elements' delegation; see the
283 # note on all_includes above and issue #171.
284 113 ▶sub sub_exporter_export_list { return @{ $_[0]->_sub_exporter_export_list }; }
Subroutine sub_exporter_export_list: called CC=1 SCAR=0.0
Pod undocumented
285  
286 # catalog of the named subs defined, e.g.
287 # new => 1, ...
288 107 has _sub_names => (
289 is => 'ro',
290 isa => HashRef,
291 handles_via => 'Hash',
292 handles => {
293 is_sub_name => 'exists',
294 },
295 lazy => 1,
296 builder => '_build_sub_names',
297 );
298  
299 107 has _tidy_whitespace => (
300 is => 'ro',
301 isa => Bool,
302 init_arg => 'tidy_whitespace',
303 lazy => 1,
304 default => 1,
305 );
306  
307 107 has _verbose => (
308 is => 'ro',
309 isa => Bool,
310 init_arg => 'verbose',
311 default => 0,
312 );
313  
314 150 ▶around BUILDARGS => sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
315 150 my ( $orig, $class, @args ) = @_;
316  
317 150 my %args = @args;
318 150 ▶ if ( my $modules = delete $args{ignore_modules} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $modules = delete $args{"ignore_modules"}) 9 141 150
319 9 my %modules = map { $_ => 1 } @{$modules};
320 9 $args{ignore_modules} = \%modules;
321 }
322  
323 150 ▶ if ( my $selection = delete $args{selection} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $selection = delete $args{"selection"}) 26 124 150
324 26 $args{ppi_selection} = PPI::Document->new( \$selection );
325 }
326  
327 150 return $class->$orig(%args);
328 107 };
329  
330 107 my %default_ignore = (
331 'Carp::Always' => 1,
332 'Class::XSAccessor' => 1,
333 'Constant::Generate' => 1,
334 'Data::Printer' => 1,
335 'DDP' => 1,
336 'Devel::Confess' => 1,
337 'DynaLoader' => 1,
338 'Encode::Guess' => 1,
339 'Exception::Class' => 1,
340 'Exporter' => 1,
341 'Exporter::Lite' => 1,
342 'Feature::Compat::Try' => 1,
343 'Filter::Simple' => 1,
344 'Git::Sub' => 1,
345 'HTTP::Message::PSGI' => 1, # HTTP::Request::(to|from)_psgi
346 'Import::Into' => 1,
347 'local::lib' => 1,
348 'LWP::ConsoleLogger::Everywhere' => 1, # wraps UAs to log requests
349 'MLDBM' => 1,
350 'Modern::Perl' => 1,
351 'Mojo::Base' => 1,
352 'Mojo::Date' => 1,
353 'Mojolicious::Lite' => 1,
354 'Moo' => 1,
355 'Moo::Role' => 1,
356 'Moose' => 1,
357 'Moose::Exporter' => 1,
358 'Moose::Role' => 1,
359 'MooseX::NonMoose' => 1,
360 'MooseX::Role::Parameterized' => 1,
361 'MooseX::SemiAffordanceAccessor' => 1,
362 'MooseX::StrictConstructor' => 1,
363 'MooseX::TraitFor::Meta::Class::BetterAnonClassNames' => 1,
364 'MooseX::Types' => 1,
365 'MooX::StrictConstructor' => 1,
366 'namespace::autoclean' => 1,
367 'namespace::clean' => 1,
368 'PerlIO::gzip' => 1,
369 'Regexp::Common' => 1,
370 'Sort::ByExample' => 1,
371 'Struct::Dumb' => 1,
372 'Sub::Exporter' => 1,
373 'Sub::Exporter::Progressive' => 1,
374 'Sub::HandlesVia' => 1,
375 'Syntax::Keyword::Try' => 1,
376 'Term::Size::Any' => 1,
377 'Test2::Util::HashBase' => 1,
378 'Test::Exception' => 1,
379 'Test::Needs' => 1,
380 'Test::NoWarnings' => 1,
381 'Test::Number::Delta' => 1,
382 'Test::Pod' => 1,
383 'Test::Pod::Coverage' => 1,
384 'Test::Requires::Git' => 1,
385 'Test::RequiresInternet' => 1,
386 'Test::Warnings' => 1,
387 'Test::Whitespaces' => 1,
388 'Test::XML' => 1,
389 'Types::Standard' => 1,
390 'URI::QueryParam' => 1,
391 );
392  
393 # Funky stuff could happen with inner packages.
394 84 ▶sub _build_my_own_inspector {
Subroutine _build_my_own_inspector: called CC=4 SCAR=13.9
395 84 my $self = shift;
396 my $pkgs
397 = $self->ppi_document->find(
398 84 ▶ sub { $_[1]->isa('PPI::Statement::Package') && $_[1]->file_scoped } );
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
$_[1]->isa("PPI::Statement::Package") and $_[1]->file_scoped 8 5069 5077
399  
400 84 ▶ if ( !$pkgs || $pkgs->[0]->namespace eq 'main' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $pkgs or $pkgs->[0]->namespace eq "main") 79 5 84
Condition66%2/3
not $pkgs or $pkgs->[0]->namespace eq "main"
l!l&&r!l&&!r
7905
MC/DC50%1/2
not $pkgs or $pkgs->[0]->namespace eq "main"
not $pkgs $pkgs->[0]->namespace eq "main"
Truth table66%2/3
not(A) or B
A= not $pkgsB= $pkgs->[0]->namespace eq "main"
ABresult
1X1
011
000
401 79 return;
402 }
403  
404 5 my $pkg = $pkgs->[0];
405  
406 # file_scoped() doesn't seem to be very reliable, so let's just try a crude
407 # check to see if this is a package we might actually find on disk before
408 # we try to require it.
409 5 my $notional_file
410 = fileparse( module_notional_filename( $pkg->namespace ) );
411 5 my $provided_file = fileparse( $self->_filename );
412 5 ▶ return unless $notional_file eq $provided_file;
Branch: 1 branch
BranchTrueFalseTotal
unless $notional_file eq $provided_file 2 3 5
413  
414 3 return App::perlimports::ExportInspector->new(
415 logger => $self->logger,
416 module_name => $pkg->namespace,
417 );
418 }
419  
420 145 ▶sub _build_includes {
Subroutine _build_includes: called CC=2 SCAR=7.0
421 145 my $self = shift;
422  
423 # version() returns a value if this a dependency on a version of Perl, e.g
424 # use 5.006;
425 # require 5.006;
426 #
427 # We check for type so that we can filter out undef types or "no".
428  
429 ## no critic (Subroutines::ProhibitCallsToUnexportedSubs)
430 return $self->_ppi_selection->find(
431 7309 ▶ sub {
Subroutine __ANON__: called CC=9 SCAR=22.1
432 7309 ▶ $_[1]->isa('PPI::Statement::Include')
Branch: 1 branch
BranchTrueFalseTotal
$_[1]->isa("PPI::Statement::Include") and not $_[1]->pragma and not $_[1]->version and $_[1]->type and $_[1]->type eq "use" || $_[1]->type eq "require" and not $self->_is_ignored($_[1]) and not $self->_has_import_switches($_[1]->module) and not App::perlimports::Sandbox::eval_pkg($_[1]->module, "$_[1]") 198 7109 7307
Condition85%18/21
$_[1]->isa("PPI::Statement::Include") and not $_[1]->pragma and not $_[1]->version and $_[1]->type and $_[1]->type eq "use" || $_[1]->type eq "require" and not $self->_is_ignored($_[1]) and not $self->_has_import_switches($_[1]->module)
!ll&&!rl&&r
71081198
$_[1]->isa("PPI::Statement::Include") and not $_[1]->pragma and not $_[1]->version and $_[1]->type and $_[1]->type eq "use" || $_[1]->type eq "require" and not $self->_is_ignored($_[1])
!ll&&!rl&&r
706149199
$_[1]->isa("PPI::Statement::Include") and not $_[1]->pragma and not $_[1]->version and $_[1]->type and $_[1]->type eq "use" || $_[1]->type eq "require"
!ll&&!rl&&r
70610248
$_[1]->isa("PPI::Statement::Include") and not $_[1]->pragma and not $_[1]->version and $_[1]->type
!ll&&!rl&&r
70610248
$_[1]->isa("PPI::Statement::Include") and not $_[1]->pragma and not $_[1]->version
!ll&&!rl&&r
70592248
$_[1]->isa("PPI::Statement::Include") and not $_[1]->pragma
!ll&&!rl&&r
6846213250
$_[1]->type eq "use" or $_[1]->type eq "require"
l!l&&r!l&&!r
196520
MC/DC62%5/8
$_[1]->isa("PPI::Statement::Include") and not $_[1]->pragma and not $_[1]->version and $_[1]->type and $_[1]->type eq "use" || $_[1]->type eq "require" and not $self->_is_ignored($_[1]) and not $self->_has_import_switches($_[1]->module)
$_[1]->isa("PPI::Statement::Include") not $_[1]->pragma not $_[1]->version $_[1]->type $_[1]->type eq "use" $_[1]->type eq "require" not $self->_is_ignored($_[1]) not $self->_has_import_switches($_[1]->module)
Truth table72%8/11
A and not(B) and not(C) and D and E or F and not(G) and not(H)
A= $_[1]->isa("PPI::Statement::Include")B= not $_[1]->pragmaC= not $_[1]->versionD= $_[1]->typeE= $_[1]->type eq "use"F= $_[1]->type eq "require"G= not $self->_is_ignored($_[1])H= not $self->_has_import_switches($_[1]->module)
ABCDEFGHresult
0XXXXXXX0
10XXXXXX0
110XXXXX0
1110XXXX0
111100XX0
11111X0X0
1111010X0
11111X100
111101100
11111X111
111101111
433 && !$_[1]->pragma # no pragmas
434 && !$_[1]->version # Perl version requirement
435 && $_[1]->type
436 && ( $_[1]->type eq 'use'
437 || $_[1]->type eq 'require' )
438 && !$self->_is_ignored( $_[1] )
439 && !$self->_has_import_switches( $_[1]->module )
440 && !App::perlimports::Sandbox::eval_pkg(
441 $_[1]->module,
442 "$_[1]"
443 );
444 }
445 145 ▶ ) || [];
Condition100%2/2
$self->_ppi_selection->find(sub { $_[1]->isa('PPI::Statement::Include') and not $_[1]->pragma and not $_[1]->version and $_[1]->type and $_[1]->type eq 'use' || $_[1]->type eq 'require' and not $self->_is_ignored($_[1]) and not $self->_has_import_switches($_[1]->module) and not App::perlimports::Sandbox::eval_pkg($_[1]->module, "$_[1]"); } ) || []
l!l
11628
MC/DC100%1/1
$self->_ppi_selection->find(sub { $_[1]->isa('PPI::Statement::Include') and not $_[1]->pragma and not $_[1]->version and $_[1]->type and $_[1]->type eq 'use' || $_[1]->type eq 'require' and not $self->_is_ignored($_[1]) and not $self->_has_import_switches($_[1]->module) and not App::perlimports::Sandbox::eval_pkg($_[1]->module, "$_[1]"); } ) || []
$self->_ppi_selection->find(sub { $_[1]->isa('PPI::Statement::Include') and not $_[1]->pragma and not $_[1]->version and $_[1]->type and $_[1]->type eq 'use' || $_[1]->type eq 'require' and not $self->_is_ignored($_[1]) and not $self->_has_import_switches($_[1]->module) and not App::perlimports::Sandbox::eval_pkg($_[1]->module, "$_[1]"); } )
Truth table100%2/2
A
A= $self->_ppi_selection->find(sub { $_[1]->isa('PPI::Statement::Include') and not $_[1]->pragma and not $_[1]->version and $_[1]->type and $_[1]->type eq 'use' || $_[1]->type eq 'require' and not $self->_is_ignored($_[1]) and not $self->_has_import_switches($_[1]->module) and not App::perlimports::Sandbox::eval_pkg($_[1]->module, "$_[1]"); } )
Aresult
11
00
446 ## use critic
447 }
448  
449 71 ▶sub _build_constants {
Subroutine _build_constants: called CC=5 SCAR=16.1
450 71 my $self = shift;
451 71 my %constant;
452  
453 my $incs = $self->_ppi_selection->find(
454 4053 ▶ sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
455 4053 ▶ $_[1]->isa('PPI::Statement::Include')
Branch: 1 branch
BranchTrueFalseTotal
$_[1]->isa("PPI::Statement::Include") and $_[1]->module eq "constant" 237 3816 4053
456 && $_[1]->module eq 'constant';
457 }
458 71 ▶ ) || [];
Condition100%2/2
$self->_ppi_selection->find(sub { $_[1]->isa('PPI::Statement::Include') and $_[1]->module eq 'constant'; } ) || []
l!l
170
MC/DC100%1/1
$self->_ppi_selection->find(sub { $_[1]->isa('PPI::Statement::Include') and $_[1]->module eq 'constant'; } ) || []
$self->_ppi_selection->find(sub { $_[1]->isa('PPI::Statement::Include') and $_[1]->module eq 'constant'; } )
Truth table100%2/2
A
A= $self->_ppi_selection->find(sub { $_[1]->isa('PPI::Statement::Include') and $_[1]->module eq 'constant'; } )
Aresult
11
00
459  
460 71 foreach my $inc (@$incs) {
461 4 ▶ next unless my @data = $self->_parse_constant($inc);
Branch: 1 branch
BranchTrueFalseTotal
unless my(@data) = $self->_parse_constant($inc) 0 4 4
462  
463 # N.B. we never alter 'use constant' imports
464 4 foreach my $data (@data) {
465 6 $constant{ $data->{name} } = $data;
466 }
467 }
468  
469 71 $self->logger->debug("constants found: @{[sort keys %constant]}");
470  
471 71 return \%constant;
472 }
473  
474 # parse hashref into list of "values"; each an arrayref of tokens
475 3 ▶sub _parse_hashlist {
Subroutine _parse_hashlist: called CC=3 SCAR=11.2
476 3 my ($elem) = @_;
477 3 ▶ return unless $elem->isa('PPI::Structure::Constructor');
Branch: 1 branch
BranchTrueFalseTotal
unless $elem->isa("PPI::Structure::Constructor") 0 3 3
478 3 my ($exp) = $elem->schildren;
479 3 ▶ return unless $exp->isa('PPI::Statement::Expression');
Branch: 1 branch
BranchTrueFalseTotal
unless $exp->isa("PPI::Statement::Expression") 0 3 3
480 3 my @tokens = $exp->schildren;
481  
482 # first and last commas are ignored, if no significant child exists beyond!
483 # interior serial commas are not handled properly: {a => 1, 'b', , 'c', 4}
484 3 return my @values = split_nodes_on_comma(@tokens);
485 }
486  
487 # until we get a PPI::Statement::Include::Constant class
488 ## no critic (Subroutines::ProhibitExcessComplexity)
489 14 ▶sub _parse_constant {
Subroutine _parse_constant: called CC=22 SCAR=36.9
490 14 my ( $self, $stm ) = @_;
491 return
492 14 ▶ unless $stm->isa('PPI::Statement::Include')
Branch: 1 branch
BranchTrueFalseTotal
unless $stm->isa("PPI::Statement::Include") and $stm->module eq "constant" 0 14 14
Condition33%1/3
$stm->isa("PPI::Statement::Include") and $stm->module eq "constant"
!ll&&!rl&&r
0014
MC/DC0%0/2
$stm->isa("PPI::Statement::Include") and $stm->module eq "constant"
$stm->isa("PPI::Statement::Include") $stm->module eq "constant"
Truth table33%1/3
A and B
A= $stm->isa("PPI::Statement::Include")B= $stm->module eq "constant"
ABresult
0X0
100
111
493 && $stm->module eq 'constant';
494  
495 14 my ( $expected, @kv );
496  
497 14 ▶ return unless my @args = $stm->arguments;
Branch: 1 branch
BranchTrueFalseTotal
unless my(@args) = $stm->arguments 0 14 14
498 14 ▶ if ( @args > 1 ) {
Branch: 2 branches
BranchTrueFalseTotal
if (@args > 1) { } 11 3 14
elsif (my $first = $args[0]) { } 3 0 3
499 11 my ( $word, $op, @defn ) = @args;
500  
501 11 ▶ $expected = $op eq '=>' && $word->isa('PPI::Token::Word') && @defn;
Condition66%4/6
$op eq '=>' && $word->isa('PPI::Token::Word') && @defn
!ll&&!rl&&r
407
$op eq "=>" and $word->isa("PPI::Token::Word")
!ll&&!rl&&r
407
MC/DC33%1/3
$op eq '=>' && $word->isa('PPI::Token::Word') && @defn
$op eq "=>" $word->isa("PPI::Token::Word") @defn
Truth table50%2/4
A and B && C
A= $op eq "=>"B= $word->isa("PPI::Token::Word")C= @defn
ABCresult
0XX0
10X0
1100
1111
502  
503 11 ▶ $expected
Condition41%5/12
$expected ||= $word->isa("PPI::Token::Quote") && ($op eq "," || $op eq "=>") && @defn
l!l&&r!l&&!r
740
$word->isa('PPI::Token::Quote') && ($op eq ',' || $op eq '=>') && @defn
!ll&&!rl&&r
004
$word->isa("PPI::Token::Quote") and $op eq "," || $op eq "=>"
!ll&&!rl&&r
004
$op eq "," or $op eq "=>"
l!l&&r!l&&!r
400
MC/DC0%0/5
$expected ||= $word->isa("PPI::Token::Quote") && ($op eq "," || $op eq "=>") && @defn
$expected $word->isa("PPI::Token::Quote") $op eq "," $op eq "=>" @defn
Truth table0%0/7
A ||= B and C or D && E
A= $expectedB= $word->isa("PPI::Token::Quote")C= $op eq ","D= $op eq "=>"E= @defn
ABCDEresult
1XXXX1
011X11
010111
00XXX0
0100X0
011X00
010100
504 ||= $word->isa('PPI::Token::Quote')
505 && ( $op eq ',' || $op eq '=>' )
506 && @defn;
507  
508 11 ▶ @kv = ( $word, \@defn ) if $expected;
Branch: 1 branch
BranchTrueFalseTotal
if $expected 11 0 11
509  
510 }
511 elsif ( my $first = $args[0] ) { # @args == 1
512 3 ▶ if ( $first->isa('PPI::Structure::Constructor') ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($first->isa("PPI::Structure::Constructor")) 3 0 3
513  
514 # e.g.: use constant { FOO => 1, BAR => 3 };
515 # keys are arrayrefs, of (hopefully) a single token
516 3 @kv = _parse_hashlist($first);
517 3 $expected++;
518 }
519 }
520  
521 14 ▶ if ( !$expected || !@kv || @kv % 2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $expected && @kv or @kv % 2) 0 14 14
Condition33%2/6
not $expected && @kv or @kv % 2
l!l&&r!l&&!r
0014
$expected && @kv
!ll&&!rl&&r
0014
MC/DC0%0/3
not $expected && @kv or @kv % 2
$expected @kv @kv % 2
Truth table25%1/4
not(A && B) or C
A= $expectedB= @kvC= @kv % 2
ABCresult
0XX1
10X1
1111
1100
522 0 $self->logger->error("failed to parse constant: $stm");
523 0 return;
524 }
525  
526 14 my @constants;
527 14 ▶ while (@kv) {
Branch: 1 branch
BranchTrueFalseTotal
if (@kv) 20 14 34
528 20 my ( $word, $val ) = splice( @kv, 0, 2 );
529 20 ▶ if ( is_plain_arrayref($word) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_plain_arrayref($word)) 9 11 20
530 9 ▶ if ( @$word > 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$word > 1) 0 9 9
531 0 $self->logger->error("failed to parse constant: @$word");
532 0 next;
533 }
534 9 $word = $word->[0];
535 }
536  
537 20 ▶ my $name = $word->isa('PPI::Token::Quote') ? $word->string : "$word";
Branch: 1 branch
BranchTrueFalseTotal
$word->isa('PPI::Token::Quote') ? : 10 10 20
538 20 my %data = (
539 token => $word,
540 name => $name,
541 definition => $val,
542 );
543  
544 # position in file after which this function exists (compile time)
545 # TODO location data can become stale
546 20 ▶ if ( my $end = $stm->last_element ) { # likely PPI:Token:Structure ';'
Branch: 1 branch
BranchTrueFalseTotal
if (my $end = $stm->last_element) 20 0 20
547 20 my $loc = _elem_loc($end);
548 20 $data{end} = $loc->{end};
549 }
550  
551 20 push @constants, \%data;
552 }
553  
554 14 return @constants;
555 }
556 ## use critic
557  
558 ## no critic (Subroutines::ProhibitExcessComplexity)
559 97 ▶sub _build_possible_imports {
Subroutine _build_possible_imports: called CC=33 SCAR=35.6
560 97 my $self = shift;
561 my $before = $self->ppi_document->find(
562 6040 ▶ sub {
Subroutine __ANON__: called CC=4 SCAR=13.9
563 6040 ▶ $_[1]->isa('PPI::Token::Word')
Branch: 1 branch
BranchTrueFalseTotal
$_[1]->isa("PPI::Token::Word") or $_[1]->isa("PPI::Token::Symbol") or $_[1]->isa("PPI::Token::Label") or $_[1]->isa("PPI::Token::Prototype") 1296 4744 6040
Condition83%5/6
$_[1]->isa("PPI::Token::Word") or $_[1]->isa("PPI::Token::Symbol") or $_[1]->isa("PPI::Token::Label")
l!l&&r!l&&!r
129604744
$_[1]->isa("PPI::Token::Word") or $_[1]->isa("PPI::Token::Symbol")
l!l&&r!l&&!r
11351614744
MC/DC66%2/3
$_[1]->isa("PPI::Token::Word") or $_[1]->isa("PPI::Token::Symbol") or $_[1]->isa("PPI::Token::Label")
$_[1]->isa("PPI::Token::Word") $_[1]->isa("PPI::Token::Symbol") $_[1]->isa("PPI::Token::Label")
Truth table75%3/4
A or B or C
A= $_[1]->isa("PPI::Token::Word")B= $_[1]->isa("PPI::Token::Symbol")C= $_[1]->isa("PPI::Token::Label")
ABCresult
1XX1
01X1
0011
0000
564 || $_[1]->isa('PPI::Token::Symbol')
565 || $_[1]->isa('PPI::Token::Label')
566 || $_[1]->isa('PPI::Token::Prototype');
567 }
568 97 ▶ ) || [];
Condition50%1/2
$self->ppi_document->find(sub { $_[1]->isa('PPI::Token::Word') or $_[1]->isa('PPI::Token::Symbol') or $_[1]->isa('PPI::Token::Label') or $_[1]->isa('PPI::Token::Prototype'); } ) || []
l!l
970
MC/DC0%0/1
$self->ppi_document->find(sub { $_[1]->isa('PPI::Token::Word') or $_[1]->isa('PPI::Token::Symbol') or $_[1]->isa('PPI::Token::Label') or $_[1]->isa('PPI::Token::Prototype'); } ) || []
$self->ppi_document->find(sub { $_[1]->isa('PPI::Token::Word') or $_[1]->isa('PPI::Token::Symbol') or $_[1]->isa('PPI::Token::Label') or $_[1]->isa('PPI::Token::Prototype'); } )
Truth table50%1/2
A
A= $self->ppi_document->find(sub { $_[1]->isa('PPI::Token::Word') or $_[1]->isa('PPI::Token::Symbol') or $_[1]->isa('PPI::Token::Label') or $_[1]->isa('PPI::Token::Prototype'); } )
Aresult
11
00
569  
570 # remember referenced package namespaces and constants
571 97 my ( %pack, %const );
572  
573 97 my @after;
574 97 for my $word ( @{$before} ) {
575  
576 # Without the sub name check, we accidentally turn
577 # use List::Util ();
578 # sub any { }
579 #
580 # into
581 #
582 # use List::Util qw( any );
583 # sub any {}
584 1296 ▶ next if $self->is_sub_name("$word");
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_sub_name("$word") 22 1274 1296
585  
586 # cant validate class/instance method names:
587 1274 ▶ next if !$word->isa('PPI::Token::Symbol') && is_method_call($word);
Branch: 1 branch
BranchTrueFalseTotal
if not $word->isa("PPI::Token::Symbol") and is_method_call($word) 9 1265 1274
Condition100%3/3
not $word->isa("PPI::Token::Symbol") and is_method_call($word)
!ll&&!rl&&r
16111049
MC/DC100%2/2
not $word->isa("PPI::Token::Symbol") and is_method_call($word)
not $word->isa("PPI::Token::Symbol") is_method_call($word)
Truth table100%3/3
not(A) and B
A= not $word->isa("PPI::Token::Symbol")B= is_method_call($word)
ABresult
0X0
100
111
588  
589 1265 ▶ next if _is_word_interpreted_as_string($word);
Branch: 1 branch
BranchTrueFalseTotal
if _is_word_interpreted_as_string($word) 18 1247 1265
590  
591 1247 ▶ next if is_package_declaration($word);
Branch: 1 branch
BranchTrueFalseTotal
if is_package_declaration($word) 8 1239 1247
592  
593 1239 ▶ if ( my $stm = $word->statement ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $stm = $word->statement) 1239 0 1239
594 1239 my $first = $stm->schild(0); # PPI:Element
595 1239 ▶ if ( $stm->isa('PPI::Statement::Package') ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($stm->isa('PPI::Statement::Package')) { } 8 1231 1239
elsif ($stm->isa('PPI::Statement::Include')) { } 705 526 1231
596 8 ▶ next if $word eq 'package' && $word == $first;
Branch: 1 branch
BranchTrueFalseTotal
if $word eq "package" and $word == $first 8 0 8
Condition33%1/3
$word eq "package" and $word == $first
!ll&&!rl&&r
008
MC/DC0%0/2
$word eq "package" and $word == $first
$word eq "package" $word == $first
Truth table33%1/3
A and B
A= $word eq "package"B= $word == $first
ABresult
0X0
100
111
597  
598 }
599 elsif ( $stm->isa('PPI::Statement::Include') ) {
600 705 ▶ if ( my $packname = $stm->module ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $packname = $stm->module) 704 1 705
601 704 ▶ $pack{$packname} ||= 1; # even if its a pragma
Condition100%2/2
$pack{$packname} ||= 1
l!l
365339
MC/DC100%1/1
$pack{$packname} ||= 1
$pack{$packname}
Truth table100%2/2
A
A= $pack{$packname}
Aresult
11
00
602  
603 704 ▶ if ( $packname eq 'constant' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($packname eq "constant") 10 694 704
604 10 ▶ next unless my @data = $self->_parse_constant($stm);
Branch: 1 branch
BranchTrueFalseTotal
unless my(@data) = $self->_parse_constant($stm) 0 10 10
605 10 ▶ $const{ $_->{name} } ||= $_ for @data;
Condition66%2/3
$const{$_->{"name"}} ||= $_
l!l&&r!l&&!r
860
MC/DC0%0/2
$const{$_->{"name"}} ||= $_
$const{$_->{"name"}} $_
Truth table66%2/3
A ||= B
A= $const{$_->{"name"}}B= $_
ABresult
1X1
011
000
606 10 ▶ next if any { $word == $_->{token} } @data;
Subroutine __ANON__: called CC=1 SCAR=0.2
Branch: 1 branch
BranchTrueFalseTotal
if &any(sub { $word == $_->{"token"}; } , @data) 0 10 10
607 }
608  
609 704 my $package_bareword = $stm->schild(1);
610 704 ▶ next if $word == $package_bareword;
Branch: 1 branch
BranchTrueFalseTotal
if $word == $package_bareword 350 354 704
611 }
612  
613 355 ▶ next if $word eq 'use' && $word == $first;
Branch: 1 branch
BranchTrueFalseTotal
if $word eq "use" and $word == $first 349 6 355
Condition66%2/3
$word eq "use" and $word == $first
!ll&&!rl&&r
60349
MC/DC50%1/2
$word eq "use" and $word == $first
$word eq "use" $word == $first
Truth table66%2/3
A and B
A= $word eq "use"B= $word == $first
ABresult
0X0
100
111
614 6 ▶ next if $stm->pragma && $word eq 'no' && $word == $first;
Branch: 1 branch
BranchTrueFalseTotal
if $stm->pragma and $word eq "no" and $word == $first 1 5 6
Condition83%5/6
$stm->pragma and $word eq "no" and $word == $first
!ll&&!rl&&r
501
$stm->pragma and $word eq "no"
!ll&&!rl&&r
231
MC/DC66%2/3
$stm->pragma and $word eq "no" and $word == $first
$stm->pragma $word eq "no" $word == $first
Truth table75%3/4
A and B and C
A= $stm->pragmaB= $word eq "no"C= $word == $first
ABCresult
0XX0
10X0
1100
1111
615 }
616 }
617  
618 next
619 531 ▶ if $const{"$word"}
Branch: 1 branch
BranchTrueFalseTotal
if $const{"$word"} and is_hash_key($word) || is_function_call($word) 7 524 531
Condition66%4/6
$const{"$word"} and is_hash_key($word) || is_function_call($word)
!ll&&!rl&&r
52407
is_hash_key($word) or is_function_call($word)
l!l&&r!l&&!r
250
MC/DC33%1/3
$const{"$word"} and is_hash_key($word) || is_function_call($word)
$const{"$word"} is_hash_key($word) is_function_call($word)
Truth table75%3/4
A and B or C
A= $const{"$word"}B= is_hash_key($word)C= is_function_call($word)
ABCresult
0XX0
1000
11X1
1011
620 && ( is_hash_key($word) || is_function_call($word) );
621 524 ▶ next if $pack{"$word"} && is_class_name($word); # class method calls
Branch: 1 branch
BranchTrueFalseTotal
if $pack{"$word"} and is_class_name($word) 5 519 524
Condition100%3/3
$pack{"$word"} and is_class_name($word)
!ll&&!rl&&r
51815
MC/DC100%2/2
$pack{"$word"} and is_class_name($word)
$pack{"$word"} is_class_name($word)
Truth table100%3/3
A and B
A= $pack{"$word"}B= is_class_name($word)
ABresult
0X0
100
111
622  
623 519 push @after, $word;
624 }
625  
626 # safe to assume our search encountered every 'use constant' statement
627 97 ▶ if ( %const && !$self->_has_constants ) {
Branch: 1 branch
BranchTrueFalseTotal
if (%const and not $self->_has_constants) 1 96 97
Condition66%2/3
%const and not $self->_has_constants
!ll&&!rl&&r
9601
MC/DC50%1/2
%const and not $self->_has_constants
%const not $self->_has_constants
Truth table66%2/3
A and not(B)
A= %constB= not $self->_has_constants
ABresult
0X0
100
111
628 1 $self->_set_constants(%const);
629 1 $self->logger->debug("constants found: @{[sort keys %const]}");
630 }
631  
632 97 return \@after;
633 }
634 ## use critic
635  
636 124 ▶sub _build_ppi_document {
Subroutine _build_ppi_document: called CC=1 SCAR=0.0
637 124 my $self = shift;
638 124 return PPI::Document->new( $self->_filename );
639 }
640  
641 # Create a key for every included module.
642 # use Carp;
643 # use Data::Dumper qw( Dumper );
644 # use POSIX ();
645 #
646 # becomes:
647 #
648 # {
649 # Carp => undef,
650 # 'Data::Dumper' => ['Dumper'],
651 # POSIX => [],
652 # }
653 #
654 # It starts out as a list of original imports, but with each include that gets
655 # processed, this list gets updated to what the include will import once
656 # tidied (an empty list if the include is removed as unused). This happens in
657 # both lint and edit mode, although lint mode never modifies the document. We
658 # do this so that we can keep track of what previous modules are really
659 # importing, avoiding duplicate imports.
660  
661 121 ▶sub _build_found_imports {
Subroutine _build_found_imports: called CC=6 SCAR=17.9
662 121 my $self = shift;
663  
664 # We're missing requires which could be followed by an import.
665 my $found = $self->ppi_document->find(
666 8020 ▶ sub {
Subroutine __ANON__: called CC=5 SCAR=16.1
667 8020 ▶ $_[1]->isa('PPI::Statement::Include')
Branch: 1 branch
BranchTrueFalseTotal
$_[1]->isa("PPI::Statement::Include") and not $_[1]->pragma and not $_[1]->version and $_[1]->type and $_[1]->type eq "use" 230 7790 8020
Condition88%8/9
$_[1]->isa("PPI::Statement::Include") and not $_[1]->pragma and not $_[1]->version and $_[1]->type
!ll&&!rl&&r
77900230
$_[1]->isa("PPI::Statement::Include") and not $_[1]->pragma and not $_[1]->version
!ll&&!rl&&r
77891230
$_[1]->isa("PPI::Statement::Include") and not $_[1]->pragma
!ll&&!rl&&r
7557232231
MC/DC75%3/4
$_[1]->isa("PPI::Statement::Include") and not $_[1]->pragma and not $_[1]->version and $_[1]->type
$_[1]->isa("PPI::Statement::Include") not $_[1]->pragma not $_[1]->version $_[1]->type
Truth table80%4/5
A and not(B) and not(C) and D
A= $_[1]->isa("PPI::Statement::Include")B= not $_[1]->pragmaC= not $_[1]->versionD= $_[1]->type
ABCDresult
0XXX0
10XX0
110X0
11100
11111
668 && !$_[1]->pragma # no pragmas
669 && !$_[1]->version # Perl version requirement
670 && $_[1]->type
671 && $_[1]->type eq 'use';
672 }
673 121 ▶ ) || [];
Condition100%2/2
$self->ppi_document->find(sub { $_[1]->isa('PPI::Statement::Include') and not $_[1]->pragma and not $_[1]->version and $_[1]->type and $_[1]->type eq 'use'; } ) || []
l!l
11110
MC/DC100%1/1
$self->ppi_document->find(sub { $_[1]->isa('PPI::Statement::Include') and not $_[1]->pragma and not $_[1]->version and $_[1]->type and $_[1]->type eq 'use'; } ) || []
$self->ppi_document->find(sub { $_[1]->isa('PPI::Statement::Include') and not $_[1]->pragma and not $_[1]->version and $_[1]->type and $_[1]->type eq 'use'; } )
Truth table100%2/2
A
A= $self->ppi_document->find(sub { $_[1]->isa('PPI::Statement::Include') and not $_[1]->pragma and not $_[1]->version and $_[1]->type and $_[1]->type eq 'use'; } )
Aresult
11
00
674  
675 121 my %imports;
676  
677 121 for my $include ( @{$found} ) {
678 169 my $pkg = $include->module;
679 169 ▶ $imports{$pkg} = undef unless exists $imports{$pkg};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $imports{$pkg} 162 7 169
680  
681 # this is probably wrong
682 #next if $self->_is_ignored($pkg);
683  
684 # If a module has been included multiple times, we want to have a
685 # cumulative tally of what has been explicitly imported.
686 169 my $found_for_include = _imports_for_include($include);
687 169 ▶ if ($found_for_include) {
Branch: 1 branch
BranchTrueFalseTotal
if ($found_for_include) 110 59 169
688 110 ▶ if ( $imports{$pkg} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($imports{$pkg}) { } 5 105 110
689 15 my %catalog = map { $_ => 1 } @{ $imports{$pkg} },
690 5 @{$found_for_include};
691 5 $imports{$pkg} = [ sort keys %catalog ];
692 }
693 else {
694 105 $imports{$pkg} = $found_for_include;
695 }
696 }
697 }
698  
699 121 return \%imports;
700 }
701  
702 84 ▶sub _build_sub_exporter_export_list {
Subroutine _build_sub_exporter_export_list: called CC=8 SCAR=20.8
703 84 my $self = shift;
704  
705 my $sub_ex = $self->ppi_document->find(
706 5077 ▶ sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
707 5077 ▶ $_[1]->isa('PPI::Statement::Include')
Branch: 1 branch
BranchTrueFalseTotal
$_[1]->isa("PPI::Statement::Include") and $_[1]->module eq "Sub::Exporter" 299 4778 5077
708 && $_[1]->module eq 'Sub::Exporter';
709 }
710 84 ▶ ) || [];
Condition100%2/2
$self->ppi_document->find(sub { $_[1]->isa('PPI::Statement::Include') and $_[1]->module eq 'Sub::Exporter'; } ) || []
l!l
183
MC/DC100%1/1
$self->ppi_document->find(sub { $_[1]->isa('PPI::Statement::Include') and $_[1]->module eq 'Sub::Exporter'; } ) || []
$self->ppi_document->find(sub { $_[1]->isa('PPI::Statement::Include') and $_[1]->module eq 'Sub::Exporter'; } )
Truth table100%2/2
A
A= $self->ppi_document->find(sub { $_[1]->isa('PPI::Statement::Include') and $_[1]->module eq 'Sub::Exporter'; } )
Aresult
11
00
711 84 ▶ return [] unless @{$sub_ex};
Branch: 1 branch
BranchTrueFalseTotal
unless @{$sub_ex;} 83 1 84
712  
713 1 my @found;
714 1 for my $include ( @{$sub_ex} ) {
715 1 my @arguments = $include->arguments;
716 1 for my $arg (@arguments) {
717 3 ▶ if ( $arg->isa('PPI::Structure::Constructor') ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($arg->isa("PPI::Structure::Constructor")) 1 2 3
718 ## no critic (BuiltinFunctions::ProhibitStringyEval)
719 1 my $thing = eval $arg;
720 1 ▶ if ( is_plain_hashref($thing) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_plain_hashref($thing)) 1 0 1
721 1 ▶ if ( is_plain_arrayref( $thing->{exports} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_plain_arrayref($thing->{"exports"})) 1 0 1
722 1 push @found, @{ $thing->{exports} };
723 }
724 }
725 }
726 }
727 }
728  
729 1 return [ uniq @found ];
730 }
731  
732 246 ▶sub _imports_for_include {
Subroutine _imports_for_include: called CC=7 SCAR=19.5
733 246 my $include = shift;
734  
735 246 my $imports = undef;
736  
737 246 for my $child ( $include->schildren ) {
738 995 ▶ if ( $child->isa('PPI::Structure::List')
Branch: 1 branch
BranchTrueFalseTotal
if ($child->isa("PPI::Structure::List") and not defined $imports) 30 965 995
Condition66%2/3
$child->isa("PPI::Structure::List") and not defined $imports
!ll&&!rl&&r
965030
MC/DC50%1/2
$child->isa("PPI::Structure::List") and not defined $imports
$child->isa("PPI::Structure::List") not defined $imports
Truth table66%2/3
A and not(B)
A= $child->isa("PPI::Structure::List")B= not defined $imports
ABresult
0X0
100
111
739 && !defined $imports ) {
740 30 $imports = [];
741 }
742 995 ▶ if ( !$child->isa('PPI::Token::QuoteLike::Words')
Branch: 1 branch
BranchTrueFalseTotal
unless ($child->isa("PPI::Token::QuoteLike::Words") or $child->isa("PPI::Token::Quote::Single")) 836 159 995
Condition100%3/3
$child->isa("PPI::Token::QuoteLike::Words") or $child->isa("PPI::Token::Quote::Single")
l!l&&r!l&&!r
14910836
MC/DC100%2/2
$child->isa("PPI::Token::QuoteLike::Words") or $child->isa("PPI::Token::Quote::Single")
$child->isa("PPI::Token::QuoteLike::Words") $child->isa("PPI::Token::Quote::Single")
Truth table100%3/3
A or B
A= $child->isa("PPI::Token::QuoteLike::Words")B= $child->isa("PPI::Token::Quote::Single")
ABresult
1X1
011
000
743 && !$child->isa('PPI::Token::Quote::Single') ) {
744 836 next;
745 }
746 159 ▶ if ( defined $imports ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $imports) { } 5 154 159
747 5 push( @{$imports}, $child->literal );
748 }
749 else {
750 154 $imports = [ $child->literal ];
751 }
752 }
753 246 return $imports;
754 }
755  
756 50 ▶sub _extract_symbols_from_snippet {
Subroutine _extract_symbols_from_snippet: called CC=14 SCAR=26.4
757 50 my $snippet = shift;
758 50 ▶ return () unless defined $snippet;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $snippet 3 47 50
759  
760 # Restore line breaks and tabs
761 47 $snippet =~ s{\\n}{\n}g;
762 47 $snippet =~ s{\\t}{\t}g;
763  
764 47 my $doc = PPI::Document->new( \$snippet );
765 47 ▶ return () unless defined $doc;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $doc 1 46 47
766  
767 my @symbols
768 46 ▶ = map { $_ . q{} } @{ $doc->find('PPI::Token::Symbol') || [] };
Condition100%2/2
$doc->find('PPI::Token::Symbol') || []
l!l
838
MC/DC100%1/1
$doc->find('PPI::Token::Symbol') || []
$doc->find('PPI::Token::Symbol')
Truth table100%2/2
A
A= $doc->find('PPI::Token::Symbol')
Aresult
11
00
769  
770 46 ▶ my $casts = $doc->find('PPI::Token::Cast') || [];
Condition100%2/2
$doc->find('PPI::Token::Cast') || []
l!l
640
MC/DC100%1/1
$doc->find('PPI::Token::Cast') || []
$doc->find('PPI::Token::Cast')
Truth table100%2/2
A
A= $doc->find('PPI::Token::Cast')
Aresult
11
00
771 46 for my $cast ( @{$casts} ) {
772  
773 # PPI Edge Case: False positive casts from regex assertions
774 #
775 # PPI can misinterpret regex assertions like \A as casts. We don't
776 # want to match on "A" in: if ( $thing =~ m{ \A b }x ) { ... }
777 #
778 # See also: Similar PPI edge case for quote operators at line ~596
779 15 ▶ next if $cast eq '\\';
Branch: 1 branch
BranchTrueFalseTotal
if $cast eq "\\" 11 4 15
780  
781 4 my $full_cast = $cast . $cast->snext_sibling;
782 4 my $cast_as_doc = PPI::Document->new( \$full_cast );
783 push @symbols,
784 1 map { $_ . q{} }
785 4 ▶ @{ $cast_as_doc->find('PPI::Token::Symbol') || [] };
Condition100%2/2
$cast_as_doc->find('PPI::Token::Symbol') || []
l!l
13
MC/DC100%1/1
$cast_as_doc->find('PPI::Token::Symbol') || []
$cast_as_doc->find('PPI::Token::Symbol')
Truth table100%2/2
A
A= $cast_as_doc->find('PPI::Token::Symbol')
Aresult
11
00
786  
787 4 ▶ my $words = $cast_as_doc->find('PPI::Token::Word') || [];
Condition100%2/2
$cast_as_doc->find('PPI::Token::Word') || []
l!l
31
MC/DC100%1/1
$cast_as_doc->find('PPI::Token::Word') || []
$cast_as_doc->find('PPI::Token::Word')
Truth table100%2/2
A
A= $cast_as_doc->find('PPI::Token::Word')
Aresult
11
00
788  
789 ## Turn ${FOO} into $FOO
790 4 ▶ if ( $words
Branch: 1 branch
BranchTrueFalseTotal
if ($words and scalar @$words == 1 and $full_cast =~ /([\$\@\%])\{$words->[0]}/) 1 3 4
Condition83%5/6
$words and scalar @$words == 1 and $full_cast =~ /([\$\@\%])\{$words->[0]}/
!ll&&!rl&&r
211
$words and scalar @$words == 1
!ll&&!rl&&r
022
MC/DC66%2/3
$words and scalar @$words == 1 and $full_cast =~ /([\$\@\%])\{$words->[0]}/
$words scalar @$words == 1 $full_cast =~ /([\$\@\%])\{$words->[0]}/
Truth table75%3/4
A and B and C
A= $wordsB= scalar @$words == 1C= $full_cast =~ /([\$\@\%])\{$words->[0]}/
ABCresult
0XX0
10X0
1100
1111
791 && scalar @$words == 1
792 && $full_cast =~ m/([\$\@\%])\{$words->[0]}/ ) {
793 1 push @symbols, $1 . $words->[0];
794 1 next;
795 }
796  
797 # This could likely be a source of false positives.
798 3 for my $word (@$words) {
799 3 ▶ push @symbols, "$word" if is_function_call($word);
Branch: 1 branch
BranchTrueFalseTotal
if is_function_call($word) 2 1 3
800 }
801 }
802  
803 46 return @symbols;
804 }
805  
806 43 ▶sub _unnest_quotes {
Subroutine _unnest_quotes: called CC=7 SCAR=19.5
807 43 my $self = shift;
808 43 my $token = shift;
809 43 my @words = @_;
810  
811 43 ▶ if ( !$token->isa('PPI::Token::Quote')
Branch: 1 branch
BranchTrueFalseTotal
if (not $token->isa("PPI::Token::Quote") or $token->isa("PPI::Token::Quote::Single")) 8 35 43
Condition100%3/3
not $token->isa("PPI::Token::Quote") or $token->isa("PPI::Token::Quote::Single")
l!l&&r!l&&!r
6235
MC/DC100%2/2
not $token->isa("PPI::Token::Quote") or $token->isa("PPI::Token::Quote::Single")
not $token->isa("PPI::Token::Quote") $token->isa("PPI::Token::Quote::Single")
Truth table100%3/3
not(A) or B
A= not $token->isa("PPI::Token::Quote")B= $token->isa("PPI::Token::Quote::Single")
ABresult
1X1
011
000
812 || $token->isa('PPI::Token::Quote::Single') ) {
813 8 return @words;
814 }
815  
816 35 push @words, _extract_symbols_from_snippet( $token->string );
817  
818 35 my $doc = PPI::Document->new( \$token->string );
819 35 ▶ return @words unless $doc;
Branch: 1 branch
BranchTrueFalseTotal
unless $doc 1 34 35
820  
821 34 my $quotes = $doc->find('PPI::Token::Quote');
822 34 ▶ return @words unless $quotes;
Branch: 1 branch
BranchTrueFalseTotal
unless $quotes 24 10 34
823  
824 10 for my $q (@$quotes) {
825  
826 # PPI Edge Case: False positive quote operators
827 #
828 # PPI sometimes misinterprets content inside double-quoted strings as
829 # quote operators. For example, when parsing pack("qq", ...), PPI may
830 # identify the bare "qq" as a PPI::Token::Quote even though it's just
831 # a string literal. Calling ->string on these false positives causes
832 # "Use of uninitialized value" errors because they lack actual content.
833 #
834 # Detection: Real quote operators have delimiters (e.g., q{}, qq[],
835 # qw()). False positives are just the bare operator name.
836 #
837 # See also: Similar PPI edge case handling for casts at line ~546
838 # where regex assertions like \A can be misinterpreted as casts.
839 10 my $quote_str = "$q";
840 10 ▶ next if $quote_str =~ m/\A(?:qq?|qw|qx|qr|m|s|tr|y)\z/;
Branch: 1 branch
BranchTrueFalseTotal
if $quote_str =~ /\A(?:qq?|qw|qx|qr|m|s|tr|y)\z/ 6 4 10
841  
842 4 push @words, _extract_symbols_from_snippet("$q");
843 4 push @words, $self->_unnest_quotes($q);
844 }
845  
846 10 return @words;
847 }
848  
849 88 ▶sub _build_interpolated_symbols {
Subroutine _build_interpolated_symbols: called CC=14 SCAR=26.4
850 88 my $self = shift;
851 88 my @symbols;
852  
853 88 for my $token (
854 @{
855 $self->ppi_document->find(
856 5278 ▶ sub {
Subroutine __ANON__: called CC=5 SCAR=16.1
857 5278 ▶ ( $_[1]->isa('PPI::Token::Quote')
Branch: 1 branch
BranchTrueFalseTotal
$_[1]->isa("PPI::Token::Quote") and not $_[1]->isa("PPI::Token::Quote::Single") or $_[1]->isa("PPI::Token::Quote::Interpolate") or $_[1]->isa("PPI::Token::QuoteLike::Regexp") or $_[1]->isa("PPI::Token::Regexp") 36 5242 5278
Condition88%8/9
$_[1]->isa("PPI::Token::Quote") and not $_[1]->isa("PPI::Token::Quote::Single") or $_[1]->isa("PPI::Token::Quote::Interpolate") or $_[1]->isa("PPI::Token::QuoteLike::Regexp")
l!l&&r!l&&!r
3335242
$_[1]->isa("PPI::Token::Quote") and not $_[1]->isa("PPI::Token::Quote::Single") or $_[1]->isa("PPI::Token::Quote::Interpolate")
l!l&&r!l&&!r
3305245
$_[1]->isa("PPI::Token::Quote") and not $_[1]->isa("PPI::Token::Quote::Single")
!ll&&!rl&&r
51895633
MC/DC75%3/4
$_[1]->isa("PPI::Token::Quote") and not $_[1]->isa("PPI::Token::Quote::Single") or $_[1]->isa("PPI::Token::Quote::Interpolate") or $_[1]->isa("PPI::Token::QuoteLike::Regexp")
$_[1]->isa("PPI::Token::Quote") not $_[1]->isa("PPI::Token::Quote::Single") $_[1]->isa("PPI::Token::Quote::Interpolate") $_[1]->isa("PPI::Token::QuoteLike::Regexp")
Truth table57%4/7
A and not(B) or C or D
A= $_[1]->isa("PPI::Token::Quote")B= not $_[1]->isa("PPI::Token::Quote::Single")C= $_[1]->isa("PPI::Token::Quote::Interpolate")D= $_[1]->isa("PPI::Token::QuoteLike::Regexp")
ABCDresult
11XX1
0X1X1
101X1
0X011
10011
0X000
10000
858 && !$_[1]->isa('PPI::Token::Quote::Single') )
859 || $_[1]->isa('PPI::Token::Quote::Interpolate')
860 || $_[1]->isa('PPI::Token::QuoteLike::Regexp')
861 || $_[1]->isa('PPI::Token::Regexp');
862 }
863 )
864 88 ▶ || []
Condition100%2/2
$self->ppi_document->find(sub { $_[1]->isa('PPI::Token::Quote') and not $_[1]->isa('PPI::Token::Quote::Single') or $_[1]->isa('PPI::Token::Quote::Interpolate') or $_[1]->isa('PPI::Token::QuoteLike::Regexp') or $_[1]->isa('PPI::Token::Regexp'); } ) || []
l!l
1672
MC/DC100%1/1
$self->ppi_document->find(sub { $_[1]->isa('PPI::Token::Quote') and not $_[1]->isa('PPI::Token::Quote::Single') or $_[1]->isa('PPI::Token::Quote::Interpolate') or $_[1]->isa('PPI::Token::QuoteLike::Regexp') or $_[1]->isa('PPI::Token::Regexp'); } ) || []
$self->ppi_document->find(sub { $_[1]->isa('PPI::Token::Quote') and not $_[1]->isa('PPI::Token::Quote::Single') or $_[1]->isa('PPI::Token::Quote::Interpolate') or $_[1]->isa('PPI::Token::QuoteLike::Regexp') or $_[1]->isa('PPI::Token::Regexp'); } )
Truth table100%2/2
A
A= $self->ppi_document->find(sub { $_[1]->isa('PPI::Token::Quote') and not $_[1]->isa('PPI::Token::Quote::Single') or $_[1]->isa('PPI::Token::Quote::Interpolate') or $_[1]->isa('PPI::Token::QuoteLike::Regexp') or $_[1]->isa('PPI::Token::Regexp'); } )
Aresult
11
00
865 }
866 ) {
867 39 ▶ if ( $token->isa('PPI::Token::Regexp')
Branch: 1 branch
BranchTrueFalseTotal
if ($token->isa("PPI::Token::Regexp") or $token->isa("PPI::Token::QuoteLike::Regexp")) 6 33 39
Condition100%3/3
$token->isa("PPI::Token::Regexp") or $token->isa("PPI::Token::QuoteLike::Regexp")
l!l&&r!l&&!r
3333
MC/DC100%2/2
$token->isa("PPI::Token::Regexp") or $token->isa("PPI::Token::QuoteLike::Regexp")
$token->isa("PPI::Token::Regexp") $token->isa("PPI::Token::QuoteLike::Regexp")
Truth table100%3/3
A or B
A= $token->isa("PPI::Token::Regexp")B= $token->isa("PPI::Token::QuoteLike::Regexp")
ABresult
1X1
011
000
868 || $token->isa('PPI::Token::QuoteLike::Regexp') ) {
869 6 for my $snippet (
870 $token->get_match_string,
871 $token->get_substitute_string,
872 ) {
873 9 push @symbols, _extract_symbols_from_snippet($snippet);
874 }
875 }
876  
877 39 push @symbols, $self->_unnest_quotes($token);
878 }
879  
880 # Crude hack to catch vars like ${FOO_BAR} in heredocs.
881 88 for my $heredoc (
882 @{
883 $self->ppi_document->find(
884 5278 ▶ sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
885 5278 $_[1]->isa('PPI::Token::HereDoc');
886 }
887 )
888 88 ▶ || []
Condition100%2/2
$self->ppi_document->find(sub { $_[1]->isa('PPI::Token::HereDoc'); } ) || []
l!l
286
MC/DC100%1/1
$self->ppi_document->find(sub { $_[1]->isa('PPI::Token::HereDoc'); } ) || []
$self->ppi_document->find(sub { $_[1]->isa('PPI::Token::HereDoc'); } )
Truth table100%2/2
A
A= $self->ppi_document->find(sub { $_[1]->isa('PPI::Token::HereDoc'); } )
Aresult
11
00
889 }
890 ) {
891 3 my $content = join "\n", $heredoc->heredoc;
892 3 ▶ next if $heredoc =~ m{'};
Branch: 1 branch
BranchTrueFalseTotal
if $heredoc =~ /'/ 1 2 3
893 2 push @symbols, _extract_symbols_from_snippet($content);
894 }
895  
896 # Catch vars like ${FOO_BAR}. This is probably not good enough.
897 88 for my $cast (
898 @{
899 $self->ppi_document->find(
900 5278 ▶ sub { $_[1]->isa('PPI::Token::Cast'); }
Subroutine __ANON__: called CC=1 SCAR=0.0
901 )
902 88 ▶ || []
Condition100%2/2
$self->ppi_document->find(sub { $_[1]->isa('PPI::Token::Cast'); } ) || []
l!l
583
MC/DC100%1/1
$self->ppi_document->find(sub { $_[1]->isa('PPI::Token::Cast'); } ) || []
$self->ppi_document->find(sub { $_[1]->isa('PPI::Token::Cast'); } )
Truth table100%2/2
A
A= $self->ppi_document->find(sub { $_[1]->isa('PPI::Token::Cast'); } )
Aresult
11
00
903 }
904 ) {
905 6 ▶ if ( !$cast->snext_sibling
Branch: 1 branch
BranchTrueFalseTotal
unless ($cast->snext_sibling and $cast->snext_sibling->isa("PPI::Structure::Block")) 4 2 6
Condition66%2/3
$cast->snext_sibling and $cast->snext_sibling->isa("PPI::Structure::Block")
!ll&&!rl&&r
042
MC/DC50%1/2
$cast->snext_sibling and $cast->snext_sibling->isa("PPI::Structure::Block")
$cast->snext_sibling $cast->snext_sibling->isa("PPI::Structure::Block")
Truth table66%2/3
A and B
A= $cast->snext_siblingB= $cast->snext_sibling->isa("PPI::Structure::Block")
ABresult
0X0
100
111
906 || !$cast->snext_sibling->isa('PPI::Structure::Block') ) {
907 4 next;
908 }
909  
910 2 my $sigil = $cast . q{};
911 2 my $sibling = $cast->snext_sibling . q{};
912 2 ▶ if ( $sibling =~ m/{(\w+)}/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sibling =~ /{(\w+)}/) 2 0 2
913 2 push @symbols, $sigil . $1;
914 }
915 }
916 88 my %symbols = map { $_ => 1 } @symbols;
917 88 return \%symbols;
918 }
919  
920 # Returns a HashRef of modules which will always be converted to avoid imports.
921 # This is mostly for speed and a matter of convenience so that we don't have to
922 # examine modules (like strictly Object Oriented modules) which we know will
923 # not have anything to export.
924  
925 103 ▶sub _build_never_exports {
Subroutine _build_never_exports: called CC=3 SCAR=13.3
926 103 my $self = shift;
927  
928 103 my %modules = (
929 'App::perlimports::Include' => 1,
930 'File::Spec' => 1,
931 'HTTP::Daemon' => 1,
932 'HTTP::Headers' => 1,
933 'HTTP::Response' => 1,
934 'HTTP::Tiny' => 1,
935 'LWP::UserAgent' => 1,
936 'URI' => 1,
937 'WWW::Mechanize' => 1,
938 );
939  
940 103 ▶ if ( $self->_has_never_export_modules ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_has_never_export_modules) 0 103 103
941 0 for my $module ( @{ $self->_never_export_modules } ) {
942 0 $modules{$module} = 1;
943 }
944 }
945  
946 103 return \%modules;
947 }
948  
949 97 ▶sub _build_sub_names {
Subroutine _build_sub_names: called CC=5 SCAR=16.4
950 97 my $self = shift;
951  
952 97 my %sub_names;
953 97 for my $sub (
954 @{
955 $self->ppi_document->find(
956 6040 ▶ sub { $_[1]->isa('PPI::Statement::Sub') }
Subroutine __ANON__: called CC=1 SCAR=0.0
957 )
958 97 ▶ || []
Condition100%2/2
$self->ppi_document->find(sub { $_[1]->isa('PPI::Statement::Sub'); } ) || []
l!l
1285
MC/DC100%1/1
$self->ppi_document->find(sub { $_[1]->isa('PPI::Statement::Sub'); } ) || []
$self->ppi_document->find(sub { $_[1]->isa('PPI::Statement::Sub'); } )
Truth table100%2/2
A
A= $self->ppi_document->find(sub { $_[1]->isa('PPI::Statement::Sub'); } )
Aresult
11
00
959 }
960 ) {
961 19 my @children = $sub->schildren;
962 19 ▶ if ( $children[0] eq 'sub'
Branch: 1 branch
BranchTrueFalseTotal
if ($children[0] eq "sub" and $children[1]->isa("PPI::Token::Word")) 19 0 19
Condition33%1/3
$children[0] eq "sub" and $children[1]->isa("PPI::Token::Word")
!ll&&!rl&&r
0019
MC/DC0%0/2
$children[0] eq "sub" and $children[1]->isa("PPI::Token::Word")
$children[0] eq "sub" $children[1]->isa("PPI::Token::Word")
Truth table33%1/3
A and B
A= $children[0] eq "sub"B= $children[1]->isa("PPI::Token::Word")
ABresult
0X0
100
111
963 && $children[1]->isa('PPI::Token::Word') ) {
964 19 $sub_names{"$children[1]"} = 1;
965 }
966 }
967  
968 97 return \%sub_names;
969 }
970  
971 199 ▶sub _has_import_switches {
Subroutine _has_import_switches: called CC=4 SCAR=13.9
972 199 my $self = shift;
973 199 my $module_name = shift;
974  
975 # If switches are being passed to import, we can't guess as what is correct
976 # here.
977 #
978 # Getopt::Long uses a leading colon rather than a dash. This overrides
979 # Exporter's defaults. You would normally assume that :config is an export
980 # tag, but instead it's something entirely different.
981 #
982 # use Getopt::Long qw(:config no_ignore_case bundling);
983 #
984 # We will leave this case as broken for the time being. I'm not sure how
985 # common that invocation is.
986  
987 199 ▶ if ( exists $self->found_imports->{$module_name}
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->found_imports->{$module_name} and &any(sub { $_ =~ /^[\-]/; } , @{$self->found_imports->{$module_name} or [];})) 1 198 199
Condition100%3/3
exists $self->found_imports->{$module_name} and &any(sub { $_ =~ /^[\-]/; } , @{$self->found_imports->{$module_name} or [];})
!ll&&!rl&&r
501481
MC/DC100%2/2
exists $self->found_imports->{$module_name} and &any(sub { $_ =~ /^[\-]/; } , @{$self->found_imports->{$module_name} or [];})
exists $self->found_imports->{$module_name} &any(sub { $_ =~ /^[\-]/; } , @{$self->found_imports->{$module_name} or [];})
Truth table100%3/3
A and B
A= exists $self->found_imports->{$module_name}B= &any(sub { $_ =~ /^[\-]/; } , @{$self->found_imports->{$module_name} or [];})
ABresult
0X0
100
111
988 160 ▶ && any { $_ =~ m{^[\-]} }
Subroutine __ANON__: called CC=1 SCAR=0.0
989 149 ▶ @{ $self->found_imports->{$module_name} || [] } ) {
Condition100%2/2
$self->found_imports->{$module_name} || []
l!l
10049
MC/DC100%1/1
$self->found_imports->{$module_name} || []
$self->found_imports->{$module_name}
Truth table100%2/2
A
A= $self->found_imports->{$module_name}
Aresult
11
00
990 1 return 1;
991 }
992 198 return 0;
993 }
994  
995 32 ▶sub _is_used_fully_qualified {
Subroutine _is_used_fully_qualified: called CC=5 SCAR=16.2
996 32 my $self = shift;
997 32 my $module_name = shift;
998  
999 # We could tighten this up and check that the word following "::" is a sub
1000 # which exists in that package.
1001 #
1002 # Module::function
1003 # Module::->new
1004 # isa => ArrayRef[Module::]
1005 return 1 if $self->ppi_document->find(
1006 3644 ▶ sub {
Subroutine __ANON__: called CC=8 SCAR=20.8
1007 (
1008 3644 ▶ $_[1]->isa('PPI::Token::Word')
Branch: 1 branch
BranchTrueFalseTotal
$_[1]->isa("PPI::Token::Word") and $_[1]->content =~ /\A$module_name::[a-zA-Z0-9_]*\z/ || $_[1]->content eq $module_name && $_[1]->snext_sibling eq "->" || $_[1]->content eq $module_name && _is_indirect_object_notation($_[1]) or $_[1]->isa("PPI::Token::Symbol") and $_[1] =~ /\A[&*\$\@\%]+$module_name::[a-zA-Z0-9_]/ 9 3635 3644
Condition100%21/21
$_[1]->isa('PPI::Token::Word') && ($_[1]->content =~ /\A$module_name::[a-zA-Z0-9_]*\z/ || $_[1]->content eq $module_name && $_[1]->snext_sibling eq '->' || $_[1]->content eq $module_name && _is_indirect_object_notation($_[1])) || $_[1]->isa('PPI::Token::Symbol') && $_[1] =~ /\A[&*\$\@\%]+$module_name::[a-zA-Z0-9_]/
l!l&&r!l&&!r
9103625
$_[1]->isa("PPI::Token::Word") and $_[1]->content =~ /\A$module_name::[a-zA-Z0-9_]*\z/ || $_[1]->content eq $module_name && $_[1]->snext_sibling eq "->" || $_[1]->content eq $module_name && _is_indirect_object_notation($_[1])
!ll&&!rl&&r
29187179
$_[1]->content =~ /\A$module_name::[a-zA-Z0-9_]*\z/ or $_[1]->content eq $module_name and $_[1]->snext_sibling eq "->" or $_[1]->content eq $module_name and _is_indirect_object_notation($_[1])
l!l&&r!l&&!r
63717
$_[1]->content =~ /\A$module_name::[a-zA-Z0-9_]*\z/ or $_[1]->content eq $module_name and $_[1]->snext_sibling eq "->"
l!l&&r!l&&!r
42720
$_[1]->content eq $module_name and $_[1]->snext_sibling eq "->"
!ll&&!rl&&r
689312
$_[1]->content eq $module_name and _is_indirect_object_notation($_[1])
!ll&&!rl&&r
689283
$_[1]->isa("PPI::Token::Symbol") and $_[1] =~ /\A[&*\$\@\%]+$module_name::[a-zA-Z0-9_]/
!ll&&!rl&&r
348713810
MC/DC100%8/8
$_[1]->isa('PPI::Token::Word') && ($_[1]->content =~ /\A$module_name::[a-zA-Z0-9_]*\z/ || $_[1]->content eq $module_name && $_[1]->snext_sibling eq '->' || $_[1]->content eq $module_name && _is_indirect_object_notation($_[1])) || $_[1]->isa('PPI::Token::Symbol') && $_[1] =~ /\A[&*\$\@\%]+$module_name::[a-zA-Z0-9_]/
$_[1]->isa("PPI::Token::Word") $_[1]->content =~ /\A$module_name::[a-zA-Z0-9_]*\z/ $_[1]->content eq $module_name $_[1]->snext_sibling eq "->" $_[1]->content eq $module_name _is_indirect_object_notation($_[1]) $_[1]->isa("PPI::Token::Symbol") $_[1] =~ /\A[&*\$\@\%]+$module_name::[a-zA-Z0-9_]/
Truth table42%8/19
A and B or C and D or E and F || G and H
A= $_[1]->isa("PPI::Token::Word")B= $_[1]->content =~ /\A$module_name::[a-zA-Z0-9_]*\z/C= $_[1]->content eq $module_nameD= $_[1]->snext_sibling eq "->"E= $_[1]->content eq $module_nameF= _is_indirect_object_notation($_[1])G= $_[1]->isa("PPI::Token::Symbol")H= $_[1] =~ /\A[&*\$\@\%]+$module_name::[a-zA-Z0-9_]/
ABCDEFGHresult
11XXXXXX1
1011XXXX1
100X11XX1
101011XX1
0XXXXX111
100X0X111
100X10111
10100X111
101010111
0XXXXX0X0
0XXXXX100
100X0X0X0
100X0X100
100X100X0
100X10100
10100X0X0
10100X100
1010100X0
101010100
1009 && (
1010 $_[1]->content =~ m{\A${module_name}::[a-zA-Z0-9_]*\z}
1011 || ( $_[1]->content eq ${module_name}
1012 && $_[1]->snext_sibling eq '->' )
1013 || ( $_[1]->content eq ${module_name}
1014 && _is_indirect_object_notation( $_[1] ) )
1015 )
1016 )
1017 || ( $_[1]->isa('PPI::Token::Symbol')
1018 && $_[1] =~ m{\A[&*\$\@\%]+${module_name}::[a-zA-Z0-9_]} );
1019 }
1020 32 ▶ );
Branch: 1 branch
BranchTrueFalseTotal
if $self->ppi_document->find(sub { $_[1]->isa("PPI::Token::Word") and $_[1]->content =~ /\A$module_name::[a-zA-Z0-9_]*\z/ || $_[1]->content eq $module_name && $_[1]->snext_sibling eq "->" || $_[1]->content eq $module_name && _is_indirect_object_notation($_[1]) or $_[1]->isa("PPI::Token::Symbol") and $_[1] =~ /\A[&*\$\@\%]+$module_name::[a-zA-Z0-9_]/; } ) 16 16 32
1021  
1022 # We could combine the regexes, but this is easy to read.
1023 16 for my $key ( keys %{ $self->interpolated_symbols } ) {
1024  
1025 # package level variable
1026 1 ▶ return 1 if $key =~ m{\A[&*\$\@\%]+${module_name}::[a-zA-Z0-9_]+\z};
Branch: 1 branch
BranchTrueFalseTotal
if $key =~ /\A[&*\$\@\%]+$module_name::[a-zA-Z0-9_]+\z/ 1 0 1
1027  
1028 # function
1029 0 ▶ return 1 if $key =~ m/\A${module_name}::[a-zA-Z0-9_]+\z/;
Branch: 1 branch
BranchTrueFalseTotal
if $key =~ /\A$module_name::[a-zA-Z0-9_]+\z/ 0 0 0
1030 }
1031  
1032 15 return 0;
1033 }
1034  
1035 # Barewords which precede another bareword but do not introduce indirect object
1036 # syntax. These are declaration/import keywords (e.g. "sub Foo", "use Foo"),
1037 # where the following bareword is a name being defined or imported rather than a
1038 # class being acted upon.
1039 107 my %not_indirect_method = map { $_ => 1 } qw(
1040 no
1041 package
1042 require
1043 sub
1044 use
1045 );
1046  
1047 # Detects indirect object syntax such as "new File $path" or "connect Database
1048 # $dsn", where a module name is used as the invocant of a method without an
1049 # arrow. In PPI this is a bareword whose previous significant sibling is another
1050 # bareword (the method name). This is a heuristic: erring towards treating the
1051 # module as used is the safe direction, since a false negative would remove an
1052 # import which is actually in use.
1053  
1054 31 ▶sub _is_indirect_object_notation {
Subroutine _is_indirect_object_notation: called CC=4 SCAR=15.2
1055 31 my $word = shift;
1056  
1057 31 my $prev = $word->sprevious_sibling;
1058 31 ▶ return 0 unless ref $prev && $prev->isa('PPI::Token::Word');
Branch: 1 branch
BranchTrueFalseTotal
unless ref $prev and $prev->isa("PPI::Token::Word") 0 31 31
Condition33%1/3
ref $prev and $prev->isa("PPI::Token::Word")
!ll&&!rl&&r
0031
MC/DC0%0/2
ref $prev and $prev->isa("PPI::Token::Word")
ref $prev $prev->isa("PPI::Token::Word")
Truth table33%1/3
A and B
A= ref $prevB= $prev->isa("PPI::Token::Word")
ABresult
0X0
100
111
1059  
1060 # The method name is a simple identifier; a package-qualified name before
1061 # the module would not be indirect object syntax.
1062 31 ▶ return 0 unless $prev->content =~ m{\A[a-zA-Z_]\w*\z};
Branch: 1 branch
BranchTrueFalseTotal
unless $prev->content =~ /\A[a-zA-Z_]\w*\z/ 0 31 31
1063  
1064 31 return !$not_indirect_method{ $prev->content };
1065 }
1066  
1067 251 ▶sub _is_ignored {
Subroutine _is_ignored: called CC=7 SCAR=19.8
1068 251 my $self = shift;
1069 251 my $element = shift;
1070  
1071 my $res
1072 = exists $default_ignore{ $element->module }
1073 || exists $self->_ignore_modules->{ $element->module }
1074 || $self->_annotations->is_ignored($element)
1075 || (
1076 0 ▶ any { $element->module =~ /$_/ }
Subroutine __ANON__: not called CC=1 SCAR=6.9
1077 251 ▶ grep { $_ } @{ $self->_ignore_modules_pattern || [] }
Condition82%14/17
exists $default_ignore{$element->module} || exists $self->_ignore_modules->{$element->module} || $self->_annotations->is_ignored($element) || &any(sub { $element->module =~ /$_/; } , grep({$_;} @{$self->_ignore_modules_pattern or [];})) || $self->inspector_for($element->module) && !$self->inspector_for($element->module)->evals_ok
l!l&&r!l&&!r
437201
exists $default_ignore{$element->module} or exists $self->_ignore_modules->{$element->module} or $self->_annotations->is_ignored($element) or &any(sub { $element->module =~ /$_/; } , grep({$_;} @{$self->_ignore_modules_pattern or [];}))
l!l&&r!l&&!r
430208
exists $default_ignore{$element->module} or exists $self->_ignore_modules->{$element->module} or $self->_annotations->is_ignored($element)
l!l&&r!l&&!r
394208
exists $default_ignore{$element->module} or exists $self->_ignore_modules->{$element->module}
l!l&&r!l&&!r
1227212
$self->_ignore_modules_pattern || []
l!l
2080
$self->inspector_for($element->module) and not $self->inspector_for($element->module)->evals_ok
!ll&&!rl&&r
01997
MC/DC66%4/6
exists $default_ignore{$element->module} || exists $self->_ignore_modules->{$element->module} || $self->_annotations->is_ignored($element) || &any(sub { $element->module =~ /$_/; } , grep({$_;} @{$self->_ignore_modules_pattern or [];})) || $self->inspector_for($element->module) && !$self->inspector_for($element->module)->evals_ok
exists $default_ignore{$element->module} exists $self->_ignore_modules->{$element->module} $self->_annotations->is_ignored($element) &any(sub { $element->module =~ /$_/; } , grep({$_;} @{$self->_ignore_modules_pattern or [];})) $self->inspector_for($element->module) not $self->inspector_for($element->module)->evals_ok
MC/DC0%0/1
$self->_ignore_modules_pattern || []
$self->_ignore_modules_pattern
Truth table71%5/7
A or B or C or D || E and not(F)
A= exists $default_ignore{$element->module}B= exists $self->_ignore_modules->{$element->module}C= $self->_annotations->is_ignored($element)D= &any(sub { $element->module =~ /$_/; } , grep({$_;} @{$self->_ignore_modules_pattern or [];}))E= $self->inspector_for($element->module)F= not $self->inspector_for($element->module)->evals_ok
ABCDEFresult
1XXXXX1
01XXXX1
001XXX1
0001XX1
0000111
00000X0
0000100
Truth table50%1/2
A
A= $self->_ignore_modules_pattern
Aresult
11
00
1078 )
1079 || ( $self->inspector_for( $element->module )
1080 && !$self->inspector_for( $element->module )->evals_ok );
1081 249 return $res;
1082 }
1083  
1084 578 ▶sub inspector_for {
Subroutine inspector_for: called CC=6 SCAR=25.9
Pod documented
1085 578 my $self = shift;
1086 578 my $module = shift;
1087  
1088 # This would produce a warning and no helpful information.
1089 578 ▶ return undef if $module eq 'Exporter';
Branch: 1 branch
BranchTrueFalseTotal
if $module eq "Exporter" 0 578 578
1090  
1091 578 ▶ if ( $self->_has_inspector_for($module) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_has_inspector_for($module)) 370 208 578
1092 370 return $self->_get_inspector_for($module);
1093 }
1094  
1095 208 ▶ if ( $self->_cache ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_cache) 0 208 208
1096 0 require Sereal::Decoder; ## no perlimports
1097 0 my $decoder = Sereal::Decoder->new( {} );
1098 0 my $file = $self->_cache_file_for_module($module);
1099 0 my $inspector;
1100 0 ▶ if ( -e $file ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-e $file) 0 0 0
1101 0 ▶ try {
Subroutine __ANON__: not called CC=1 SCAR=6.9
1102 0 $inspector = $decoder->decode_from_file($file);
1103 0 $self->_set_inspector_for( $module, $inspector );
1104 }
1105 0 ▶ catch {
Subroutine __ANON__: not called CC=1 SCAR=6.9
1106 0 $self->logger->error($_);
1107 0 };
1108 0 ▶ if ($inspector) {
Branch: 1 branch
BranchTrueFalseTotal
if ($inspector) 0 0 0
1109 0 $self->logger->info("Using cached version of $module");
1110 0 $inspector->set_logger( $self->logger );
1111 0 return $inspector;
1112 }
1113 }
1114 }
1115  
1116 208 ▶ try {
Subroutine __ANON__: called CC=1 SCAR=0.0
1117 208 $self->_set_inspector_for(
1118 $module,
1119 App::perlimports::ExportInspector->new(
1120 logger => $self->logger,
1121 module_name => $module,
1122 )
1123 );
1124 }
1125 2 ▶ catch {
Subroutine __ANON__: called CC=1 SCAR=1.2
1126 2 $self->logger->info( 'inspector_for' . $_ );
1127 0 $self->_set_inspector_for( $module, undef );
1128 208 };
1129  
1130 206 return $self->_get_inspector_for($module);
1131 }
1132  
1133 # given a PPI:Statement:Include that exists in the doc,
1134 # instantiate a App:perlimports:Include to process it.
1135 132 ▶sub _include_analyzer {
Subroutine _include_analyzer: called CC=1 SCAR=0.0
1136 132 my ( $self, $include ) = @_;
1137  
1138 return my $e = App::perlimports::Include->new(
1139 document => $self,
1140 include => $include,
1141 indent => $self->_indent,
1142 logger => $self->logger,
1143 132 found_imports => $self->found_imports->{ $include->module },
1144 pad_brackets => $self->_pad_brackets,
1145 pad_imports => $self->_padding,
1146 preserve_require => $self->_preserve_require,
1147 tidy_whitespace => $self->_tidy_whitespace,
1148 );
1149 }
1150  
1151 95 ▶sub tidied_document {
Subroutine tidied_document: called CC=1 SCAR=0.0
Pod documented
1152 95 return shift->_lint_or_tidy_document;
1153 }
1154  
1155 15 ▶sub linter_success {
Subroutine linter_success: called CC=1 SCAR=0.0
Pod documented
1156 15 return shift->_lint_or_tidy_document;
1157 }
1158  
1159 # Kind of on odd interface, but right now we return either a tidied document or
1160 # the result of linting. Could probably clean this up at some point, but I'm
1161 # not sure yet how much the linting will change.
1162 # N.B. In lint mode, we never modify the document.
1163 110 ▶sub _lint_or_tidy_document {
Subroutine _lint_or_tidy_document: called CC=18 SCAR=29.3
1164 110 my $self = shift;
1165  
1166 110 my $linter_error = 0;
1167 110 ▶ $linter_error = 1 if $self->_sort_lint_error;
Branch: 1 branch
BranchTrueFalseTotal
if $self->_sort_lint_error 3 107 110
1168  
1169 110 my %processed; # modules we changed/confirmed the use statement
1170  
1171 INCLUDE:
1172 110 foreach my $include ( $self->all_includes ) {
1173  
1174 # If a module is used more than once, that's usually a mistake.
1175 127 ▶ if ( !$self->_preserve_duplicates
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->_preserve_duplicates and exists $processed{$include->module}) 1 126 127
Condition100%3/3
not $self->_preserve_duplicates and exists $processed{$include->module}
!ll&&!rl&&r
12151
MC/DC100%2/2
not $self->_preserve_duplicates and exists $processed{$include->module}
not $self->_preserve_duplicates exists $processed{$include->module}
Truth table100%3/3
not(A) and B
A= not $self->_preserve_duplicatesB= exists $processed{$include->module}
ABresult
0X0
100
111
1176 && exists $processed{ $include->module } ) {
1177  
1178 1 ▶ if ( $self->lint ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->lint) 0 1 1
1179 0 $self->_warn_diff_for_linter(
1180 'has already been used and should be removed',
1181 $include,
1182 $include->content,
1183 q{}
1184 );
1185 0 $linter_error = 1;
1186 0 next INCLUDE;
1187 }
1188  
1189 1 $self->logger->info( $include->module
1190 . ' has already been used. Removing at line '
1191 . $include->line_number );
1192 1 _remove_with_trailing_characters($include);
1193 1 next INCLUDE;
1194 }
1195  
1196 126 $self->logger->notice( '📦 ' . "Processing include: $include" );
1197  
1198 126 my $e = $self->_include_analyzer($include);
1199 126 my $elem;
1200 126 ▶ try {
Subroutine __ANON__: called CC=1 SCAR=0.0
1201 # may return the original include!
1202 126 $elem = $e->formatted_ppi_statement;
1203 }
1204 0 ▶ catch {
Subroutine __ANON__: not called CC=1 SCAR=6.9
1205 0 my $error = $_;
1206 0 $self->logger->error( 'Error in ' . $self->_filename );
1207 0 $self->logger->error( 'Trying to format: ' . $include );
1208 0 $self->logger->error( 'Error is: ' . $error );
1209 126 };
1210  
1211 126 ▶ next INCLUDE unless $elem;
Branch: 1 branch
BranchTrueFalseTotal
unless $elem 0 126 126
1212  
1213 # If this is a module with bare imports which is not used anywhere,
1214 # maybe we can just remove it.
1215 126 ▶ if ( !$self->_preserve_unused ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->_preserve_unused) 43 83 126
1216 43 my @args = $elem->arguments;
1217  
1218 43 ▶ if ( $args[0]
Branch: 1 branch
BranchTrueFalseTotal
if ($args[0] and $args[0] eq "()" and not $self->_is_used_fully_qualified($include->module)) 8 35 43
Condition100%6/6
$args[0] and $args[0] eq "()" and not $self->_is_used_fully_qualified($include->module)
!ll&&!rl&&r
25108
$args[0] and $args[0] eq "()"
!ll&&!rl&&r
12418
MC/DC100%3/3
$args[0] and $args[0] eq "()" and not $self->_is_used_fully_qualified($include->module)
$args[0] $args[0] eq "()" not $self->_is_used_fully_qualified($include->module)
Truth table100%4/4
A and B and not(C)
A= $args[0]B= $args[0] eq "()"C= not $self->_is_used_fully_qualified($include->module)
ABCresult
0XX0
10X0
1100
1111
1219 && $args[0] eq '()'
1220 && !$self->_is_used_fully_qualified( $include->module ) ) {
1221  
1222 # This include no longer imports anything, so later includes
1223 # must not rely on it for symbols it used to import.
1224 8 $self->_reset_found_import( $include->module, [] );
1225  
1226 8 ▶ if ( $self->lint ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->lint) 1 7 8
1227 1 $self->_warn_diff_for_linter(
1228 'appears to be unused and should be removed',
1229 $include, $include->content,
1230 q{}
1231 );
1232 1 $linter_error = 1;
1233 1 next INCLUDE;
1234 }
1235  
1236 7 $self->logger->info( 'Removing '
1237 . $include->module
1238 . ' as it appears to be unused' );
1239 7 _remove_with_trailing_characters($include);
1240  
1241 7 next INCLUDE;
1242 }
1243 }
1244  
1245 # if the 'new' statement is actually just the original, skip!
1246 118 ▶ if ( $elem == $include ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($elem == $include) 40 78 118
1247 40 $processed{ $include->module } = 1;
1248 40 next INCLUDE;
1249 }
1250  
1251 ## no critic (Subroutines::ProhibitCallsToUnexportedSubs)
1252 # Let's see if the import itself might break something
1253 78 ▶ if ( my $err
Branch: 1 branch
BranchTrueFalseTotal
if (my $err = App::perlimports::Sandbox::eval_pkg($elem->module, "$elem")) 1 77 78
1254 = App::perlimports::Sandbox::eval_pkg( $elem->module, "$elem" ) )
1255 {
1256 1 $self->logger->warning(
1257 sprintf(
1258 'New include (%s) triggers error (%s)', $elem, $err
1259 )
1260 );
1261 1 next INCLUDE;
1262 }
1263 ## use critic
1264  
1265 77 my $inserted = $include->replace($elem);
1266 77 ▶ if ( !$inserted ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $inserted) { } 0 77 77
1267 0 $self->logger->error( 'Could not insert ' . $elem );
1268 }
1269 else {
1270 77 $processed{ $include->module } = 1;
1271  
1272 77 $self->logger->info("resetting imports for |$elem|");
1273  
1274 # Track what this include will import once tidied, in lint mode
1275 # too, so that later includes are checked against it.
1276 77 $self->_reset_found_import(
1277 $include->module,
1278 _imports_for_include($elem)
1279 );
1280  
1281 77 ▶ if ( $self->lint ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->lint) 16 61 77
1282 my $before = join q{ },
1283 16 map { $_->content } $include->arguments;
1284 16 my $after = join q{ }, map { $_->content } $elem->arguments;
1285  
1286 16 ▶ if ( $before ne $after ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($before ne $after) 16 0 16
1287 16 $self->_warn_diff_for_linter(
1288 'import arguments need tidying',
1289 $include,
1290 $include->content,
1291 $elem->content
1292 );
1293 16 $linter_error = 1;
1294 }
1295 16 next INCLUDE;
1296 }
1297 }
1298 }
1299  
1300 110 $self->_maybe_cache_inspectors;
1301  
1302 # We need to do serialize in order to preserve HEREDOCs.
1303 # See https://metacpan.org/pod/PPI::Document#serialize
1304 110 ▶ return !$linter_error if $self->lint;
Branch: 1 branch
BranchTrueFalseTotal
if $self->lint 15 95 110
1305  
1306 95 return $self->_tidied_and_sorted;
1307 }
1308  
1309 # Returns 1 (and warns via the linter) if we're linting with --sort enabled
1310 # and the current includes are not in sorted order; returns 0 otherwise.
1311 110 ▶sub _sort_lint_error {
Subroutine _sort_lint_error: called CC=4 SCAR=13.9
1312 110 my $self = shift;
1313  
1314 110 ▶ return 0 unless $self->lint && $self->_sort;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->lint and $self->_sort 106 4 110
Condition100%3/3
$self->lint and $self->_sort
!ll&&!rl&&r
95114
MC/DC100%2/2
$self->lint and $self->_sort
$self->lint $self->_sort
Truth table100%3/3
A and B
A= $self->lintB= $self->_sort
ABresult
0X0
100
111
1315  
1316 4 my $before = $self->_ppi_selection->serialize;
1317 4 my $after = App::perlimports::Sorter->new(
1318 logger => $self->logger,
1319 source => $before,
1320 )->sorted_document;
1321  
1322 4 ▶ return 0 if $before eq $after;
Branch: 1 branch
BranchTrueFalseTotal
if $before eq $after 1 3 4
1323  
1324 3 $self->_warn_unsorted_includes( $before, $after );
1325 3 return 1;
1326 }
1327  
1328 # Serializes the tidied document and, when --sort is enabled, reorders its
1329 # include statements.
1330 95 ▶sub _tidied_and_sorted {
Subroutine _tidied_and_sorted: called CC=2 SCAR=6.9
1331 95 my $self = shift;
1332  
1333 95 my $tidied = $self->_ppi_selection->serialize;
1334 95 ▶ return $tidied unless $self->_sort;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_sort 91 4 95
1335  
1336 4 return App::perlimports::Sorter->new(
1337 logger => $self->logger,
1338 source => $tidied,
1339 )->sorted_document;
1340 }
1341  
1342 # given PPI:Element, returns hashref describing location, e.g.:
1343 # {start => {line => 3, column => 4}, end => {...}}
1344 20 ▶sub _elem_loc {
Subroutine _elem_loc: called CC=3 SCAR=11.3
1345 20 my ($elem) = @_;
1346  
1347 20 my $line = $elem->line_number;
1348 20 my $column = $elem->column_number;
1349  
1350 20 my $content = $elem->content;
1351 20 my @lines = split( m{\n}, $content );
1352  
1353 20 my $loc = { start => { line => $line, column => $column } };
1354  
1355 20 ▶ if ( @lines <= 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@lines <= 1) { } 20 0 20
1356  
1357 # A single-line statement's own indentation is not part of its
1358 # content, so the (inclusive) end column is measured from the start
1359 # column rather than from column 1.
1360 $loc->{end} = {
1361 20 ▶ line => $line,
Condition50%1/2
$lines[0] // ""
l!l
200
MC/DC0%0/1
$lines[0] // ""
$lines[0]
Truth table50%1/2
A
A= $lines[0]
Aresult
11
00
1362 column => $column + length( $lines[0] // q{} ) - 1,
1363 };
1364 }
1365 else {
1366  
1367 # Continuation lines begin at column 1 and their leading whitespace is
1368 # part of the statement content, so the final line's length is already
1369 # the absolute end column.
1370 $loc->{end} = {
1371 0 line => $line + @lines - 1,
1372 column => length( $lines[-1] ),
1373 };
1374 }
1375  
1376 20 return $loc;
1377 }
1378  
1379 17 ▶sub _warn_diff_for_linter {
Subroutine _warn_diff_for_linter: called CC=4 SCAR=14.1
1380 17 my $self = shift;
1381 17 my $reason = shift;
1382 17 my $include = shift;
1383 17 my $before = shift;
1384 17 my $after = shift;
1385 17 my $after_deleted = !$after;
1386  
1387 17 my $json;
1388 my $justification;
1389  
1390 17 ▶ if ( $self->json ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->json) { } 0 17 17
1391  
1392 0 $json = {
1393 filename => $self->_filename,
1394 location => _elem_loc($include),
1395 module => $include->module,
1396 reason => $reason,
1397 };
1398 }
1399 else {
1400 17 $justification = sprintf(
1401 '❌ %s (%s) at %s line %i',
1402 $include->module, $reason, $self->_filename, $include->line_number
1403 );
1404 }
1405  
1406 17 my $padding = $include->line_number - 1;
1407 17 $before = sprintf( "%s%s\n", "\n" x $padding, $before );
1408 17 $after = sprintf( "%s%s\n", "\n" x $padding, $after );
1409 17 ▶ chomp $after if $after_deleted;
Branch: 1 branch
BranchTrueFalseTotal
if $after_deleted 1 16 17
1410  
1411 17 my $diff = Text::Diff::diff(
1412 \$before, \$after,
1413 {
1414 CONTEXT => 0,
1415 STYLE => 'Unified',
1416 }
1417 );
1418  
1419 17 ▶ if ( $self->json ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->json) { } 0 17 17
1420 0 $json->{diff} = $diff;
1421 0 $self->logger->error( $self->_json_encoder->encode($json) );
1422 }
1423 else {
1424 17 $self->logger->error($justification);
1425 17 $self->logger->error($diff);
1426 }
1427 }
1428  
1429 3 ▶sub _warn_unsorted_includes {
Subroutine _warn_unsorted_includes: called CC=3 SCAR=11.0
1430 3 my ( $self, $before, $after ) = @_;
1431  
1432 3 my $reason = 'includes are not sorted';
1433  
1434 # Use CONTEXT => 0 for parity with the per-include diagnostic
1435 # (_warn_diff_for_linter): the before/after are full-document
1436 # serializations whose line numbers already align, so surrounding context
1437 # lines would only bloat the output and diverge from the other shape.
1438 3 my $diff = Text::Diff::diff(
1439 \$before, \$after,
1440 {
1441 CONTEXT => 0,
1442 STYLE => 'Unified',
1443 }
1444 );
1445  
1446 3 ▶ if ( $self->json ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->json) 2 1 3
1447  
1448 # Unlike a per-include diagnostic, this reports a reordering rather
1449 # than a single include, so there's no module to attach and we omit
1450 # "module" entirely. The location spans the lines that actually
1451 # changed rather than every include in the file: a `require` buried in
1452 # a sub far below never participates in a top-of-file reordering.
1453 2 my $json = {
1454 filename => $self->_filename,
1455 reason => $reason,
1456 diff => $diff,
1457 };
1458  
1459 2 my $location = _changed_line_span( $before, $diff );
1460 2 ▶ $json->{location} = $location if $location;
Branch: 1 branch
BranchTrueFalseTotal
if $location 2 0 2
1461  
1462 2 $self->logger->error( $self->_json_encoder->encode($json) );
1463 2 return;
1464 }
1465  
1466 $self->logger->error(
1467 1 sprintf( '❌ %s (%s)', $self->_filename, $reason ) );
1468 1 $self->logger->error($diff);
1469  
1470 1 return;
1471 }
1472  
1473 # Given the original document ($before) and the unified diff describing the
1474 # reordering, return the 1-indexed line/column span of the original lines the
1475 # diff touches. This keeps a document-wide sort diagnostic pointed at the
1476 # reordered includes rather than at unrelated includes elsewhere in the file
1477 # (a `require` buried in a sub far below never participates in a top-of-file
1478 # reordering). Deriving the span from the diff -- rather than comparing lines
1479 # positionally -- is important because the Sorter can insert a blank line
1480 # after the hoisted pragmas, which shifts every following line and would make
1481 # a positional comparison flag the whole tail of the file. Columns are
1482 # 1-indexed and inclusive. When the change touches multiple non-contiguous
1483 # blocks the span covers the first changed line through the last. Returns
1484 # undef when the diff has no hunks (the caller only reaches here when the
1485 # versions differ).
1486 2 ▶sub _changed_line_span {
Subroutine _changed_line_span: called CC=13 SCAR=27.1
1487 2 my ( $before, $diff ) = @_;
1488  
1489 # Unified hunk headers look like "@@ -L,S +l,s @@". A missing S means 1;
1490 # S == 0 marks a pure insertion positioned after original line L. We take
1491 # the union of the original-file ranges the hunks touch.
1492 2 my ( $first_elem, $last_elem );
1493 2 ▶ while ( $diff =~ m{^\@\@ \s+ [-](\d+) (?: [,](\d+) )? \s}gmx ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($diff =~ /^\@\@ \s+ [-](\d+) (?: [,](\d+) )? \s/gmx) 4 2 6
1494 4 ▶ my ( $start, $size ) = ( $1, defined $2 ? $2 : 1 );
Branch: 1 branch
BranchTrueFalseTotal
defined $2 ? : 2 2 4
1495 4 ▶ my $end = $size > 0 ? $start + $size - 1 : $start;
Branch: 1 branch
BranchTrueFalseTotal
$size > 0 ? : 2 2 4
1496 4 ▶ $first_elem = $start if !defined $first_elem || $start < $first_elem;
Branch: 1 branch
BranchTrueFalseTotal
if not defined $first_elem or $start < $first_elem 2 2 4
Condition66%2/3
not defined $first_elem or $start < $first_elem
l!l&&r!l&&!r
202
MC/DC50%1/2
not defined $first_elem or $start < $first_elem
not defined $first_elem $start < $first_elem
Truth table66%2/3
not(A) or B
A= not defined $first_elemB= $start < $first_elem
ABresult
1X1
011
000
1497 4 ▶ $last_elem = $end if !defined $last_elem || $end > $last_elem;
Branch: 1 branch
BranchTrueFalseTotal
if not defined $last_elem or $end > $last_elem 4 0 4
Condition66%2/3
not defined $last_elem or $end > $last_elem
l!l&&r!l&&!r
220
MC/DC0%0/2
not defined $last_elem or $end > $last_elem
not defined $last_elem $end > $last_elem
Truth table66%2/3
not(A) or B
A= not defined $last_elemB= $end > $last_elem
ABresult
1X1
011
000
1498 }
1499 2 ▶ return unless defined $first_elem;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $first_elem 0 2 2
1500  
1501 2 my @before = split( m{\n}, $before, -1 );
1502 2 my $start_line = $before[ $first_elem - 1 ];
1503 2 ▶ my $start_column
Branch: 1 branch
BranchTrueFalseTotal
defined $start_line && $start_line =~ /\S/ ? : 2 0 2
Condition33%1/3
defined $start_line and $start_line =~ /\S/
!ll&&!rl&&r
002
MC/DC0%0/2
defined $start_line and $start_line =~ /\S/
defined $start_line $start_line =~ /\S/
Truth table33%1/3
A and B
A= defined $start_lineB= $start_line =~ /\S/
ABresult
0X0
100
111
1504 = ( defined $start_line && $start_line =~ m{\S} ) ? $-[0] + 1 : 1;
1505  
1506 return {
1507 2 ▶ start => { line => $first_elem, column => $start_column },
Condition50%2/4
length($before[$last_elem - 1] // '') || 1
l!l
20
$before[$last_elem - 1] // ""
l!l
20
MC/DC0%0/1
length($before[$last_elem - 1] // '') || 1
$before[$last_elem - 1]
Truth table50%1/2
A
A= $before[$last_elem - 1]
Aresult
11
00
1508 end => {
1509 line => $last_elem,
1510 column => length( $before[ $last_elem - 1 ] // q{} ) || 1,
1511 },
1512 };
1513 }
1514  
1515 8 ▶sub _remove_with_trailing_characters {
Subroutine _remove_with_trailing_characters: called CC=5 SCAR=16.7
1516 8 my $include = shift;
1517  
1518 8 ▶ while ( my $next = $include->next_sibling ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $next = $include->next_sibling) 12 0 12
1519 12 ▶ if ( !$next->isa('PPI::Token::Whitespace')
Branch: 1 branch
BranchTrueFalseTotal
unless ($next->isa("PPI::Token::Whitespace") or $next->isa("PPI::Token::Comment")) 0 12 12
Condition66%2/3
$next->isa("PPI::Token::Whitespace") or $next->isa("PPI::Token::Comment")
l!l&&r!l&&!r
1020
MC/DC0%0/2
$next->isa("PPI::Token::Whitespace") or $next->isa("PPI::Token::Comment")
$next->isa("PPI::Token::Whitespace") $next->isa("PPI::Token::Comment")
Truth table66%2/3
A or B
A= $next->isa("PPI::Token::Whitespace")B= $next->isa("PPI::Token::Comment")
ABresult
1X1
011
000
1520 && !$next->isa('PPI::Token::Comment') ) {
1521 0 last;
1522 }
1523 12 $next->remove;
1524 12 ▶ last if $next eq "\n";
Branch: 1 branch
BranchTrueFalseTotal
if $next eq "\n" 8 4 12
1525 }
1526 8 $include->remove;
1527 8 return;
1528 }
1529  
1530 0 ▶sub _build_cache_dir {
Subroutine _build_cache_dir: not called CC=3 SCAR=24.8
1531 my $base_path
1532 = defined $ENV{HOME} && -d path( $ENV{HOME}, '.cache' )
1533 0 ▶ ? path( $ENV{HOME}, '.cache' )
Branch: 1 branch
BranchTrueFalseTotal
defined $ENV{'HOME'} && -d path($ENV{'HOME'}, '.cache') ? : 0 0 0
Condition0%0/3
defined $ENV{"HOME"} and -d path($ENV{"HOME"}, ".cache")
!ll&&!rl&&r
000
MC/DC0%0/2
defined $ENV{"HOME"} and -d path($ENV{"HOME"}, ".cache")
defined $ENV{"HOME"} -d path($ENV{"HOME"}, ".cache")
Truth table0%0/3
A and B
A= defined $ENV{"HOME"}B= -d path($ENV{"HOME"}, ".cache")
ABresult
0X0
100
111
1534 : path('/tmp');
1535  
1536 0 my $cache_dir = $base_path->child( 'perlimports', $VERSION );
1537 0 $cache_dir->mkpath;
1538  
1539 0 return $cache_dir;
1540 }
1541  
1542 0 ▶sub _cache_file_for_module {
Subroutine _cache_file_for_module: not called CC=1 SCAR=6.9
1543 0 my $self = shift;
1544 0 my $module = shift;
1545  
1546 0 return $self->_cache_dir->child($module);
1547 }
1548  
1549 110 ▶sub _maybe_cache_inspectors {
Subroutine _maybe_cache_inspectors: called CC=5 SCAR=30.1
1550 110 my $self = shift;
1551 110 ▶ return unless $self->_cache;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_cache 110 0 110
1552  
1553 0 my @names = sort $self->all_inspector_names;
1554 0 $self->logger->info('maybe cache');
1555 0 ▶ return unless @names;
Branch: 1 branch
BranchTrueFalseTotal
unless @names 0 0 0
1556  
1557 0 my $append = 0;
1558 0 require Sereal::Encoder; ## no perlimports
1559 0 my $encoder = Sereal::Encoder->new(
1560 { croak_on_bless => 0, undef_unknown => 1, } );
1561  
1562 0 for my $name ( $self->all_inspector_names ) {
1563 0 my $file = $self->_cache_file_for_module($name);
1564 0 ▶ next if -e $file;
Branch: 1 branch
BranchTrueFalseTotal
if -e $file 0 0 0
1565  
1566 0 $self->logger->info("I would like to cache $name at $file");
1567 0 $encoder->encode_to_file(
1568 $file,
1569 $self->inspector_for($name),
1570 $append
1571 );
1572 }
1573 0 return;
1574 }
1575  
1576 1265 ▶sub _is_word_interpreted_as_string {
Subroutine _is_word_interpreted_as_string: called CC=11 SCAR=24.0
1577 1265 my ($word) = @_;
1578  
1579 1265 ▶ return unless $word->statement && $word->isa('PPI::Token::Word');
Branch: 1 branch
BranchTrueFalseTotal
unless $word->statement and $word->isa("PPI::Token::Word") 161 1104 1265
Condition66%2/3
$word->statement and $word->isa("PPI::Token::Word")
!ll&&!rl&&r
01611104
MC/DC50%1/2
$word->statement and $word->isa("PPI::Token::Word")
$word->statement $word->isa("PPI::Token::Word")
Truth table66%2/3
A and B
A= $word->statementB= $word->isa("PPI::Token::Word")
ABresult
0X0
100
111
1580 1104 my @children = $word->statement->schildren;
1581  
1582 # https://perldoc.perl.org/perlref#Not-so-symbolic-references
1583 1104 ▶ return 1 if is_hash_key($word) && @children == 1;
Branch: 1 branch
BranchTrueFalseTotal
if is_hash_key($word) and @children == 1 5 1099 1104
Condition100%3/3
is_hash_key($word) and @children == 1
!ll&&!rl&&r
1082175
MC/DC100%2/2
is_hash_key($word) and @children == 1
is_hash_key($word) @children == 1
Truth table100%3/3
A and B
A= is_hash_key($word)B= @children == 1
ABresult
0X0
100
111
1584  
1585 # The => operator (sometimes pronounced "fat comma") is a synonym for
1586 # the comma except that it causes a word on its left to be interpreted
1587 # as a string if it begins with a letter or underscore and is composed
1588 # only of letters, digits and underscores. This includes operands that
1589 # might otherwise be interpreted as operators, constants, single number
1590 # v-strings or function calls.
1591 # https://perldoc.perl.org/perlop#Comma-Operator
1592 1099 ▶ return unless $word->content =~ /^[a-zA-Z_][a-zA-Z0-9_]*$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $word->content =~ /^[a-zA-Z_][a-zA-Z0-9_]*$/ 120 979 1099
1593  
1594 979 ▶ while ( my $current = shift @children ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $current = shift @children) 1479 0 1479
1595 1479 ▶ last if refaddr($current) == refaddr($word);
Branch: 1 branch
BranchTrueFalseTotal
if builtin::refaddr($current) == builtin::refaddr($word) 979 500 1479
1596 }
1597 979 ▶ return unless ( my $current = shift @children );
Branch: 1 branch
BranchTrueFalseTotal
unless my $current = shift @children 9 970 979
1598 970 ▶ return 1
Branch: 1 branch
BranchTrueFalseTotal
if $current->isa("PPI::Token::Operator") and $current->content eq "=>" 13 957 970
Condition100%3/3
$current->isa("PPI::Token::Operator") and $current->content eq "=>"
!ll&&!rl&&r
9451213
MC/DC100%2/2
$current->isa("PPI::Token::Operator") and $current->content eq "=>"
$current->isa("PPI::Token::Operator") $current->content eq "=>"
Truth table100%3/3
A and B
A= $current->isa("PPI::Token::Operator")B= $current->content eq "=>"
ABresult
0X0
100
111
1599 if $current->isa('PPI::Token::Operator')
1600 && $current->content eq '=>';
1601 }
1602  
1603 107 1;
1604  
1605 # ABSTRACT: Make implicit imports explicit
1606  
1607 __END__
« blib/lib/App/perlimports/Include.pm ↑ Summary blib/lib/App/perlimports/Annotations.pm »