File Coverage

blib/lib/Perl/Critic/Policy/TooMuchCode/ProhibitUnnecessaryScalarKeyword.pm
Criterion Covered Total %
statement 22 23 95.6
branch 5 8 62.5
condition 6 18 33.3
subroutine 6 7 85.7
pod 3 3 100.0
total 42 59 71.1


line stmt bran cond sub pod time code
1             package Perl::Critic::Policy::TooMuchCode::ProhibitUnnecessaryScalarKeyword;
2 4     4   2866 use strict;
  4         10  
  4         122  
3 4     4   23 use warnings;
  4         10  
  4         112  
4              
5 4     4   31 use Perl::Critic::Utils;
  4         44  
  4         78  
6 4     4   3622 use parent 'Perl::Critic::Policy';
  4         12  
  4         25  
7              
8 0     0 1 0 sub default_themes { return qw(maintenance) }
9 1     1 1 14902 sub applies_to { return 'PPI::Token::Word' }
10              
11             sub violates {
12 3     3 1 47 my ( $self, $elem, undef ) = @_;
13 3 100       8 return unless $elem->content eq 'scalar';
14 1         9 my $e = $elem->snext_sibling;
15 1 50 33     58 return unless $e && $e->isa('PPI::Token::Symbol') && $e->raw_type eq '@';
      33        
16 1         19 $e = $elem->sprevious_sibling;
17 1 50 33     44 return unless $e && $e->isa('PPI::Token::Operator') && $e->content eq '=';
      33        
18 1         15 $e = $e->sprevious_sibling;
19 1 50 33     32 return unless $e && $e->isa('PPI::Token::Symbol') && $e->raw_type eq '$';
      33        
20              
21 1         17 return $self->violation('Unnecessary scalar keyword', "Assigning an array to a scalar implies scalar context.", $elem);
22             }
23              
24             1;
25              
26             __END__
27              
28             =head1 NAME
29              
30             TooMuchCode::ProhibitUnnecessaryScalarKeyword - Finds `scalar` in scalar context.
31              
32             =head1 DESCRIPTION
33              
34             This policy dictates that the use of `scalar` for in statement like this needs to be removed:
35              
36             my $n = scalar @items;
37              
38             If the left-hand side of assignment is a single scalar variable, then the assignment is in scalar
39             context. There is no need to add C<scalar> keyword.