Perl/Lint/Policy/Variables/ProhibitPunctuationVars.pm

filter: Statementstmt 40.3% 48 / 119 Branchbran 18.4% 14 / 76 Conditioncond 6.2% 1 / 16 MC/DCmcdc 0.0% 0 / 10 Subroutinesub 100% 8 / 8 Podpod 0.0% 0 / 1 totaltotal 30.8% 71 / 230 CC 46 SCAR 69.0
CC
46
Cov
30%
evaluate
1147.2
BEGIN
1.0
BEGIN
1.0
CRAP 995.8
« Perl/Lint/Policy/InputOutput/ProhibitTwoArgOpen.pm ↑ Summary Perl/Lint/Policy/ValuesAndExpressions/ProhibitLeadingZeros.pm »
1 package Perl::Lint::Policy::Variables::ProhibitPunctuationVars;
2 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use Compiler::Lexer;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use List::Util qw/any/;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use Perl::Lint::Constants::Type;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 3 ▶use parent "Perl::Lint::Policy";
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 3 ▶use constant {
Subroutine BEGIN: called CC=1 SCAR=0.0
10 3 DESC => 'Magic punctuation variable %s used',
11 EXPL => [79],
12 3 };
13  
14 3 my %var_token_types = (
15 &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  
24 3 my %expands_regexp_token_types = (
25 &REG_EXEC => 1,
26 &REG_DECL => 1,
27 &REG_DOUBLE_QUOTE => 1,
28 );
29  
30 3 my %special_variable_token_types = (
31 &SPECIFIC_VALUE => 1,
32 &ARRAY_SIZE => 1,
33 );
34  
35 3 my %magic_variables = (
36 '$1' => 1, '$2' => 1, '$3' => 1,
37 '$4' => 1, '$5' => 1, '$6' => 1,
38 '$7' => 1, '$8' => 1, '$9' => 1,
39 '$_' => 1, '$&' => 1, '$`' => 1,
40 '$+' => 1, '@+' => 1, '@*' => 1,
41 '%+' => 1, '$*' => 1, '$.' => 1,
42 '$/' => 1, '$|' => 1, '$(' => 1,
43 '$"' => 1, '$;' => 1, '$%' => 1,
44 '$=' => 1, '$-' => 1, '@-' => 1,
45 '%-' => 1, '$)' => 1, '$~' => 1,
46 '$^' => 1, '$:' => 1, '$?' => 1,
47 '$!' => 1, '%!' => 1, '$@' => 1,
48 '$$' => 1, '$<' => 1, '$>' => 1,
49 '$0' => 1, '$[' => 1, '$]' => 1,
50 '@_' => 1,
51  
52 q{$'} => 1,
53  
54 '$^L' => 1, '$^A' => 1, '$^E' => 1,
55 '$^C' => 1, '$^D' => 1, '$^F' => 1,
56 '$^H' => 1, '$^I' => 1, '$^M' => 1,
57 '$^N' => 1, '$^O' => 1, '$^P' => 1,
58 '$^R' => 1, '$^S' => 1, '$^T' => 1,
59 '$^V' => 1, '$^W' => 1, '$^X' => 1,
60 '%^H' => 1,
61  
62 '$\\' => 1,
63 '$::|' => 1,
64 '$}' => 1,
65 '$,' => 1,
66 '$#' => 1,
67 '$#+' => 1,
68 '$#-' => 1,
69 );
70  
71 3 my %ignore_for_interpolation = (
72 q{$'} => 1,
73 q{$$} => 1,
74 q{$#} => 1,
75 q{$:} => 1,
76 );
77  
78 2 ▶sub evaluate {
Subroutine evaluate: called CC=46 SCAR=70.5
Pod undocumented
79 2 my ($class, $file, $tokens, $src, $args) = @_;
80  
81 2 my $string_mode = '';
82 2 my %exempt_vars = (
83 '$_' => 1, '@_' => 1, '$]' => 1,
84 '$1' => 1, '$2' => 1, '$3' => 1,
85 '$4' => 1, '$5' => 1, '$6' => 1,
86 '$7' => 1, '$8' => 1, '$9' => 1,
87 );
88  
89 2 ▶ if (my $this_policies_arg = $args->{prohibit_punctuation_vars}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $this_policies_arg = $args->{"prohibit_punctuation_vars"}) 0 2 2
90 0 ▶ $string_mode = $this_policies_arg->{string_mode} || '';
Condition0%0/2
$this_policies_arg->{'string_mode'} || ''
l!l
00
MC/DC0%0/1
$this_policies_arg->{'string_mode'} || ''
$this_policies_arg->{'string_mode'}
Truth table0%0/2
A
A= $this_policies_arg->{'string_mode'}
Aresult
11
00
91 0 ▶ if ($string_mode eq 'thorough') {
Branch: 1 branch
BranchTrueFalseTotal
if ($string_mode eq "thorough") 0 0 0
92 0 %exempt_vars = ();
93 }
94  
95 0 ▶ for my $exempt_var (split(/\s+/, $this_policies_arg->{allow} || '')) {
Condition0%0/2
$this_policies_arg->{'allow'} || ''
l!l
00
MC/DC0%0/1
$this_policies_arg->{'allow'} || ''
$this_policies_arg->{'allow'}
Truth table0%0/2
A
A= $this_policies_arg->{'allow'}
Aresult
11
00
96 0 $exempt_vars{$exempt_var} = 1;
97 }
98 }
99  
100 2 my $lexer_for_str = Compiler::Lexer->new;
101  
102 2 my @violations;
103 2 ▶ for (
Branch: 1 branch
BranchTrueFalseTotal
if (my $token = $tokens->[$i]) 51 2 53
104 my $i = 0, my $token_type, my $token_data, my $is_ref = 0, my $is_raw_heredoc_tag = 0;
105 my $token = $tokens->[$i];
106 $i++
107 ) {
108 51 $token_type = $token->{type};
109 51 $token_data = $token->{data};
110  
111 51 ▶ if ($special_variable_token_types{$token_type}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($special_variable_token_types{$token_type}) 0 51 51
112 0 ▶ if ($is_ref) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_ref) 0 0 0
113 0 $is_ref = 0;
114 0 next;
115 }
116  
117 0 ▶ if ($exempt_vars{$token_data}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($exempt_vars{$token_data}) 0 0 0
118 0 next;
119 }
120  
121 0 ▶ if (! $magic_variables{$token_data}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($magic_variables{$token_data}) 0 0 0
122 0 next;
123 }
124  
125 push @violations, {
126 filename => $file,
127 line => $token->{line},
128 0 description => sprintf(DESC, $token_data),
129 explanation => EXPL,
130 policy => __PACKAGE__,
131 };
132 0 next;
133 }
134  
135 51 ▶ if ($var_token_types{$token_type}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($var_token_types{$token_type}) 2 49 51
136 2 ▶ if ($is_ref) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_ref) 0 2 2
137 0 $is_ref = 0;
138 0 next;
139 }
140  
141 2 ▶ if ($exempt_vars{$token_data}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($exempt_vars{$token_data}) 0 2 2
142 0 next;
143 }
144  
145 2 ▶ if (! $magic_variables{$token_data}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($magic_variables{$token_data}) 2 0 2
146 2 next;
147 }
148  
149 0 ▶ if (substr($token_data, 1, 1) =~ /\A[^a-zA-Z]\Z/) {
Branch: 1 branch
BranchTrueFalseTotal
if (substr($token_data, 1, 1) =~ /\A[^a-zA-Z]\Z/) 0 0 0
150 push @violations, {
151 filename => $file,
152 line => $token->{line},
153 0 description => sprintf(DESC, $token_data),
154 explanation => EXPL,
155 policy => __PACKAGE__,
156 };
157 }
158 0 next;
159 }
160  
161 49 ▶ if ($token_type == REF) {
Branch: 1 branch
BranchTrueFalseTotal
if ($token_type == 10) 0 49 49
162 0 $is_ref = 1;
163 0 next;
164 }
165  
166 49 ▶ if ($token_type == HERE_DOCUMENT_RAW_TAG) {
Branch: 1 branch
BranchTrueFalseTotal
if ($token_type == 177) 0 49 49
167 0 $is_raw_heredoc_tag = 1;
168 0 next;
169 }
170  
171 49 ▶ if ($token_type == HERE_DOCUMENT_END) {
Branch: 1 branch
BranchTrueFalseTotal
if ($token_type == 182) 0 49 49
172 0 $is_raw_heredoc_tag = 0;
173 0 next;
174 }
175  
176 49 ▶ if ($expands_regexp_token_types{$token_type}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($expands_regexp_token_types{$token_type}) 0 49 49
177 0 $i += 2;
178 0 $token = $tokens->[$i];
179 0 ▶ if ($token->{type} != REG_EXP) { # when content is empty
Branch: 1 branch
BranchTrueFalseTotal
if ($token->{"type"} != 187) 0 0 0
180 0 next;
181 }
182 0 $token_data = $token->data;
183 0 $token_type = STRING;
184 } # fall through
185  
186 49 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if ($token_type == 172 or $token_type == 174) 0 49 49
Condition33%1/3
$token_type == 172 or $token_type == 174
l!l&&r!l&&!r
0049
MC/DC0%0/2
$token_type == 172 or $token_type == 174
$token_type == 172 $token_type == 174
Truth table33%1/3
A or B
A= $token_type == 172B= $token_type == 174
ABresult
1X1
011
000
187 $token_type == STRING ||
188 $token_type == EXEC_STRING
189 # ($token_type == HERE_DOCUMENT && $is_raw_heredoc_tag)
190 ) {
191 0 ▶ if ($string_mode eq 'disable') {
Branch: 1 branch
BranchTrueFalseTotal
if ($string_mode eq "disable") 0 0 0
192 0 next;
193 }
194  
195 0 my $parts = $lexer_for_str->tokenize($token_data);
196 0 my $ref_count = 0;
197 0 ▶ for (my $j = 0, my $part_type, my $used_var; my $part = $parts->[$j]; $j++) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $part = $parts->[$j]) 0 0 0
198 0 $part_type = $part->{type};
199 0 $used_var = $part->{data};
200  
201 0 ▶ if ($part_type == REF) {
Branch: 1 branch
BranchTrueFalseTotal
if ($part_type == 10) 0 0 0
202 0 $ref_count++;
203 0 next;
204 }
205  
206 0 ▶ if ($ref_count % 2 != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ref_count % 2 != 0) 0 0 0
207 0 $ref_count = 0;
208 0 next;
209 }
210  
211 0 ▶ if ($part_type == SPECIFIC_VALUE) {
Branch: 2 branches
BranchTrueFalseTotal
if ($part_type == 137) { } 0 0 0
elsif ($part_type != 66) { } 0 0 0
212 0 ▶ if ($used_var eq '$:') {
Branch: 1 branch
BranchTrueFalseTotal
if ($used_var eq "\$:") 0 0 0
213 0 $part = $parts->[$j+1];
214  
215 0 ▶ if ($part && $part->{type} == COLON) {
Branch: 1 branch
BranchTrueFalseTotal
if ($part and $part->{"type"} == 105) 0 0 0
Condition0%0/3
$part and $part->{"type"} == 105
!ll&&!rl&&r
000
MC/DC0%0/2
$part and $part->{"type"} == 105
$part $part->{"type"} == 105
Truth table0%0/3
A and B
A= $partB= $part->{"type"} == 105
ABresult
0X0
100
111
216 0 $part = $parts->[$j+2];
217 0 ▶ if ($part && $part->{type} == BIT_OR) {
Branch: 1 branch
BranchTrueFalseTotal
if ($part and $part->{'type'} == 13) { } 0 0 0
Condition0%0/3
$part and $part->{"type"} == 13
!ll&&!rl&&r
000
MC/DC0%0/2
$part and $part->{"type"} == 13
$part $part->{"type"} == 13
Truth table0%0/3
A and B
A= $partB= $part->{"type"} == 13
ABresult
0X0
100
111
218 0 $used_var = '$::|';
219 }
220 else {
221 0 next;
222 }
223 }
224 }
225 # TODO
226 # elsif ($used_var eq q{$'}) {
227 # $part = $parts->[$j+1];
228 # if ($part && $part->{type} == KEY) {
229 # # next;
230 # }
231 # }
232 }
233 elsif ($part_type != ARRAY_SIZE) {
234 0 ▶ if (!$var_token_types{$part_type}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($var_token_types{$part_type}) 0 0 0
235 0 next;
236 }
237  
238 0 $part = $parts->[++$j];
239 0 ▶ if ($part) {
Branch: 1 branch
BranchTrueFalseTotal
if ($part) 0 0 0
240 0 ▶ if ($used_var eq '$') {
Branch: 3 branches
BranchTrueFalseTotal
if ($used_var eq '$') { } 0 0 0
elsif ($used_var eq '@') { } 0 0 0
elsif ($used_var eq '%-') { } 0 0 0
241 0 ▶ if ($part->{type} == RIGHT_BRACE) {
Branch: 1 branch
BranchTrueFalseTotal
if ($part->{"type"} == 110) 0 0 0
242 0 $used_var = '$}';
243 }
244 }
245 elsif ($used_var eq '@') {
246 0 ▶ if ($part->{type} == MUL) {
Branch: 1 branch
BranchTrueFalseTotal
if ($part->{"type"} == 3) 0 0 0
247 0 $used_var = '@*';
248 }
249 }
250 elsif ($used_var eq '%-') {
251 0 ▶ if ($part->{type} == INT) { # for formatting. e.g. "%-04f"
Branch: 1 branch
BranchTrueFalseTotal
if ($part->{"type"} == 170) 0 0 0
252 0 next;
253 }
254 }
255 }
256 }
257  
258 0 ▶ if ($exempt_vars{$used_var}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($exempt_vars{$used_var}) 0 0 0
259 0 next;
260 }
261  
262 0 ▶ if ($string_mode eq 'simple' && $ignore_for_interpolation{$used_var}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($string_mode eq "simple" and $ignore_for_interpolation{$used_var}) 0 0 0
Condition0%0/3
$string_mode eq "simple" and $ignore_for_interpolation{$used_var}
!ll&&!rl&&r
000
MC/DC0%0/2
$string_mode eq "simple" and $ignore_for_interpolation{$used_var}
$string_mode eq "simple" $ignore_for_interpolation{$used_var}
Truth table0%0/3
A and B
A= $string_mode eq "simple"B= $ignore_for_interpolation{$used_var}
ABresult
0X0
100
111
263 0 next;
264 }
265  
266 0 ▶ if ($magic_variables{$used_var}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($magic_variables{$used_var}) 0 0 0
267 push @violations, {
268 filename => $file,
269 line => $token->{line},
270 0 description => sprintf(DESC, $used_var),
271 explanation => EXPL,
272 policy => __PACKAGE__,
273 };
274 }
275 }
276  
277 0 next;
278 }
279 }
280  
281 2 return \@violations;
282 }
283  
284 3 1;
285  
« Perl/Lint/Policy/InputOutput/ProhibitTwoArgOpen.pm ↑ Summary Perl/Lint/Policy/ValuesAndExpressions/ProhibitLeadingZeros.pm »