blib/lib/Perl/Critic/Policy/Variables/NameReuse.pm

filter: Statementstmt 97.8% 45 / 46 Branchbran 55.5% 10 / 18 Conditioncond 55.5% 15 / 27 MC/DCmcdc 7.1% 1 / 14 Subroutinesub 90.0% 9 / 10 Podpod 80.0% 4 / 5 totaltotal 70.0% 84 / 120 CC 25 SCAR 33.8
CC
25
Cov
77%
violates
39.6
default_themes
2.0
BEGIN
1.0
CRAP 29.4
↑ Summary
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') || []
l!l
110
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')
Aresult
11
00
25 11 foreach my $symbol (@$symbols) {
26 32 ▶ next if $symbol->isa('PPI::Token::Magic'); # skip magic variables
Branch: 1 branch
BranchTrueFalseTotal
if $symbol->isa("PPI::Token::Magic") 0 32 32
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
BranchTrueFalseTotal
if $actual =~ /::/ 0 32 32
29 32 ▶ (my $name = $actual) =~ s/^[\$\@\%]// or next;
Branch: 1 branch
BranchTrueFalseTotal
(my $name = $actual) =~ s/^[\$\@\%]// or next 32 0 32
30 32 ▶ next if $name eq 'INC' or $name eq 'ARGV';
Branch: 1 branch
BranchTrueFalseTotal
if $name eq "INC" or $name eq "ARGV" 0 32 32
Condition33%1/3
$name eq "INC" or $name eq "ARGV"
l!l&&r!l&&!r
0032
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"
ABresult
1X1
011
000
31 32 ▶ $seen{$name}{$actual} //= $symbol;
Condition66%2/3
$seen{$name}{$actual} //= $symbol
l!l&&r!l&&!r
8240
MC/DC0%0/2
$seen{$name}{$actual} //= $symbol
$seen{$name}{$actual} $symbol
Truth table66%2/3
A //= B
A= $seen{$name}{$actual}B= $symbol
ABresult
1X1
011
000
32 }
33  
34 11 ▶ my $indexes = $elem->find('PPI::Token::ArrayIndex') || [];
Condition100%2/2
$elem->find('PPI::Token::ArrayIndex') || []
l!l
38
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')
Aresult
11
00
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
BranchTrueFalseTotal
if $symbol =~ /::/ 0 3 3
37 3 ▶ (my $name = $symbol) =~ s/^\$\#// or next;
Branch: 1 branch
BranchTrueFalseTotal
(my $name = $symbol) =~ s/^\$\#// or next 3 0 3
38 3 ▶ next if $name =~ m/^\W$/; # skip magic variables
Branch: 1 branch
BranchTrueFalseTotal
if $name =~ /^\W$/ 0 3 3
39 3 ▶ next if $name eq 'INC' or $name eq 'ARGV';
Branch: 1 branch
BranchTrueFalseTotal
if $name eq "INC" or $name eq "ARGV" 0 3 3
Condition33%1/3
$name eq "INC" or $name eq "ARGV"
l!l&&r!l&&!r
003
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"
ABresult
1X1
011
000
40 3 my $actual = '@' . $name;
41 3 ▶ $seen{$name}{$actual} //= $symbol;
Condition66%2/3
$seen{$name}{$actual} //= $symbol
l!l&&r!l&&!r
120
MC/DC0%0/2
$seen{$name}{$actual} //= $symbol
$seen{$name}{$actual} $symbol
Truth table66%2/3
A //= B
A= $seen{$name}{$actual}B= $symbol
ABresult
1X1
011
000
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
BranchTrueFalseTotal
if (keys %$by_actual > 1) 8 7 15
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)
l!l&&r!l&&!r
4100
$by_actual->{$a}->logical_line_number // 0
l!l
140
$by_actual->{$b}->logical_line_number // 0
l!l
140
$by_actual->{$a}->visual_column_number // 0
l!l
100
$by_actual->{$b}->visual_column_number // 0
l!l
100
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
ABresult
1X1
011
000
Truth table50%1/2
A
A= $by_actual->{$b}->logical_line_number
Aresult
11
00
Truth table50%1/2
A
A= $by_actual->{$b}->visual_column_number
Aresult
11
00
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
↑ Summary