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   68262 use strict;
  133         168  
  133         3030  
3 133     133   420 use warnings;
  133         145  
  133         2427  
4 133     133   781 use Perl::Lint::Constants::Type;
  133         159  
  133         60952  
5 133     133   623 use parent "Perl::Lint::Policy";
  133         152  
  133         643  
6              
7             use constant {
8 133         34127 DESC => q{Variable declared in conditional statement},
9             EXPL => q{Declare variables outside of the condition},
10 133     133   6787 };
  133         200  
11              
12             sub evaluate {
13 8     8 0 11 my ($class, $file, $tokens) = @_;
14              
15 8         7 my @violations;
16 8         8 my $token_num = scalar @$tokens;
17 8         29 for (my $i = 0; $i < $token_num; $i++) {
18 169         120 my $token = $tokens->[$i];
19 169         123 my $token_type = $token->{type};
20              
21 169 100 100     444 if ($token_type == VAR_DECL || $token_type == OUR_DECL) {
22 29         37 for ($i++; $i < $token_num; $i++) {
23 140         88 $token = $tokens->[$i];
24              
25 140 100       161 if ($token->{type} == ASSIGN) {
26 29         19 my $is_before_right_paren = 0;
27 29         42 for ($i++; $i < $token_num; $i++) {
28 70         43 $token = $tokens->[$i];
29 70         50 $token_type = $token->{type};
30              
31 70 100 100     357 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         71 description => DESC,
45             explanation => EXPL,
46             policy => __PACKAGE__,
47             };
48 24         21 last;
49             }
50             else {
51 41         32 my $_is_before_right_paren = $is_before_right_paren;
52 41         28 $is_before_right_paren = 0;
53 41 100 100     66 if ($_is_before_right_paren && $token_type == LEFT_BRACE) {
54 3         4 last;
55             }
56             }
57              
58 43 100       76 last if $token->{type} == SEMI_COLON;
59             }
60 29         45 last;
61             }
62              
63 111 50       183 last if $token->{type} == SEMI_COLON;
64             }
65             }
66             }
67              
68 8         22 return \@violations;
69             }
70              
71             1;
72