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   87907 use strict;
  133         240  
  133         4928  
3 133     133   562 use warnings;
  133         187  
  133         3138  
4 133     133   963 use Perl::Lint::Constants::Type;
  133         169  
  133         94899  
5 133     133   774 use parent "Perl::Lint::Policy";
  133         203  
  133         814  
6              
7             use constant {
8 133         44723 DESC => q{Variable declared in conditional statement},
9             EXPL => q{Declare variables outside of the condition},
10 133     133   8847 };
  133         270  
11              
12             sub evaluate {
13 8     8 0 13 my ($class, $file, $tokens) = @_;
14              
15 8         7 my @violations;
16 8         13 my $token_num = scalar @$tokens;
17 8         22 for (my $i = 0; $i < $token_num; $i++) {
18 169         124 my $token = $tokens->[$i];
19 169         141 my $token_type = $token->{type};
20              
21 169 100 100     618 if ($token_type == VAR_DECL || $token_type == OUR_DECL) {
22 29         50 for ($i++; $i < $token_num; $i++) {
23 140         101 $token = $tokens->[$i];
24              
25 140 100       194 if ($token->{type} == ASSIGN) {
26 29         21 my $is_before_right_paren = 0;
27 29         41 for ($i++; $i < $token_num; $i++) {
28 70         56 $token = $tokens->[$i];
29 70         58 $token_type = $token->{type};
30              
31 70 100 100     416 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 24         80 push @violations, {
42             filename => $file,
43             line => $token->{line},
44             description => DESC,
45             explanation => EXPL,
46             policy => __PACKAGE__,
47             };
48 24         26 last;
49             }
50             else {
51 41         31 my $_is_before_right_paren = $is_before_right_paren;
52 41         30 $is_before_right_paren = 0;
53 41 100 100     81 if ($_is_before_right_paren && $token_type == LEFT_BRACE) {
54 3         4 last;
55             }
56             }
57              
58 43 100       101 last if $token->{type} == SEMI_COLON;
59             }
60 29         47 last;
61             }
62              
63 111 50       219 last if $token->{type} == SEMI_COLON;
64             }
65             }
66             }
67              
68 8         31 return \@violations;
69             }
70              
71             1;
72