« blib/lib/Perl/Lint/Policy/BuiltinFunctions/ProhibitStringyEval.pm
↑ Summary
blib/lib/Perl/Lint/Policy/BuiltinFunctions/ProhibitUniversalCan.pm »
| 1 | package Perl::Lint::Policy::BuiltinFunctions::ProhibitStringySplit; | ||
| 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 => 'String delimiter used with "split"', | ||
| 9 | EXPL => 'Express it as a regex instead', | ||
| 10 | }; | ||
| 11 | |||
| 12 | sub evaluate { | ||
| 13 | my ($class, $file, $tokens, $src, $args) = @_; | ||
| 14 | |||
| 15 | my @violations; | ||
| 16 | for (my $i = 0; my $token = $tokens->[$i]; $i++) { | ||
| 17 | my $token_type = $token->{type}; | ||
| 18 | my $token_data = $token->{data}; | ||
| 19 | |||
| 20 | if ($token_type == BUILTIN_FUNC && $token_data eq 'split') { | ||
| 21 | $token = $tokens->[++$i]; | ||
| 22 | |||
| 23 | if ($token->{type} == LEFT_PAREN) { | ||
| 24 | $token = $tokens->[++$i]; | ||
| 25 | } | ||
| 26 | |||
| 27 | $token_type = $token->{type}; | ||
| 28 | $token_data = $token->{data}; | ||
| 29 | |||
| 30 | if ( | ||
| 31 | ($token_type == STRING || $token_type == RAW_STRING) && | ||
| 32 | $token_data ne ' ' | ||
| 33 | ) { | ||
| 34 | push @violations, { | ||
| 35 | filename => $file, | ||
| 36 | line => $token->{line}, | ||
| 37 | description => DESC, | ||
| 38 | explanation => EXPL, | ||
| 39 | policy => __PACKAGE__, | ||
| 40 | }; | ||
| 41 | } | ||
| 42 | elsif ($token_type == REG_QUOTE || $token_type == REG_DOUBLE_QUOTE) { | ||
| 43 | $i += 2; | ||
| 44 | if ($tokens->[$i]->{data} ne ' ') { | ||
| 45 | push @violations, { | ||
| 46 | filename => $file, | ||
| 47 | line => $token->{line}, | ||
| 48 | description => DESC, | ||
| 49 | explanation => EXPL, | ||
| 50 | policy => __PACKAGE__, | ||
| 51 | }; | ||
| 52 | } | ||
| 53 | } | ||
| 54 | } | ||
| 55 | } | ||
| 56 | |||
| 57 | return \@violations; | ||
| 58 | } | ||
| 59 | |||
| 60 | 1; | ||
| 61 | |||