File Coverage

blib/lib/Perl/Lint/Policy/BuiltinFunctions/RequireSimpleSortBlock.pm
Criterion Covered Total %
statement 39 39 100.0
branch 14 14 100.0
condition 2 3 66.6
subroutine 6 6 100.0
pod 0 1 0.0
total 61 63 96.8


line stmt bran cond sub pod time code
1             package Perl::Lint::Policy::BuiltinFunctions::RequireSimpleSortBlock;
2 133     133   67441 use strict;
  133         185  
  133         3252  
3 133     133   481 use warnings;
  133         163  
  133         2677  
4 133     133   800 use Perl::Lint::Constants::Type;
  133         168  
  133         57809  
5 133     133   579 use parent "Perl::Lint::Policy";
  133         174  
  133         548  
6              
7             use constant {
8 133         31242 DESC => 'Sort blocks should have a single statement',
9             EXPL => [149],
10 133     133   6395 };
  133         182  
11              
12             sub evaluate {
13 4     4 0 6 my ($class, $file, $tokens, $src, $args) = @_;
14              
15 4         5 my @violations;
16 4         15 for (my $i = 0; my $token = $tokens->[$i]; $i++) {
17 76         48 my $token_type = $token->{type};
18 76         55 my $token_data = $token->{data};
19              
20 76 100 66     172 if ($token_type == BUILTIN_FUNC && $token_data eq 'sort') {
21 21         14 $token = $tokens->[++$i];
22 21 100       28 if ($token->{type} == LEFT_PAREN) {
23 6         6 $token = $tokens->[++$i];
24             }
25              
26 21         17 my $token_type = $token->{type};
27              
28 21 100       28 if ($token_type != LEFT_BRACE) {
29 5         8 next;
30             }
31              
32 16         11 my $left_brace_num = 1;
33 16         9 my $concat_stmt = ''; # XXX
34 16         25 for ($i++; $token = $tokens->[$i]; $i++) {
35 240         162 $token_type = $token->{type};
36              
37 240 100       272 if ($token_type == LEFT_BRACE) {
    100          
38 14         18 $left_brace_num++;
39             }
40             elsif ($token_type == RIGHT_BRACE) {
41 30 100       41 if (--$left_brace_num <= 0) {
42 16 100       36 if (scalar(@_ = split /;/, $concat_stmt) > 1) { # XXX
43             push @violations, {
44             filename => $file,
45             line => $token->{line},
46 6         19 description => DESC,
47             explanation => EXPL,
48             policy => __PACKAGE__,
49             };
50             }
51 16         29 last;
52             }
53             }
54             else {
55 196         246 $concat_stmt .= $token->{data};
56             }
57             }
58             }
59             }
60              
61 4         19 return \@violations;
62             }
63              
64             1;
65