« blib/lib/Perl/Lint/Policy/Modules/RequireExplicitPackage.pm
↑ Summary
blib/lib/Perl/Lint/Policy/RegularExpressions/ProhibitCaptureWithoutTest.pm »
| 1 | package Perl::Lint::Policy::NamingConventions::ProhibitAmbiguousNames; | ||
| 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 String::CamelCase qw/wordsplit/; |
|
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_FORBIDDEN_WORDS => [qw/abstract bases close contract last left no record right second set/]; | ||
| 9 | |||
| 10 | use constant { | ||
| 11 | DESC => 'The variable names that are not to be allowed', | ||
| 12 | EXPL => [48], | ||
| 13 | }; | ||
| 14 | |||
| 15 | sub evaluate { | ||
| 16 | my ($class, $file, $tokens, $src, $args) = @_; | ||
| 17 | |||
| 18 | my @forbidden_words = @{+DEFAULT_FORBIDDEN_WORDS}; | ||
| 19 | if (defined(my $forbiddens = $args->{prohibit_ambiguous_names}->{forbid})) { | ||
| 20 | @forbidden_words = split / /, $forbiddens; | ||
| 21 | } | ||
| 22 | |||
| 23 | my @violations; | ||
| 24 | my $token_num = scalar @$tokens; | ||
| 25 | for (my $i = 0; $i < $token_num; $i++) { | ||
| 26 | my $token = $tokens->[$i]; | ||
| 27 | my $token_type = $token->{type}; | ||
| 28 | |||
| 29 | if ($token_type == FOR_STATEMENT || $token_type == FOREACH_STATEMENT) { | ||
| 30 | my $next_token_type = $tokens->[++$i]->{type}; | ||
| 31 | $i++ if $next_token_type == VAR_DECL || $next_token_type == OUR_DECL; | ||
| 32 | $i++; | ||
| 33 | next; | ||
| 34 | } | ||
| 35 | |||
| 36 | my @word_blocks; | ||
| 37 | if ($token_type == VAR_DECL || $token_type == OUR_DECL || $token_type == LOCAL_DECL) { | ||
| 38 | my $left_paren_num = 0; | ||
| 39 | for ($i++; $i < $token_num; $i++) { | ||
| 40 | my $token = $tokens->[$i]; | ||
| 41 | my $token_type = $token->{type}; | ||
| 42 | |||
| 43 | if ( | ||
| 44 | $token_type == VAR || | ||
| 45 | $token_type == LOCAL_VAR || | ||
| 46 | $token_type == LOCAL_ARRAY_VAR || | ||
| 47 | $token_type == LOCAL_HASH_VAR || | ||
| 48 | $token_type == GLOBAL_VAR | ||
| 49 | ) { | ||
| 50 | push @word_blocks, [wordsplit(substr $token->{data}, 1)]; | ||
| 51 | } | ||
| 52 | elsif ($token_type == NAMESPACE_RESOLVER || $token_type == GLOB) { | ||
| 53 | next; | ||
| 54 | } | ||
| 55 | elsif ($token_type == NAMESPACE) { | ||
| 56 | push @word_blocks, [$tokens->[$i]->{data}]; | ||
| 57 | } | ||
| 58 | elsif ($token_type == LEFT_PAREN) { | ||
| 59 | $left_paren_num++; | ||
| 60 | } | ||
| 61 | elsif ($token_type == RIGHT_PAREN) { | ||
| 62 | $left_paren_num--; | ||
| 63 | } | ||
| 64 | elsif ($left_paren_num <= 0) { | ||
| 65 | last; | ||
| 66 | } | ||
| 67 | } | ||
| 68 | } | ||
| 69 | elsif ($token_type == FUNCTION_DECL) { | ||
| 70 | for ($i++; $i < $token_num; $i++) { | ||
| 71 | my $token = $tokens->[$i]; | ||
| 72 | my $token_type = $token->{type}; | ||
| 73 | |||
| 74 | if ($token_type == FUNCTION || $token_type == NAMESPACE) { | ||
| 75 | push @word_blocks, [$token->{data}]; | ||
| 76 | } | ||
| 77 | elsif ($token_type == LEFT_BRACE) { | ||
| 78 | last; | ||
| 79 | } | ||
| 80 | } | ||
| 81 | } | ||
| 82 | |||
| 83 | for my $word_block (@word_blocks) { | ||
| 84 | for my $word (@$word_block) { | ||
| 85 | if (grep {$_ eq $word} @forbidden_words) { | ||
| 86 | push @violations, { | ||
| 87 | filename => $file, | ||
| 88 | line => $token->{line}, | ||
| 89 | description => DESC, | ||
| 90 | explanation => EXPL, | ||
| 91 | policy => __PACKAGE__, | ||
| 92 | }; | ||
| 93 | last; | ||
| 94 | } | ||
| 95 | } | ||
| 96 | } | ||
| 97 | } | ||
| 98 | |||
| 99 | return \@violations; | ||
| 100 | } | ||
| 101 | |||
| 102 | 1; | ||
| 103 | |||