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   70201 use strict;
  133         207  
  133         3167  
3 133     133   489 use warnings;
  133         186  
  133         2553  
4 133     133   807 use Perl::Lint::Constants::Type;
  133         164  
  133         60056  
5 133     133   600 use parent "Perl::Lint::Policy";
  133         212  
  133         586  
6              
7             use constant {
8 133         37329 DESC => 'Code structure is deeply nested',
9             EXPL => 'Consider refactoring',
10 133     133   6757 };
  133         210  
11              
12             sub evaluate {
13 12     12 0 31 my ($class, $file, $tokens, $src, $args) = @_;
14              
15 12   100     77 my $max_nexts = $args->{prohibit_deep_nests}->{max_nests} || 5;
16              
17 12         12 my @violations;
18 12         11 my $lbnum = 0;
19 12         13 my $parent_of_nests_line_number = 0;
20 12         47 for (my $i = 0, my $token_type, my $token_data; my $token = $tokens->[$i]; $i++) {
21 527         352 $token_type = $token->{type};
22 527         371 $token_data = $token->{data};
23              
24 527 100       596 if ($token_type == LEFT_BRACE) {
25 90 100       107 if ($lbnum == 0) {
26 16         28 $parent_of_nests_line_number = $token->{line};
27             }
28              
29 90         57 $lbnum++;
30              
31 90 100       110 if ($lbnum > $max_nexts) {
32 8         28 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         124 next;
42             }
43              
44 437 100       453 if ($token_type == RIGHT_BRACE) {
45 90         61 $lbnum--;
46 90         118 next;
47             }
48              
49 347 100 100     1542 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         71 $token = $tokens->[++$i];
56              
57 95 50       118 if (!$token) {
58 0         0 last;
59             }
60              
61 95 100       129 if ($token->{type} == POINTER) {
62 1         3 $token = $tokens->[++$i];
63 1 50       3 last if !$token;
64             }
65              
66 95 100       123 if ($token->{type} == LEFT_BRACE) {
67 4         6 my $unnecessary_lbnum = 1;
68              
69 4         12 for ($i++; $token = $tokens->[$i]; $i++) {
70 8         9 $token_type = $token->{type};
71              
72 8 50       24 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         138 next;
82             }
83             }
84              
85 12         53 return \@violations;
86             }
87              
88             1;
89