File Coverage

blib/lib/Perl/Lint/Policy/BuiltinFunctions/ProhibitVoidGrep.pm
Criterion Covered Total %
statement 45 45 100.0
branch 26 26 100.0
condition 8 9 88.8
subroutine 6 6 100.0
pod 0 1 0.0
total 85 87 97.7


line stmt bran cond sub pod time code
1             package Perl::Lint::Policy::BuiltinFunctions::ProhibitVoidGrep;
2 133     133   93590 use strict;
  133         263  
  133         5309  
3 133     133   644 use warnings;
  133         235  
  133         3764  
4 133     133   974 use Perl::Lint::Constants::Type;
  133         322  
  133         81724  
5 133     133   806 use parent "Perl::Lint::Policy";
  133         251  
  133         797  
6              
7             # TODO REFACTOR!!!
8              
9             use constant {
10 133         46864 DESC => '"grep" used in void context',
11             EXPL => 'Use a "for" loop instead',
12 133     133   9125 };
  133         377  
13              
14             sub evaluate {
15 6     6 0 13 my ($class, $file, $tokens, $args) = @_;
16              
17 6         9 my @violations;
18 6         9 my $is_in_context = 0;
19 6         6 my $is_in_grep = 0;
20 6         10 my $is_in_ctrl_statement = 0;
21 6         11 my $left_brace_num = 0;
22 6         25 for (my $i = 0; my $token = $tokens->[$i]; $i++) {
23 378         321 my $token_type = $token->{type};
24 378         368 my $token_data = $token->{data};
25 378 100 100     2602 if ($token_type == BUILTIN_FUNC) {
    100 100        
    100 66        
    100          
    100          
26 50 100       79 if ($token_data eq 'grep') {
27 42 100       84 next if $is_in_grep;
28              
29 24 100       53 if ($is_in_ctrl_statement) {
    100          
30 5 100       12 if ($left_brace_num) {
31 3         11 push @violations, {
32             filename => $file,
33             line => $token->{line},
34             description => DESC,
35             explanation => EXPL,
36             policy => __PACKAGE__,
37             };
38             }
39             }
40             elsif (!$is_in_context) {
41 11         54 push @violations, {
42             filename => $file,
43             line => $token->{line},
44             description => DESC,
45             explanation => EXPL,
46             policy => __PACKAGE__,
47             };
48             }
49              
50 24         28 $is_in_grep = 1;
51 24         50 next;
52             }
53 8         18 $is_in_context = 1;
54             }
55             elsif ($token_type == ASSIGN) {
56 3         11 $is_in_context = 1;
57             }
58             elsif ( # NOTE enough?
59             $token_type == IF_STATEMENT ||
60             $token_type == FOR_STATEMENT ||
61             $token_type == WHILE_STATEMENT ||
62             $token_type == UNLESS_STATEMENT
63             ) {
64 5         10 $is_in_ctrl_statement = 1;
65             }
66             elsif ($is_in_ctrl_statement) {
67 50 100       109 if ($token_type == LEFT_BRACE) {
    100          
68 8         14 $left_brace_num++;
69             }
70             elsif ($token_type == RIGHT_BRACE) {
71 8         7 $left_brace_num--;
72 8 100       16 if ($left_brace_num <= 0) {
73 5         6 $is_in_ctrl_statement = 0;
74 5         11 $is_in_grep = 0;
75             }
76             }
77             }
78             elsif ($token_type == SEMI_COLON) {
79 20         23 $is_in_context = 0;
80 20         42 $is_in_grep = 0;
81             }
82             }
83              
84 6         32 return \@violations;
85             }
86              
87             1;
88