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   95281 use strict;
  134         279  
  134         8176  
3 134     134   652 use warnings;
  134         223  
  134         3643  
4 134     134   1008 use Perl::Lint::Constants::Type;
  134         244  
  134         86204  
5 134     134   871 use parent "Perl::Lint::Policy";
  134         244  
  134         827  
6              
7             use constant {
8 134         50291 DESC => 'Long number not separated with underscores',
9             EXPL => [59],
10 134     134   9106 };
  134         263  
11              
12             sub evaluate {
13 7     7 0 17 my ($class, $file, $tokens, $src, $args) = @_;
14              
15 7         11 my $min_value = 100_000;
16 7 100       29 if (my $this_policies_arg = $args->{require_number_separators}) {
17 2   33     8 $min_value = $this_policies_arg->{min_value} || $min_value;
18             }
19              
20 7         10 my @violations;
21 7         40 for (my $i = 0; my $token = $tokens->[$i]; $i++) {
22 310         261 my $token_type = $token->{type};
23              
24 310 100 100     1009 if ($token_type == INT || $token_type == DOUBLE) {
25 60         136 (my $num = $token->{data}) =~ s/\A[+-]//;
26              
27 60         138 my ($decimal_part, $fractional_part) = split /\./, $num;
28 60         108 my @decimals = split /_/, $decimal_part;
29 60         57 my @fractionals = ();
30              
31 60         54 my $joined;
32 60 100       100 if (defined $fractional_part) {
33 26         39 @fractionals = split /_/, $fractional_part;
34 26         54 $joined = join('', @decimals) . '.' . join('', @fractionals);
35             } else {
36 34         61 $joined = join '', @decimals;
37             }
38              
39 60 100 33     2561 if ((eval($joined) // -$min_value - 1) < $min_value) { ## no critic
40             # ~~~~~~~~~~~~~~~ If reach here, $joined is not a number
41 22         77 next;
42             }
43              
44 38         80 for my $part (@decimals, @fractionals) {
45 63 100       2179 if (eval($part) >= 1000) { ## no critic
46 28         124 push @violations, {
47             filename => $file,
48             line => $token->{line},
49             description => DESC,
50             explanation => EXPL,
51             policy => __PACKAGE__,
52             };
53 28         109 last;
54             }
55             }
56             }
57             }
58              
59 7         34 return \@violations;
60             }
61              
62             1;
63