blib/lib/Perl/Lint/Policy/ValuesAndExpressions/ProhibitMagicNumbers.pm

filter: Statementstmt 83.3% 10 / 12 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 4 / 4 Podpod n/a totaltotal 87.5% 14 / 16 SCAR 0.0
CC
1
Cov
83%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
« blib/lib/Perl/Lint/Policy/ValuesAndExpressions/ProhibitInterpolationOfLiterals.pm ↑ Summary blib/lib/Perl/Lint/Policy/Variables/ProhibitEvilVariables.pm »
1 package Perl::Lint::Policy::ValuesAndExpressions::ProhibitMagicNumbers;
2 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶no warnings qw/numeric/;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use Perl::Lint::Constants::Type;
Subroutine BEGIN: called CC=1 SCAR=2.6
6 use parent "Perl::Lint::Policy";
7  
8 use constant {
9 DESC => 'Unnamed numeric literals make code less maintainable', # TODO
10 EXPL => 'Unnamed numeric literals make code less maintainable',
11 };
12  
13 my $file;
14 my $tokens;
15  
16 my %allowed_values;
17 my %allowed_types;
18 my %constant_creator_subroutines;
19 my $allow_to_the_right_of_a_fat_comma;
20 my $is_readonly_array1_ctx;
21  
22 sub evaluate {
23 my $class = shift;
24 $file = shift;
25 $tokens = shift;
26 my ($src, $args) = @_;
27  
28 %allowed_values = (
29 0 => 1,
30 1 => 1,
31 2 => 1,
32 );
33  
34 %allowed_types = (
35 Int => 1,
36 Float => 1,
37 );
38  
39 %constant_creator_subroutines = (
40 plan => 1,
41 Readonly => 1,
42 const => 1,
43 );
44  
45 $allow_to_the_right_of_a_fat_comma = 1;
46  
47 # initializing
48 if (my $this_policies_arg = $args->{prohibit_magic_numbers}) {
49 $allow_to_the_right_of_a_fat_comma =
50 $this_policies_arg->{allow_to_the_right_of_a_fat_comma} // 1;
51  
52 my $allowed_values = $this_policies_arg->{allowed_values};
53 if (defined $allowed_values) {
54 delete $allowed_values{2}; # remove `2` from allowed list when allowed_values is specified
55  
56 for my $allowed_value (split /\s+/, $allowed_values) {
57 my ($begin, $end) = split /[.][.]/, $allowed_value; # for range notation (e.g. `1..42`)
58 if (defined $begin && defined $end) {
59 # used range notation
60 my ($delta) = $end =~ /:by [(] (.+) [)] \z/x; # for range notation with by (e.g. `-2.0..2.0:by(0.5)`)
61 $delta //= 1; # default delta
62  
63 for (my $num = $begin; $num <= $end; $num += $delta) {
64 $allowed_values{$num} = 1;
65 }
66 }
67 else {
68 # not used range notation
69 $allowed_values{$allowed_value} = 1;
70 }
71 }
72 }
73  
74 my $allowed_types = $this_policies_arg->{allowed_types};
75 if (defined $allowed_types) {
76 delete $allowed_types{Float}; # remove `Float` from allowed types list when allowed_types is specified
77  
78 for my $allowed_type (split /\s+/, $allowed_types) {
79 $allowed_types{$allowed_type} = 1;
80 }
81 }
82  
83 my $constant_creator_subroutines = $this_policies_arg->{constant_creator_subroutines};
84 if (defined $constant_creator_subroutines) {
85 for my $sub (split /\s+/, $constant_creator_subroutines) {
86 $constant_creator_subroutines{$sub} = 1;
87 }
88 }
89 }
90  
91 my @violations;
92 for (my $i = 0, my $token_type, my $token_data; my $token = $tokens->[$i]; $i++) {
93 $is_readonly_array1_ctx = 0;
94  
95 $token_type = $token->{type};
96 $token_data = $token->{data};
97  
98 if (
99 $token_type == USE_DECL ||
100 $token_type == REQUIRE_DECL ||
101 ($token_type == KEY && $constant_creator_subroutines{$token_data})
102 ) {
103 for ($i++; $token = $tokens->[$i]; $i++) {
104 $token_type = $token->{type};
105 if ($token_type == SEMI_COLON) {
106 last;
107 }
108 }
109 next;
110 }
111  
112 if ($token_type == NAMESPACE && $token_data eq 'Readonly') {
113 $token = $tokens->[++$i] or last;
114  
115 if ($token->{type} == NAMESPACE_RESOLVER) {
116 $token = $tokens->[++$i] or last;
117 $token_data = $token->{data};
118  
119 if ($token->{type} == NAMESPACE) {
120 if ($token_data eq 'Scalar' || $token_data eq 'Array') {
121 # when `Readonly::Scalar` or `Readonly::Array`,
122 # skip tokens to semi colon (means don't evaluate).
123 for ($i++; $token = $tokens->[$i]; $i++) {
124 $token_type = $token->{type};
125 if ($token_type == SEMI_COLON) {
126 last;
127 }
128 }
129 next;
130 }
131 elsif ($token_data eq 'Array1') {
132 # when `Readonly::Array1`
133 $i += 2; # skip to assigning token
134  
135 $token = $tokens->[$i];
136 $token_type = $token->{type};
137 $token_data = $token->{data};
138  
139 $is_readonly_array1_ctx = 1;
140  
141 # no break!
142 }
143 }
144 }
145 }
146  
147 # for the $VERSION variable
148 if (
149 $token_data eq '$VERSION' &&
150 ($token_type == VAR || $token_type == GLOBAL_VAR)
151 ) {
152 # skip to end of line. Don't evaluate it.
153 for ($i++; $token = $tokens->[$i]; $i++) {
154 last if $token->{type} == SEMI_COLON;
155 }
156 next;
157 }
158  
159 if (
160 $token_type == ASSIGN ||
161 ($token_type == ARROW && (!$allow_to_the_right_of_a_fat_comma || $is_readonly_array1_ctx))
162 ) {
163 push @violations, @{$class->_scan(\$i)};
164 next;
165 }
166  
167 if ($token_type == FUNCTION) {
168 $token = $tokens->[++$i];
169  
170 my $statement = [];
171 my @statements = ();
172  
173 my $lbnum = 1;
174 for ($i++; $token = $tokens->[$i]; $i++) {
175 $token_type = $token->{type};
176  
177 if ($token_type == LEFT_BRACE) {
178 $lbnum++;
179 }
180 elsif ($token_type == RIGHT_BRACE) {
181 last if --$lbnum <= 0;
182 }
183 elsif ($token_type == SEMI_COLON) {
184 push @statements, $statement;
185 $statement = [];
186 }
187 else {
188 push @$statement, $token;
189 }
190 }
191  
192 if (scalar @statements > 1) { # when exists multiple statements in function
193 my $last_statement = pop @statements;
194  
195 my $return_value_token = pop @$last_statement or next;
196 if ($return_value_token->{type} == RETURN) {
197 $return_value_token = pop @$last_statement or next;
198 }
199  
200 my $invalid_token;
201 if ($return_value_token->{type} == INT) {
202 $invalid_token = $class->_validate_int_token($return_value_token);
203 }
204 elsif ($return_value_token->{type} == DOUBLE) {
205 $invalid_token = $class->_validate_doble_token($return_value_token);
206 }
207  
208 if ($invalid_token) {
209 push @violations, {
210 filename => $file,
211 line => $token->{line},
212 description => DESC,
213 explanation => EXPL,
214 policy => __PACKAGE__,
215 };
216 }
217 }
218  
219 next;
220 }
221  
222 if ($token_type == FOR_STATEMENT || $token_type == FOREACH_STATEMENT) {
223 for ($i++; $token = $tokens->[$i]; $i++) {
224 $token_type = $token->{type};
225  
226 if ($token_type == SEMI_COLON || $token_type == LEFT_BRACE) {
227 last;
228 }
229 elsif ($token_type == SLICE) { # e.g. for my $foo (1..42)
230 my $begin = $tokens->[$i-1] or last;
231 my $end = $tokens->[$i+1] or last;
232 if ($begin->{type} == INT && $end->{type} == INT) {
233 if (!$allowed_values{$begin->{data}} || !$allowed_values{$end->{data}}) {
234 push @violations, {
235 filename => $file,
236 line => $token->{line},
237 description => DESC,
238 explanation => EXPL,
239 policy => __PACKAGE__,
240 };
241 }
242 }
243 }
244 }
245 next;
246 }
247  
248 # for index of array
249 if ($token_type == LEFT_BRACKET) {
250 my $next_token = $tokens->[$i+1] or last;
251 if ($next_token->{type} == INT) {
252 my $int_token = $next_token;
253 $next_token = $tokens->[$i+2] or last;
254  
255 my $invalid_token;
256 if ($next_token->{type} == RIGHT_BRACKET) {
257 my $num = $int_token->{data} + 0;
258 if (!$allowed_values{$num} && $num ne -1) { # -1 is allowed specially when it is used as index of array
259 $invalid_token = $int_token;
260 }
261 }
262 elsif ($next_token->{type} != COMMA) { # if it is not enumeration (probably it is any handling for index of array)
263 $invalid_token = $next_token;
264 }
265  
266 if ($invalid_token) {
267 push @violations, {
268 filename => $file,
269 line => $invalid_token->{line},
270 description => DESC,
271 explanation => EXPL,
272 policy => __PACKAGE__,
273 };
274 }
275 }
276 next;
277 }
278 }
279  
280 return \@violations;
281 }
282  
283 my $is_in_assigning_context;
284 sub _scan {
285 my ($class, $i) = @_;
286  
287 my $token = $tokens->[$$i] or return;
288 my $token_type = $token->{type};
289 my $token_data = $token->{data};
290  
291 if ($token_type == ASSIGN) {
292 $is_in_assigning_context = 1;
293  
294 $token = $tokens->[++$$i] or return;
295 $token_type = $token->{type};
296 $token_data = $token->{data};
297 }
298 elsif ($token_type == ARROW) {
299 $is_in_assigning_context = 0;
300 if (!$allow_to_the_right_of_a_fat_comma || $is_readonly_array1_ctx) {
301 $is_in_assigning_context = 1;
302 }
303  
304 $token = $tokens->[++$$i] or return;
305 $token_type = $token->{type};
306 $token_data = $token->{data};
307 }
308  
309 my $invalid_token;
310  
311 my @violations;
312 if ($token_type == DOUBLE) {
313 $invalid_token = $class->_validate_doble_token($token, $$i);
314 }
315 elsif ($token_type == INT) {
316 $invalid_token = $class->_validate_int_token($token);
317 }
318 elsif ($token_type == LEFT_PAREN) {
319 my $lpnum = 1;
320 for ($$i++; $token = $tokens->[$$i]; $$i++) {
321 $token_type = $token->{type};
322 if ($token_type == LEFT_PAREN) {
323 $lpnum++;
324 }
325 elsif ($token_type == RIGHT_PAREN) {
326 last if --$lpnum <= 0;
327 }
328 else {
329 push @violations, @{$class->_scan($i)};
330 }
331 }
332 }
333 elsif ($token_type == LEFT_BRACKET) {
334 my $lbnum = 1;
335  
336 for ($$i++; $token = $tokens->[$$i]; $$i++) {
337 $token_type = $token->{type};
338 if ($token_type == LEFT_BRACKET) {
339 $lbnum++;
340 }
341 elsif ($token_type == RIGHT_BRACKET) {
342 last if --$lbnum <= 0;
343 }
344 else {
345 push @violations, @{$class->_scan($i)};
346 }
347 }
348 }
349  
350 if ($is_in_assigning_context && $invalid_token) {
351 push @violations, {
352 filename => $file,
353 line => $invalid_token->{line},
354 description => DESC,
355 explanation => EXPL,
356 policy => __PACKAGE__,
357 };
358 }
359  
360 return \@violations;
361 }
362  
363 sub _validate_int_token {
364 my ($class, $token) = @_;
365  
366 my $token_data = $token->{data};
367  
368 if (my ($base_type) = $token_data =~ /\A[0-9]([b0xe]).+\z/) {
369 if ($1 eq 'b') {
370 return $token if !$allowed_types{Binary};
371 }
372 elsif ($1 eq '0') {
373 return $token if !$allowed_types{Octal};
374 }
375 elsif ($1 eq 'x') {
376 return $token if !$allowed_types{Hex};
377 }
378 elsif ($1 eq 'e') {
379 return $token if !$allowed_types{Exp};
380 }
381 }
382  
383 if (!$allowed_types{Int}) {
384 return $token;
385 }
386  
387 if (!$allowed_values{all_integers} && !$allowed_values{$token_data+0}) { # `+0` to convert to number
388 return $token;
389 }
390  
391 return;
392 }
393  
394 sub _validate_doble_token {
395 my ($class, $token, $i) = @_;
396  
397 my $token_data = $token->{data};
398  
399 if ($i && $allowed_types{Float} && $allowed_values{$token_data+0}) { # `+0` to convert to number
400 my $next_token = $tokens->[$i+1];
401 if ($next_token && $next_token->{type} == DOUBLE) {
402 return $next_token;
403 }
404 }
405 elsif (!$allowed_values{all_integers} || $token_data !~ /[.]0+\z/) {
406 return $token;
407 }
408  
409 return;
410 }
411  
412 1;
413  
« blib/lib/Perl/Lint/Policy/ValuesAndExpressions/ProhibitInterpolationOfLiterals.pm ↑ Summary blib/lib/Perl/Lint/Policy/Variables/ProhibitEvilVariables.pm »