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   4005 use strict;
  5         14  
  5         173  
4 5     5   30 use warnings;
  5         11  
  5         135  
5 5     5   33 use Perl::Critic::Utils;
  5         13  
  5         90  
6 5     5   7377 use PPIx::Utils::Traversal qw(get_constant_name_elements_from_declaring_statement);
  5         59814  
  5         357  
7 5     5   44 use Scalar::Util qw(refaddr);
  5         13  
  5         203  
8 5     5   29 use parent 'Perl::Critic::Policy';
  5         15  
  5         42  
9              
10 5     5   329 use Perl::Critic::TooMuchCode;
  5         15  
  5         2177  
11              
12 0     0 1 0 sub default_themes { return qw( maintenance ) }
13 8     8 1 110447 sub applies_to { return 'PPI::Document' }
14              
15             #---------------------------------------------------------------------------
16              
17             sub violates {
18 8     8 1 123 my ( $self, $elem, $doc ) = @_;
19              
20 8         26 my %defined_constants;
21             my %used;
22              
23 8   50 284   52 my $include_statements = $elem->find(sub { $_[1]->isa('PPI::Statement::Include') }) || [];
  284         3222  
24 8         145 for my $st (@$include_statements) {
25 11 50 33     68 next unless $st->schild(0) eq 'use' && $st->module eq 'constant';
26 11         751 my @constants = get_constant_name_elements_from_declaring_statement( $st );
27 11         1831 for my $tok (@constants) {
28 15         53 push @{ $defined_constants{"$tok"} }, $st;
  15         45  
29             }
30             }
31              
32 8 50   284   65 for my $el_word (@{ $elem->find( sub { $_[1]->isa('PPI::Token::Word') }) ||[]}) {
  8         62  
  284         3118  
33 50         526 my $st = $el_word->statement;
34 50 100       685 if ($defined_constants{"$el_word"}) {
35 19         89 for my $st (@{ $defined_constants{"$el_word"} }) {
  19         45  
36 19 100       120 unless ($el_word->descendant_of($st)) {
37 4         58 $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         98 Perl::Critic::TooMuchCode::__get_terop_usage(\%used, $doc);
47              
48 8         147 my @violations;
49 8         92 my @to_report = grep { !$used{$_} } (sort keys %defined_constants);
  15         59  
50 8         29 for my $tok (@to_report) {
51 10         1402 for my $el (@{ $defined_constants{$tok} }) {
  10         29  
52 10         79 push @violations, $self->violation( 'Unused constant', "A constant <$tok> is defined but not used.", $el );
53             }
54             }
55              
56 8         1732 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