File Coverage

blib/lib/Perl/Lint/Policy/Variables/ProhibitConditionalDeclarations.pm
Criterion Covered Total %
statement 41 41 100.0
branch 13 14 92.8
condition 18 18 100.0
subroutine 6 6 100.0
pod 0 1 0.0
total 78 80 97.5


line stmt bran cond sub pod time code
1             package Perl::Lint::Policy::Variables::ProhibitConditionalDeclarations;
2 133     133   66469 use strict;
  133         171  
  133         3179  
3 133     133   391 use warnings;
  133         146  
  133         2406  
4 133     133   744 use Perl::Lint::Constants::Type;
  133         191  
  133         60906  
5 133     133   550 use parent "Perl::Lint::Policy";
  133         150  
  133         585  
6              
7             use constant {
8 133         34421 DESC => q{Variable declared in conditional statement},
9             EXPL => q{Declare variables outside of the condition},
10 133     133   6606 };
  133         197  
11              
12             sub evaluate {
13 8     8 0 12 my ($class, $file, $tokens) = @_;
14              
15 8         6 my @violations;
16 8         8 my $token_num = scalar @$tokens;
17 8         21 for (my $i = 0; $i < $token_num; $i++) {
18 169         127 my $token = $tokens->[$i];
19 169         129 my $token_type = $token->{type};
20              
21 169 100 100     456 if ($token_type == VAR_DECL || $token_type == OUR_DECL) {
22 29         37 for ($i++; $i < $token_num; $i++) {
23 140         105 $token = $tokens->[$i];
24              
25 140 100       175 if ($token->{type} == ASSIGN) {
26 29         20 my $is_before_right_paren = 0;
27 29         42 for ($i++; $i < $token_num; $i++) {
28 70         46 $token = $tokens->[$i];
29 70         54 $token_type = $token->{type};
30              
31 70 100 100     387 if ($token_type == RIGHT_PAREN) {
    100 100        
      100        
      100        
32 5         6 $is_before_right_paren = 1;
33             }
34             elsif (
35             $token_type == IF_STATEMENT ||
36             $token_type == UNLESS_STATEMENT ||
37             $token_type == WHILE_STATEMENT ||
38             $token_type == FOR_STATEMENT ||
39             $token_type == FOREACH_STATEMENT
40             ) {
41             push @violations, {
42             filename => $file,
43             line => $token->{line},
44 24         62 description => DESC,
45             explanation => EXPL,
46             policy => __PACKAGE__,
47             };
48 24         27 last;
49             }
50             else {
51 41         37 my $_is_before_right_paren = $is_before_right_paren;
52 41         19 $is_before_right_paren = 0;
53 41 100 100     87 if ($_is_before_right_paren && $token_type == LEFT_BRACE) {
54 3         3 last;
55             }
56             }
57              
58 43 100       85 last if $token->{type} == SEMI_COLON;
59             }
60 29         46 last;
61             }
62              
63 111 50       188 last if $token->{type} == SEMI_COLON;
64             }
65             }
66             }
67              
68 8         26 return \@violations;
69             }
70              
71             1;
72