File Coverage

blib/lib/Perl/Lint/Policy/ValuesAndExpressions/RequireNumberSeparators.pm
Criterion Covered Total %
statement 39 39 100.0
branch 10 10 100.0
condition 5 9 55.5
subroutine 6 6 100.0
pod 0 1 0.0
total 60 65 92.3


line stmt bran cond sub pod time code
1             package Perl::Lint::Policy::ValuesAndExpressions::RequireNumberSeparators;
2 134     134   67836 use strict;
  134         195  
  134         5395  
3 134     134   509 use warnings;
  134         167  
  134         2563  
4 134     134   764 use Perl::Lint::Constants::Type;
  134         178  
  134         58051  
5 134     134   594 use parent "Perl::Lint::Policy";
  134         178  
  134         583  
6              
7             use constant {
8 134         36835 DESC => 'Long number not separated with underscores',
9             EXPL => [59],
10 134     134   6597 };
  134         183  
11              
12             sub evaluate {
13 7     7 0 10 my ($class, $file, $tokens, $src, $args) = @_;
14              
15 7         8 my $min_value = 100_000;
16 7 100       15 if (my $this_policies_arg = $args->{require_number_separators}) {
17 2   33     6 $min_value = $this_policies_arg->{min_value} || $min_value;
18             }
19              
20 7         6 my @violations;
21 7         21 for (my $i = 0; my $token = $tokens->[$i]; $i++) {
22 310         219 my $token_type = $token->{type};
23              
24 310 100 100     889 if ($token_type == INT || $token_type == DOUBLE) {
25 60         107 (my $num = $token->{data}) =~ s/\A[+-]//;
26              
27 60         106 my ($decimal_part, $fractional_part) = split /\./, $num;
28 60         77 my @decimals = split /_/, $decimal_part;
29 60         56 my @fractionals = ();
30              
31 60         32 my $joined;
32 60 100       77 if (defined $fractional_part) {
33 26         23 @fractionals = split /_/, $fractional_part;
34 26         47 $joined = join('', @decimals) . '.' . join('', @fractionals);
35             } else {
36 34         47 $joined = join '', @decimals;
37             }
38              
39 60 100 33     2127 if ((eval($joined) // -$min_value - 1) < $min_value) { ## no critic
40             # ~~~~~~~~~~~~~~~ If reach here, $joined is not a number
41 22         81 next;
42             }
43              
44 38         70 for my $part (@decimals, @fractionals) {
45 63 100       1886 if (eval($part) >= 1000) { ## no critic
46             push @violations, {
47             filename => $file,
48             line => $token->{line},
49 28         85 description => DESC,
50             explanation => EXPL,
51             policy => __PACKAGE__,
52             };
53 28         88 last;
54             }
55             }
56             }
57             }
58              
59 7         24 return \@violations;
60             }
61              
62             1;
63