File Coverage

blib/lib/Perl/Lint/Policy/BuiltinFunctions/ProhibitComplexMappings.pm
Criterion Covered Total %
statement 52 54 96.3
branch 26 30 86.6
condition 7 11 63.6
subroutine 6 6 100.0
pod 0 1 0.0
total 91 102 89.2


line stmt bran cond sub pod time code
1             package Perl::Lint::Policy::BuiltinFunctions::ProhibitComplexMappings;
2 133     133   69314 use strict;
  133         188  
  133         3089  
3 133     133   431 use warnings;
  133         167  
  133         2455  
4 133     133   787 use Perl::Lint::Constants::Type;
  133         174  
  133         59428  
5 133     133   593 use parent "Perl::Lint::Policy";
  133         182  
  133         607  
6              
7             use constant {
8 133         43695 DESC => 'Map blocks should have a single statement',
9             EXPL => [113],
10 133     133   6591 };
  133         191  
11              
12             sub evaluate {
13 6     6 0 11 my ($class, $file, $tokens, $src, $args) = @_;
14              
15 6   100     31 my $max_statements = $args->{prohibit_complex_mappings}->{max_statements} || 1;
16 6 50       15 if (--$max_statements < 0) {
17 0         0 $max_statements = 0;
18             }
19              
20 6         10 my @violations;
21 6         7 my $statements_num = 0;
22 6         25 for (my $i = 0; my $token = $tokens->[$i]; $i++) {
23 121         91 my $token_type = $token->{type};
24 121         77 my $token_data = $token->{data};
25              
26 121 100 66     251 if ($token_type == BUILTIN_FUNC && $token_data eq 'map') {
27 20         23 $token = $tokens->[++$i];
28 20 100       35 if ($token->{type} == LEFT_PAREN) {
29 1         2 $token = $tokens->[++$i];
30             }
31              
32 20 100       30 if ($token->{type} != LEFT_BRACE) {
33 5         9 next;
34             }
35              
36 15         13 my $left_brace_num = 1;
37 15         10 my $placed_semi_colon = 0;
38 15         31 for ($i++; my $token = $tokens->[$i]; $i++) {
39 98         74 $token_type = $token->{type};
40 98 100       264 if ($token_type == IF_STATEMENT) {
    100          
    100          
    100          
    100          
41             push @violations, {
42             filename => $file,
43             line => $token->{line},
44 1         4 description => DESC,
45             explanation => EXPL,
46             policy => __PACKAGE__,
47             };
48 1         3 last;
49             }
50             elsif ($token_type == DO) { # skip semi-colons that are in do statement
51 1         4 $token = $tokens->[++$i];
52 1 50       4 if ($token->{type} == LEFT_BRACE) {
53 1         1 my $left_brace_num = 1;
54 1         3 for ($i++; my $token = $tokens->[$i]; $i++) {
55 4         6 $token_type = $token->{type};
56 4 50       10 if ($token_type == LEFT_BRACE) {
    100          
57 0         0 $left_brace_num++;
58             }
59             elsif ($token_type == RIGHT_BRACE) {
60 1 50       5 last if --$left_brace_num <= 0;
61             }
62             }
63             }
64             }
65             elsif ($token_type == SEMI_COLON) {
66 11         12 my $next_token = $tokens->[$i+1];
67 11         12 my $next_token_type = $next_token->{type};
68 11         8 $statements_num++;
69 11 100 33     52 if ($statements_num > $max_statements && ($left_brace_num != 1 || ($next_token_type && $next_token_type != RIGHT_BRACE))) {
      66        
70             push @violations, {
71             filename => $file,
72             line => $token->{line},
73 7         26 description => DESC,
74             explanation => EXPL,
75             policy => __PACKAGE__,
76             };
77 7         18 last;
78             }
79             }
80             elsif ($token_type == LEFT_BRACE) {
81 9         13 $left_brace_num++;
82             }
83             elsif ($token_type == RIGHT_BRACE) {
84 16 100       41 last if --$left_brace_num <= 0;
85             }
86             }
87             }
88             }
89              
90 6         22 return \@violations;
91             }
92              
93             1;
94