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   97232 use strict;
  133         259  
  133         5152  
3 133     133   878 use warnings;
  133         250  
  133         3807  
4 133     133   1227 use Perl::Lint::Constants::Type;
  133         231  
  133         85658  
5 133     133   943 use parent "Perl::Lint::Policy";
  133         265  
  133         877  
6              
7             use constant {
8 133         47273 DESC => 'Sort blocks should have a single statement',
9             EXPL => [149],
10 133     133   9237 };
  133         292  
11              
12             sub evaluate {
13 4     4 0 9 my ($class, $file, $tokens, $src, $args) = @_;
14              
15 4         5 my @violations;
16 4         17 for (my $i = 0; my $token = $tokens->[$i]; $i++) {
17 76         59 my $token_type = $token->{type};
18 76         72 my $token_data = $token->{data};
19              
20 76 100 66     220 if ($token_type == BUILTIN_FUNC && $token_data eq 'sort') {
21 21         23 $token = $tokens->[++$i];
22 21 100       32 if ($token->{type} == LEFT_PAREN) {
23 6         7 $token = $tokens->[++$i];
24             }
25              
26 21         22 my $token_type = $token->{type};
27              
28 21 100       30 if ($token_type != LEFT_BRACE) {
29 5         10 next;
30             }
31              
32 16         17 my $left_brace_num = 1;
33 16         13 my $concat_stmt = ''; # XXX
34 16         28 for ($i++; $token = $tokens->[$i]; $i++) {
35 240         197 $token_type = $token->{type};
36              
37 240 100       349 if ($token_type == LEFT_BRACE) {
    100          
38 14         29 $left_brace_num++;
39             }
40             elsif ($token_type == RIGHT_BRACE) {
41 30 100       56 if (--$left_brace_num <= 0) {
42 16 100       59 if (scalar(@_ = split /;/, $concat_stmt) > 1) { # XXX
43 6         22 push @violations, {
44             filename => $file,
45             line => $token->{line},
46             description => DESC,
47             explanation => EXPL,
48             policy => __PACKAGE__,
49             };
50             }
51 16         40 last;
52             }
53             }
54             else {
55 196         349 $concat_stmt .= $token->{data};
56             }
57             }
58             }
59             }
60              
61 4         24 return \@violations;
62             }
63              
64             1;
65