File Coverage

blib/lib/Perl/Lint/Policy/ControlStructures/ProhibitDeepNests.pm
Criterion Covered Total %
statement 47 49 95.9
branch 20 24 83.3
condition 13 14 92.8
subroutine 6 6 100.0
pod 0 1 0.0
total 86 94 91.4


line stmt bran cond sub pod time code
1             package Perl::Lint::Policy::ControlStructures::ProhibitDeepNests;
2 133     133   94505 use strict;
  133         263  
  133         4927  
3 133     133   616 use warnings;
  133         234  
  133         3559  
4 133     133   1053 use Perl::Lint::Constants::Type;
  133         224  
  133         82833  
5 133     133   816 use parent "Perl::Lint::Policy";
  133         241  
  133         867  
6              
7             use constant {
8 133         47264 DESC => 'Code structure is deeply nested',
9             EXPL => 'Consider refactoring',
10 133     133   8804 };
  133         278  
11              
12             sub evaluate {
13 12     12 0 31 my ($class, $file, $tokens, $src, $args) = @_;
14              
15 12   100     76 my $max_nexts = $args->{prohibit_deep_nests}->{max_nests} || 5;
16              
17 12         28 my @violations;
18 12         23 my $lbnum = 0;
19 12         18 my $parent_of_nests_line_number = 0;
20 12         57 for (my $i = 0, my $token_type, my $token_data; my $token = $tokens->[$i]; $i++) {
21 527         454 $token_type = $token->{type};
22 527         472 $token_data = $token->{data};
23              
24 527 100       699 if ($token_type == LEFT_BRACE) {
25 90 100       147 if ($lbnum == 0) {
26 16         25 $parent_of_nests_line_number = $token->{line};
27             }
28              
29 90         79 $lbnum++;
30              
31 90 100       173 if ($lbnum > $max_nexts) {
32 8         39 push @violations, {
33             filename => $file,
34             line => $parent_of_nests_line_number,
35             description => DESC,
36             explanation => EXPL,
37             policy => __PACKAGE__,
38             };
39             }
40              
41 90         149 next;
42             }
43              
44 437 100       559 if ($token_type == RIGHT_BRACE) {
45 90         65 $lbnum--;
46 90         144 next;
47             }
48              
49 347 100 100     1807 if (
      100        
      100        
      66        
50             $token_type == VAR ||
51             $token_type == GLOBAL_VAR ||
52             $token_type == FUNCTION_DECL ||
53             ($token_type == BUILTIN_FUNC && $token_data eq 'eval')
54             ) {
55 95         120 $token = $tokens->[++$i];
56              
57 95 50       142 if (!$token) {
58 0         0 last;
59             }
60              
61 95 100       146 if ($token->{type} == POINTER) {
62 1         2 $token = $tokens->[++$i];
63 1 50       3 last if !$token;
64             }
65              
66 95 100       146 if ($token->{type} == LEFT_BRACE) {
67 4         6 my $unnecessary_lbnum = 1;
68              
69 4         15 for ($i++; $token = $tokens->[$i]; $i++) {
70 8         7 $token_type = $token->{type};
71              
72 8 50       29 if ($token_type == LEFT_BRACE) {
    100          
73 0         0 $unnecessary_lbnum++;
74             }
75             elsif ($token_type == RIGHT_BRACE) {
76 4 50       13 last if --$unnecessary_lbnum <= 0;
77             }
78             }
79             }
80              
81 95         171 next;
82             }
83             }
84              
85 12         70 return \@violations;
86             }
87              
88             1;
89