« 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
| |||||||||||||||||||||||||||||
| 73 | 0 | my $variable_specifications = $this_policies_arg->{variables}; | |||||||||||||||||||||||||||
| 74 | 0 | ▶ | if ($variable_specifications) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 75 | 0 | ▶ | while (my ($variable, $regex_string, @descrs) = $variable_specifications =~ m/ $variables_regex /xms) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 76 | 0 | substr $variable_specifications, 0, $+[0], ''; | |||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | 0 | ▶ | if ($variable) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 89 | 0 | ▶ | open my $fh, '<', $variable_specification_files or die "Cannot open file: $!"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 90 | 0 | ▶ | while (my $line = <$fh>) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | 0 | ▶ | if (my ($variable, $regex_string, $description) = | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 98 | $line =~ m< $variables_file_line_regex >xms) { | ||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | 0 | ▶ | if ($variable) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
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}
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}
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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 "\$^"
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 "\$^"
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 154 | 0 | $token_type = $token->{type}; | |||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||
| 156 | 0 | $var .= $token->{data}; | |||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||
| 158 | 0 | ▶ | if ($token_type == $opener) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 159 | 0 | $left_bracket_num++; | |||||||||||||||||||||||||||
| 160 | } | ||||||||||||||||||||||||||||
| 161 | elsif ($token_type == $closer) { | ||||||||||||||||||||||||||||
| 162 | 0 | ▶ | last if --$left_bracket_num <= 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
Condition0%0/3
not $line and $alt_evil_var
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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 182 | 0 | $used_var = $_used_var; | |||||||||||||||||||||||||||
| 183 | 0 | last; | |||||||||||||||||||||||||||
| 184 | } | ||||||||||||||||||||||||||||
| 185 | } | ||||||||||||||||||||||||||||
| 186 | } | ||||||||||||||||||||||||||||
| 187 | } | ||||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||
| 189 | 0 | ▶ | if ($line) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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 | |||||||||||||||||||||||||||||