File Coverage

blib/lib/Perl/Lint/Policy/Variables/ProhibitAugmentedAssignmentInDeclaration.pm
Criterion Covered Total %
statement 30 30 100.0
branch 8 8 100.0
condition 11 12 91.6
subroutine 6 6 100.0
pod 0 1 0.0
total 55 57 96.4


line stmt bran cond sub pod time code
1             package Perl::Lint::Policy::Variables::ProhibitAugmentedAssignmentInDeclaration;
2 133     133   92119 use strict;
  133         243  
  133         5099  
3 133     133   571 use warnings;
  133         213  
  133         3149  
4 133     133   610 use Compiler::Lexer::Constants;
  133         172  
  133         3310  
5 133     133   1377 use parent "Perl::Lint::Policy";
  133         585  
  133         615  
6              
7             use constant {
8 133         48703 DESC => q{Augmented assignment operator '%s' used in declaration},
9             EXPL => q{Use simple assignment when initializing variables},
10             AUGMENTED_ASSIGNMENTS => {
11             '**=' => 1, '+=' => 1, '-=' => 1, '.=' => 1,
12             '*=' => 1, '/=' => 1, '%=' => 1, 'x=' => 1,
13             '&=' => 1, '|=' => 1, '^=' => 1, '<<=' => 1,
14             '>>=' => 1, '&&=' => 1, '||=' => 1, '//=' => 1,
15             },
16             VAR => Compiler::Lexer::TokenType::T_Var,
17             LOCAL_VAR => Compiler::Lexer::TokenType::T_LocalVar,
18             GLOBAL_VAR => Compiler::Lexer::TokenType::T_GlobalVar,
19             SEMI_COLON => Compiler::Lexer::TokenType::T_SemiColon,
20             ASSIGN => Compiler::Lexer::TokenType::T_Assign,
21             DECL => Compiler::Lexer::Kind::T_Decl,
22             KIND_ASSIGN => Compiler::Lexer::Kind::T_Assign,
23 133     133   16815 };
  133         236  
24              
25             sub evaluate {
26 6     6 0 11 my ($class, $file, $tokens) = @_;
27              
28 6         10 my @violations;
29 6         12 my $token_num = scalar @$tokens;
30 6         21 for (my $i = 0; $i < $token_num; $i++) {
31 419 100       845 if ($tokens->[$i]->{kind} == DECL) {
32 86         58 $i++;
33 86         82 my $var_type = $tokens->[$i]->{type};
34 86 100 100     227 if ($var_type == VAR || $var_type == LOCAL_VAR || $var_type == GLOBAL_VAR) {
      100        
35 82         120 for ($i++; $i < $token_num; $i++) {
36 88         72 my $token = $tokens->[$i];
37 88 100 100     288 if (
    100 66        
38             $token->{kind} == KIND_ASSIGN &&
39             AUGMENTED_ASSIGNMENTS->{$token->{data}} # XXX Not good
40             ) {
41 73         278 push @violations, {
42             filename => $file,
43             line => $token->{line},
44             description => sprintf(DESC, $token->{data}),
45             explanation => EXPL,
46             policy => __PACKAGE__,
47             };
48 73         143 last;
49             }
50             elsif ($token->{type} == ASSIGN || $token->{type} == SEMI_COLON) {
51 9         19 last;
52             }
53             }
54             }
55             }
56             }
57 6         36 return \@violations;
58             }
59              
60             1;
61