lib/Perl/Critic/Policy/Variables/ProhibitReusedNames.pm

filter: Statementstmt 51.8% 28 / 54 Branchbran 0.0% 0 / 14 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 84.6% 11 / 13 Podpod 80.0% 4 / 5 totaltotal 47.2% 43 / 91 CC 9 SCAR 33.5
CC
9
Cov
39%
violates
72.0
__ANON__
6.0
BEGIN
1.0
CRAP 28.5
« lib/Perl/Critic/Policy/TestingAndDebugging/ProhibitNoWarnings.pm ↑ Summary lib/Perl/Critic/Policy/BuiltinFunctions/ProhibitShiftRef.pm »
1 package Perl::Critic::Policy::Variables::ProhibitReusedNames;
2  
3 40 ▶use 5.010001;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 40 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 40 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 40 ▶use List::SomeUtils qw(part);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 40 ▶use Readonly;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 40 ▶use Perl::Critic::Utils qw{ :severities :classification :data_conversion };
Subroutine BEGIN: called CC=1 SCAR=0.0
10 40 ▶use parent 'Perl::Critic::Policy';
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 40 our $VERSION = '1.156';
13  
14 #-----------------------------------------------------------------------------
15  
16 40 Readonly::Scalar my $DESC => q{Reused variable name in lexical scope: };
17 40 Readonly::Scalar my $EXPL => q{Invent unique variable names};
18  
19 #-----------------------------------------------------------------------------
20  
21 91 ▶sub supported_parameters {
Subroutine supported_parameters: called CC=1 SCAR=0.0
Pod undocumented
22 return (
23 {
24 91 name => 'allow',
25 description => 'The variables to not consider as duplicates.',
26 default_string => '$self $class', ## no critic (RequireInterpolationOfMetachars)
27 behavior => 'string list',
28 },
29 );
30 }
31  
32 74 ▶sub default_severity { return $SEVERITY_MEDIUM }
Subroutine default_severity: called CC=1 SCAR=0.0
Pod documented
33 73 ▶sub default_themes { return qw( core bugs ) }
Subroutine default_themes: called CC=1 SCAR=0.0
Pod documented
34 2 ▶sub applies_to { return 'PPI::Statement::Variable' }
Subroutine applies_to: called CC=1 SCAR=0.0
Pod documented
35  
36 #-----------------------------------------------------------------------------
37  
38 0 ▶sub violates {
Subroutine violates: not called CC=8 SCAR=42.8
Pod documented
39 0 my ( $self, $elem, undef ) = @_;
40 0 ▶ return if 'local' eq $elem->type;
Branch: 1 branch
BranchTrueFalseTotal
if "local" eq $elem->type 0 0 0
41  
42 0 my $allow = $self->{_allow};
43 0 my $names = [ grep { not $allow->{$_} } $elem->variables() ];
44 # Assert: it is impossible for @$names to be empty in valid Perl syntax
45 # But if it IS empty, this code should still work but will be inefficient
46  
47 # Walk up the PDOM looking for declared variables in the same
48 # scope or outer scopes. Quit when we hit the root or when we find
49 # violations for all vars (the latter is a shortcut).
50 0 my $outer = $elem;
51 0 my @violations;
52 0 while (1) {
53 0 my $up = $outer->sprevious_sibling;
54 0 ▶ if (not $up) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($up) 0 0 0
55 0 $up = $outer->parent;
56 0 ▶ last if !$up; # top of PDOM, we're done
Branch: 1 branch
BranchTrueFalseTotal
unless $up 0 0 0
57 }
58 0 $outer = $up;
59  
60 0 ▶ if ($outer->isa('PPI::Statement::Variable') && 'local' ne $outer->type) {
Branch: 1 branch
BranchTrueFalseTotal
if ($outer->isa("PPI::Statement::Variable") and "local" ne $outer->type) 0 0 0
Condition0%0/3
$outer->isa("PPI::Statement::Variable") and "local" ne $outer->type
!ll&&!rl&&r
000
MC/DC0%0/2
$outer->isa("PPI::Statement::Variable") and "local" ne $outer->type
$outer->isa("PPI::Statement::Variable") "local" ne $outer->type
Truth table0%0/3
A and B
A= $outer->isa("PPI::Statement::Variable")B= "local" ne $outer->type
ABresult
0X0
100
111
61 0 my %vars = map {$_ => undef} $outer->variables;
62 0 my $hits;
63 0 ▶ ($hits, $names) = part { exists $vars{$_} ? 0 : 1 } @{$names};
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
exists $vars{$_} ? : 0 0 0
64 0 ▶ if ($hits) {
Branch: 1 branch
BranchTrueFalseTotal
if ($hits) 0 0 0
65 0 push @violations, map { $self->violation( $DESC . $_, $EXPL, $elem ) } @{$hits};
66 0 ▶ last if not $names; # found violations for ALL variables, we're done
Branch: 1 branch
BranchTrueFalseTotal
unless $names 0 0 0
67 }
68 }
69 }
70 0 return @violations;
71 }
72  
73 40 1;
74  
75 __END__
« lib/Perl/Critic/Policy/TestingAndDebugging/ProhibitNoWarnings.pm ↑ Summary lib/Perl/Critic/Policy/BuiltinFunctions/ProhibitShiftRef.pm »