« blib/lib/Perl/Lint/Policy/BuiltinFunctions/ProhibitBooleanGrep.pm
↑ Summary
blib/lib/Perl/Lint/Policy/BuiltinFunctions/ProhibitLvalueSubstr.pm »
| 1 | package Perl::Lint::Policy::BuiltinFunctions::ProhibitComplexMappings; | ||
| 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 Perl::Lint::Constants::Type; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 5 | use parent "Perl::Lint::Policy"; | ||
| 6 | |||
| 7 | use constant { | ||
| 8 | DESC => 'Map blocks should have a single statement', | ||
| 9 | EXPL => [113], | ||
| 10 | }; | ||
| 11 | |||
| 12 | sub evaluate { | ||
| 13 | my ($class, $file, $tokens, $src, $args) = @_; | ||
| 14 | |||
| 15 | my $max_statements = $args->{prohibit_complex_mappings}->{max_statements} || 1; | ||
| 16 | if (--$max_statements < 0) { | ||
| 17 | $max_statements = 0; | ||
| 18 | } | ||
| 19 | |||
| 20 | my @violations; | ||
| 21 | my $statements_num = 0; | ||
| 22 | for (my $i = 0; my $token = $tokens->[$i]; $i++) { | ||
| 23 | my $token_type = $token->{type}; | ||
| 24 | my $token_data = $token->{data}; | ||
| 25 | |||
| 26 | if ($token_type == BUILTIN_FUNC && $token_data eq 'map') { | ||
| 27 | $token = $tokens->[++$i]; | ||
| 28 | if ($token->{type} == LEFT_PAREN) { | ||
| 29 | $token = $tokens->[++$i]; | ||
| 30 | } | ||
| 31 | |||
| 32 | if ($token->{type} != LEFT_BRACE) { | ||
| 33 | next; | ||
| 34 | } | ||
| 35 | |||
| 36 | my $left_brace_num = 1; | ||
| 37 | my $placed_semi_colon = 0; | ||
| 38 | for ($i++; my $token = $tokens->[$i]; $i++) { | ||
| 39 | $token_type = $token->{type}; | ||
| 40 | if ($token_type == IF_STATEMENT) { | ||
| 41 | push @violations, { | ||
| 42 | filename => $file, | ||
| 43 | line => $token->{line}, | ||
| 44 | description => DESC, | ||
| 45 | explanation => EXPL, | ||
| 46 | policy => __PACKAGE__, | ||
| 47 | }; | ||
| 48 | last; | ||
| 49 | } | ||
| 50 | elsif ($token_type == DO) { # skip semi-colons that are in do statement | ||
| 51 | $token = $tokens->[++$i]; | ||
| 52 | if ($token->{type} == LEFT_BRACE) { | ||
| 53 | my $left_brace_num = 1; | ||
| 54 | for ($i++; my $token = $tokens->[$i]; $i++) { | ||
| 55 | $token_type = $token->{type}; | ||
| 56 | if ($token_type == LEFT_BRACE) { | ||
| 57 | $left_brace_num++; | ||
| 58 | } | ||
| 59 | elsif ($token_type == RIGHT_BRACE) { | ||
| 60 | last if --$left_brace_num <= 0; | ||
| 61 | } | ||
| 62 | } | ||
| 63 | } | ||
| 64 | } | ||
| 65 | elsif ($token_type == SEMI_COLON) { | ||
| 66 | my $next_token = $tokens->[$i+1]; | ||
| 67 | my $next_token_type = $next_token->{type}; | ||
| 68 | $statements_num++; | ||
| 69 | if ($statements_num > $max_statements && ($left_brace_num != 1 || ($next_token_type && $next_token_type != RIGHT_BRACE))) { | ||
| 70 | push @violations, { | ||
| 71 | filename => $file, | ||
| 72 | line => $token->{line}, | ||
| 73 | description => DESC, | ||
| 74 | explanation => EXPL, | ||
| 75 | policy => __PACKAGE__, | ||
| 76 | }; | ||
| 77 | last; | ||
| 78 | } | ||
| 79 | } | ||
| 80 | elsif ($token_type == LEFT_BRACE) { | ||
| 81 | $left_brace_num++; | ||
| 82 | } | ||
| 83 | elsif ($token_type == RIGHT_BRACE) { | ||
| 84 | last if --$left_brace_num <= 0; | ||
| 85 | } | ||
| 86 | } | ||
| 87 | } | ||
| 88 | } | ||
| 89 | |||
| 90 | return \@violations; | ||
| 91 | } | ||
| 92 | |||
| 93 | 1; | ||
| 94 | |||