blib/lib/Perl/Critic/Policy/ProhibitUnusedImports.pm

filter: Statementstmt 94.5% 157 / 166 Branchbran 79.4% 62 / 78 Conditioncond 71.4% 30 / 42 MC/DCmcdc 53.8% 14 / 26 Subroutinesub 89.2% 25 / 28 Podpod 100% 7 / 7 totaltotal 85.0% 295 / 347 CC 73 SCAR 23.1
CC
73
Cov
87%
_module_referenced
19.0
violates
16.1
_literal_arguments
15.1
CRAP 10.0
↑ Summary
1 package Perl::Critic::Policy::ProhibitUnusedImports;
2 2 $Perl::Critic::Policy::ProhibitUnusedImports::VERSION = '1.003';
3 # ABSTRACT: An import nobody uses is a dependency nobody knew they had.
4  
5 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 2 ▶use 5.014;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 2 ▶use re '/aa';
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 2 ▶use Readonly;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 2 ▶use Perl::Critic::Utils qw{ :severities :classification :ppi };
Subroutine BEGIN: called CC=1 SCAR=0.0
15 2 ▶use parent qw{Perl::Critic::Policy};
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17  
18 2 Readonly::Scalar my $DESC => q{Unused import};
19 2 Readonly::Scalar my $EXPL => q{Remove it, or the prerequisite it drags along outlives the code that wanted it};
20  
21 2 Readonly::Scalar my $DEFAULT_ALLOW => join ' ', qw{
22 strict warnings utf8 feature lib autodie parent base constant re
23 vars subs overload open integer bytes locale sigtrap version experimental
24 Filter::Simple Carp::Always FindBin::libs Test::NoWarnings
25 Test2::Plugin::NoWarnings Mojo::Base
26 };
27  
28  
29 2 ▶sub supported_parameters {
Subroutine supported_parameters: called CC=1 SCAR=0.0
Pod documented
30 return (
31 {
32 2 name => 'allow',
33 description => 'Modules that are never reported as unused, in addition to the built-in list.',
34 default_string => $DEFAULT_ALLOW,
35 behavior => 'string list',
36 }
37 );
38 }
39  
40 2 ▶sub initialize_if_enabled {
Subroutine initialize_if_enabled: called CC=2 SCAR=6.9
Pod documented
41 2 my ( $self, $config ) = @_;
42  
43 # 'string list' hands us the configured value in place of the default, and
44 # a user naming one bootstrap module of their own did not mean to start
45 # reporting `use strict`.
46 2 $self->{_allow}{$_} = 1 for split m/\s+/, $DEFAULT_ALLOW;
47  
48 2 return $self->SUPER::initialize_if_enabled($config);
49 }
50  
51 7 ▶sub default_severity { return $SEVERITY_LOW }
Subroutine default_severity: called CC=1 SCAR=0.0
Pod documented
52 0 ▶sub default_themes { return qw(maintenance performance) }
Subroutine default_themes: not called CC=1 SCAR=6.9
Pod documented
53 31 ▶sub applies_to { return 'PPI::Statement::Include' }
Subroutine applies_to: called CC=1 SCAR=0.0
Pod documented
54 0 ▶sub is_safe { return 0 }
Subroutine is_safe: not called CC=1 SCAR=6.9
Pod documented
55  
56  
57 32 ▶sub violates {
Subroutine violates: called CC=16 SCAR=27.8
Pod documented
58 32 my ( $self, $elem, $document ) = @_;
59  
60 32 ▶ return if $elem->type() ne 'use';
Branch: 1 branch
BranchTrueFalseTotal
if $elem->type ne "use" 0 32 32
61  
62 # `use 5.014` and friends are a version check, not an import. PPI gives
63 # those an empty module and a version, rather than no module at all.
64 32 my $module = $elem->module();
65 32 ▶ return if !defined $module || !length $module;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $module and length $module 1 31 32
Condition66%2/3
defined $module and length $module
!ll&&!rl&&r
0131
MC/DC50%1/2
defined $module and length $module
defined $module length $module
Truth table66%2/3
A and B
A= defined $moduleB= length $module
ABresult
0X0
100
111
66 31 ▶ return if $elem->version();
Branch: 1 branch
BranchTrueFalseTotal
if $elem->version 0 31 31
67 31 ▶ return if $self->{_allow}{$module};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_allow"}{$module} 12 19 31
68  
69 19 my @imports = $self->_imported_names( $elem, $module );
70 19 ▶ return if !defined $imports[0] && @imports; # a tag we could not expand
Branch: 1 branch
BranchTrueFalseTotal
if not defined $imports[0] and @imports 1 18 19
Condition100%3/3
not defined $imports[0] and @imports
!ll&&!rl&&r
991
MC/DC100%2/2
not defined $imports[0] and @imports
not defined $imports[0] @imports
Truth table100%3/3
not(A) and B
A= not defined $imports[0]B= @imports
ABresult
0X0
100
111
71  
72 # Nothing came in, so the only reason to have loaded it is to name it.
73 18 ▶ return $self->violation( $DESC, $EXPL, $elem )
Branch: 1 branch
BranchTrueFalseTotal
unless @imports or _module_referenced($document, $elem, $module) 4 14 18
Condition100%3/3
@imports or _module_referenced($document, $elem, $module)
l!l&&r!l&&!r
954
MC/DC100%2/2
@imports or _module_referenced($document, $elem, $module)
@imports _module_referenced($document, $elem, $module)
Truth table100%3/3
A or B
A= @importsB= _module_referenced($document, $elem, $module)
ABresult
1X1
011
000
74 if !@imports && !_module_referenced( $document, $elem, $module );
75  
76 14 ▶ return if !@imports;
Branch: 1 branch
BranchTrueFalseTotal
unless @imports 5 9 14
77  
78 # perl calls these when another file uses this package, so no call is written.
79 9 ▶ return if grep { $_ eq 'import' || $_ eq 'unimport' } @imports;
Branch: 1 branch
BranchTrueFalseTotal
if grep {$_ eq "import" or $_ eq "unimport";} @imports 2 7 9
Condition100%3/3
$_ eq 'import' || $_ eq 'unimport'
l!l&&r!l&&!r
2115
MC/DC100%2/2
$_ eq 'import' || $_ eq 'unimport'
$_ eq 'import' $_ eq 'unimport'
Truth table100%3/3
A || B
A= $_ eq 'import'B= $_ eq 'unimport'
ABresult
1X1
011
000
80  
81 7 my $used = _used_names( $document, $elem );
82 7 foreach my $name (@imports) {
83 14 ▶ return if $used->{$name};
Branch: 1 branch
BranchTrueFalseTotal
if $used->{$name} 4 10 14
84 }
85  
86 # Nothing it brought in is called anywhere, and it may still be here for
87 # its own name's sake.
88 3 ▶ return if _module_referenced( $document, $elem, $module );
Branch: 1 branch
BranchTrueFalseTotal
if _module_referenced($document, $elem, $module) 0 3 3
89 3 return $self->violation( $DESC, $EXPL, $elem );
90 }
91  
92 # The names this statement puts into our namespace.
93 19 ▶sub _imported_names {
Subroutine _imported_names: called CC=9 SCAR=22.1
94 19 my ( $self, $elem, $module ) = @_;
95  
96 19 my @listed = _literal_arguments($elem);
97  
98 # `use Module ();` -- an explicit empty list imports nothing.
99 19 ▶ return () if !@listed && _has_empty_list($elem);
Branch: 1 branch
BranchTrueFalseTotal
if not @listed and _has_empty_list($elem) 9 10 19
Condition100%3/3
not @listed and _has_empty_list($elem)
!ll&&!rl&&r
829
MC/DC100%2/2
not @listed and _has_empty_list($elem)
not @listed _has_empty_list($elem)
Truth table100%3/3
not(A) and B
A= not @listedB= _has_empty_list($elem)
ABresult
0X0
100
111
100  
101 10 ▶ if (@listed) {
Branch: 1 branch
BranchTrueFalseTotal
if (@listed) 8 2 10
102 8 my ( @names, @tags );
103 8 foreach my $item (@listed) {
104 11 ▶ $item =~ m/\A[:-]/ ? push( @tags, $item ) : push( @names, $item );
Branch: 1 branch
BranchTrueFalseTotal
$item =~ /\A[:-]/aa ? : 1 10 11
105 }
106  
107 # A tag stands for a list only the module knows. Expand it if we can
108 # load the module, and say so with a single undef if we cannot, so the
109 # caller leaves the statement alone rather than guessing.
110 8 foreach my $tag (@tags) {
111 1 ▶ next if $tag =~ m/\A-/; # -norequire and friends are not imports
Branch: 1 branch
BranchTrueFalseTotal
if $tag =~ /\A-/aa 0 1 1
112 1 ▶ my @expanded = _tag_exports( $module, $tag ) or return (undef);
Branch: 1 branch
BranchTrueFalseTotal
my(@expanded) = _tag_exports($module, $tag) or return undef 0 1 1
113 0 push @names, @expanded;
114 }
115  
116 7 return map { _strip_sigil($_) } @names;
117 }
118  
119 # No list at all: whatever @EXPORT says, if it will tell us.
120 2 return map { _strip_sigil($_) } _default_exports($module);
121 }
122  
123 # The names behind an import tag, out of the module's %EXPORT_TAGS.
124 1 ▶sub _tag_exports {
Subroutine _tag_exports: called CC=3 SCAR=11.2
125 1 my ( $module, $tag ) = @_;
126  
127 1 $tag =~ s/\A://;
128 1 ▶ return () if !_load($module);
Branch: 1 branch
BranchTrueFalseTotal
unless _load($module) 0 1 1
129  
130 2 ▶ no strict 'refs'; ## no critic (ProhibitNoStrict)
Subroutine BEGIN: called CC=1 SCAR=0.0
131 1 my $tags = \%{"${module}::EXPORT_TAGS"};
132 1 my $names = $tags->{$tag};
133 1 ▶ return () if ref $names ne 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if ref $names ne "ARRAY" 1 0 1
134 0 return @$names;
135 }
136  
137 18 ▶sub _strip_sigil {
Subroutine _strip_sigil: called CC=1 SCAR=0.0
138 18 my ($name) = @_;
139 18 $name =~ s/\A[\$\@\%\&\*]//;
140 18 return $name;
141 }
142  
143 # Every literal in the import list, from a qw() or a run of quoted strings.
144 19 ▶sub _literal_arguments {
Subroutine _literal_arguments: called CC=10 SCAR=27.1
145 19 my ($elem) = @_;
146  
147 19 my @args;
148 19 foreach my $token ( $elem->schildren() ) {
149 74 ▶ if ( $token->isa('PPI::Token::QuoteLike::Words') ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($token->isa('PPI::Token::QuoteLike::Words')) { } 8 66 74
elsif ($token->isa('PPI::Token::Quote')) { } 0 66 66
elsif ($token->isa('PPI::Structure::List')) { } 9 57 66
150 8 push @args, $token->literal();
151 }
152 elsif ( $token->isa('PPI::Token::Quote') ) {
153 0 push @args, $token->string();
154 }
155 elsif ( $token->isa('PPI::Structure::List') ) {
156 9 ▶ foreach my $inner ( $token->find('PPI::Token::Quote') || () ) {
Condition33%1/3
$token->find('PPI::Token::Quote') || ()
l!l&&r!l&&!r
090
MC/DC0%0/2
$token->find('PPI::Token::Quote') || ()
$token->find('PPI::Token::Quote') ()
Truth table33%1/3
A || B
A= $token->find('PPI::Token::Quote')B= ()
ABresult
1X1
011
000
157 0 push @args, $inner->string();
158 }
159 9 ▶ foreach my $inner ( $token->find('PPI::Token::QuoteLike::Words') || () ) {
Condition33%1/3
$token->find('PPI::Token::QuoteLike::Words') || ()
l!l&&r!l&&!r
090
MC/DC0%0/2
$token->find('PPI::Token::QuoteLike::Words') || ()
$token->find('PPI::Token::QuoteLike::Words') ()
Truth table33%1/3
A || B
A= $token->find('PPI::Token::QuoteLike::Words')B= ()
ABresult
1X1
011
000
160 0 push @args, $inner->literal();
161 }
162 }
163 }
164  
165 19 ▶ return grep { defined $_ && length $_ } @args;
Condition33%1/3
defined $_ && length $_
!ll&&!rl&&r
0011
MC/DC0%0/2
defined $_ && length $_
defined $_ length $_
Truth table33%1/3
A && B
A= defined $_B= length $_
ABresult
0X0
100
111
166 }
167  
168 11 ▶sub _has_empty_list {
Subroutine _has_empty_list: called CC=4 SCAR=13.9
169 11 my ($elem) = @_;
170  
171 11 foreach my $token ( $elem->schildren() ) {
172 33 ▶ next if !$token->isa('PPI::Structure::List');
Branch: 1 branch
BranchTrueFalseTotal
unless $token->isa("PPI::Structure::List") 24 9 33
173 9 ▶ return !$token->schildren() ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
!$token->schildren ? : 9 0 9
174 }
175 2 return 0;
176 }
177  
178 # Load the module and ask its symbol table, the way Exporter would.
179 2 ▶sub _default_exports {
Subroutine _default_exports: called CC=2 SCAR=7.0
180 2 my ($module) = @_;
181  
182 2 ▶ return () if !_load($module);
Branch: 1 branch
BranchTrueFalseTotal
unless _load($module) 0 2 2
183  
184 2 ▶ no strict 'refs'; ## no critic (ProhibitNoStrict)
Subroutine BEGIN: called CC=1 SCAR=0.0
185 2 return @{"${module}::EXPORT"};
186 }
187  
188 3 ▶sub _load {
Subroutine _load: called CC=3 SCAR=11.0
189 3 my ($module) = @_;
190  
191 3 my $path = $module;
192 3 $path =~ s{::}{/}g;
193 3 $path .= '.pm';
194  
195 3 ▶ return 1 if $INC{$path};
Branch: 1 branch
BranchTrueFalseTotal
if $INC{$path} 2 1 3
196  
197 1 ▶ local $SIG{__WARN__} = sub { };
Subroutine __ANON__: not called CC=1 SCAR=0.0
198 1 ▶ return eval { require $path; 1 } ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
eval { do { require $path; 1 } } ? : 1 0 1
199 }
200  
201 # Is any name imported by $elem actually used elsewhere in the file?
202 7 ▶sub _used_names {
Subroutine _used_names: called CC=8 SCAR=21.1
203 7 my ( $document, $elem ) = @_;
204  
205 7 my %used;
206 7 ▶ foreach my $word ( @{ $document->find('PPI::Token::Word') || [] } ) {
Condition50%1/2
$document->find('PPI::Token::Word') || []
l!l
70
MC/DC0%0/1
$document->find('PPI::Token::Word') || []
$document->find('PPI::Token::Word')
Truth table50%1/2
A
A= $document->find('PPI::Token::Word')
Aresult
11
00
207 20 ▶ next if _inside( $word, $elem );
Branch: 1 branch
BranchTrueFalseTotal
if _inside($word, $elem) 14 6 20
208 6 my $name = $word->content();
209 6 $name =~ s/\A.*:://;
210 6 $used{$name} = 1;
211 }
212 7 ▶ foreach my $symbol ( @{ $document->find('PPI::Token::Symbol') || [] } ) {
Condition100%2/2
$document->find('PPI::Token::Symbol') || []
l!l
61
MC/DC100%1/1
$document->find('PPI::Token::Symbol') || []
$document->find('PPI::Token::Symbol')
Truth table100%2/2
A
A= $document->find('PPI::Token::Symbol')
Aresult
11
00
213 8 ▶ next if _inside( $symbol, $elem );
Branch: 1 branch
BranchTrueFalseTotal
if _inside($symbol, $elem) 0 8 8
214 8 my $name = $symbol->symbol();
215 8 $name =~ s/\A[\$\@\%\&\*]//;
216 8 $name =~ s/\A.*:://;
217 8 $used{$name} = 1;
218 }
219 7 foreach my $name ( _interpolated_names( $document, $elem ) ) {
220 0 $name =~ s/\A.*:://;
221 0 $used{$name} = 1;
222 }
223  
224 7 return \%used;
225 }
226  
227 # Is the module named anywhere else -- Module::thing, Module->new, $Module::VAR?
228 12 ▶sub _module_referenced {
Subroutine _module_referenced: called CC=18 SCAR=29.4
229 12 my ( $document, $elem, $module ) = @_;
230  
231 # Another `use Foo::Bar;` is not a use of Foo. Without this, one unused
232 # import hides behind a sibling whose name happens to start the same way,
233 # which is exactly the case this policy exists to find.
234 12 my %other_modules;
235 12 ▶ foreach my $include ( @{ $document->find('PPI::Statement::Include') || [] } ) {
Condition50%1/2
$document->find('PPI::Statement::Include') || []
l!l
120
MC/DC0%0/1
$document->find('PPI::Statement::Include') || []
$document->find('PPI::Statement::Include')
Truth table50%1/2
A
A= $document->find('PPI::Statement::Include')
Aresult
11
00
236 13 ▶ next if $include == $elem;
Branch: 1 branch
BranchTrueFalseTotal
if $include == $elem 12 1 13
237 1 my $name = $include->module();
238 1 ▶ $other_modules{$name} = 1 if defined $name && length $name;
Branch: 1 branch
BranchTrueFalseTotal
if defined $name and length $name 1 0 1
Condition33%1/3
defined $name and length $name
!ll&&!rl&&r
001
MC/DC0%0/2
defined $name and length $name
defined $name length $name
Truth table33%1/3
A and B
A= defined $nameB= length $name
ABresult
0X0
100
111
239 }
240  
241 12 ▶ foreach my $word ( @{ $document->find('PPI::Token::Word') || [] } ) {
Condition50%1/2
$document->find('PPI::Token::Word') || []
l!l
120
MC/DC0%0/1
$document->find('PPI::Token::Word') || []
$document->find('PPI::Token::Word')
Truth table50%1/2
A
A= $document->find('PPI::Token::Word')
Aresult
11
00
242 38 ▶ next if _inside( $word, $elem );
Branch: 1 branch
BranchTrueFalseTotal
if _inside($word, $elem) 24 14 38
243 14 my $content = $word->content();
244 14 ▶ next if $other_modules{$content};
Branch: 1 branch
BranchTrueFalseTotal
if $other_modules{$content} 1 13 14
245 13 ▶ return 1 if $content eq $module || $content =~ m/\A\Q$module\E::/;
Branch: 1 branch
BranchTrueFalseTotal
if $content eq $module or $content =~ /\A\Q$module\E::/aa 2 11 13
Condition100%3/3
$content eq $module or $content =~ /\A\Q$module\E::/aa
l!l&&r!l&&!r
1111
MC/DC100%2/2
$content eq $module or $content =~ /\A\Q$module\E::/aa
$content eq $module $content =~ /\A\Q$module\E::/aa
Truth table100%3/3
A or B
A= $content eq $moduleB= $content =~ /\A\Q$module\E::/aa
ABresult
1X1
011
000
246 }
247 10 ▶ foreach my $symbol ( @{ $document->find('PPI::Token::Symbol') || [] } ) {
Condition100%2/2
$document->find('PPI::Token::Symbol') || []
l!l
73
MC/DC100%1/1
$document->find('PPI::Token::Symbol') || []
$document->find('PPI::Token::Symbol')
Truth table100%2/2
A
A= $document->find('PPI::Token::Symbol')
Aresult
11
00
248 7 ▶ next if _inside( $symbol, $elem );
Branch: 1 branch
BranchTrueFalseTotal
if _inside($symbol, $elem) 0 7 7
249 7 ▶ return 1 if $symbol->symbol() =~ m/\A[\$\@\%\&\*]\Q$module\E::/;
Branch: 1 branch
BranchTrueFalseTotal
if $symbol->symbol =~ /\A[\$\@\%\&\*]\Q$module\E::/aa 1 6 7
250 }
251 9 foreach my $name ( _interpolated_names( $document, $elem ) ) {
252 2 ▶ return 1 if $name =~ m/\A\Q$module\E::/;
Branch: 1 branch
BranchTrueFalseTotal
if $name =~ /\A\Q$module\E::/aa 2 0 2
253 }
254  
255 7 return 0;
256 }
257  
258 # Names that only ever appear inside an interpolating string. PPI hands those
259 # back as one token, so "$FindBin::Bin/../lib" contains no Symbol to find and
260 # the import that provided it reads as unused.
261 2 Readonly::Scalar my $INTERPOLATED_RX => qr/ [\$\@] \{? (\w+ (?: ::\w+ )*) /x;
262  
263 16 ▶sub _interpolated_names {
Subroutine _interpolated_names: called CC=6 SCAR=17.9
264 16 my ( $document, $elem ) = @_;
265  
266 16 my @classes = qw{
267 PPI::Token::Quote::Double
268 PPI::Token::Quote::Interpolate
269 PPI::Token::QuoteLike::Backtick
270 PPI::Token::QuoteLike::Command
271 PPI::Token::QuoteLike::Readline
272 PPI::Token::HereDoc
273 };
274  
275 16 my @names;
276 16 foreach my $class (@classes) {
277 96 ▶ foreach my $token ( @{ $document->find($class) || [] } ) {
Condition100%2/2
$document->find($class) || []
l!l
591
MC/DC100%1/1
$document->find($class) || []
$document->find($class)
Truth table100%2/2
A
A= $document->find($class)
Aresult
11
00
278 5 ▶ next if _inside( $token, $elem );
Branch: 1 branch
BranchTrueFalseTotal
if _inside($token, $elem) 0 5 5
279 5 ▶ my $content = $token->can('heredoc') ? join( '', $token->heredoc() ) : $token->content();
Branch: 1 branch
BranchTrueFalseTotal
$token->can('heredoc') ? : 1 4 5
280 5 push @names, $content =~ m/$INTERPOLATED_RX/g;
281 }
282 }
283  
284 16 return @names;
285 }
286  
287 78 ▶sub _inside {
Subroutine _inside: called CC=3 SCAR=11.0
288 78 my ( $token, $ancestor ) = @_;
289  
290 78 ▶ for ( my $node = $token; $node; $node = $node->parent() ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($node) 200 40 240
291 200 ▶ return 1 if $node == $ancestor;
Branch: 1 branch
BranchTrueFalseTotal
if $node == $ancestor 38 162 200
292 }
293 40 return 0;
294 }
295  
296 2 1;
297  
298 __END__
↑ Summary