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