File Coverage

blib/lib/Perl/Lint/Policy/Variables/RequireLexicalLoopIterators.pm
Criterion Covered Total %
statement 30 30 100.0
branch 7 8 87.5
condition 10 12 83.3
subroutine 6 6 100.0
pod 0 1 0.0
total 53 57 92.9


line stmt bran cond sub pod time code
1             package Perl::Lint::Policy::Variables::RequireLexicalLoopIterators;
2 133     133   869626 use strict;
  133         328  
  133         5805  
3 133     133   636 use warnings;
  133         201  
  133         4203  
4 133     133   1960 use Perl::Lint::Constants::Type;
  133         200  
  133         92064  
5 133     133   887 use parent "Perl::Lint::Policy";
  133         216  
  133         1086  
6              
7             use constant {
8 133         54268 DESC => 'Loop iterator is not lexical',
9             EXPL => [108],
10 133     133   10546 };
  133         249  
11              
12             sub evaluate {
13 8     8 0 13 my ($class, $file, $tokens, $src, $args) = @_;
14              
15 8         11 my $perl_version_threshold = 5.003;
16 8         9 my $perl_version = 6; # to hit always on default. Yes, this is Perl6 :)
17              
18 8         9 my @violations;
19 8         28 for (my $i = 0, my $token_type; my $token = $tokens->[$i]; $i++) {
20 154         146 $token_type = $token->{type};
21              
22 154 100 100     840 if (
    100 66        
      66        
23             $token_type == FOR_STATEMENT ||
24             ($token_type == FOREACH_STATEMENT && $perl_version > $perl_version_threshold)
25             ) {
26 18         22 $token = $tokens->[++$i];
27 18         17 $token_type = $token->{type};
28              
29 18 100 100     86 if ($token_type != VAR_DECL && $token_type != LEFT_PAREN) {
30 9         46 push @violations, {
31             filename => $file,
32             line => $token->{line},
33             description => DESC,
34             explanation => EXPL,
35             policy => __PACKAGE__,
36             };
37             }
38             }
39             elsif ($token_type == USE_DECL || $token_type == REQUIRE_DECL) {
40 1         4 $token = $tokens->[++$i];
41 1 50       3 if ($token->{type} == DOUBLE) {
42 1         4 $perl_version = $token->{data};
43             }
44             }
45             }
46              
47 8         37 return \@violations;
48             }
49              
50             1;
51