File Coverage

blib/lib/Perl/Lint/Policy/Subroutines/ProhibitNestedSubs.pm
Criterion Covered Total %
statement 30 30 100.0
branch 11 12 91.6
condition n/a
subroutine 6 6 100.0
pod 0 1 0.0
total 47 49 95.9


line stmt bran cond sub pod time code
1             package Perl::Lint::Policy::Subroutines::ProhibitNestedSubs;
2 133     133   90102 use strict;
  133         278  
  133         4941  
3 133     133   656 use warnings;
  133         224  
  133         3408  
4 133     133   973 use Perl::Lint::Constants::Type;
  133         226  
  133         86409  
5 133     133   984 use parent "Perl::Lint::Policy";
  133         256  
  133         862  
6              
7             # TODO msg!
8             use constant {
9 133         32188 DESC => 'Nested named subroutine',
10             EXPL => 'Declaring a named sub inside another named sub does not prevent the '
11             . 'inner sub from being global',
12 133     133   9764 };
  133         343  
13              
14             sub evaluate {
15 6     6 0 10 my ($class, $file, $tokens, $args) = @_;
16              
17 6         6 my @violations;
18 6         21 for (my $i = 0; my $token = $tokens->[$i]; $i++) {
19 26         24 my $token_type = $token->{type};
20              
21 26 100       53 if ($token_type == FUNCTION) {
22 9         7 my $left_brace_num = 0;
23 9         20 for ($i++; my $token = $tokens->[$i]; $i++) {
24 81         62 my $token_type = $token->{type};
25 81 100       175 if ($token_type == FUNCTION) {
    100          
    100          
26 4 50       7 if ($left_brace_num > 0) {
27 4         22 push @violations, {
28             filename => $file,
29             line => $token->{line},
30             description => DESC,
31             explanation => EXPL,
32             policy => __PACKAGE__,
33             };
34             }
35             }
36             elsif ($token_type == LEFT_BRACE) {
37 18         30 $left_brace_num++;
38             }
39             elsif ($token_type == RIGHT_BRACE) {
40 18         13 $left_brace_num--;
41 18 100       50 last if $left_brace_num <= 0;
42             }
43             }
44             }
45             }
46              
47 6         22 return \@violations;
48             }
49              
50             1;
51