File Coverage

blib/lib/Perl/Critic/Policy/CodeLayout/ProhibitQuotedWordLists.pm
Criterion Covered Total %
statement 27 44 61.3
branch 5 24 20.8
condition 2 15 13.3
subroutine 11 12 91.6
pod 4 5 80.0
total 49 100 49.0


line stmt bran cond sub pod time code
1             package Perl::Critic::Policy::CodeLayout::ProhibitQuotedWordLists;
2              
3 40     40   28070 use 5.010001;
  40         227  
4 40     40   294 use strict;
  40         134  
  40         866  
5 40     40   238 use warnings;
  40         125  
  40         946  
6              
7 40     40   243 use Readonly;
  40         138  
  40         2173  
8              
9 40     40   318 use Perl::Critic::Utils qw{ :characters :severities :classification};
  40         129  
  40         2143  
10 40     40   19888 use parent 'Perl::Critic::Policy';
  40         141  
  40         282  
11              
12             our $VERSION = '1.150';
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 91     91 0 2427 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 73     73 1 325 sub default_severity { return $SEVERITY_LOW }
40 84     84 1 415 sub default_themes { return qw( core cosmetic ) }
41 30     30 1 88 sub applies_to { return 'PPI::Structure::List' }
42              
43             #-----------------------------------------------------------------------------
44              
45             sub violates {
46 21     21 1 49 my ( $self, $elem, undef ) = @_;
47              
48             # Don't worry about subroutine calls
49 21         55 my $sibling = $elem->sprevious_sibling();
50 21 50       454 return if not $sibling;
51              
52 21 100       97 return if $sibling->isa('PPI::Token::Symbol');
53 15 50 33     37 return if $sibling->isa('PPI::Token::Operator') and $sibling eq '->';
54 15 50 33     58 return if $sibling->isa('PPI::Token::Word') and not is_included_module_name($sibling);
55              
56             # Get the list elements
57 0           my $expr = $elem->schild(0);
58 0 0         return if not $expr;
59 0           my @children = $expr->schildren();
60 0 0         return if not @children;
61              
62 0           my $count = 0;
63 0           for my $child ( @children ) {
64 0 0 0       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 0 0         return if not _is_literal($child);
70              
71 0           my $string = $child->string();
72 0 0         return if $string =~ m{ \s }xms;
73 0 0         return if $string eq $EMPTY;
74 0 0 0       return if not $self->{_strict} and $string !~ m{\A [\w-]+ \z}xms;
75 0           $count++;
76             }
77              
78             # Were there enough?
79 0 0         return if $count < $self->{_min_elements};
80              
81             # If we get here, then all elements were literals
82 0           return $self->violation( $DESC, $EXPL, $elem );
83             }
84              
85             sub _is_literal {
86 0     0     my $elem = shift;
87 0   0       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 :