| 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
| |||||||||||||||||||||||||||||
| 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
Condition66%2/3
defined $module and length $module
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
| |||||||||||||||||||||||||||||
| 66 | 31 | ▶ | return if $elem->version(); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 67 | 31 | ▶ | return if $self->{_allow}{$module}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
Condition100%3/3
not defined $imports[0] and @imports
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
| |||||||||||||||||||||||||||||
| 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
Condition100%3/3
@imports or _module_referenced($document, $elem, $module)
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)
| |||||||||||||||||||||||||||||
| 74 | if !@imports && !_module_referenced( $document, $elem, $module ); | ||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | 14 | ▶ | return if !@imports; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
Condition100%3/3
$_ eq 'import' || $_ eq 'unimport'
MC/DC100%2/2
$_ eq 'import' || $_ eq 'unimport'
$_ eq 'import'
$_ eq 'unimport'
Truth table100%3/3
A || B
A= $_ eq 'import'B= $_ eq 'unimport'
| |||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | 7 | my $used = _used_names( $document, $elem ); | |||||||||||||||||||||||||||
| 82 | 7 | foreach my $name (@imports) { | |||||||||||||||||||||||||||
| 83 | 14 | ▶ | return if $used->{$name}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
Condition100%3/3
not @listed and _has_empty_list($elem)
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)
| |||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | 10 | ▶ | if (@listed) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 112 | 1 | ▶ | my @expanded = _tag_exports( $module, $tag ) or return (undef); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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') || ()
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= ()
| |||||||||||||||||||||||||||||
| 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') || ()
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= ()
| |||||||||||||||||||||||||||||
| 160 | 0 | push @args, $inner->literal(); | |||||||||||||||||||||||||||
| 161 | } | ||||||||||||||||||||||||||||
| 162 | } | ||||||||||||||||||||||||||||
| 163 | } | ||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||
| 165 | 19 | ▶ | return grep { defined $_ && length $_ } @args; | ||||||||||||||||||||||||||
|
Condition33%1/3
defined $_ && length $_
MC/DC0%0/2
defined $_ && length $_
defined $_
length $_
Truth table33%1/3
A && B
A= defined $_B= length $_
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 173 | 9 | ▶ | return !$token->schildren() ? 1 : 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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') || []
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')
| |||||||||||||||||||||||||||||
| 207 | 20 | ▶ | next if _inside( $word, $elem ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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') || []
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')
| |||||||||||||||||||||||||||||
| 213 | 8 | ▶ | next if _inside( $symbol, $elem ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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') || []
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')
| |||||||||||||||||||||||||||||
| 236 | 13 | ▶ | next if $include == $elem; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 237 | 1 | my $name = $include->module(); | |||||||||||||||||||||||||||
| 238 | 1 | ▶ | $other_modules{$name} = 1 if defined $name && length $name; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $name and length $name
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
| |||||||||||||||||||||||||||||
| 239 | } | ||||||||||||||||||||||||||||
| 240 | |||||||||||||||||||||||||||||
| 241 | 12 | ▶ | foreach my $word ( @{ $document->find('PPI::Token::Word') || [] } ) { | ||||||||||||||||||||||||||
|
Condition50%1/2
$document->find('PPI::Token::Word') || []
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')
| |||||||||||||||||||||||||||||
| 242 | 38 | ▶ | next if _inside( $word, $elem ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 243 | 14 | my $content = $word->content(); | |||||||||||||||||||||||||||
| 244 | 14 | ▶ | next if $other_modules{$content}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 245 | 13 | ▶ | return 1 if $content eq $module || $content =~ m/\A\Q$module\E::/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$content eq $module or $content =~ /\A\Q$module\E::/aa
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
| |||||||||||||||||||||||||||||
| 246 | } | ||||||||||||||||||||||||||||
| 247 | 10 | ▶ | foreach my $symbol ( @{ $document->find('PPI::Token::Symbol') || [] } ) { | ||||||||||||||||||||||||||
|
Condition100%2/2
$document->find('PPI::Token::Symbol') || []
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')
| |||||||||||||||||||||||||||||
| 248 | 7 | ▶ | next if _inside( $symbol, $elem ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 249 | 7 | ▶ | return 1 if $symbol->symbol() =~ m/\A[\$\@\%\&\*]\Q$module\E::/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 250 | } | ||||||||||||||||||||||||||||
| 251 | 9 | foreach my $name ( _interpolated_names( $document, $elem ) ) { | |||||||||||||||||||||||||||
| 252 | 2 | ▶ | return 1 if $name =~ m/\A\Q$module\E::/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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) || []
MC/DC100%1/1
$document->find($class) || []
$document->find($class)
Truth table100%2/2
A
A= $document->find($class)
| |||||||||||||||||||||||||||||
| 278 | 5 | ▶ | next if _inside( $token, $elem ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 279 | 5 | ▶ | my $content = $token->can('heredoc') ? join( '', $token->heredoc() ) : $token->content(); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 291 | 200 | ▶ | return 1 if $node == $ancestor; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 292 | } | ||||||||||||||||||||||||||||
| 293 | 40 | return 0; | |||||||||||||||||||||||||||
| 294 | } | ||||||||||||||||||||||||||||
| 295 | |||||||||||||||||||||||||||||
| 296 | 2 | 1; | |||||||||||||||||||||||||||
| 297 | |||||||||||||||||||||||||||||
| 298 | __END__ | ||||||||||||||||||||||||||||