File Coverage

blib/lib/App/PerlNitpick/Rule/DedupeIncludeStatements.pm
Criterion Covered Total %
statement 19 19 100.0
branch 4 6 66.6
condition 1 3 33.3
subroutine 3 3 100.0
pod 0 1 0.0
total 27 32 84.3


line stmt bran cond sub pod time code
1             package App::PerlNitpick::Rule::DedupeIncludeStatements;
2 1     1   278233 use Moose;
  1         383332  
  1         7  
3              
4             sub rewrite {
5 3     3 0 13313 my ($self, $document) = @_;
6              
7 3         6 my %used;
8             my @to_delete;
9 3 50       4 for my $el (@{ $document->find('PPI::Statement::Include') ||[]}) {
  3         11  
10 6 50 33     1480 next unless $el->type && $el->type eq 'use';
11 6         202 my $module = $el->module;
12 6         107 my $code = "$el";
13 6 100       108 if ($used{$code}) {
14 2         5 push @to_delete, $el;
15             } else {
16 4         9 $used{$code} = 1;
17             }
18             }
19              
20 3         6 for my $el (@to_delete) {
21 2         8 $el->remove;
22             }
23              
24 3         88 return $document;
25             }
26              
27 1     1   6345 no Moose;
  1         3  
  1         5  
28             1;
29              
30             __END__
31              
32             =head1 DedupeIncludeStatements
33              
34             In this rule, multiple identical "use" statements of the same module are merged.
35              
36             For example, this code:
37              
38             use File::Temp;
39             use Foobar;
40             use File::Temp;
41              
42             ... is transformed to:
43              
44             use File::Temp;
45             use Foobar;
46              
47             Two statements are consider identical if, and only if, they are literally the same, character-to-character.