| 1 | package Perl::Critic::Policy::Community::Each; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 1 | ▶ | use Perl::Critic::Utils qw(:severities :classification :ppi); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 1 | ▶ | use parent 'Perl::Critic::Policy'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 1 | our $VERSION = 'v1.0.4'; | |||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 1 | ▶ | use constant DESC => 'each() called'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 1 | ▶ | use constant EXPL => 'The each function may cause undefined behavior when operating on the hash while iterating. Use a foreach loop over the hash\'s keys or values instead.'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | 2 | ▶ | sub supported_parameters { () } | ||||||||||||||||||||||||||
|
Subroutine
supported_parameters: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 15 | 2 | ▶ | sub default_severity { $SEVERITY_LOW } | ||||||||||||||||||||||||||
|
Subroutine
default_severity: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 16 | 0 | ▶ | sub default_themes { 'community' } | ||||||||||||||||||||||||||
|
Subroutine
default_themes: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 17 | 2 | ▶ | sub applies_to { 'PPI::Token::Word' } | ||||||||||||||||||||||||||
|
Subroutine
applies_to: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | 9 | ▶ | sub violates { | ||||||||||||||||||||||||||
|
Subroutine
violates: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 20 | 9 | my ($self, $elem) = @_; | |||||||||||||||||||||||||||
| 21 | 9 | ▶ | return () unless $elem eq 'each' and is_function_call $elem; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$elem eq "each" and is_function_call($elem)
MC/DC50%1/2
$elem eq "each" and is_function_call($elem)
$elem eq "each"
is_function_call($elem)
Truth table66%2/3
A and B
A= $elem eq "each"B= is_function_call($elem)
| |||||||||||||||||||||||||||||
| 22 | 2 | return $self->violation(DESC, EXPL, $elem); | |||||||||||||||||||||||||||
| 23 | } | ||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | 1 | 1; | |||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | =head1 NAME | ||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | Perl::Critic::Policy::Community::Each - Don't use each to iterate through a | ||||||||||||||||||||||||||||
| 30 | hash | ||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | The C<each()> function relies on an iterator internal to a hash (or array), | ||||||||||||||||||||||||||||
| 35 | which is the same iterator used by C<keys()> and C<values()>. So deleting or | ||||||||||||||||||||||||||||
| 36 | adding hash elements during iteration, or just calling C<keys()> or C<values()> | ||||||||||||||||||||||||||||
| 37 | on the hash, will cause undefined behavior and the code will likely break. This | ||||||||||||||||||||||||||||
| 38 | could occur even by passing the hash to other functions which operate on the | ||||||||||||||||||||||||||||
| 39 | hash. Instead, use a C<foreach> loop iterating through the keys or values of | ||||||||||||||||||||||||||||
| 40 | the hash. | ||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | while (my ($key, $value) = each %hash) { ... } # not ok | ||||||||||||||||||||||||||||
| 43 | foreach my $key (keys %hash) { my $value = $hash{$key}; ... } # ok | ||||||||||||||||||||||||||||
| 44 | foreach my $i (0..$#array) { my $elem = $array[$i]; ... } # ok | ||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | =head1 AFFILIATION | ||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | This policy is part of L<Perl::Critic::Community>. | ||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | =head1 CONFIGURATION | ||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | This policy is not configurable except for the standard options. | ||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | Dan Book, C<dbook@cpan.org> | ||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | Copyright 2015, Dan Book. | ||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | This library is free software; you may redistribute it and/or modify it under | ||||||||||||||||||||||||||||
| 63 | the terms of the Artistic License version 2.0. | ||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | =head1 SEE ALSO | ||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | L<Perl::Critic>, L<http://blogs.perl.org/users/rurban/2014/04/do-not-use-each.html> | ||||||||||||||||||||||||||||