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

filter: Statementstmt 91.5% 54 / 59 Branchbran 66.6% 16 / 24 Conditioncond 66.6% 4 / 6 MC/DCmcdc 33.3% 1 / 3 Subroutinesub 100% 18 / 18 Podpod 83.3% 5 / 6 totaltotal 84.4% 98 / 116 CC 15 SCAR 13.5
CC
15
Cov
83%
_is_vars_pragma
10.4
_is_package_var
5.0
_is_our_var
4.1
CRAP 3.9
« lib/Perl/Critic/Policy/InputOutput/ProhibitOneArgSelect.pm ↑ Summary lib/Perl/Critic/Policy/ClassHierarchies/ProhibitOneArgBless.pm »
1 package Perl::Critic::Policy::Variables::ProhibitPackageVars;
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  
7 40 ▶use Readonly;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 40 ▶use List::SomeUtils qw(all);
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 40 ▶use Perl::Critic::Utils qw{
Subroutine BEGIN: called CC=1 SCAR=0.0
12 :booleans :characters :severities :data_conversion
13 40 };
14 40 ▶use parent 'Perl::Critic::Policy';
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 40 our $VERSION = '1.156';
17  
18 #-----------------------------------------------------------------------------
19  
20 40 Readonly::Scalar my $DESC => q{Package variable declared or used};
21 40 Readonly::Scalar my $EXPL => [ 73, 75 ];
22  
23 #-----------------------------------------------------------------------------
24  
25 92 ▶sub supported_parameters {
Subroutine supported_parameters: called CC=1 SCAR=0.0
Pod undocumented
26 return (
27 {
28 92 name => 'packages',
29 description => 'The base set of packages to allow variables for.',
30 default_string => 'Data::Dumper File::Find FindBin Log::Log4perl Test::Builder Text::Wrap',
31 behavior => 'string list',
32 },
33 {
34 name => 'add_packages',
35 description => 'The set of packages to allow variables for, in addition to those given in "packages".',
36 default_string => $EMPTY,
37 behavior => 'string list',
38 },
39 );
40 }
41  
42 75 ▶sub default_severity { return $SEVERITY_MEDIUM }
Subroutine default_severity: called CC=1 SCAR=0.0
Pod documented
43 85 ▶sub default_themes { return qw(core pbp maintenance) }
Subroutine default_themes: called CC=1 SCAR=0.0
Pod documented
44 31 ▶sub applies_to { return qw(PPI::Token::Symbol
Subroutine applies_to: called CC=1 SCAR=0.0
Pod documented
45 PPI::Statement::Variable
46 PPI::Statement::Include) }
47  
48 #-----------------------------------------------------------------------------
49  
50 55 ▶sub initialize_if_enabled {
Subroutine initialize_if_enabled: called CC=1 SCAR=0.0
Pod documented
51 55 my ($self, undef) = @_;
52  
53 $self->{_all_packages} = {
54 55 hashify keys %{ $self->{_packages} }, keys %{ $self->{_add_packages} }
55 };
56  
57 55 return $TRUE;
58 }
59  
60 #-----------------------------------------------------------------------------
61  
62 315 ▶sub violates {
Subroutine violates: called CC=4 SCAR=14.0
Pod documented
63 315 my ( $self, $elem, undef ) = @_;
64  
65 315 ▶ if ( $self->_is_package_var($elem) ||
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_is_package_var($elem) or _is_our_var($elem) or _is_vars_pragma($elem)) 1 314 315
Condition66%4/6
$self->_is_package_var($elem) or _is_our_var($elem) or _is_vars_pragma($elem)
l!l&&r!l&&!r
10314
$self->_is_package_var($elem) or _is_our_var($elem)
l!l&&r!l&&!r
10314
MC/DC33%1/3
$self->_is_package_var($elem) or _is_our_var($elem) or _is_vars_pragma($elem)
$self->_is_package_var($elem) _is_our_var($elem) _is_vars_pragma($elem)
Truth table50%2/4
A or B or C
A= $self->_is_package_var($elem)B= _is_our_var($elem)C= _is_vars_pragma($elem)
ABCresult
1XX1
01X1
0011
0000
66 _is_our_var($elem) ||
67 _is_vars_pragma($elem) )
68 {
69  
70 1 return $self->violation( $DESC, $EXPL, $elem );
71 }
72  
73 314 return; # ok
74 }
75  
76 #-----------------------------------------------------------------------------
77  
78 315 ▶sub _is_package_var {
Subroutine _is_package_var: called CC=5 SCAR=16.2
79 315 my $self = shift;
80 315 my $elem = shift;
81 315 ▶ return if !$elem->isa('PPI::Token::Symbol');
Branch: 1 branch
BranchTrueFalseTotal
unless $elem->isa("PPI::Token::Symbol") 142 173 315
82 173 my ($package, $name) = $elem =~ m{ \A [@\$%] (.*) :: (\w+) \z }xms;
83 173 ▶ return if not defined $package;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $package 172 1 173
84 1 ▶ return if _all_upcase( $name );
Branch: 1 branch
BranchTrueFalseTotal
if _all_upcase($name) 0 1 1
85 1 ▶ return if $self->{_all_packages}->{$package};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_all_packages"}{$package} 0 1 1
86 1 return 1;
87 }
88  
89 #-----------------------------------------------------------------------------
90  
91 314 ▶sub _is_our_var {
Subroutine _is_our_var: called CC=4 SCAR=14.1
92 314 my $elem = shift;
93 314 ▶ return if not $elem->isa('PPI::Statement::Variable');
Branch: 1 branch
BranchTrueFalseTotal
unless $elem->isa("PPI::Statement::Variable") 229 85 314
94 85 ▶ return if $elem->type() ne 'our';
Branch: 1 branch
BranchTrueFalseTotal
if $elem->type ne "our" 58 27 85
95 27 ▶ return if _all_upcase( $elem->variables() );
Branch: 1 branch
BranchTrueFalseTotal
if _all_upcase($elem->variables) 27 0 27
96 0 return 1;
97 }
98  
99 #-----------------------------------------------------------------------------
100  
101 314 ▶sub _is_vars_pragma {
Subroutine _is_vars_pragma: called CC=5 SCAR=23.4
102 314 my $elem = shift;
103 314 ▶ return if !$elem->isa('PPI::Statement::Include');
Branch: 1 branch
BranchTrueFalseTotal
unless $elem->isa("PPI::Statement::Include") 257 57 314
104 57 ▶ return if $elem->pragma() ne 'vars';
Branch: 1 branch
BranchTrueFalseTotal
if $elem->pragma ne "vars" 57 0 57
105  
106 # Older Perls don't support the C<our> keyword, so we try to let
107 # people use the C<vars> pragma instead, but only if all the
108 # variable names are uppercase. Since there are lots of ways to
109 # pass arguments to pragmas (e.g. "$foo" or qw($foo) ) we just use
110 # a regex to match things that look like variables names.
111  
112 0 my @varnames = $elem =~ m{ [@\$%&] (\w+) }gxms;
113  
114 0 ▶ return if !@varnames; # no valid variables specified
Branch: 1 branch
BranchTrueFalseTotal
unless @varnames 0 0 0
115 0 ▶ return if _all_upcase( @varnames );
Branch: 1 branch
BranchTrueFalseTotal
if _all_upcase(@varnames) 0 0 0
116 0 return 1;
117 }
118  
119 28 ▶sub _all_upcase { ##no critic(ArgUnpacking)
Subroutine _all_upcase: called CC=1 SCAR=0.0
120 28 ▶ return all { $_ eq uc } @_;
Subroutine __ANON__: called CC=1 SCAR=0.0
121 }
122  
123 40 1;
124  
125 __END__
« lib/Perl/Critic/Policy/InputOutput/ProhibitOneArgSelect.pm ↑ Summary lib/Perl/Critic/Policy/ClassHierarchies/ProhibitOneArgBless.pm »