File Coverage

blib/lib/Perl/Critic/Policy/CodeLayout/ProhibitQuotedWordLists.pm
Criterion Covered Total %
statement 44 44 100.0
branch 23 24 95.8
condition 15 15 100.0
subroutine 12 12 100.0
pod 4 5 80.0
total 98 100 98.0


line stmt bran cond sub pod time code
1             package Perl::Critic::Policy::CodeLayout::ProhibitQuotedWordLists;
2              
3 40     40   27942 use 5.010001;
  40         200  
4 40     40   304 use strict;
  40         149  
  40         893  
5 40     40   226 use warnings;
  40         152  
  40         1004  
6              
7 40     40   264 use Readonly;
  40         130  
  40         2111  
8              
9 40     40   326 use Perl::Critic::Utils qw{ :characters :severities :classification};
  40         172  
  40         2053  
10 40     40   20320 use parent 'Perl::Critic::Policy';
  40         143  
  40         300  
11              
12             our $VERSION = '1.148';
13              
14             #-----------------------------------------------------------------------------
15              
16             Readonly::Scalar my $DESC => q{List of quoted literal words};
17             Readonly::Scalar my $EXPL => q{Use 'qw()' instead};
18              
19             #-----------------------------------------------------------------------------
20              
21             sub supported_parameters {
22             return (
23             {
24 99     99 0 2464 name => 'min_elements',
25             description => 'The minimum number of words in a list that will be complained about.',
26             default_string => '2',
27             behavior => 'integer',
28             integer_minimum => 1,
29             },
30             {
31             name => 'strict',
32             description => 'Complain even if there are non-word characters in the values.',
33             default_string => '0',
34             behavior => 'boolean',
35             },
36             );
37             }
38              
39 80     80 1 363 sub default_severity { return $SEVERITY_LOW }
40 84     84 1 398 sub default_themes { return qw( core cosmetic ) }
41 38     38 1 179 sub applies_to { return 'PPI::Structure::List' }
42              
43             #-----------------------------------------------------------------------------
44              
45             sub violates {
46 51     51 1 160 my ( $self, $elem, undef ) = @_;
47              
48             # Don't worry about subroutine calls
49 51         143 my $sibling = $elem->sprevious_sibling();
50 51 100       1445 return if not $sibling;
51              
52 50 100       215 return if $sibling->isa('PPI::Token::Symbol');
53 43 100 100     155 return if $sibling->isa('PPI::Token::Operator') and $sibling eq '->';
54 42 100 100     416 return if $sibling->isa('PPI::Token::Word') and not is_included_module_name($sibling);
55              
56             # Get the list elements
57 23         170 my $expr = $elem->schild(0);
58 23 100       304 return if not $expr;
59 21         55 my @children = $expr->schildren();
60 21 50       360 return if not @children;
61              
62 21         42 my $count = 0;
63 21         48 for my $child ( @children ) {
64 80 100 100     636 next if $child->isa('PPI::Token::Operator') && $child eq $COMMA;
65              
66             # All elements must be literal strings,
67             # and must contain 1 or more word characters.
68              
69 52 100       137 return if not _is_literal($child);
70              
71 45         133 my $string = $child->string();
72 45 100       334 return if $string =~ m{ \s }xms;
73 43 100       107 return if $string eq $EMPTY;
74 42 100 100     203 return if not $self->{_strict} and $string !~ m{\A [\w-]+ \z}xms;
75 40         90 $count++;
76             }
77              
78             # Were there enough?
79 9 100       35 return if $count < $self->{_min_elements};
80              
81             # If we get here, then all elements were literals
82 7         31 return $self->violation( $DESC, $EXPL, $elem );
83             }
84              
85             sub _is_literal {
86 52     52   84 my $elem = shift;
87 52   100     240 return $elem->isa('PPI::Token::Quote::Single')
88             || $elem->isa('PPI::Token::Quote::Literal');
89             }
90              
91             1;
92              
93             __END__
94              
95             =pod
96              
97             =head1 NAME
98              
99             Perl::Critic::Policy::CodeLayout::ProhibitQuotedWordLists - Write C<qw(foo bar baz)> instead of C<('foo', 'bar', 'baz')>.
100              
101              
102             =head1 AFFILIATION
103              
104             This Policy is part of the core L<Perl::Critic|Perl::Critic>
105             distribution.
106              
107              
108             =head1 DESCRIPTION
109              
110             Conway doesn't mention this, but I think C<qw()> is an underused
111             feature of Perl. Whenever you need to declare a list of one-word
112             literals, the C<qw()> operator is wonderfully concise, and makes it
113             easy to add to the list in the future.
114              
115             @list = ('foo', 'bar', 'baz'); #not ok
116             @list = qw(foo bar baz); #ok
117              
118             use Foo ('foo', 'bar', 'baz'); #not ok
119             use Foo qw(foo bar baz); #ok
120              
121             =head1 CONFIGURATION
122              
123             This policy can be configured to only pay attention to word lists with
124             at least a particular number of elements. By default, this value is
125             2, which means that lists containing zero or one elements are ignored.
126             The minimum list size to be looked at can be specified by giving a
127             value for C<min_elements> in F<.perlcriticrc> like this:
128              
129             [CodeLayout::ProhibitQuotedWordLists]
130             min_elements = 4
131              
132             This would cause this policy to only complain about lists containing
133             four or more words.
134              
135             By default, this policy won't complain if any of the values in the list
136             contain non-word characters. If you want it to, set the C<strict>
137             option to a true value.
138              
139             [CodeLayout::ProhibitQuotedWordLists]
140             strict = 1
141              
142              
143             =head1 NOTES
144              
145             In the PPI parlance, a "list" is almost anything with parentheses.
146             I've tried to make this Policy smart by targeting only "lists" that
147             could be sensibly expressed with C<qw()>. However, there may be some
148             edge cases that I haven't covered. If you find one, send me a note.
149              
150              
151             =head1 IMPORTANT CHANGES
152              
153             This policy was formerly called C<RequireQuotedWords> which seemed a
154             little counter-intuitive. If you get lots of "Cannot load policy
155             module" errors, then you probably need to change C<RequireQuotedWords>
156             to C<ProhibitQuotedWordLists> in your F<.perlcriticrc> file.
157              
158              
159             =head1 AUTHOR
160              
161             Jeffrey Ryan Thalhammer <jeff@imaginative-software.com>
162              
163              
164             =head1 COPYRIGHT
165              
166             Copyright (c) 2005-2011 Imaginative Software Systems. All rights reserved.
167              
168             This program is free software; you can redistribute it and/or modify
169             it under the same terms as Perl itself. The full text of this license
170             can be found in the LICENSE file included with this module.
171              
172             =cut
173              
174             # Local Variables:
175             # mode: cperl
176             # cperl-indent-level: 4
177             # fill-column: 78
178             # indent-tabs-mode: nil
179             # c-indentation-style: bsd
180             # End:
181             # ex: set ts=8 sts=4 sw=4 tw=78 ft=perl expandtab shiftround :