| 1 | package Perl::Critic::Policy::Variables::NameReuse; | ||||||||||||||||||||||||||||||||||||||||||||||||
| 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 | our $VERSION = 'v0.1.0'; | |||||||||||||||||||||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||||||||||||||||||||||
| 8 | 1 | ▶ | use Perl::Critic::Utils qw(:severities :classification :ppi); | ||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||
| 9 | 1 | ▶ | use parent 'Perl::Critic::Policy'; | ||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||||||||||||||||||||
| 11 | 1 | ▶ | use constant EXPL => 'Using the same name for multiple types of variables can be confusing, e.g. %foo and $foo. Use different names for different variables.'; | ||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||||||||||||||||||||||
| 13 | 11 | ▶ | sub supported_parameters { () } | ||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
supported_parameters: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||
| 14 | 19 | ▶ | sub default_severity { $SEVERITY_LOW } | ||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
default_severity: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||
| 15 | 0 | ▶ | sub default_themes { () } | ||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
default_themes: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||
| 16 | 11 | ▶ | sub applies_to { 'PPI::Document' } | ||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
applies_to: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||||||||||||||||||||||
| 18 | 11 | ▶ | sub violates { | ||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
violates: called CC=25 SCAR=36.8
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||
| 19 | 11 | my ($self, $elem) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||||||||||||||||||||||
| 21 | 11 | my @violations; | |||||||||||||||||||||||||||||||||||||||||||||||
| 22 | my %seen; | ||||||||||||||||||||||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||||||||||||||||||||||
| 24 | 11 | ▶ | my $symbols = $elem->find('PPI::Token::Symbol') || []; | ||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$elem->find('PPI::Token::Symbol') || []
MC/DC0%0/1
$elem->find('PPI::Token::Symbol') || []
$elem->find('PPI::Token::Symbol')
Truth table50%1/2
A
A= $elem->find('PPI::Token::Symbol')
| |||||||||||||||||||||||||||||||||||||||||||||||||
| 25 | 11 | foreach my $symbol (@$symbols) { | |||||||||||||||||||||||||||||||||||||||||||||||
| 26 | 32 | ▶ | next if $symbol->isa('PPI::Token::Magic'); # skip magic variables | ||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||
| 27 | 32 | my $actual = $symbol->symbol; | |||||||||||||||||||||||||||||||||||||||||||||||
| 28 | 32 | ▶ | next if $actual =~ m/::/; # let's not concern ourselves with fully qualified package variables | ||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||
| 29 | 32 | ▶ | (my $name = $actual) =~ s/^[\$\@\%]// or next; | ||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||
| 30 | 32 | ▶ | next if $name eq 'INC' or $name eq 'ARGV'; | ||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$name eq "INC" or $name eq "ARGV"
MC/DC0%0/2
$name eq "INC" or $name eq "ARGV"
$name eq "INC"
$name eq "ARGV"
Truth table33%1/3
A or B
A= $name eq "INC"B= $name eq "ARGV"
| |||||||||||||||||||||||||||||||||||||||||||||||||
| 31 | 32 | ▶ | $seen{$name}{$actual} //= $symbol; | ||||||||||||||||||||||||||||||||||||||||||||||
|
Condition66%2/3
$seen{$name}{$actual} //= $symbol
MC/DC0%0/2
$seen{$name}{$actual} //= $symbol
$seen{$name}{$actual}
$symbol
Truth table66%2/3
A //= B
A= $seen{$name}{$actual}B= $symbol
| |||||||||||||||||||||||||||||||||||||||||||||||||
| 32 | } | ||||||||||||||||||||||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||||||||||||||||||||||
| 34 | 11 | ▶ | my $indexes = $elem->find('PPI::Token::ArrayIndex') || []; | ||||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$elem->find('PPI::Token::ArrayIndex') || []
MC/DC100%1/1
$elem->find('PPI::Token::ArrayIndex') || []
$elem->find('PPI::Token::ArrayIndex')
Truth table100%2/2
A
A= $elem->find('PPI::Token::ArrayIndex')
| |||||||||||||||||||||||||||||||||||||||||||||||||
| 35 | 11 | foreach my $symbol (@$indexes) { | |||||||||||||||||||||||||||||||||||||||||||||||
| 36 | 3 | ▶ | next if $symbol =~ m/::/; # let's not concern ourselves with fully qualified package variables | ||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||
| 37 | 3 | ▶ | (my $name = $symbol) =~ s/^\$\#// or next; | ||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||
| 38 | 3 | ▶ | next if $name =~ m/^\W$/; # skip magic variables | ||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||
| 39 | 3 | ▶ | next if $name eq 'INC' or $name eq 'ARGV'; | ||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$name eq "INC" or $name eq "ARGV"
MC/DC0%0/2
$name eq "INC" or $name eq "ARGV"
$name eq "INC"
$name eq "ARGV"
Truth table33%1/3
A or B
A= $name eq "INC"B= $name eq "ARGV"
| |||||||||||||||||||||||||||||||||||||||||||||||||
| 40 | 3 | my $actual = '@' . $name; | |||||||||||||||||||||||||||||||||||||||||||||||
| 41 | 3 | ▶ | $seen{$name}{$actual} //= $symbol; | ||||||||||||||||||||||||||||||||||||||||||||||
|
Condition66%2/3
$seen{$name}{$actual} //= $symbol
MC/DC0%0/2
$seen{$name}{$actual} //= $symbol
$seen{$name}{$actual}
$symbol
Truth table66%2/3
A //= B
A= $seen{$name}{$actual}B= $symbol
| |||||||||||||||||||||||||||||||||||||||||||||||||
| 42 | } | ||||||||||||||||||||||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||||||||||||||||||||||
| 44 | 11 | foreach my $name (keys %seen) { | |||||||||||||||||||||||||||||||||||||||||||||||
| 45 | 15 | my $by_actual = $seen{$name}; | |||||||||||||||||||||||||||||||||||||||||||||||
| 46 | 15 | ▶ | if (keys %$by_actual > 1) { | ||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||
| 47 | 8 | my @sorted = sort { (($by_actual->{$a}->logical_line_number // 0) <=> ($by_actual->{$b}->logical_line_number // 0)) | |||||||||||||||||||||||||||||||||||||||||||||||
| 48 | 14 | ▶ | || (($by_actual->{$a}->visual_column_number // 0) <=> ($by_actual->{$b}->visual_column_number // 0)) } keys %$by_actual; | ||||||||||||||||||||||||||||||||||||||||||||||
|
Condition54%6/11
($by_actual->{$a}->logical_line_number // 0) <=> ($by_actual->{$b}->logical_line_number // 0) || ($by_actual->{$a}->visual_column_number // 0) <=> ($by_actual->{$b}->visual_column_number // 0)
$by_actual->{$a}->logical_line_number // 0
$by_actual->{$b}->logical_line_number // 0
$by_actual->{$a}->visual_column_number // 0
$by_actual->{$b}->visual_column_number // 0
MC/DC0%0/2
($by_actual->{$a}->logical_line_number // 0) <=> ($by_actual->{$b}->logical_line_number // 0) || ($by_actual->{$a}->visual_column_number // 0) <=> ($by_actual->{$b}->visual_column_number // 0)
$by_actual->{$a}->logical_line_number
$by_actual->{$a}->visual_column_number
MC/DC0%0/1
$by_actual->{$b}->logical_line_number // 0
$by_actual->{$b}->logical_line_number
MC/DC0%0/1
$by_actual->{$b}->visual_column_number // 0
$by_actual->{$b}->visual_column_number
Truth table33%1/3
A || B
A= $by_actual->{$a}->logical_line_numberB= $by_actual->{$a}->visual_column_number
Truth table50%1/2
A
A= $by_actual->{$b}->logical_line_number
Truth table50%1/2
A
A= $by_actual->{$b}->visual_column_number
| |||||||||||||||||||||||||||||||||||||||||||||||||
| 49 | 8 | push @violations, $self->violation("Reused variable name '$name' for '$_'", EXPL, $by_actual->{$_}) for @sorted; | |||||||||||||||||||||||||||||||||||||||||||||||
| 50 | } | ||||||||||||||||||||||||||||||||||||||||||||||||
| 51 | } | ||||||||||||||||||||||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||||||||||||||||||||||
| 53 | 11 | return @violations; | |||||||||||||||||||||||||||||||||||||||||||||||
| 54 | } | ||||||||||||||||||||||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||||||||||||||||||||||
| 56 | 1 | 1; | |||||||||||||||||||||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||||||||||||||||||||||
| 58 | =head1 NAME | ||||||||||||||||||||||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||||||||||||||||||||||
| 60 | Perl::Critic::Policy::Variables::NameReuse - Don't reuse names for different | ||||||||||||||||||||||||||||||||||||||||||||||||
| 61 | types of variables | ||||||||||||||||||||||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||||||||||||||||||||||
| 63 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||||||||||||||||||||||
| 65 | perlcritic --single-policy=Variables::NameReuse script.pl | ||||||||||||||||||||||||||||||||||||||||||||||||
| 66 | perlcritic --single-policy=Variables::NameReuse lib/ | ||||||||||||||||||||||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||||||||||||||||||||||
| 68 | # .perlcriticrc | ||||||||||||||||||||||||||||||||||||||||||||||||
| 69 | severity = 1 | ||||||||||||||||||||||||||||||||||||||||||||||||
| 70 | only = 1 | ||||||||||||||||||||||||||||||||||||||||||||||||
| 71 | [Variables::NameReuse] | ||||||||||||||||||||||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||||||||||||||||||||||
| 73 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||||||||||||||||||||||
| 75 | This policy checks for the existence of multiple variables with the same name | ||||||||||||||||||||||||||||||||||||||||||||||||
| 76 | in a file. This can be confusing especially when accessing elements of | ||||||||||||||||||||||||||||||||||||||||||||||||
| 77 | variables or using L<list or key-value slices|perldata/Slices>. For example, | ||||||||||||||||||||||||||||||||||||||||||||||||
| 78 | the code could access both C<$foo> and C<$foo[0]> but these actually refer to | ||||||||||||||||||||||||||||||||||||||||||||||||
| 79 | the unrelated variables C<$foo> and C<@foo>. | ||||||||||||||||||||||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||||||||||||||||||||||
| 81 | my $foo = @foo; # not ok | ||||||||||||||||||||||||||||||||||||||||||||||||
| 82 | my @bar = @bar{'a','b'}; # not ok | ||||||||||||||||||||||||||||||||||||||||||||||||
| 83 | my $count = @foo; # ok | ||||||||||||||||||||||||||||||||||||||||||||||||
| 84 | my @values = @bar{'a','b'}; # ok | ||||||||||||||||||||||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||||||||||||||||||||||
| 86 | =head1 AFFILIATION | ||||||||||||||||||||||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||||||||||||||||||||||
| 88 | This policy has no affiliation. | ||||||||||||||||||||||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||||||||||||||||||||||
| 90 | =head1 CONFIGURATION | ||||||||||||||||||||||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||||||||||||||||||||||
| 92 | This policy is not configurable except for the standard options. | ||||||||||||||||||||||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||||||||||||||||||||||
| 94 | =head1 BUGS | ||||||||||||||||||||||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||||||||||||||||||||||
| 96 | Report any issues on the public bugtracker. | ||||||||||||||||||||||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||||||||||||||||||||||
| 98 | =head1 AUTHOR | ||||||||||||||||||||||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||||||||||||||||||||||
| 100 | Dan Book <dbook@cpan.org> | ||||||||||||||||||||||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||||||||||||||||||||||
| 102 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||||||||||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||||||||||||||||||||||
| 104 | This software is Copyright (c) 2018 by Dan Book. | ||||||||||||||||||||||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||||||||||||||||||||||
| 106 | This is free software, licensed under: | ||||||||||||||||||||||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||||||||||||||||||||||
| 108 | The Artistic License 2.0 (GPL Compatible) | ||||||||||||||||||||||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||||||||||||||||||||||
| 110 | =head1 SEE ALSO | ||||||||||||||||||||||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||||||||||||||||||||||
| 112 | L<Perl::Critic::Policy::Variables::ProhibitReusedNames> - instead prohibits | ||||||||||||||||||||||||||||||||||||||||||||||||
| 113 | redeclaring the same variable name across different scopes | ||||||||||||||||||||||||||||||||||||||||||||||||