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 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369
|
# Copyright 2011, 2012, 2013, 2014, 2015, 2016, 2017 Kevin Ryde
# This file is part of Perl-Critic-Pulp.
# Perl-Critic-Pulp is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by the
# Free Software Foundation; either version 3, or (at your option) any later
# version.
#
# Perl-Critic-Pulp is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
# or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
# for more details.
#
# You should have received a copy of the GNU General Public License along
# with Perl-Critic-Pulp. If not, see <http://www.gnu.org/licenses/>.
package Perl::Critic::Policy::ValuesAndExpressions::ProhibitDuplicateHashKeys;
use 5.006;
use strict;
use warnings;
use base 'Perl::Critic::Policy';
use Perl::Critic::Utils;
use Perl::Critic::Policy::CodeLayout::RequireFinalSemicolon;
use Perl::Critic::Policy::Miscellanea::TextDomainPlaceholders;
use Perl::Critic::Policy::ValuesAndExpressions::ConstantBeforeLt;
use Perl::Critic::Pulp::Utils 'elem_is_comma_operator';
# uncomment this to run the ### lines
#use Smart::Comments;
our $VERSION = 96;
use constant supported_parameters => ();
use constant default_severity => $Perl::Critic::Utils::SEVERITY_MEDIUM;
use constant default_themes => qw(pulp bugs);
use constant applies_to => ('PPI::Structure::Constructor',
'PPI::Structure::List',
# this policy is not for blocks, but PPI
# mis-reports some anonymous hashref
# constructors as blocks, so look at them
'PPI::Structure::Block');
sub violates {
my ($self, $elem, $document) = @_;
### ProhibitDuplicateHashKeys violates() ...
### consider: (ref $elem)." $elem"
if ($elem->isa('PPI::Structure::Constructor')) {
### constructor ...
unless ($elem->start eq '{') {
### constructor is not a hash ...
return;
}
} elsif ($elem->isa('PPI::Structure::Block')) {
### block ...
if (Perl::Critic::Policy::CodeLayout::RequireFinalSemicolon::_block_is_hash_constructor($elem) == 1) {
### block is a hash, continue ...
} else {
### block is a block, or not certain, stop ...
return;
}
} else { # PPI::Structure::List
_elem_is_assigned_to_hash($elem) || return;
}
$elem = $elem->schild(0) || return;
if ($elem->isa('PPI::Statement')) {
$elem = $elem->schild(0) || return;
}
### first elem: (ref $elem)." $elem"
my @elems = Perl::Critic::Policy::ValuesAndExpressions::ConstantBeforeLt::_elem_and_ssiblings($elem);
### elems len: scalar(@elems)
@elems = map {_expand_qw($_)} @elems;
### expanded len: scalar(@elems)
my $state = 'key';
my @violations;
my %seen_key;
while (@elems) {
### $state
my ($comma, @arg) = _take_to_comma(\@elems);
if (! @arg) {
### consecutive commas ...
next;
}
$elem = $arg[0];
### first of arg: (ref $elem)." $elem"
### arg elem count: scalar(@arg)
if ($elem->isa('PPI::Token::Cast') && $elem eq '%') {
### skip cast % even num elements ...
$state = 'key';
next;
}
# %$foo is an even number of things
if (@arg == 1
&& $elem->isa('PPI::Token::Symbol')
&& $elem->raw_type eq '%') {
### skip hash var even num elements ...
$state = 'key';
next;
}
if ($state eq 'unknown' && $comma eq '=>') {
$state = 'key';
}
if ($state eq 'key') {
my $str;
my $any_vars;
if ($elem->isa('Perl::Critic::Pulp::ProhibitDuplicateHashKeys::Qword')) {
### qword ...
$str = $elem->{'word'};
$any_vars = 0;
$elem = $elem->{'elem'};
} else {
($str, $any_vars) = Perl::Critic::Policy::Miscellanea::TextDomainPlaceholders::_arg_string(\@arg, $document);
}
### $str
if (defined $str
&& ! $any_vars
&& $seen_key{$str}++) {
### found duplicate ...
push @violations, $self->violation ("Duplicate hash key \"$str\"",
'',
$elem);
}
if ($any_vars >= 2) {
### expression, go to unknown ...
$state = 'unknown';
} else {
$state = 'value';
}
} elsif ($state eq 'value') {
if ($comma eq '=>') {
### hmm, something like a=>b=>..., assume next is a value still ...
$state = 'value';
} else {
$state = 'key';
}
}
}
### done ...
return @violations;
}
sub _expand_qw {
my ($elem) = @_;
if (! $elem->isa('PPI::Token::QuoteLike::Words')) {
return $elem;
}
my @words = $elem->literal;
### @words
return map {
Perl::Critic::Pulp::ProhibitDuplicateHashKeys::Qword->new
(word => $_,
elem => $elem);
} @words;
}
sub _take_to_comma {
my ($aref) = @_;
my @ret;
while (@$aref) {
my $elem = shift @$aref;
if ($elem->isa('Perl::Critic::Pulp::ProhibitDuplicateHashKeys::Qword')) {
push @ret, $elem;
return ',', @ret;
}
if (elem_is_comma_operator($elem)) {
return $elem, @ret; # found a comma
}
push @ret, $elem; # not a comma
}
return '', @ret; # no final comma
}
# $elem is any PPI::Element
# return true if it's assigned to a hash,
# %foo = ELEM
# %$foo = ELEM
# %{expr()} = ELEM
#
sub _elem_is_assigned_to_hash {
my ($elem) = @_;
### _elem_is_assigned_to_hash() ...
$elem = $elem->sprevious_sibling || return 0;
($elem->isa('PPI::Token::Operator') && $elem eq '=')
or return 0;
$elem = $elem->sprevious_sibling || return 0;
### assign to: "$elem"
# %{expr} = () deref
if ($elem->isa('PPI::Structure::Block')) {
$elem = $elem->sprevious_sibling || return 0;
### cast hash ...
return ($elem->isa('PPI::Token::Cast') && $elem eq '%');
}
if ($elem->isa('PPI::Token::Symbol')) {
if ($elem->symbol_type eq '%') {
### yes, %foo ...
return 1;
}
if ($elem->symbol_type eq '$') {
### symbol scalar ...
# %$x=() or %$$$x=() deref
for (;;) {
$elem = $elem->sprevious_sibling || return 0;
### prev: (ref $elem)." $elem"
if ($elem->isa('PPI::Token::Magic')) {
# PPI 1.215 mistakes %$$$r as magic variable $$
} elsif ($elem->isa('PPI::Token::Cast')) {
if ($elem ne '$') {
### cast hash: ($elem eq '%')
return ($elem eq '%');
}
} else {
return 0;
}
}
}
}
### no ...
return 0;
}
{
package Perl::Critic::Pulp::ProhibitDuplicateHashKeys::Qword;
sub new {
my ($class, %self) = @_;
return bless \%self, $class;
}
}
1;
__END__
=for stopwords Ryde hashref runtime
=head1 NAME
Perl::Critic::Policy::ValuesAndExpressions::ProhibitDuplicateHashKeys - disallow duplicate literal hash keys
=head1 DESCRIPTION
This policy is part of the L<C<Perl::Critic::Pulp>|Perl::Critic::Pulp>
add-on. It reports duplicate literal hash keys in a hash assignment or
anonymous hashref.
my %hash = (red => 1,
green => 2,
red => 3, # bad
);
my $hashref = { red => 1,
red => 3, # bad
};
Writing duplicate literal keys is probably a mistake or too much cut and
paste, and if the values are different will make it unclear to human readers
what was meant. On that basis this policy is under the "bugs" theme and
medium severity (see L<Perl::Critic/POLICY THEMES>).
Perl is happy to run code like the above. The value of the last "red" is
stored. Doing this at runtime is good since you can give defaults which
further values from a caller or similar can replace. For example,
sub new {
my $class = shift;
return bless { foo => 'default',
bar => 'default',
@_ }, $class;
}
MyClass->new (foo => 'caller value'); # overriding 'default'
=head2 Expressions
Expressions within a hash list cannot be checked in general. Some
concatenations of literals are recognised though they're probably unusual.
my %hash = (ab => 1,
'a'.'b' => 2); # bad
my %hash = (__PACKAGE__.'a' => 1,
__PACKAGE__.'a' => 2); # bad
Function calls etc within a list might return an odd or even number of
values. Fat commas C<=E<gt>> are taken as indicating a key when in doubt.
my %hash = (blah() => 1, # guided by =>
a => 2,
a => 3); # bad
my %hash = (blah(),
a => 2, # guided by =>
a => 3); # bad
A hash substitution is always an even number of arguments,
my %hash = (a => 1,
%blah, # even number
a => 5); # bad, duplicate
C<qw()> words are recognised too
my %hash = (qw(foo value1
foo value2)); # bad
=head2 Disabling
If you don't care about this you can always disable
C<ProhibitDuplicateHashKeys> from your F<.perlcriticrc> file in the usual
way (see L<Perl::Critic/CONFIGURATION>),
[-ValuesAndExpressions::ProhibitDuplicateHashKeys]
=head1 SEE ALSO
L<Perl::Critic::Pulp>,
L<Perl::Critic>
L<Perl::Critic::Policy::CodeLayout::RequireTrailingCommas>,
L<Perl::Critic::Policy::CodeLayout::RequireTrailingCommaAtNewline>
=head1 HOME PAGE
http://user42.tuxfamily.org/perl-critic-pulp/index.html
=head1 COPYRIGHT
Copyright 2011, 2012, 2013, 2014, 2015, 2016, 2017 Kevin Ryde
Perl-Critic-Pulp is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by the Free
Software Foundation; either version 3, or (at your option) any later
version.
Perl-Critic-Pulp is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
more details.
You should have received a copy of the GNU General Public License along with
Perl-Critic-Pulp. If not, see <http://www.gnu.org/licenses/>.
=cut
|