File Coverage

blib/lib/Perl/Lint/Policy/TestingAndDebugging/ProhibitProlongedStrictureOverride.pm
Criterion Covered Total %
statement 66 66 100.0
branch 25 28 89.2
condition 13 18 72.2
subroutine 8 8 100.0
pod 0 1 0.0
total 112 121 92.5


line stmt bran cond sub pod time code
1             package Perl::Lint::Policy::TestingAndDebugging::ProhibitProlongedStrictureOverride;
2 133     133   97006 use strict;
  133         210  
  133         4659  
3 133     133   590 use warnings;
  133         177  
  133         3104  
4 133     133   1062 use utf8;
  133         184  
  133         899  
5 133     133   3576 use Perl::Lint::Constants::Type;
  133         178  
  133         87968  
6 133     133   8704 use parent "Perl::Lint::Policy";
  133         273  
  133         757  
7              
8 133     133   8920 use constant DEFAULT_ALLOW_STATEMENTS_NUM => 3;
  133         192  
  133         8696  
9              
10             use constant {
11 133         67556 DESC => q{Don't turn off strict for large blocks of code},
12             EXPL => [433],
13 133     133   831 };
  133         168  
14              
15             sub evaluate {
16 8     8 0 13 my ($class, $file, $tokens, $src, $args) = @_;
17              
18 8         11 my $statements_arg;
19 8 100       23 if (my $this_policies_arg = $args->{prohibit_prolonged_stricture_override}) {
20 1         2 $statements_arg = $this_policies_arg->{statements};
21             }
22 8 100       22 my $allow_statements_num = defined $statements_arg ? $statements_arg : DEFAULT_ALLOW_STATEMENTS_NUM;
23              
24 8         7 my @violations;
25 8         12 my $token_num = scalar @$tokens;
26 8         7 my $no_strict = undef;
27 8         12 my $statements_num_of_after_no_strict = 0;
28 8         21 TOP: for (my $i = 0; $i < $token_num; $i++) {
29 79         63 my $token = $tokens->[$i];
30 79         76 my $token_type = $token->{type};
31              
32 79 100 100     373 if ($token_type == FUNCTION_DECL) {
    100 100        
    100          
33 4         8 my $no_strict = undef;
34 4         5 my $statements_num_of_after_no_strict = 0;
35 4         5 my $left_brace_num = 0;
36 4         11 for ($i++; $i < $token_num; $i++) {
37 56         42 my $token = $tokens->[$i];
38 56         45 my $token_type = $token->{type};
39              
40 56 100 100     236 if ($token_type == BUILTIN_FUNC and $token->{data} eq 'no') {
    100 66        
    100          
    100          
41 4         7 $token = $tokens->[++$i];
42 4         6 $token_type = $token->{type};
43              
44 4         8 my @allows;
45 4 50 33     19 if ($token_type == KEY && $token->{data} eq 'strict') {
46 4         5 $no_strict = $token;
47 4         9 $i++;
48             }
49             }
50             elsif ($token_type == SEMI_COLON && $no_strict) {
51 14         11 $statements_num_of_after_no_strict++;
52 14 100       29 if ($statements_num_of_after_no_strict > $allow_statements_num) {
53 2         9 push @violations, {
54             filename => $file,
55             line => $no_strict->{line},
56             description => DESC,
57             explanation => EXPL,
58             policy => __PACKAGE__,
59             };
60 2         6 next TOP;
61             }
62             }
63             elsif ($token_type == LEFT_BRACE) {
64 4         9 $left_brace_num++;
65             }
66             elsif ($token_type == RIGHT_BRACE) {
67 2         4 $left_brace_num--;
68 2 50       8 if ($left_brace_num <= 0) {
69 2         8 last;
70             }
71             }
72             }
73             }
74             elsif ($token_type == BUILTIN_FUNC and $token->{data} eq 'no') {
75 4         6 $token = $tokens->[++$i];
76 4         6 $token_type = $token->{type};
77              
78 4         4 my @allows;
79 4 50 33     22 if ($token_type == KEY && $token->{data} eq 'strict') {
80 4         6 $no_strict = $token;
81 4         8 $i++;
82             }
83             }
84             elsif ($token_type == SEMI_COLON && $no_strict) {
85 11         7 $statements_num_of_after_no_strict++;
86 11 100       57 if ($statements_num_of_after_no_strict > $allow_statements_num) {
87 2         12 push @violations, {
88             filename => $file,
89             line => $no_strict->{line},
90             description => DESC,
91             explanation => EXPL,
92             policy => __PACKAGE__,
93             };
94 2         4 last;
95             }
96             }
97             }
98              
99 8         37 return \@violations;
100             }
101              
102             1;
103