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   68272 use strict;
  133         177  
  133         3502  
3 133     133   428 use warnings;
  133         166  
  133         2748  
4 133     133   450 use Compiler::Lexer::Constants;
  133         135  
  133         2496  
5 133     133   788 use parent "Perl::Lint::Policy";
  133         374  
  133         477  
6              
7             use constant {
8 133         35930 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   13411 };
  133         166  
24              
25             sub evaluate {
26 6     6 0 13 my ($class, $file, $tokens) = @_;
27              
28 6         6 my @violations;
29 6         10 my $token_num = scalar @$tokens;
30 6         20 for (my $i = 0; $i < $token_num; $i++) {
31 419 100       675 if ($tokens->[$i]->{kind} == DECL) {
32 86         52 $i++;
33 86         61 my $var_type = $tokens->[$i]->{type};
34 86 100 100     200 if ($var_type == VAR || $var_type == LOCAL_VAR || $var_type == GLOBAL_VAR) {
      100        
35 82         107 for ($i++; $i < $token_num; $i++) {
36 88         71 my $token = $tokens->[$i];
37 88 100 100     275 if (
    100 66        
38             $token->{kind} == KIND_ASSIGN &&
39             AUGMENTED_ASSIGNMENTS->{$token->{data}} # XXX Not good
40             ) {
41             push @violations, {
42             filename => $file,
43             line => $token->{line},
44 73         246 description => sprintf(DESC, $token->{data}),
45             explanation => EXPL,
46             policy => __PACKAGE__,
47             };
48 73         141 last;
49             }
50             elsif ($token->{type} == ASSIGN || $token->{type} == SEMI_COLON) {
51 9         23 last;
52             }
53             }
54             }
55             }
56             }
57 6         30 return \@violations;
58             }
59              
60             1;
61