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

filter: Statementstmt 86.0% 43 / 50 Branchbran 46.1% 12 / 26 Conditioncond 55.5% 5 / 9 MC/DCmcdc 40.0% 2 / 5 Subroutinesub 100% 13 / 13 Podpod 80.0% 4 / 5 totaltotal 73.1% 79 / 108 CC 18 SCAR 22.6
CC
18
Cov
71%
_is_non_local_magic_dest
21.3
violates
7.8
_is_magic_var
4.6
CRAP 9.6
« lib/Perl/Critic/Policy/Modules/ProhibitEvilModules.pm ↑ Summary lib/Perl/Critic/Policy/Variables/ProhibitLocalVars.pm »
1 package Perl::Critic::Policy::Variables::RequireLocalizedPunctuationVars;
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 Readonly;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 40 ▶use Perl::Critic::Utils qw{ :severities :classification $EMPTY hashify};
Subroutine BEGIN: called CC=1 SCAR=0.0
9 40 ▶use parent 'Perl::Critic::Policy';
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 40 our $VERSION = '1.156';
12  
13 #-----------------------------------------------------------------------------
14  
15 40 Readonly::Scalar my $DESC => q{Magic variable "%s" should be assigned as "local"};
16 40 Readonly::Scalar my $EXPL => [ 81, 82 ];
17  
18 #-----------------------------------------------------------------------------
19  
20 91 ▶sub supported_parameters {
Subroutine supported_parameters: called CC=1 SCAR=0.0
Pod undocumented
21 return (
22 {
23 91 name => 'allow',
24 description =>
25 q<Global variables to exclude from this policy.>,
26 default_string => $EMPTY,
27 behavior => 'string list',
28 list_always_present_values => [ qw< $_ $ARG @_ > ],
29 },
30 );
31 }
32  
33 74 ▶sub default_severity { return $SEVERITY_HIGH }
Subroutine default_severity: called CC=1 SCAR=0.0
Pod documented
34 91 ▶sub default_themes { return qw(core pbp bugs certrec ) }
Subroutine default_themes: called CC=1 SCAR=0.0
Pod documented
35 33 ▶sub applies_to { return 'PPI::Token::Operator' }
Subroutine applies_to: called CC=1 SCAR=0.0
Pod documented
36  
37 #-----------------------------------------------------------------------------
38  
39 140 ▶sub violates {
Subroutine violates: called CC=6 SCAR=20.5
Pod documented
40 140 my ( $self, $elem, undef ) = @_;
41  
42 140 ▶ return if $elem->content() ne q{=};
Branch: 1 branch
BranchTrueFalseTotal
if $elem->content ne "=" 22 118 140
43  
44 118 my $destination = $elem->sprevious_sibling;
45 118 ▶ return if !$destination; # huh? assignment in void context??
Branch: 1 branch
BranchTrueFalseTotal
unless $destination 0 118 118
46 118 ▶ while ($destination->isa('PPI::Structure::Subscript')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($destination->isa("PPI::Structure::Subscript")) 0 118 118
47 0 ▶ $destination = $destination->sprevious_sibling()
Branch: 1 branch
BranchTrueFalseTotal
$destination = $destination->sprevious_sibling or return 0 0 0
48 or return;
49 }
50  
51 118 ▶ if (my $var = $self->_is_non_local_magic_dest($destination)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $var = $self->_is_non_local_magic_dest($destination)) 0 118 118
52 0 return $self->violation( sprintf( $DESC, $var ), $EXPL, $elem );
53 }
54 118 return; # OK
55 }
56  
57 118 ▶sub _is_non_local_magic_dest {
Subroutine _is_non_local_magic_dest: called CC=10 SCAR=30.6
58 118 my ($self, $elem) = @_;
59  
60 118 state $local_or_my = { hashify( 'local', 'my' ) };
61  
62 # Quick exit if in good form
63 118 my $modifier = $elem->sprevious_sibling;
64 return
65 if
66 $modifier
67 && $modifier->isa('PPI::Token::Word')
68 118 ▶ && $local_or_my->{$modifier->content()};
Branch: 1 branch
BranchTrueFalseTotal
if $modifier and $modifier->isa("PPI::Token::Word") and $$local_or_my{$modifier->content} 61 57 118
Condition83%5/6
$modifier and $modifier->isa("PPI::Token::Word") and $$local_or_my{$modifier->content}
!ll&&!rl&&r
282961
$modifier and $modifier->isa("PPI::Token::Word")
!ll&&!rl&&r
28090
MC/DC66%2/3
$modifier and $modifier->isa("PPI::Token::Word") and $$local_or_my{$modifier->content}
$modifier $modifier->isa("PPI::Token::Word") $$local_or_my{$modifier->content}
Truth table75%3/4
A and B and C
A= $modifierB= $modifier->isa("PPI::Token::Word")C= $$local_or_my{$modifier->content}
ABCresult
0XX0
10X0
1100
1111
69  
70 # Implementation note: Can't rely on PPI::Token::Magic,
71 # unfortunately, because we need English too
72  
73 57 ▶ if ($elem->isa('PPI::Token::Symbol')) {
Branch: 2 branches
BranchTrueFalseTotal
if ($elem->isa('PPI::Token::Symbol')) { } 57 0 57
elsif ($elem->isa('PPI::Structure::List') or $elem->isa('PPI::Statement::Expression')) { } 0 0 0
74 57 ▶ return $self->_is_magic_var($elem) ? $elem : undef;
Branch: 1 branch
BranchTrueFalseTotal
$self->_is_magic_var($elem) ? : 0 57 57
75 }
76 ▶ elsif (
Condition0%0/3
$elem->isa("PPI::Structure::List") or $elem->isa("PPI::Statement::Expression")
l!l&&r!l&&!r
000
MC/DC0%0/2
$elem->isa("PPI::Structure::List") or $elem->isa("PPI::Statement::Expression")
$elem->isa("PPI::Structure::List") $elem->isa("PPI::Statement::Expression")
Truth table0%0/3
A or B
A= $elem->isa("PPI::Structure::List")B= $elem->isa("PPI::Statement::Expression")
ABresult
1X1
011
000
77 $elem->isa('PPI::Structure::List')
78 or $elem->isa('PPI::Statement::Expression')
79 ) {
80 0 for my $child ($elem->schildren) {
81 0 my $var = $self->_is_non_local_magic_dest($child);
82 0 ▶ return $var if $var;
Branch: 1 branch
BranchTrueFalseTotal
if $var 0 0 0
83 }
84 }
85  
86 0 return;
87 }
88  
89 #-----------------------------------------------------------------------------
90  
91 57 ▶sub _is_magic_var {
Subroutine _is_magic_var: called CC=4 SCAR=15.2
92 57 my ($self, $elem) = @_;
93  
94 57 my $variable_name = $elem->symbol();
95 57 ▶ return if $self->{_allow}{$variable_name};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_allow"}{$variable_name} 0 57 57
96 57 ▶ return 1 if $elem->isa('PPI::Token::Magic'); # optimization(?), and
Branch: 1 branch
BranchTrueFalseTotal
if $elem->isa("PPI::Token::Magic") 0 57 57
97 # helps with PPI 1.118 carat
98 # bug. This bug is gone as of
99 # 1.208, which is required for
100 # P::C 1.113. RT 65514
101 57 ▶ return if not is_perl_global( $elem );
Branch: 1 branch
BranchTrueFalseTotal
unless is_perl_global($elem) 57 0 57
102  
103 0 return 1;
104 }
105  
106 40 1;
107  
108 __END__
« lib/Perl/Critic/Policy/Modules/ProhibitEvilModules.pm ↑ Summary lib/Perl/Critic/Policy/Variables/ProhibitLocalVars.pm »