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