File Coverage

blib/lib/App/PerlNitpick/Rule/RemoveUnnecessaryScalarKeyword.pm
Criterion Covered Total %
statement 29 29 100.0
branch 2 4 50.0
condition n/a
subroutine 7 7 100.0
pod 0 1 0.0
total 38 41 92.6


line stmt bran cond sub pod time code
1             package App::PerlNitpick::Rule::RemoveUnnecessaryScalarKeyword;
2             # ABSTRACT: Remove unnecessary scalar keyword
3              
4             =encoding UTF-8
5              
6             This nitpicking rules removes C<scalar> keywords that are use in scalar context, for example, in this statement:
7              
8             my $n = scalar @items;
9              
10             Since the left hand side is a single scalar variable, the assignment is already in scalar context. It is not necessary to include C<scalar> on the right-hand side.
11              
12             =cut
13              
14 1     1   240836 use Moose;
  1         488038  
  1         7  
15 1     1   9223 use PPI::Document;
  1         115375  
  1         47  
16 1     1   657 use Perl::Critic::Document;
  1         1355530  
  1         82  
17 1     1   15 use Perl::Critic::Policy::TooMuchCode::ProhibitUnnecessaryScalarKeyword;
  1         3  
  1         38  
18              
19 1     1   557 use App::PerlNitpick::PCPWrap;
  1         3  
  1         34  
20              
21 1     1   7 no Moose;
  1         2  
  1         15  
22              
23             sub rewrite {
24 1     1 0 9860 my ($self, $doc) = @_;
25              
26 1         9 my $o = App::PerlNitpick::PCPWrap->new('Perl::Critic::Policy::TooMuchCode::ProhibitUnnecessaryScalarKeyword');
27              
28 1 50       9 my $elems = $doc->find( $o->applies_to ) or return $doc;
29 1         485 my @vio = map { $o->violates($_, $doc) } @$elems;
  2         37  
30              
31 1         3 for (@vio) {
32 1         3 my ($msg, $explain, $el) = @$_;
33 1 50       8 if ($el->next_sibling eq ' ') {
34 1         54 $el->next_sibling->remove;
35             }
36 1         77 $el->remove;
37             }
38              
39 1         45 return $doc;
40             }
41              
42             1;