Perl/Lint/Policy/Variables/ProhibitEvilVariables.pm

filter: Statementstmt 29.9% 32 / 107 Branchbran 0.0% 0 / 50 Conditioncond 0.0% 0 / 9 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 88.8% 8 / 9 Podpod 0.0% 0 / 1 totaltotal 21.9% 40 / 182 CC 33 SCAR 68.1
CC
33
Cov
19%
evaluate
1122.0
BEGIN
1.0
BEGIN
1.0
CRAP 903.3
« Perl/Lint/Policy/ValuesAndExpressions/ProhibitLeadingZeros.pm ↑ Summary Perl/Lint/Policy/Variables/RequireNegativeIndices.pm »
1 package Perl::Lint::Policy::Variables::ProhibitEvilVariables;
2 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use Perl::Lint::Constants::Type;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use parent "Perl::Lint::Policy";
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 2 ▶use constant {
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 DESC => 'The names of or patterns for variables to forbid.',
10 EXPL => 'Find an alternative variable (used: "%s")',
11 2 };
12  
13 2 ▶use constant VAR_TOKENS => {
Subroutine BEGIN: called CC=1 SCAR=0.0
14 &VAR => 1,
15 &CODE_VAR => 1,
16 &ARRAY_VAR => 1,
17 &HASH_VAR => 1,
18  
19 &GLOBAL_VAR => 1,
20 &GLOBAL_ARRAY_VAR => 1,
21 &GLOBAL_HASH_VAR => 1,
22  
23 &LOCAL_VAR => 1,
24 &LOCAL_ARRAY_VAR => 1,
25 &LOCAL_HASH_VAR => 1,
26  
27 &SPECIFIC_VALUE => 1,
28 2 };
29  
30 2 ▶use constant DEREFERENCE_TOKENS => {
Subroutine BEGIN: called CC=1 SCAR=0.0
31 &SCALAR_DEREFERENCE => 1,
32 &HASH_DEREFERENCE => 1,
33 2 };
34  
35 2 my $variable_name_regex = qr< [\$\@%] \S+ >xms;
36 2 my $regular_expression_regex = qr< [/] ( [^/]+ ) [/] >xms;
37 2 my @description_regexes = (
38 qr< [{] ( [^}]+ ) [}] >xms,
39 qr{ < ( [^>]+ ) > }xms,
40 qr{ [[] ( [^]]+ ) []] }xms,
41 qr{ [(] ( [^)]+ ) [)] }xms,
42 );
43 2 my $description_regex = qr< @{[join '|', @description_regexes]} >xms;
44 2 my $variables_regex = qr<
45 \A
46 \s*
47 (?:
48 ( $variable_name_regex )
49 | $regular_expression_regex
50 )
51 (?: \s* $description_regex )?
52 \s*
53 >xms;
54 2 my $variables_file_line_regex = qr<
55 \A
56 \s*
57 (?:
58 ( $variable_name_regex )
59 | $regular_expression_regex
60 )
61 \s*
62 ( \S (?: .* \S )? )?
63 \s*
64 \z
65 >xms;
66  
67 0 ▶sub evaluate {
Subroutine evaluate: not called CC=33 SCAR=70.2
Pod undocumented
68 0 my ($class, $file, $tokens, $src, $args) = @_;
69  
70 0 my @evil_variables;
71 my @evil_variables_regex;
72 0 ▶ if (my $this_policies_arg = $args->{prohibit_evil_variables}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $this_policies_arg = $args->{"prohibit_evil_variables"}) 0 0 0
73 0 my $variable_specifications = $this_policies_arg->{variables};
74 0 ▶ if ($variable_specifications) {
Branch: 1 branch
BranchTrueFalseTotal
if ($variable_specifications) 0 0 0
75 0 ▶ while (my ($variable, $regex_string, @descrs) = $variable_specifications =~ m/ $variables_regex /xms) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = $variable_specifications =~ / $variables_regex /msx) 0 0 0
76 0 substr $variable_specifications, 0, $+[0], '';
77  
78 0 ▶ if ($variable) {
Branch: 1 branch
BranchTrueFalseTotal
if ($variable) { } 0 0 0
79 0 push @evil_variables, $variable;
80 }
81 else {
82 0 push @evil_variables_regex, $regex_string;
83 }
84 }
85 }
86  
87 0 my $variable_specification_files = $this_policies_arg->{variables_file};
88 0 ▶ if ($variable_specification_files) {
Branch: 1 branch
BranchTrueFalseTotal
if ($variable_specification_files) 0 0 0
89 0 ▶ open my $fh, '<', $variable_specification_files or die "Cannot open file: $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $variable_specification_files or die "Cannot open file: $!" 0 0 0
90 0 ▶ while (my $line = <$fh>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $line = readline $fh)) 0 0 0
91 0 $line =~ s< [#] .* \z ><>xms;
92 0 $line =~ s< \s+ \z ><>xms;
93 0 $line =~ s< \A \s+ ><>xms;
94  
95 0 ▶ next if not $line;
Branch: 1 branch
BranchTrueFalseTotal
unless $line 0 0 0
96  
97 0 ▶ if (my ($variable, $regex_string, $description) =
Branch: 1 branch
BranchTrueFalseTotal
if (() = $line =~ / $variables_file_line_regex /msx) 0 0 0
98 $line =~ m< $variables_file_line_regex >xms) {
99  
100 0 ▶ if ($variable) {
Branch: 1 branch
BranchTrueFalseTotal
if ($variable) { } 0 0 0
101 0 push @evil_variables, $variable;
102 }
103 else {
104 0 push @evil_variables_regex, $regex_string;
105 }
106 }
107 }
108 }
109 }
110  
111 0 my %used_var_with_line_num;
112 0 ▶ for (my $i = 0, my $token_type, my $token_data; my $token = $tokens->[$i]; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $token = $tokens->[$i]) 0 0 0
113 0 $token_type = $token->{type};
114 0 $token_data = $token->{data};
115  
116 0 ▶ if (VAR_TOKENS->{$token_type} || DEREFERENCE_TOKENS->{$token_type}) {
Branch: 1 branch
BranchTrueFalseTotal
if ({"137" => 1,"166" => 1,"167" => 1,"168" => 1,"169" => 1,"191" => 1,"192" => 1,"193" => 1,"194" => 1,"195" => 1,"196" => 1}->{$token_type} or {"114" => 1,"115" => 1}->{$token_type}) 0 0 0
Condition0%0/3
{"137" => 1,"166" => 1,"167" => 1,"168" => 1,"169" => 1,"191" => 1,"192" => 1,"193" => 1,"194" => 1,"195" => 1,"196" => 1}->{$token_type} or {"114" => 1,"115" => 1}->{$token_type}
l!l&&r!l&&!r
000
MC/DC0%0/2
{"137" => 1,"166" => 1,"167" => 1,"168" => 1,"169" => 1,"191" => 1,"192" => 1,"193" => 1,"194" => 1,"195" => 1,"196" => 1}->{$token_type} or {"114" => 1,"115" => 1}->{$token_type}
{"137" => 1,"166" => 1,"167" => 1,"168" => 1,"169" => 1,"191" => 1,"192" => 1,"193" => 1,"194" => 1,"195" => 1,"196" => 1}->{$token_type} {"114" => 1,"115" => 1}->{$token_type}
Truth table0%0/3
A or B
A= {"137" => 1,"166" => 1,"167" => 1,"168" => 1,"169" => 1,"191" => 1,"192" => 1,"193" => 1,"194" => 1,"195" => 1,"196" => 1}->{$token_type}B= {"114" => 1,"115" => 1}->{$token_type}
ABresult
1X1
011
000
117 0 my $var = $token_data;
118 0 my $line = $token->{line};
119  
120 0 my $opener;
121 my $closer;
122 0 ▶ if (DEREFERENCE_TOKENS->{$token_type}) { # XXX workaround
Branch: 2 branches
BranchTrueFalseTotal
if ({114 => 1, 115 => 1}->{$token_type}) { } 0 0 0
elsif ($token_type == 137 and $token_data eq '$^') { } 0 0 0
123 0 $opener = LEFT_BRACE;
124 0 $closer = RIGHT_BRACE;
125 }
126 ▶ elsif ($token_type == SPECIFIC_VALUE && $token_data eq '$^') { # XXX ad hoc
Condition0%0/3
$token_type == 137 and $token_data eq "\$^"
!ll&&!rl&&r
000
MC/DC0%0/2
$token_type == 137 and $token_data eq "\$^"
$token_type == 137 $token_data eq "\$^"
Truth table0%0/3
A and B
A= $token_type == 137B= $token_data eq "\$^"
ABresult
0X0
100
111
127 0 $token = $tokens->[++$i];
128 0 $var .= $token->{data};
129 0 $used_var_with_line_num{$var} = $line;
130 0 next;
131 }
132 else {
133 0 $token = $tokens->[++$i];
134 0 $token_type = $token->{type};
135  
136 0 ▶ if ($token_type == LEFT_BRACE) {
Branch: 2 branches
BranchTrueFalseTotal
if ($token_type == 109) { } 0 0 0
elsif ($token_type == 111) { } 0 0 0
137 0 $opener = LEFT_BRACE;
138 0 $closer = RIGHT_BRACE;
139 }
140 elsif ($token_type == LEFT_BRACKET) {
141 0 $opener = LEFT_BRACKET;
142 0 $closer = RIGHT_BRACKET;
143 }
144 else {
145 0 $used_var_with_line_num{$var} = $line;
146 0 next;
147 }
148  
149 0 $var .= $token->{data}; # data of opener
150 }
151  
152 0 my $left_bracket_num = 1;
153 0 ▶ for ($i++; $token = $tokens->[$i]; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($token = $tokens->[$i]) 0 0 0
154 0 $token_type = $token->{type};
155  
156 0 $var .= $token->{data};
157  
158 0 ▶ if ($token_type == $opener) {
Branch: 2 branches
BranchTrueFalseTotal
if ($token_type == $opener) { } 0 0 0
elsif ($token_type == $closer) { } 0 0 0
159 0 $left_bracket_num++;
160 }
161 elsif ($token_type == $closer) {
162 0 ▶ last if --$left_bracket_num <= 0;
Branch: 1 branch
BranchTrueFalseTotal
if --$left_bracket_num <= 0 0 0 0
163 }
164 }
165 0 $used_var_with_line_num{$var} = $line;
166 }
167 }
168  
169 0 my @violations;
170 0 for my $evil_var (@evil_variables) {
171 0 (my $alt_evil_var = $evil_var) =~ s/\A[\%\@]/\$/;
172  
173 0 my $line = $used_var_with_line_num{$evil_var};
174 0 my $used_var = $evil_var;
175 0 ▶ if (! $line && $alt_evil_var) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $line and $alt_evil_var) 0 0 0
Condition0%0/3
not $line and $alt_evil_var
!ll&&!rl&&r
000
MC/DC0%0/2
not $line and $alt_evil_var
not $line $alt_evil_var
Truth table0%0/3
not(A) and B
A= not $lineB= $alt_evil_var
ABresult
0X0
100
111
176 0 $line = $used_var_with_line_num{$alt_evil_var};
177 0 $used_var = $alt_evil_var;
178  
179 0 ▶ if (! $line) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($line) 0 0 0
180 0 for my $_used_var (keys %used_var_with_line_num) {
181 0 ▶ if ($line = $_used_var =~ /\A\Q$alt_evil_var\E [\[\{]/x) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line = $_used_var =~ /\A\Q$alt_evil_var\E [\[\{]/x) 0 0 0
182 0 $used_var = $_used_var;
183 0 last;
184 }
185 }
186 }
187 }
188  
189 0 ▶ if ($line) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line) 0 0 0
190 0 push @violations, {
191 filename => $file,
192 line => $line,
193 description => DESC,
194 explanation => sprintf(EXPL, $used_var),
195 policy => __PACKAGE__,
196 };
197 }
198 }
199  
200 0 for my $regex (@evil_variables_regex) {
201 0 for my $used_var (keys %used_var_with_line_num) {
202 0 ▶ if ($used_var =~ /$regex/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($used_var =~ /$regex/) 0 0 0
203 push @violations, {
204 filename => $file,
205 0 line => $used_var_with_line_num{$used_var},
206 description => DESC,
207 explanation => sprintf(EXPL, $used_var),
208 policy => __PACKAGE__,
209 };
210 }
211 }
212 }
213  
214 0 return \@violations;
215 }
216  
217 2 1;
218  
« Perl/Lint/Policy/ValuesAndExpressions/ProhibitLeadingZeros.pm ↑ Summary Perl/Lint/Policy/Variables/RequireNegativeIndices.pm »