blib/lib/Perl/Critic/Policy/CodeLayout/ProhibitQuotedWordLists.pm

filter: Statementstmt 64.5% 31 / 48 Branchbran 20.8% 5 / 24 Conditioncond 13.3% 2 / 15 MC/DCmcdc 0.0% 0 / 10 Subroutinesub 91.6% 11 / 12 Podpod 80.0% 4 / 5 totaltotal 46.4% 53 / 114 CC 19 SCAR 46.1
CC
19
Cov
44%
violates
167.0
_is_literal
6.0
BEGIN
1.0
CRAP 101.0
« blib/lib/Perl/Critic/TestUtils.pm ↑ Summary blib/lib/Perl/Critic/Policy/ControlStructures/ProhibitMutatingListFunctions.pm »
1 package Perl::Critic::Policy::CodeLayout::ProhibitQuotedWordLists;
2  
3 40 ▶use 5.010001;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 40 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 40 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 40 ▶use Readonly;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 40 ▶use Perl::Critic::Utils qw{ :characters :severities :classification};
Subroutine BEGIN: called CC=1 SCAR=0.0
10 40 ▶use parent 'Perl::Critic::Policy';
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 40 our $VERSION = '1.150';
13  
14 #-----------------------------------------------------------------------------
15  
16 40 Readonly::Scalar my $DESC => q{List of quoted literal words};
17 40 Readonly::Scalar my $EXPL => q{Use 'qw()' instead};
18  
19 #-----------------------------------------------------------------------------
20  
21 sub supported_parameters {
22 return (
23 {
24 92 ▶ name => 'min_elements',
Subroutine supported_parameters: called CC=1 SCAR=0.0
Pod undocumented
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 74 ▶sub default_severity { return $SEVERITY_LOW }
Subroutine default_severity: called CC=1 SCAR=0.0
Pod documented
40 84 ▶sub default_themes { return qw( core cosmetic ) }
Subroutine default_themes: called CC=1 SCAR=0.0
Pod documented
41 31 ▶sub applies_to { return 'PPI::Structure::List' }
Subroutine applies_to: called CC=1 SCAR=0.0
Pod documented
42  
43 #-----------------------------------------------------------------------------
44  
45 sub violates {
46 22 ▶ my ( $self, $elem, undef ) = @_;
Subroutine violates: called CC=18 SCAR=51.2
Pod documented
47  
48 # Don't worry about subroutine calls
49 22 my $sibling = $elem->sprevious_sibling();
50 22 ▶ return if not $sibling;
Branch: 1 branch
BranchTrueFalseTotal
unless $sibling 0 22 22
51  
52 22 ▶ return if $sibling->isa('PPI::Token::Symbol');
Branch: 1 branch
BranchTrueFalseTotal
if $sibling->isa("PPI::Token::Symbol") 6 16 22
53 16 ▶ return if $sibling->isa('PPI::Token::Operator') and $sibling eq '->';
Branch: 1 branch
BranchTrueFalseTotal
if $sibling->isa("PPI::Token::Operator") and $sibling eq "->" 0 16 16
Condition33%1/3
$sibling->isa("PPI::Token::Operator") and $sibling eq "->"
!ll&&!rl&&r
1600
MC/DC0%0/2
$sibling->isa("PPI::Token::Operator") and $sibling eq "->"
$sibling->isa("PPI::Token::Operator") $sibling eq "->"
Truth table33%1/3
A and B
A= $sibling->isa("PPI::Token::Operator")B= $sibling eq "->"
ABresult
0X0
100
111
54 16 ▶ return if $sibling->isa('PPI::Token::Word') and not is_included_module_name($sibling);
Branch: 1 branch
BranchTrueFalseTotal
if $sibling->isa("PPI::Token::Word") and not is_included_module_name($sibling) 16 0 16
Condition33%1/3
$sibling->isa("PPI::Token::Word") and not is_included_module_name($sibling)
!ll&&!rl&&r
0016
MC/DC0%0/2
$sibling->isa("PPI::Token::Word") and not is_included_module_name($sibling)
$sibling->isa("PPI::Token::Word") not is_included_module_name($sibling)
Truth table33%1/3
A and not(B)
A= $sibling->isa("PPI::Token::Word")B= not is_included_module_name($sibling)
ABresult
0X0
100
111
55  
56 # Get the list elements
57 0 my $expr = $elem->schild(0);
58 0 ▶ return if not $expr;
Branch: 1 branch
BranchTrueFalseTotal
unless $expr 0 0 0
59 0 my @children = $expr->schildren();
60 0 ▶ return if not @children;
Branch: 1 branch
BranchTrueFalseTotal
unless @children 0 0 0
61  
62 0 my $count = 0;
63 0 for my $child ( @children ) {
64 0 ▶ next if $child->isa('PPI::Token::Operator') && $child eq $COMMA;
Branch: 1 branch
BranchTrueFalseTotal
if $child->isa("PPI::Token::Operator") and $child eq $Perl::Critic::Policy::CodeLayout::ProhibitQuotedWordLists::COMMA 0 0 0
Condition0%0/3
$child->isa("PPI::Token::Operator") and $child eq $Perl::Critic::Policy::CodeLayout::ProhibitQuotedWordLists::COMMA
!ll&&!rl&&r
000
MC/DC0%0/2
$child->isa("PPI::Token::Operator") and $child eq $Perl::Critic::Policy::CodeLayout::ProhibitQuotedWordLists::COMMA
$child->isa("PPI::Token::Operator") $child eq $Perl::Critic::Policy::CodeLayout::ProhibitQuotedWordLists::COMMA
Truth table0%0/3
A and B
A= $child->isa("PPI::Token::Operator")B= $child eq $Perl::Critic::Policy::CodeLayout::ProhibitQuotedWordLists::COMMA
ABresult
0X0
100
111
65  
66 # All elements must be literal strings,
67 # and must contain 1 or more word characters.
68  
69 0 ▶ return if not _is_literal($child);
Branch: 1 branch
BranchTrueFalseTotal
unless _is_literal($child) 0 0 0
70  
71 0 my $string = $child->string();
72 0 ▶ return if $string =~ m{ \s }xms;
Branch: 1 branch
BranchTrueFalseTotal
if $string =~ / \s /msx 0 0 0
73 0 ▶ return if $string eq $EMPTY;
Branch: 1 branch
BranchTrueFalseTotal
if $string eq $Perl::Critic::Policy::CodeLayout::ProhibitQuotedWordLists::EMPTY 0 0 0
74 0 ▶ return if not $self->{_strict} and $string !~ m{\A [\w-]+ \z}xms;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"_strict"} or $string =~ /\A [\w-]+ \z/msx 0 0 0
Condition0%0/3
$self->{"_strict"} or $string =~ /\A [\w-]+ \z/msx
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{"_strict"} or $string =~ /\A [\w-]+ \z/msx
$self->{"_strict"} $string =~ /\A [\w-]+ \z/msx
Truth table0%0/3
A or B
A= $self->{"_strict"}B= $string =~ /\A [\w-]+ \z/msx
ABresult
1X1
011
000
75 0 $count++;
76 }
77  
78 # Were there enough?
79 0 ▶ return if $count < $self->{_min_elements};
Branch: 1 branch
BranchTrueFalseTotal
if $count < $self->{"_min_elements"} 0 0 0
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 ▶ my $elem = shift;
Subroutine _is_literal: not called CC=2 SCAR=17.9
87 0 ▶ return $elem->isa('PPI::Token::Quote::Single')
Condition0%0/3
$elem->isa('PPI::Token::Quote::Single') || $elem->isa('PPI::Token::Quote::Literal')
l!l&&r!l&&!r
000
MC/DC0%0/2
$elem->isa('PPI::Token::Quote::Single') || $elem->isa('PPI::Token::Quote::Literal')
$elem->isa('PPI::Token::Quote::Single') $elem->isa('PPI::Token::Quote::Literal')
Truth table0%0/3
A || B
A= $elem->isa('PPI::Token::Quote::Single')B= $elem->isa('PPI::Token::Quote::Literal')
ABresult
1X1
011
000
88 || $elem->isa('PPI::Token::Quote::Literal');
89 }
90  
91 40 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 :
« blib/lib/Perl/Critic/TestUtils.pm ↑ Summary blib/lib/Perl/Critic/Policy/ControlStructures/ProhibitMutatingListFunctions.pm »