lib/Perl/Critic/Policy/Variables/ProhibitUnusedVariables.pm

filter: Statementstmt 89.6% 69 / 77 Branchbran 54.5% 12 / 22 Conditioncond 83.3% 5 / 6 MC/DCmcdc 66.6% 2 / 3 Subroutinesub 100% 17 / 17 Podpod 80.0% 4 / 5 totaltotal 83.8% 109 / 130 CC 24 SCAR 19.8
CC
24
Cov
82%
_get_regexp_symbol_usage
12.0
violates
11.5
_get_symbol_usage
8.1
CRAP 7.2
« lib/Perl/Critic/Policy/ValuesAndExpressions/ProhibitLongChainsOfMethodCalls.pm ↑ Summary lib/Perl/Critic/OptionsProcessor.pm »
1 package Perl::Critic::Policy::Variables::ProhibitUnusedVariables;
2  
3 40 ▶use 5.010001;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 40 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 40 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 40 ▶use Readonly;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 40 ▶use List::SomeUtils qw( any );
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 40 ▶use PPI::Token::Symbol;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 40 ▶use PPIx::QuoteLike;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 40 ▶use Perl::Critic::Utils qw< :characters :severities >;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 40 ▶use parent 'Perl::Critic::Policy';
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 40 our $VERSION = '1.156';
17  
18 #-----------------------------------------------------------------------------
19  
20 40 Readonly::Scalar my $EXPL =>
21 q<Unused variables clutter code and make it harder to read>;
22  
23 #-----------------------------------------------------------------------------
24  
25 90 ▶sub supported_parameters { return () }
Subroutine supported_parameters: called CC=1 SCAR=0.0
Pod undocumented
26 74 ▶sub default_severity { return $SEVERITY_MEDIUM }
Subroutine default_severity: called CC=1 SCAR=0.0
Pod documented
27 73 ▶sub default_themes { return qw< core maintenance certrec > }
Subroutine default_themes: called CC=1 SCAR=0.0
Pod documented
28 31 ▶sub applies_to { return qw< PPI::Document > }
Subroutine applies_to: called CC=1 SCAR=0.0
Pod documented
29  
30 #-----------------------------------------------------------------------------
31  
32 31 ▶sub violates {
Subroutine violates: called CC=9 SCAR=24.4
Pod documented
33 31 my ( $self, undef, $document ) = @_;
34  
35 31 my %symbol_usage;
36 31 _get_symbol_usage( \%symbol_usage, $document );
37 31 _get_regexp_symbol_usage( \%symbol_usage, $document );
38 31 ▶ return if not %symbol_usage;
Branch: 1 branch
BranchTrueFalseTotal
unless %symbol_usage 2 29 31
39  
40 29 my $declarations = $document->find('PPI::Statement::Variable');
41 29 ▶ return if not $declarations;
Branch: 1 branch
BranchTrueFalseTotal
unless $declarations 2 27 29
42  
43 27 my @violations;
44  
45 DECLARATION:
46 27 foreach my $declaration ( @{$declarations} ) {
47 85 ▶ next DECLARATION if 'my' ne $declaration->type();
Branch: 1 branch
BranchTrueFalseTotal
if "my" ne $declaration->type 27 58 85
48  
49 58 my @children = $declaration->schildren();
50 58 ▶ next DECLARATION if any { $_->content() eq q<=> } @children;
Subroutine __ANON__: called CC=1 SCAR=0.2
Branch: 1 branch
BranchTrueFalseTotal
if &any(sub { $_->content eq "="; } , @children) 58 0 58
51  
52 VARIABLE:
53 0 foreach my $variable ( $declaration->variables() ) {
54 0 my $count = $symbol_usage{ $variable };
55 0 ▶ next VARIABLE if not $count; # BUG!
Branch: 1 branch
BranchTrueFalseTotal
unless $count 0 0 0
56 0 ▶ next VARIABLE if $count > 1;
Branch: 1 branch
BranchTrueFalseTotal
if $count > 1 0 0 0
57  
58 0 push
59 @violations,
60 $self->violation(
61 qq<"$variable" is declared but not used.>,
62 $EXPL,
63 $declaration,
64 );
65 }
66 }
67  
68 27 return @violations;
69 }
70  
71 31 ▶sub _get_symbol_usage {
Subroutine _get_symbol_usage: called CC=8 SCAR=20.9
72 31 my ( $symbol_usage, $document ) = @_;
73  
74 31 my $symbols = $document->find('PPI::Token::Symbol');
75 31 ▶ return if not $symbols;
Branch: 1 branch
BranchTrueFalseTotal
unless $symbols 2 29 31
76  
77 29 foreach my $symbol ( @{$symbols} ) {
78 173 $symbol_usage->{ $symbol->symbol() }++;
79 }
80  
81 29 foreach my $class ( qw{
82 PPI::Token::Quote::Double
83 PPI::Token::Quote::Interpolate
84 PPI::Token::QuoteLike::Backtick
85 PPI::Token::QuoteLike::Command
86 PPI::Token::QuoteLike::Readline
87 PPI::Token::HereDoc
88 } ) {
89 174 foreach my $double_quotish (
90 174 ▶ @{ $document->find( $class ) || [] }
Condition100%2/2
$document->find($class) || []
l!l
1173
MC/DC100%1/1
$document->find($class) || []
$document->find($class)
Truth table100%2/2
A
A= $document->find($class)
Aresult
11
00
91 ) {
92 1 ▶ my $str = PPIx::QuoteLike->new( $double_quotish )
Branch: 1 branch
BranchTrueFalseTotal
my $str = "PPIx::QuoteLike"->new($double_quotish) or next 1 0 1
93 or next;
94 1 foreach my $var ( $str->variables() ) {
95 0 $symbol_usage->{ $var }++;
96 }
97 }
98 }
99  
100 29 return;
101 }
102  
103 31 ▶sub _get_regexp_symbol_usage {
Subroutine _get_regexp_symbol_usage: called CC=9 SCAR=24.8
104 31 my ( $symbol_usage, $document ) = @_;
105  
106 31 foreach my $class ( qw{
107 PPI::Token::Regexp::Match
108 PPI::Token::Regexp::Substitute
109 PPI::Token::QuoteLike::Regexp
110 } ) {
111  
112 93 ▶ foreach my $regex ( @{ $document->find( $class ) || [] } ) {
Condition100%2/2
$document->find($class) || []
l!l
192
MC/DC100%1/1
$document->find($class) || []
$document->find($class)
Truth table100%2/2
A
A= $document->find($class)
Aresult
11
00
113  
114 1 ▶ my $ppix = $document->ppix_regexp_from_element( $regex ) or next;
Branch: 1 branch
BranchTrueFalseTotal
my $ppix = $document->ppix_regexp_from_element($regex) or next 1 0 1
115 1 ▶ $ppix->failures() and next;
Branch: 1 branch
BranchTrueFalseTotal
$ppix->failures and next 0 1 1
116  
117 1 foreach my $code ( @{
118 1 ▶ $ppix->find( 'PPIx::Regexp::Token::Code' ) || [] } ) {
Condition50%1/2
$ppix->find('PPIx::Regexp::Token::Code') || []
l!l
01
MC/DC0%0/1
$ppix->find('PPIx::Regexp::Token::Code') || []
$ppix->find('PPIx::Regexp::Token::Code')
Truth table50%1/2
A
A= $ppix->find('PPIx::Regexp::Token::Code')
Aresult
11
00
119 0 ▶ my $subdoc = $code->ppi() or next;
Branch: 1 branch
BranchTrueFalseTotal
my $subdoc = $code->ppi or next 0 0 0
120 0 _get_symbol_usage( $symbol_usage, $subdoc );
121 }
122  
123 }
124  
125 }
126  
127 31 return;
128 }
129  
130 #-----------------------------------------------------------------------------
131  
132 40 1;
133  
134 __END__
« lib/Perl/Critic/Policy/ValuesAndExpressions/ProhibitLongChainsOfMethodCalls.pm ↑ Summary lib/Perl/Critic/OptionsProcessor.pm »