File Coverage

blib/lib/Perl/Lint/Policy/ControlStructures/ProhibitCStyleForLoops.pm
Criterion Covered Total %
statement 35 36 97.2
branch 12 14 85.7
condition 3 3 100.0
subroutine 7 7 100.0
pod 0 1 0.0
total 57 61 93.4


line stmt bran cond sub pod time code
1             package Perl::Lint::Policy::ControlStructures::ProhibitCStyleForLoops;
2 134     134   69415 use strict;
  134         168  
  134         3214  
3 134     134   423 use warnings;
  134         167  
  134         2435  
4 134     134   761 use Perl::Lint::Constants::Type;
  134         134  
  134         60277  
5 134     134   893 use Perl::Lint::Constants::Kind;
  134         250  
  134         6705  
6 134     134   454 use parent "Perl::Lint::Policy";
  134         149  
  134         537  
7              
8             use constant {
9 134         27852 DESC => 'C-style "for" loop used',
10             EXPL => [100],
11 134     134   6768 };
  134         151  
12              
13             sub evaluate {
14 5     5 0 8 my ($class, $file, $tokens, $src, $args) = @_;
15              
16 5         7 my @violations;
17 5         20 for (my $i = 0; my $token = $tokens->[$i]; $i++) {
18 114         77 my $token_type = $token->{type};
19              
20 114 100 100     403 if ($token_type == FOR_STATEMENT || $token_type == FOREACH_STATEMENT) {
21 11         11 my $token = $tokens->[++$i];
22 11 100       25 if ($token->{type} == LEFT_PAREN) {
23 8         9 my $semi_colon_count = 0;
24 8         5 my $left_paren_num = 1;
25 8         21 for ($i++; my $token = $tokens->[$i]; $i++) {
26 80         54 my $token_type = $token->{type};
27 80 50       218 if ($token_type == LEFT_PAREN) {
    100          
    100          
28 0         0 $left_paren_num++;
29             }
30             elsif ($token_type == RIGHT_PAREN) {
31 8 50       20 last if --$left_paren_num <= 0;
32             }
33             elsif ($token_type == SEMI_COLON) {
34 14         26 $semi_colon_count++;
35             }
36             }
37              
38 8 100       18 if ($semi_colon_count == 2) {
39             push @violations, {
40             filename => $file,
41             line => $token->{line},
42 7         50 description => DESC,
43             explanation => EXPL,
44             policy => __PACKAGE__,
45             };
46             }
47             }
48             }
49             }
50              
51 5         18 return \@violations;
52             }
53              
54             1;
55