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   72077 use strict;
  134         184  
  134         3088  
3 134     134   436 use warnings;
  134         207  
  134         2390  
4 134     134   763 use Perl::Lint::Constants::Type;
  134         262  
  134         59563  
5 134     134   552 use parent "Perl::Lint::Policy";
  134         163  
  134         550  
6              
7             # TODO TODO TODO integrate duplicated functions between assign context and use context !!!!
8              
9             use constant {
10 134         60104 DESC => 'List of quoted literal words',
11             EXPL => 'Use "qw()" instead',
12 134     134   6451 };
  134         166  
13              
14             sub evaluate {
15 11     11 0 27 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     50 my $strict = $args->{prohibit_quoted_word_lists}->{strict} || 0;
19              
20 11         17 my @violations;
21 11         42 for (my $i = 0; my $token = $tokens->[$i]; $i++) {
22 151         108 my $token_type = $token->{type};
23              
24 151 100 66     717 if (
    100 100        
25             $token_type == ARRAY_VAR ||
26             $token_type == LOCAL_ARRAY_VAR ||
27             $token_type == GLOBAL_ARRAY_VAR
28             ) {
29 22         32 $token = $tokens->[++$i];
30 22         18 $token_type = $token->{type};
31 22 50       36 if ($token_type == ASSIGN) {
32 22         26 $token = $tokens->[++$i];
33 22         20 $token_type = $token->{type};
34 22 50       31 if ($token_type == LEFT_PAREN) {
35 22         15 my $left_paren_num = 1;
36 22         18 my $is_violated = 1;
37 22         11 my $elem_num = 0;
38              
39 22         40 for ($i++; $token = $tokens->[$i]; $i++) {
40 131         78 $token_type = $token->{type};
41 131 50 66     408 if ($token_type == LEFT_PAREN) {
    100          
    100          
    100          
42 0         0 $left_paren_num++;
43             }
44             elsif ($token_type == RIGHT_PAREN) {
45 22 50       38 if (--$left_paren_num <= 0) {
46 22 100 100     59 if ($is_violated && $elem_num >= $min_elements) {
47             push @violations, {
48             filename => $file,
49             line => $token->{line},
50 10         36 description => DESC,
51             explanation => EXPL,
52             policy => __PACKAGE__
53             };
54             }
55 22         43 last;
56             }
57             }
58             elsif ($token_type == STRING || $token_type == RAW_STRING) {
59 57         34 $elem_num++;
60              
61 57 100       73 next if $strict;
62              
63 51 100       144 if ($token->{data} !~ /\A[a-zA-Z-]+\Z/) {
64 7         65 $is_violated = 0;
65             }
66             }
67             elsif ($token_type != COMMA) {
68 13         11 $elem_num++;
69 13         16 $is_violated = 0;
70             }
71             }
72             }
73             }
74             }
75             elsif ($token_type == USE_DECL) {
76 13         30 USE_STATEMENT: for ($i++; $token = $tokens->[$i]; $i++) {
77 34         20 $token_type = $token->{type};
78              
79 34 100       73 if ($token_type == LEFT_PAREN) {
    100          
80 5         7 my $left_paren_num = 1;
81 5         3 my $is_violated = 1;
82 5         7 my $elem_num = 0;
83              
84 5         11 for ($i++; $token = $tokens->[$i]; $i++) {
85 29         21 $token_type = $token->{type};
86 29 50 66     101 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     16 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         17 last USE_STATEMENT;
101             }
102             }
103             elsif ($token_type == STRING || $token_type == RAW_STRING) {
104 13         11 $elem_num++;
105              
106 13 100       20 next if $strict;
107              
108 10 100       29 if ($token->{data} !~ /\A[a-zA-Z-]+\Z/) {
109 1         4 $is_violated = 0;
110             }
111             }
112             elsif ($token_type != COMMA) {
113 3         3 $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         43 return \@violations;
126             }
127              
128             1;
129