File Coverage

blib/lib/Perl/Lint/Policy/CodeLayout/ProhibitQuotedWordLists.pm
Criterion Covered Total %
statement 65 67 97.0
branch 38 44 86.3
condition 19 22 86.3
subroutine 6 6 100.0
pod 0 1 0.0
total 128 140 91.4


line stmt bran cond sub pod time code
1             package Perl::Lint::Policy::CodeLayout::ProhibitQuotedWordLists;
2 134     134   72017 use strict;
  134         165  
  134         3192  
3 134     134   405 use warnings;
  134         214  
  134         2436  
4 134     134   1005 use Perl::Lint::Constants::Type;
  134         261  
  134         60190  
5 134     134   564 use parent "Perl::Lint::Policy";
  134         156  
  134         577  
6              
7             # TODO TODO TODO integrate duplicated functions between assign context and use context !!!!
8              
9             use constant {
10 134         60324 DESC => 'List of quoted literal words',
11             EXPL => 'Use "qw()" instead',
12 134     134   6971 };
  134         197  
13              
14             sub evaluate {
15 11     11 0 26 my ($class, $file, $tokens, $src, $args) = @_;
16              
17 11   100     60 my $min_elements = $args->{prohibit_quoted_word_lists}->{min_elements} || 2;
18 11   100     49 my $strict = $args->{prohibit_quoted_word_lists}->{strict} || 0;
19              
20 11         21 my @violations;
21 11         46 for (my $i = 0; my $token = $tokens->[$i]; $i++) {
22 151         108 my $token_type = $token->{type};
23              
24 151 100 66     673 if (
    100 100        
25             $token_type == ARRAY_VAR ||
26             $token_type == LOCAL_ARRAY_VAR ||
27             $token_type == GLOBAL_ARRAY_VAR
28             ) {
29 22         24 $token = $tokens->[++$i];
30 22         24 $token_type = $token->{type};
31 22 50       39 if ($token_type == ASSIGN) {
32 22         23 $token = $tokens->[++$i];
33 22         22 $token_type = $token->{type};
34 22 50       40 if ($token_type == LEFT_PAREN) {
35 22         17 my $left_paren_num = 1;
36 22         21 my $is_violated = 1;
37 22         19 my $elem_num = 0;
38              
39 22         46 for ($i++; $token = $tokens->[$i]; $i++) {
40 131         89 $token_type = $token->{type};
41 131 50 66     416 if ($token_type == LEFT_PAREN) {
    100          
    100          
    100          
42 0         0 $left_paren_num++;
43             }
44             elsif ($token_type == RIGHT_PAREN) {
45 22 50       39 if (--$left_paren_num <= 0) {
46 22 100 100     57 if ($is_violated && $elem_num >= $min_elements) {
47             push @violations, {
48             filename => $file,
49             line => $token->{line},
50 10         39 description => DESC,
51             explanation => EXPL,
52             policy => __PACKAGE__
53             };
54             }
55 22         51 last;
56             }
57             }
58             elsif ($token_type == STRING || $token_type == RAW_STRING) {
59 57         35 $elem_num++;
60              
61 57 100       72 next if $strict;
62              
63 51 100       150 if ($token->{data} !~ /\A[a-zA-Z-]+\Z/) {
64 7         12 $is_violated = 0;
65             }
66             }
67             elsif ($token_type != COMMA) {
68 13         9 $elem_num++;
69 13         17 $is_violated = 0;
70             }
71             }
72             }
73             }
74             }
75             elsif ($token_type == USE_DECL) {
76 13         24 USE_STATEMENT: for ($i++; $token = $tokens->[$i]; $i++) {
77 34         29 $token_type = $token->{type};
78              
79 34 100       79 if ($token_type == LEFT_PAREN) {
    100          
80 5         7 my $left_paren_num = 1;
81 5         5 my $is_violated = 1;
82 5         7 my $elem_num = 0;
83              
84 5         15 for ($i++; $token = $tokens->[$i]; $i++) {
85 29         21 $token_type = $token->{type};
86 29 50 66     106 if ($token_type == LEFT_PAREN) {
    100          
    100          
    100          
87 0         0 $left_paren_num++;
88             }
89             elsif ($token_type == RIGHT_PAREN) {
90 5 50       9 if (--$left_paren_num <= 0) {
91 5 100 100     21 if ($is_violated && $elem_num >= $min_elements) {
92             push @violations, {
93             filename => $file,
94             line => $token->{line},
95 2         12 description => DESC,
96             explanation => EXPL,
97             policy => __PACKAGE__,
98             };
99             }
100 5         19 last USE_STATEMENT;
101             }
102             }
103             elsif ($token_type == STRING || $token_type == RAW_STRING) {
104 13         8 $elem_num++;
105              
106 13 100       19 next if $strict;
107              
108 10 100       33 if ($token->{data} !~ /\A[a-zA-Z-]+\Z/) {
109 1         2 $is_violated = 0;
110             }
111             }
112             elsif ($token_type != COMMA) {
113 3         4 $elem_num++;
114 3         5 $is_violated = 0;
115             }
116             }
117             }
118             elsif ($token_type == SEMI_COLON) {
119 8         12 last;
120             }
121             }
122             }
123             }
124              
125 11         46 return \@violations;
126             }
127              
128             1;
129