File Coverage

blib/lib/Perl/Critic/Policy/TooMuchCode/ProhibitUnusedConstant.pm
Criterion Covered Total %
statement 50 51 98.0
branch 6 8 75.0
condition 2 5 40.0
subroutine 11 12 91.6
pod 3 3 100.0
total 72 79 91.1


line stmt bran cond sub pod time code
1             package Perl::Critic::Policy::TooMuchCode::ProhibitUnusedConstant;
2              
3 5     5   3857 use strict;
  5         15  
  5         170  
4 5     5   28 use warnings;
  5         18  
  5         185  
5 5     5   35 use Perl::Critic::Utils;
  5         12  
  5         103  
6 5     5   7492 use PPIx::Utils::Traversal qw(get_constant_name_elements_from_declaring_statement);
  5         61960  
  5         388  
7 5     5   49 use Scalar::Util qw(refaddr);
  5         13  
  5         208  
8 5     5   35 use parent 'Perl::Critic::Policy';
  5         15  
  5         42  
9              
10 5     5   361 use Perl::Critic::TooMuchCode;
  5         13  
  5         2107  
11              
12 0     0 1 0 sub default_themes { return qw( maintenance ) }
13 8     8 1 107223 sub applies_to { return 'PPI::Document' }
14              
15             #---------------------------------------------------------------------------
16              
17             sub violates {
18 8     8 1 117 my ( $self, $elem, $doc ) = @_;
19              
20 8         27 my %defined_constants;
21             my %used;
22              
23 8   50 284   53 my $include_statements = $elem->find(sub { $_[1]->isa('PPI::Statement::Include') }) || [];
  284         3081  
24 8         141 for my $st (@$include_statements) {
25 11 50 33     68 next unless $st->schild(0) eq 'use' && $st->module eq 'constant';
26 11         748 my @constants = get_constant_name_elements_from_declaring_statement( $st );
27 11         1818 for my $tok (@constants) {
28 15         52 push @{ $defined_constants{"$tok"} }, $st;
  15         39  
29             }
30             }
31              
32 8 50   284   59 for my $el_word (@{ $elem->find( sub { $_[1]->isa('PPI::Token::Word') }) ||[]}) {
  8         61  
  284         3078  
33 50         524 my $st = $el_word->statement;
34 50 100       747 if ($defined_constants{"$el_word"}) {
35 19         91 for my $st (@{ $defined_constants{"$el_word"} }) {
  19         46  
36 19 100       179 unless ($el_word->descendant_of($st)) {
37 4         57 $used{"$el_word"}++;
38             }
39             }
40             }
41             }
42              
43             ## Look for the signature of misparsed ternary operator.
44             ## https://github.com/adamkennedy/PPI/issues/62
45             ## Once PPI is fixed, this workaround can be eliminated.
46 8         94 Perl::Critic::TooMuchCode::__get_terop_usage(\%used, $doc);
47              
48 8         130 my @violations;
49 8         82 my @to_report = grep { !$used{$_} } (sort keys %defined_constants);
  15         57  
50 8         26 for my $tok (@to_report) {
51 10         1763 for my $el (@{ $defined_constants{$tok} }) {
  10         32  
52 10         66 push @violations, $self->violation( 'Unused constant', "A constant <$tok> is defined but not used.", $el );
53             }
54             }
55              
56 8         1624 return @violations;
57             }
58              
59             1;
60              
61             =encoding utf-8
62              
63             =head1 NAME
64              
65             TooMuchCode::ProhibitUnusedConstant -- Find unused constants.
66              
67             =head1 DESCRIPTION
68              
69             This policy finds constant declarations by "constant" pragma, and further looks to see if they exist in the rest of the code.
70             (The scope of searching is within the same file.)
71              
72             It identifies constants defined in two simple forms, such as:
73              
74             use constant PI => 3.14;
75              
76             ... and
77              
78             use constant { PI => 3.14, TAU => 6.28 };
79              
80             =cut