File Coverage

blib/lib/Perl/Lint/Policy/BuiltinFunctions/ProhibitReverseSortBlock.pm
Criterion Covered Total %
statement 38 38 100.0
branch 20 20 100.0
condition 15 18 83.3
subroutine 6 6 100.0
pod 0 1 0.0
total 79 83 95.1


line stmt bran cond sub pod time code
1             package Perl::Lint::Policy::BuiltinFunctions::ProhibitReverseSortBlock;
2 134     134   97213 use strict;
  134         303  
  134         5176  
3 134     134   643 use warnings;
  134         263  
  134         4150  
4 134     134   1030 use Perl::Lint::Constants::Type;
  134         238  
  134         86953  
5 134     134   866 use parent "Perl::Lint::Policy";
  134         293  
  134         896  
6              
7             use constant {
8 134         47749 DESC => 'Forbid $b before $a in sort blocks',
9             EXPL => [152],
10 134     134   9855 };
  134         276  
11              
12             sub evaluate {
13 9     9 0 16 my ($class, $file, $tokens, $src, $args) = @_;
14              
15 9         14 my @violations;
16 9         33 for (my $i = 0; my $token = $tokens->[$i]; $i++) {
17 279         218 my $token_type = $token->{type};
18 279         217 my $token_data = $token->{data};
19              
20 279 100 100     714 if ($token_type == BUILTIN_FUNC && $token_data eq 'sort') {
21 30         27 $token = $tokens->[++$i];
22 30 100       48 if ($token->{type} == LEFT_PAREN) {
23 10         12 $token = $tokens->[++$i];
24             }
25              
26 30 100       47 if ($token->{type} == LEFT_BRACE) {
27 24         22 my $left_brace_num = 1;
28 24         18 my $is_b_at_before_comparator = 0;
29 24         43 for ($i++; $token = $tokens->[$i]; $i++) {
30 306         231 $token_type = $token->{type};
31 306         248 $token_data = $token->{data};
32 306 100 100     2234 if ($token_type == COMPARE || $token_type == STRING_COMPARE) {
    100 100        
    100 66        
    100 66        
    100 66        
33 31 100       56 if ($is_b_at_before_comparator) {
34 10         45 push @violations, {
35             filename => $file,
36             line => $token->{line},
37             description => DESC,
38             explanation => EXPL,
39             policy => __PACKAGE__,
40             };
41             }
42             }
43             elsif ($token_type == LEFT_BRACE) {
44 18         29 $left_brace_num++;
45             }
46             elsif ($token_type == RIGHT_BRACE) {
47 42 100       102 last if --$left_brace_num <= 0;
48             }
49             elsif ($token_type == VAR && $token_data eq '$b') {
50 26         44 $is_b_at_before_comparator = 1;
51             }
52             elsif ( # XXX enough?
53             $token_type == OR ||
54             $token_type == AND ||
55             $token_type == ALPHABET_OR ||
56             $token_type == ALPHABET_AND
57             ) {
58 7         14 $is_b_at_before_comparator = 0;
59             }
60             }
61             }
62             }
63             }
64              
65 9         43 return \@violations;
66             }
67              
68             1;
69