blib/lib/Perl/Critic/Policy/ValuesAndExpressions/ProhibitQuotesAsQuotelikeOperatorDelimiters.pm

filter: Statementstmt 79.6% 47 / 59 Branchbran 30.0% 3 / 10 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 85.0% 17 / 20 Podpod 83.3% 5 / 6 totaltotal 75.7% 72 / 95 CC 6 SCAR 8.4
CC
6
Cov
72%
_info_for_transliterate
6.0
violates
5.6
_info_for_match
2.1
CRAP 2.3
« blib/lib/Perl/Critic/Utils/McCabe.pm ↑ Summary blib/lib/Perl/Critic/Policy.pm »
1 package Perl::Critic::Policy::ValuesAndExpressions::ProhibitQuotesAsQuotelikeOperatorDelimiters;
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 40 ▶use Readonly;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 40 use Perl::Critic::Utils qw{
9 :booleans :characters :severities :data_conversion
10 40 ▶};
Subroutine BEGIN: called CC=1 SCAR=0.0
11 40 ▶use parent 'Perl::Critic::Policy';
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 40 our $VERSION = '1.150';
14  
15 #-----------------------------------------------------------------------------
16  
17 40 Readonly::Hash my %DESCRIPTIONS => (
18 $QUOTE => q{Single-quote used as quote-like operator delimiter},
19 $DQUOTE => q{Double-quote used as quote-like operator delimiter},
20 $BACKTICK => q{Back-quote (back-tick) used as quote-like operator delimiter},
21 );
22  
23 40 Readonly::Scalar my $EXPL =>
24 q{Using quotes as delimiters for quote-like operators obfuscates code};
25  
26 40 Readonly::Array my @OPERATORS => qw{ m q qq qr qw qx s tr y };
27  
28 40 Readonly::Hash my %INFO_RETRIEVERS_BY_PPI_CLASS => (
29 'PPI::Token::Quote::Literal' => \&_info_for_single_character_operator,
30 'PPI::Token::Quote::Interpolate' => \&_info_for_two_character_operator,
31 'PPI::Token::QuoteLike::Command' => \&_info_for_two_character_operator,
32 'PPI::Token::QuoteLike::Regexp' => \&_info_for_two_character_operator,
33 'PPI::Token::QuoteLike::Words' => \&_info_for_two_character_operator,
34 'PPI::Token::Regexp::Match' => \&_info_for_match,
35 'PPI::Token::Regexp::Substitute' => \&_info_for_single_character_operator,
36 'PPI::Token::Regexp::Transliterate' => \&_info_for_transliterate,
37 );
38  
39 #-----------------------------------------------------------------------------
40  
41 sub supported_parameters {
42 return (
43 {
44 93 ▶ name => 'single_quote_allowed_operators',
Subroutine supported_parameters: called CC=1 SCAR=0.0
Pod undocumented
45 description =>
46 'The operators to allow single-quotes as delimiters for.',
47 default_string => 'm s qr qx',
48 behavior => 'enumeration',
49 enumeration_values => [ @OPERATORS ],
50 enumeration_allow_multiple_values => 1,
51 },
52 {
53 name => 'double_quote_allowed_operators',
54 description =>
55 'The operators to allow double-quotes as delimiters for.',
56 default_string => $EMPTY,
57 behavior => 'enumeration',
58 enumeration_values => [ @OPERATORS ],
59 enumeration_allow_multiple_values => 1,
60 },
61 {
62 name => 'back_quote_allowed_operators',
63 description =>
64 'The operators to allow back-quotes (back-ticks) as delimiters for.',
65 default_string => $EMPTY,
66 behavior => 'enumeration',
67 enumeration_values => [ @OPERATORS ],
68 enumeration_allow_multiple_values => 1,
69 },
70 );
71 }
72  
73 75 ▶sub default_severity { return $SEVERITY_MEDIUM }
Subroutine default_severity: called CC=1 SCAR=0.0
Pod documented
74 74 ▶sub default_themes { return qw( core maintenance ) }
Subroutine default_themes: called CC=1 SCAR=0.0
Pod documented
75  
76 sub applies_to {
77 31 ▶ return qw{
Subroutine applies_to: called CC=1 SCAR=0.0
Pod documented
78 PPI::Token::Quote::Interpolate
79 PPI::Token::Quote::Literal
80 PPI::Token::QuoteLike::Command
81 PPI::Token::QuoteLike::Regexp
82 PPI::Token::QuoteLike::Words
83 PPI::Token::Regexp::Match
84 PPI::Token::Regexp::Substitute
85 PPI::Token::Regexp::Transliterate
86 };
87 }
88  
89 #-----------------------------------------------------------------------------
90  
91 sub initialize_if_enabled {
92 55 ▶ my ($self, $config) = @_;
Subroutine initialize_if_enabled: called CC=1 SCAR=0.0
Pod documented
93  
94 $self->{_allowed_operators_by_delimiter} = {
95 55 $QUOTE => $self->_single_quote_allowed_operators(),
96 $DQUOTE => $self->_double_quote_allowed_operators(),
97 $BACKTICK => $self->_back_quote_allowed_operators(),
98 };
99  
100 55 return $TRUE;
101 }
102  
103 #-----------------------------------------------------------------------------
104  
105 sub _single_quote_allowed_operators {
106 55 ▶ my ( $self ) = @_;
Subroutine _single_quote_allowed_operators: called CC=1 SCAR=0.0
107  
108 55 return $self->{_single_quote_allowed_operators};
109 }
110  
111 sub _double_quote_allowed_operators {
112 55 ▶ my ( $self ) = @_;
Subroutine _double_quote_allowed_operators: called CC=1 SCAR=0.0
113  
114 55 return $self->{_double_quote_allowed_operators};
115 }
116  
117 sub _back_quote_allowed_operators {
118 55 ▶ my ( $self ) = @_;
Subroutine _back_quote_allowed_operators: called CC=1 SCAR=0.0
119  
120 55 return $self->{_back_quote_allowed_operators};
121 }
122  
123 sub _allowed_operators_by_delimiter {
124 1 ▶ my ( $self ) = @_;
Subroutine _allowed_operators_by_delimiter: called CC=1 SCAR=0.0
125  
126 1 return $self->{_allowed_operators_by_delimiter};
127 }
128  
129 #-----------------------------------------------------------------------------
130  
131 sub violates {
132 1 ▶ my ( $self, $elem, undef ) = @_;
Subroutine violates: called CC=4 SCAR=17.3
Pod documented
133  
134 1 my $info_retriever = $INFO_RETRIEVERS_BY_PPI_CLASS{ ref $elem };
135 1 ▶ return if not $info_retriever;
Branch: 1 branch
BranchTrueFalseTotal
unless $info_retriever 0 1 1
136  
137 1 my ($operator, $delimiter) = $info_retriever->( $elem );
138  
139 my $allowed_operators =
140 1 $self->_allowed_operators_by_delimiter()->{$delimiter};
141 1 ▶ return if not $allowed_operators;
Branch: 1 branch
BranchTrueFalseTotal
unless $allowed_operators 1 0 1
142  
143 0 ▶ if ( not $allowed_operators->{$operator} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($allowed_operators->{$operator}) 0 0 0
144 0 return $self->violation( $DESCRIPTIONS{$delimiter}, $EXPL, $elem );
145 }
146  
147 0 return;
148 }
149  
150 #-----------------------------------------------------------------------------
151  
152 sub _info_for_single_character_operator {
153 0 ▶ my ( $elem ) = @_;
Subroutine _info_for_single_character_operator: not called CC=1 SCAR=6.9
154  
155 ## no critic (ProhibitParensWithBuiltins)
156 0 return ( substr ($elem, 0, 1), substr ($elem, 1, 1) );
157 ## use critic
158 }
159  
160 #-----------------------------------------------------------------------------
161  
162 sub _info_for_two_character_operator {
163 0 ▶ my ( $elem ) = @_;
Subroutine _info_for_two_character_operator: not called CC=1 SCAR=6.9
164  
165 ## no critic (ProhibitParensWithBuiltins)
166 0 return ( substr ($elem, 0, 2), substr ($elem, 2, 1) );
167 ## use critic
168 }
169  
170 #-----------------------------------------------------------------------------
171  
172 sub _info_for_match {
173 1 ▶ my ( $elem ) = @_;
Subroutine _info_for_match: called CC=2 SCAR=7.6
174  
175 1 ▶ if ( $elem =~ m/ ^ m /xms ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($elem =~ / ^ m /msx) 0 1 1
176 0 return ('m', substr $elem, 1, 1);
177 }
178  
179 1 return ('m', q{/});
180 }
181  
182 #-----------------------------------------------------------------------------
183  
184 sub _info_for_transliterate {
185 0 ▶ my ( $elem ) = @_;
Subroutine _info_for_transliterate: not called CC=2 SCAR=17.9
186  
187 0 ▶ if ( $elem =~ m/ ^ tr /xms ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($elem =~ / ^ tr /msx) 0 0 0
188 0 return ('tr', substr $elem, 2, 1);
189 }
190  
191 0 return ('y', substr $elem, 1, 1);
192 }
193  
194  
195 40 1;
196  
197 __END__
198  
199 #-----------------------------------------------------------------------------
200  
201 =pod
202  
203 =for stopwords Schwern
204  
205 =head1 NAME
206  
207 Perl::Critic::Policy::ValuesAndExpressions::ProhibitQuotesAsQuotelikeOperatorDelimiters - Don't use quotes (C<'>, C<">, C<`>) as delimiters for the quote-like operators.
208  
209  
210 =head1 AFFILIATION
211  
212 This Policy is part of the core L<Perl::Critic|Perl::Critic>
213 distribution.
214  
215  
216 =head1 DESCRIPTION
217  
218 With the obvious exception of using single-quotes to prevent
219 interpolation, using quotes with the quote-like operators kind of
220 defeats the purpose of them and produces obfuscated code, causing
221 problems for future maintainers and their editors/IDEs.
222  
223 $x = q"q"; #not ok
224 $x = q'q'; #not ok
225 $x = q`q`; #not ok
226  
227 $x = qq"q"; #not ok
228 $x = qr"r"; #not ok
229 $x = qw"w"; #not ok
230  
231 $x = qx`date`; #not ok
232  
233 $x =~ m"m"; #not ok
234 $x =~ s"s"x"; #not ok
235 $x =~ tr"t"r"; #not ok
236 $x =~ y"x"y"; #not ok
237  
238 $x =~ m'$x'; #ok
239 $x =~ s'$x'y'; #ok
240 $x = qr'$x'm; #ok
241 $x = qx'finger foo@bar'; #ok
242  
243  
244 =head1 CONFIGURATION
245  
246 This policy has three options: C<single_quote_allowed_operators>,
247 C<double_quote_allowed_operators>, and
248 C<back_quote_allowed_operators>, which control which operators are
249 allowed to use each of C<'>, C<">, C<`> as delimiters, respectively.
250  
251 The values allowed for these options are a whitespace delimited
252 selection of the C<m>, C<q>, C<qq>, C<qr>, C<qw>, C<qx>, C<s>, C<tr>,
253 and C<y> operators.
254  
255 By default, double quotes and back quotes (backticks) are not allowed
256 as delimiters for any operators and single quotes are allowed as
257 delimiters for the C<m>, C<qr>, C<qx>, and C<s> operators. These
258 defaults are equivalent to having the following in your
259 F<.perlcriticrc>:
260  
261 [ValuesAndExpressions::ProhibitQuotesAsQuotelikeOperatorDelimiters]
262 single_quote_allowed_operators = m s qr qx
263 double_quote_allowed_operators =
264 back_quote_allowed_operators =
265  
266  
267 =head1 SUGGESTED BY
268  
269 Michael Schwern
270  
271  
272 =head1 AUTHOR
273  
274 Elliot Shank C<< <perl@galumph.com> >>
275  
276  
277 =head1 COPYRIGHT
278  
279 Copyright (c) 2007-2011 Elliot Shank.
280  
281 This program is free software; you can redistribute it and/or modify
282 it under the same terms as Perl itself. The full text of this license
283 can be found in the LICENSE file included with this module.
284  
285 =cut
286  
287 # Local Variables:
288 # mode: cperl
289 # cperl-indent-level: 4
290 # fill-column: 78
291 # indent-tabs-mode: nil
292 # c-indentation-style: bsd
293 # End:
294 # ex: set ts=8 sts=4 sw=4 tw=78 ft=perl expandtab shiftround :
« blib/lib/Perl/Critic/Utils/McCabe.pm ↑ Summary blib/lib/Perl/Critic/Policy.pm »