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   68838 use strict;
  133         240  
  133         3349  
3 133     133   465 use warnings;
  133         186  
  133         2610  
4 133     133   822 use Perl::Lint::Constants::Type;
  133         173  
  133         60071  
5 133     133   592 use parent "Perl::Lint::Policy";
  133         195  
  133         574  
6              
7             use constant {
8 133         37335 DESC => 'Code structure is deeply nested',
9             EXPL => 'Consider refactoring',
10 133     133   6597 };
  133         205  
11              
12             sub evaluate {
13 12     12 0 25 my ($class, $file, $tokens, $src, $args) = @_;
14              
15 12   100     64 my $max_nexts = $args->{prohibit_deep_nests}->{max_nests} || 5;
16              
17 12         13 my @violations;
18 12         15 my $lbnum = 0;
19 12         18 my $parent_of_nests_line_number = 0;
20 12         43 for (my $i = 0, my $token_type, my $token_data; my $token = $tokens->[$i]; $i++) {
21 527         364 $token_type = $token->{type};
22 527         343 $token_data = $token->{data};
23              
24 527 100       586 if ($token_type == LEFT_BRACE) {
25 90 100       104 if ($lbnum == 0) {
26 16         21 $parent_of_nests_line_number = $token->{line};
27             }
28              
29 90         68 $lbnum++;
30              
31 90 100       103 if ($lbnum > $max_nexts) {
32 8         31 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         109 next;
42             }
43              
44 437 100       457 if ($token_type == RIGHT_BRACE) {
45 90         61 $lbnum--;
46 90         110 next;
47             }
48              
49 347 100 100     1515 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         68 $token = $tokens->[++$i];
56              
57 95 50       107 if (!$token) {
58 0         0 last;
59             }
60              
61 95 100       137 if ($token->{type} == POINTER) {
62 1         2 $token = $tokens->[++$i];
63 1 50       3 last if !$token;
64             }
65              
66 95 100       109 if ($token->{type} == LEFT_BRACE) {
67 4         4 my $unnecessary_lbnum = 1;
68              
69 4         11 for ($i++; $token = $tokens->[$i]; $i++) {
70 8         9 $token_type = $token->{type};
71              
72 8 50       22 if ($token_type == LEFT_BRACE) {
    100          
73 0         0 $unnecessary_lbnum++;
74             }
75             elsif ($token_type == RIGHT_BRACE) {
76 4 50       12 last if --$unnecessary_lbnum <= 0;
77             }
78             }
79             }
80              
81 95         130 next;
82             }
83             }
84              
85 12         55 return \@violations;
86             }
87              
88             1;
89