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

filter: Statementstmt 41.7% 48 / 115 Branchbran 5.0% 2 / 40 Conditioncond 6.6% 1 / 15 MC/DCmcdc 0.0% 0 / 9 Subroutinesub 68.1% 15 / 22 Podpod 83.3% 5 / 6 totaltotal 34.3% 71 / 207 CC 30 SCAR 32.5
CC
30
Cov
30%
violates
72.0
_handle_variable_specification
42.0
_compute_symbol_names
30.0
CRAP 25.7
« lib/Perl/Critic/Policy/InputOutput/RequireBracedFileHandleWithPrint.pm ↑ Summary lib/Perl/Critic/Policy/Subroutines/ProhibitManyArgs.pm »
1 package Perl::Critic::Policy::Variables::ProhibitEvilVariables;
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 English qw(-no_match_vars);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 40 ▶use List::SomeUtils qw(firstval);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 40 ▶use Readonly;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 use Perl::Critic::Exception::Configuration::Option::Policy::ParameterValue
12 40 ▶ qw{ throw_policy_value };
Subroutine BEGIN: called CC=1 SCAR=0.0
13 40 ▶use Perl::Critic::Utils qw{
Subroutine BEGIN: called CC=1 SCAR=0.0
14 :characters :severities :data_conversion
15 40 };
16  
17 40 ▶use parent 'Perl::Critic::Policy';
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 40 our $VERSION = '1.156';
20  
21 #-----------------------------------------------------------------------------
22  
23 40 Readonly::Scalar my $EXPL => q{Find an alternative variable};
24  
25 40 Readonly::Hash my %SUBSCRIPTED_TYPE => hashify(qw{@ %});
26  
27 40 Readonly::Scalar my $VARIABLE_NAME_REGEX => qr< [\$\@%] \S+ >xms;
28 40 Readonly::Scalar my $REGULAR_EXPRESSION_REGEX =>
29 qr< [/] ( [^/]+ ) [/] >xms;
30 40 Readonly::Array my @DESCRIPTION_REGEXES =>
31 qr< [{] ( [^}]+ ) [}] >xms,
32 qr{ < ( [^>]+ ) > }xms,
33 qr{ [[] ( [^]]+ ) []] }xms,
34 qr{ [(] ( [^)]+ ) [)] }xms,
35 ;
36 40 Readonly::Scalar my $DESCRIPTION_REGEX =>
37 40 qr< @{[join '|', @DESCRIPTION_REGEXES]} >xms;
38  
39 # It's kind of unfortunate that I had to put capturing parentheses in the
40 # component regexes above, because they're not visible here and so make
41 # figuring out the positions of captures hard. Too bad we can't make the
42 # minimum perl version 5.10. :]
43 40 Readonly::Scalar my $VARIABLES_REGEX =>
44 qr<
45 \A
46 \s*
47 (?:
48 ( $VARIABLE_NAME_REGEX )
49 | $REGULAR_EXPRESSION_REGEX
50 )
51 (?: \s* $DESCRIPTION_REGEX )?
52 \s*
53 >xms;
54  
55 40 Readonly::Scalar my $VARIABLES_FILE_LINE_REGEX =>
56 qr<
57 \A
58 \s*
59 (?:
60 ( $VARIABLE_NAME_REGEX )
61 | $REGULAR_EXPRESSION_REGEX
62 )
63 \s*
64 ( \S (?: .* \S )? )?
65 \s*
66 \z
67 >xms;
68  
69 # Indexes in the arrays of regexes for the "variables" option.
70 40 Readonly::Scalar my $INDEX_REGEX => 0;
71 40 Readonly::Scalar my $INDEX_DESCRIPTION => 1;
72  
73 #-----------------------------------------------------------------------------
74  
75 92 ▶sub supported_parameters {
Subroutine supported_parameters: called CC=1 SCAR=0.0
Pod undocumented
76 return (
77 {
78 92 name => 'variables',
79 description => 'The names of or patterns for variables to forbid.',
80 default_string => $EMPTY,
81 parser => \&_parse_variables,
82 },
83 {
84 name => 'variables_file',
85 description => 'A file containing names of or patterns for variables to forbid.',
86 default_string => $EMPTY,
87 parser => \&_parse_variables_file,
88 },
89 );
90 }
91  
92 81 ▶sub default_severity { return $SEVERITY_HIGHEST }
Subroutine default_severity: called CC=1 SCAR=0.0
Pod documented
93 75 ▶sub default_themes { return qw( core bugs ) }
Subroutine default_themes: called CC=1 SCAR=0.0
Pod documented
94 0 ▶sub applies_to { return qw{PPI::Token::Symbol} }
Subroutine applies_to: not called CC=1 SCAR=6.9
Pod documented
95  
96 #-----------------------------------------------------------------------------
97  
98 90 ▶sub _parse_variables {
Subroutine _parse_variables: called CC=5 SCAR=30.1
99 90 my ($self, undef, $config_string) = @_;
100  
101 90 ▶ return if not $config_string;
Branch: 1 branch
BranchTrueFalseTotal
unless $config_string 90 0 90
102 0 ▶ return if $config_string =~ m< \A \s* \z >xms;
Branch: 1 branch
BranchTrueFalseTotal
if $config_string =~ / \A \s* \z /msx 0 0 0
103  
104 0 my $variable_specifications = $config_string;
105  
106 0 ▶ while ( my ($variable, $regex_string, @descrs) =
Branch: 1 branch
BranchTrueFalseTotal
if (() = $variable_specifications =~ / $VARIABLES_REGEX /msx) 0 0 0
107 $variable_specifications =~ m< $VARIABLES_REGEX >xms) {
108  
109 0 substr $variable_specifications, 0, $LAST_MATCH_END[0], $EMPTY;
110 0 ▶ my $description = firstval { defined } @descrs;
Subroutine __ANON__: not called CC=1 SCAR=6.9
111  
112 0 $self->_handle_variable_specification(
113 variable => $variable,
114 regex_string => $regex_string,
115 description => $description,
116 option_name => 'variables',
117 option_value => $config_string,
118 );
119 }
120  
121 0 ▶ if ($variable_specifications) {
Branch: 1 branch
BranchTrueFalseTotal
if ($variable_specifications) 0 0 0
122 0 throw_policy_value
123 policy => $self->get_short_name(),
124 option_name => 'variables',
125 option_value => $config_string,
126 message_suffix =>
127 qq{contains unparseable data: "$variable_specifications"};
128 }
129  
130 0 return;
131 }
132  
133 90 ▶sub _parse_variables_file {
Subroutine _parse_variables_file: called CC=6 SCAR=32.9
134 90 my ($self, undef, $config_string) = @_;
135  
136 90 ▶ return if not $config_string;
Branch: 1 branch
BranchTrueFalseTotal
unless $config_string 90 0 90
137 0 ▶ return if $config_string =~ m< \A \s* \z >xms;
Branch: 1 branch
BranchTrueFalseTotal
if $config_string =~ / \A \s* \z /msx 0 0 0
138  
139 0 ▶ open my $handle, '<', $config_string
Branch: 1 branch
BranchTrueFalseTotal
open my $handle, "<", $config_string or throw_policy_value("policy", $self->get_short_name, "option_name", "variables_file", "option_value", $config_string, "message_suffix", "refers to a file that could not be opened: $Perl::Critic::Policy::Variables::ProhibitEvilVariables::OS_ERROR") 0 0 0
140 or throw_policy_value
141 policy => $self->get_short_name(),
142 option_name => 'variables_file',
143 option_value => $config_string,
144 message_suffix =>
145 qq<refers to a file that could not be opened: $OS_ERROR>;
146 0 ▶ while ( my $line = <$handle> ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $line = readline $handle)) 0 0 0
147 0 $self->_handle_variable_specification_on_line($line, $config_string);
148 }
149 0 ▶ close $handle or warn qq<Could not close "$config_string": $OS_ERROR\n>;
Branch: 1 branch
BranchTrueFalseTotal
close $handle or warn qq[Could not close "$config_string": $Perl::Critic::Policy::Variables::ProhibitEvilVariables::OS_ERROR\n] 0 0 0
150  
151 0 return;
152 }
153  
154 0 ▶sub _handle_variable_specification_on_line {
Subroutine _handle_variable_specification_on_line: not called CC=3 SCAR=24.8
155 0 my ($self, $line, $config_string) = @_;
156  
157 0 $line =~ s< [#] .* \z ><>xms;
158 0 $line =~ s< \s+ \z ><>xms;
159 0 $line =~ s< \A \s+ ><>xms;
160  
161 0 ▶ return if not $line;
Branch: 1 branch
BranchTrueFalseTotal
unless $line 0 0 0
162  
163 0 ▶ if ( my ($variable, $regex_string, $description) =
Branch: 1 branch
BranchTrueFalseTotal
if (() = $line =~ / $VARIABLES_FILE_LINE_REGEX /msx) { } 0 0 0
164 $line =~ m< $VARIABLES_FILE_LINE_REGEX >xms) {
165  
166 0 $self->_handle_variable_specification(
167 variable => $variable,
168 regex_string => $regex_string,
169 description => $description,
170 option_name => 'variables_file',
171 option_value => $config_string,
172 );
173 }
174 else {
175 0 throw_policy_value
176 policy => $self->get_short_name(),
177 option_name => 'variables_file',
178 option_value => $config_string,
179 message_suffix =>
180 qq{contains unparseable data: "$line"};
181 }
182  
183 0 return;
184 }
185  
186 0 ▶sub _handle_variable_specification {
Subroutine _handle_variable_specification: not called CC=6 SCAR=37.4
187 0 my ($self, %arguments) = @_;
188  
189 0 ▶ my $description = $arguments{description} || $EMPTY;
Condition0%0/3
$arguments{'description'} || $Perl::Critic::Policy::Variables::ProhibitEvilVariables::EMPTY
l!l&&r!l&&!r
000
MC/DC0%0/2
$arguments{'description'} || $Perl::Critic::Policy::Variables::ProhibitEvilVariables::EMPTY
$arguments{'description'} $Perl::Critic::Policy::Variables::ProhibitEvilVariables::EMPTY
Truth table0%0/3
A || B
A= $arguments{'description'}B= $Perl::Critic::Policy::Variables::ProhibitEvilVariables::EMPTY
ABresult
1X1
011
000
190  
191 0 ▶ if ( my $regex_string = $arguments{regex_string} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $regex_string = $arguments{'regex_string'}) { } 0 0 0
192 # These are variable name patterns (e.g. /acme/)
193 0 my $actual_regex;
194  
195 0 eval { $actual_regex = qr/$regex_string/sm; ## no critic (ExtendedFormatting)
196 0 1 }
197 or throw_policy_value
198 policy => $self->get_short_name(),
199 option_name => $arguments{option_name},
200 option_value => $arguments{option_value},
201 0 ▶ message_suffix =>
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $actual_regex = qr/$regex_string/ms; 1 } } or throw_policy_value("policy", $self->get_short_name, "option_name", $arguments{"option_name"}, "option_value", $arguments{"option_value"}, "message_suffix", qq[contains an invalid regular expression: "$regex_string"]) 0 0 0
202 qq{contains an invalid regular expression: "$regex_string"};
203  
204 # Can't use a hash due to stringification, so this is an AoA.
205 push
206 0 ▶ @{ $self->{_evil_variables_regexes} ||= [] },
Condition0%0/2
$self->{"_evil_variables_regexes"} ||= []
l!l
00
MC/DC0%0/1
$self->{"_evil_variables_regexes"} ||= []
$self->{"_evil_variables_regexes"}
Truth table0%0/2
A
A= $self->{"_evil_variables_regexes"}
Aresult
11
00
207 [ $actual_regex, $description ];
208 }
209 else {
210 # These are literal variable names (e.g. $[)
211 0 ▶ $self->{_evil_variables} ||= {};
Condition0%0/2
$self->{"_evil_variables"} ||= {}
l!l
00
MC/DC0%0/1
$self->{"_evil_variables"} ||= {}
$self->{"_evil_variables"}
Truth table0%0/2
A
A= $self->{"_evil_variables"}
Aresult
11
00
212 0 my $name = $arguments{variable};
213 0 $self->{_evil_variables}{$name} = $description;
214 }
215  
216 0 return;
217 }
218  
219 #-----------------------------------------------------------------------------
220  
221 75 ▶sub initialize_if_enabled {
Subroutine initialize_if_enabled: called CC=2 SCAR=8.1
Pod documented
222 75 my ($self, undef) = @_;
223  
224 # Disable if no variables are specified; there's no point in running if
225 # there aren't any.
226 return
227 exists $self->{_evil_variables}
228 75 ▶ || exists $self->{_evil_variables_regexes};
Condition33%1/3
exists $self->{'_evil_variables'} || exists $self->{'_evil_variables_regexes'}
l!l&&r!l&&!r
0075
MC/DC0%0/2
exists $self->{'_evil_variables'} || exists $self->{'_evil_variables_regexes'}
exists $self->{'_evil_variables'} exists $self->{'_evil_variables_regexes'}
Truth table33%1/3
A || B
A= exists $self->{'_evil_variables'}B= exists $self->{'_evil_variables_regexes'}
ABresult
1X1
011
000
229 }
230  
231 #-----------------------------------------------------------------------------
232  
233 0 ▶sub violates {
Subroutine violates: not called CC=8 SCAR=42.8
Pod documented
234 0 my ( $self, $elem, undef ) = @_;
235 0 ▶ return if not $elem;
Branch: 1 branch
BranchTrueFalseTotal
unless $elem 0 0 0
236  
237 0 ▶ my @names = _compute_symbol_names( $elem )
Branch: 1 branch
BranchTrueFalseTotal
my(@names) = _compute_symbol_names($elem) or return 0 0 0
238 or return;
239  
240 0 my $evil_variables = $self->{_evil_variables};
241 0 my $evil_variables_regexes = $self->{_evil_variables_regexes};
242  
243 0 foreach my $variable (@names) {
244 exists $evil_variables->{$variable}
245 and return $self->_make_violation(
246 $variable,
247 0 ▶ $evil_variables->{$variable},
Branch: 1 branch
BranchTrueFalseTotal
exists $evil_variables->{$variable} and return $self->_make_violation($variable, $evil_variables->{$variable}, $elem) 0 0 0
248 $elem,
249 );
250 }
251  
252 0 foreach my $variable (@names) {
253 0 foreach my $regex ( @{$evil_variables_regexes} ) {
254 0 ▶ $variable =~ $regex->[$INDEX_REGEX]
Branch: 1 branch
BranchTrueFalseTotal
$variable =~ /$regex->[$INDEX_REGEX]/ and return $self->_make_violation($variable, $regex->[$INDEX_DESCRIPTION], $elem) 0 0 0
255 and return $self->_make_violation(
256 $variable,
257 $regex->[$INDEX_DESCRIPTION],
258 $elem,
259 );
260 }
261 }
262  
263 0 return; # ok!
264 }
265  
266 #-----------------------------------------------------------------------------
267  
268 # We are unconditionally interested in the names of the symbol itself. If the
269 # symbol is subscripted, we are interested in the subscripted form as well.
270  
271 0 ▶sub _compute_symbol_names {
Subroutine _compute_symbol_names: not called CC=5 SCAR=34.0
272 0 my ($elem) = @_;
273  
274 0 my @names;
275  
276 0 my $name = $elem->symbol();
277 0 push @names, $name;
278  
279 0 ▶ if ($SUBSCRIPTED_TYPE{$elem->symbol_type()}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($SUBSCRIPTED_TYPE{$elem->symbol_type}) 0 0 0
280 0 $name = $elem->content();
281 0 my $next = $elem->snext_sibling();
282 0 my @subscr;
283 0 ▶ while ($next and $next->isa('PPI::Structure::Subscript')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($next and $next->isa("PPI::Structure::Subscript")) 0 0 0
Condition0%0/3
$next and $next->isa("PPI::Structure::Subscript")
!ll&&!rl&&r
000
MC/DC0%0/2
$next and $next->isa("PPI::Structure::Subscript")
$next $next->isa("PPI::Structure::Subscript")
Truth table0%0/3
A and B
A= $nextB= $next->isa("PPI::Structure::Subscript")
ABresult
0X0
100
111
284 0 push @subscr, $next->content();
285 0 $next = $next->snext_sibling();
286 }
287 0 ▶ if (@subscr) {
Branch: 1 branch
BranchTrueFalseTotal
if (@subscr) 0 0 0
288 0 push @names, join $EMPTY, $name, @subscr;
289 }
290 }
291  
292 0 return @names;
293 }
294  
295 #-----------------------------------------------------------------------------
296  
297 0 ▶sub _make_violation {
Subroutine _make_violation: not called CC=2 SCAR=17.9
298 0 my ($self, $variable, $description, $elem) = @_;
299 0 ▶ return $self->violation(
Condition0%0/2
$description || qq[Prohibited variable "$variable" used]
l!l
00
MC/DC0%0/1
$description || qq[Prohibited variable "$variable" used]
$description
Truth table0%0/2
A
A= $description
Aresult
11
00
300 $description || qq<Prohibited variable "$variable" used>,
301 $EXPL,
302 $elem,
303 );
304 }
305  
306 40 1;
307  
308 __END__
« lib/Perl/Critic/Policy/InputOutput/RequireBracedFileHandleWithPrint.pm ↑ Summary lib/Perl/Critic/Policy/Subroutines/ProhibitManyArgs.pm »