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   96165 use strict;
  133         281  
  133         5187  
3 133     133   639 use warnings;
  133         244  
  133         3599  
4 133     133   1086 use Perl::Lint::Constants::Type;
  133         255  
  133         83615  
5 133     133   850 use parent "Perl::Lint::Policy";
  133         230  
  133         859  
6              
7             use constant {
8 133         60872 DESC => 'Map blocks should have a single statement',
9             EXPL => [113],
10 133     133   8832 };
  133         325  
11              
12             sub evaluate {
13 6     6 0 19 my ($class, $file, $tokens, $src, $args) = @_;
14              
15 6   100     36 my $max_statements = $args->{prohibit_complex_mappings}->{max_statements} || 1;
16 6 50       23 if (--$max_statements < 0) {
17 0         0 $max_statements = 0;
18             }
19              
20 6         13 my @violations;
21 6         9 my $statements_num = 0;
22 6         37 for (my $i = 0; my $token = $tokens->[$i]; $i++) {
23 121         133 my $token_type = $token->{type};
24 121         109 my $token_data = $token->{data};
25              
26 121 100 66     347 if ($token_type == BUILTIN_FUNC && $token_data eq 'map') {
27 20         27 $token = $tokens->[++$i];
28 20 100       37 if ($token->{type} == LEFT_PAREN) {
29 1         2 $token = $tokens->[++$i];
30             }
31              
32 20 100       40 if ($token->{type} != LEFT_BRACE) {
33 5         12 next;
34             }
35              
36 15         14 my $left_brace_num = 1;
37 15         22 my $placed_semi_colon = 0;
38 15         36 for ($i++; my $token = $tokens->[$i]; $i++) {
39 98         78 $token_type = $token->{type};
40 98 100       319 if ($token_type == IF_STATEMENT) {
    100          
    100          
    100          
    100          
41 1         4 push @violations, {
42             filename => $file,
43             line => $token->{line},
44             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         2 $token = $tokens->[++$i];
52 1 50       4 if ($token->{type} == LEFT_BRACE) {
53 1         2 my $left_brace_num = 1;
54 1         4 for ($i++; my $token = $tokens->[$i]; $i++) {
55 4         6 $token_type = $token->{type};
56 4 50       14 if ($token_type == LEFT_BRACE) {
    100          
57 0         0 $left_brace_num++;
58             }
59             elsif ($token_type == RIGHT_BRACE) {
60 1 50       6 last if --$left_brace_num <= 0;
61             }
62             }
63             }
64             }
65             elsif ($token_type == SEMI_COLON) {
66 11         22 my $next_token = $tokens->[$i+1];
67 11         17 my $next_token_type = $next_token->{type};
68 11         12 $statements_num++;
69 11 100 33     74 if ($statements_num > $max_statements && ($left_brace_num != 1 || ($next_token_type && $next_token_type != RIGHT_BRACE))) {
      66        
70 7         33 push @violations, {
71             filename => $file,
72             line => $token->{line},
73             description => DESC,
74             explanation => EXPL,
75             policy => __PACKAGE__,
76             };
77 7         21 last;
78             }
79             }
80             elsif ($token_type == LEFT_BRACE) {
81 9         16 $left_brace_num++;
82             }
83             elsif ($token_type == RIGHT_BRACE) {
84 16 100       50 last if --$left_brace_num <= 0;
85             }
86             }
87             }
88             }
89              
90 6         40 return \@violations;
91             }
92              
93             1;
94