File Coverage

blib/lib/Perl/Lint/Policy/BuiltinFunctions/ProhibitVoidMap.pm
Criterion Covered Total %
statement 48 48 100.0
branch 28 28 100.0
condition 12 12 100.0
subroutine 6 6 100.0
pod 0 1 0.0
total 94 95 98.9


line stmt bran cond sub pod time code
1             package Perl::Lint::Policy::BuiltinFunctions::ProhibitVoidMap;
2 133     133   69323 use strict;
  133         192  
  133         3131  
3 133     133   446 use warnings;
  133         164  
  133         2428  
4 133     133   820 use Perl::Lint::Constants::Type;
  133         158  
  133         58719  
5 133     133   591 use parent "Perl::Lint::Policy";
  133         186  
  133         553  
6              
7             # TODO REFACTOR!!!
8              
9             use constant {
10 133         35961 DESC => '"map" used in void context',
11             EXPL => 'Use a "for" loop instead',
12 133     133   6477 };
  133         179  
13              
14             sub evaluate {
15 6     6 0 9 my ($class, $file, $tokens, $args) = @_;
16              
17 6         5 my @violations;
18 6         7 my $is_in_context = 0;
19 6         2 my $is_before_comma = 0; # XXX to decide is in context or not
20 6         6 my $is_in_map = 0;
21 6         7 my $is_in_ctrl_statement = 0;
22 6         4 my $left_brace_num = 0;
23 6         17 for (my $i = 0; my $token = $tokens->[$i]; $i++) {
24 352         221 my $token_type = $token->{type};
25 352         230 my $token_data = $token->{data};
26 352 100 100     1765 if ($token_type == BUILTIN_FUNC) {
    100 100        
    100 100        
    100          
    100          
    100          
27 48 100       62 if ($token_data eq 'map') {
28 37 100       51 next if $is_in_map;
29              
30 25 100 100     55 if ($is_in_ctrl_statement) {
    100          
31 6 100       11 if ($left_brace_num) {
32             push @violations, {
33             filename => $file,
34             line => $token->{line},
35 4         11 description => DESC,
36             explanation => EXPL,
37             policy => __PACKAGE__,
38             };
39             }
40             }
41             elsif (!$is_in_context && !$is_before_comma) {
42             push @violations, {
43             filename => $file,
44             line => $token->{line},
45 10         30 description => DESC,
46             explanation => EXPL,
47             policy => __PACKAGE__,
48             };
49             }
50              
51 25         21 $is_in_map = 1;
52 25         37 next;
53             }
54 11         18 $is_in_context = 1;
55             }
56             elsif ($token_type == ASSIGN) {
57 3         4 $is_in_context = 1;
58             }
59             elsif ( # NOTE enough?
60             $token_type == IF_STATEMENT ||
61             $token_type == FOR_STATEMENT ||
62             $token_type == WHILE_STATEMENT ||
63             $token_type == UNLESS_STATEMENT
64             ) {
65 6         10 $is_in_ctrl_statement = 1;
66             }
67             elsif ($is_in_ctrl_statement) {
68 62 100       114 if ($token_type == LEFT_BRACE) {
    100          
69 10         15 $left_brace_num++;
70             }
71             elsif ($token_type == RIGHT_BRACE) {
72 10         5 $left_brace_num--;
73 10 100       18 if ($left_brace_num <= 0) {
74 6         5 $is_in_ctrl_statement = 0;
75 6         9 $is_in_map = 0;
76             }
77             }
78             }
79             elsif ($token_type == SEMI_COLON) {
80 21         13 $is_in_context = 0;
81 21         28 $is_in_map = 0;
82             }
83             elsif ($token_type == COMMA) {
84 11         14 $is_before_comma = 1;
85             }
86             else {
87 201         258 $is_before_comma = 0;
88             }
89             }
90              
91 6         20 return \@violations;
92             }
93              
94             1;
95