« blib/lib/Perl/Lint/Policy/Modules/RequireEndWithOne.pm
↑ Summary
blib/lib/Perl/Lint/Policy/NamingConventions/ProhibitAmbiguousNames.pm »
| 1 | package Perl::Lint::Policy::Modules::RequireExplicitPackage; | ||
| 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 List::Util qw/any/; |
|
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 { | ||
| 9 | DESC => 'Code not contained in explicit package', | ||
| 10 | EXPL => 'Violates encapsulation', | ||
| 11 | }; | ||
| 12 | |||
| 13 | sub evaluate { | ||
| 14 | my ($class, $file, $tokens, $src, $args) = @_; | ||
| 15 | |||
| 16 | my @violations; | ||
| 17 | my $exempt_scripts = $args->{require_explicit_package}->{exempt_scripts}; | ||
| 18 | my $allow_import_of = $args->{require_explicit_package}->{allow_import_of}; | ||
| 19 | my $token = $tokens->[0]; | ||
| 20 | if (($exempt_scripts || !$token || $token->{type} == PACKAGE) && !$allow_import_of) { | ||
| 21 | return []; | ||
| 22 | } | ||
| 23 | else { | ||
| 24 | for (my $i = 0; my $token = $tokens->[$i]; $i++) { | ||
| 25 | my $token_type = $token->{type}; | ||
| 26 | my $token_data = $token->{data}; | ||
| 27 | |||
| 28 | if ($token_type == USE_DECL) { | ||
| 29 | my $used_name = ''; | ||
| 30 | for ($i++; my $token = $tokens->[$i]; $i++) { | ||
| 31 | my $token_type = $token->{type}; | ||
| 32 | my $token_data = $token->{data}; | ||
| 33 | if ( | ||
| 34 | $token_type != NAMESPACE && | ||
| 35 | $token_type != NAMESPACE_RESOLVER && | ||
| 36 | $token_type != USED_NAME | ||
| 37 | ) { | ||
| 38 | last; | ||
| 39 | } | ||
| 40 | else { | ||
| 41 | $used_name .= $token_data; | ||
| 42 | } | ||
| 43 | } | ||
| 44 | if (!any {$_ eq $used_name} @$allow_import_of) { | ||
| 45 | push @violations, { | ||
| 46 | filename => $file, | ||
| 47 | line => $token->{line}, | ||
| 48 | description => DESC, | ||
| 49 | explanation => EXPL, | ||
| 50 | policy => __PACKAGE__, | ||
| 51 | }; | ||
| 52 | last; | ||
| 53 | } | ||
| 54 | } | ||
| 55 | elsif ($token_type == PACKAGE) { | ||
| 56 | last; | ||
| 57 | } | ||
| 58 | else { | ||
| 59 | push @violations, { | ||
| 60 | filename => $file, | ||
| 61 | line => $token->{line}, | ||
| 62 | description => DESC, | ||
| 63 | explanation => EXPL, | ||
| 64 | policy => __PACKAGE__, | ||
| 65 | }; | ||
| 66 | last; | ||
| 67 | } | ||
| 68 | } | ||
| 69 | } | ||
| 70 | |||
| 71 | return \@violations; | ||
| 72 | } | ||
| 73 | |||
| 74 | 1; | ||
| 75 | |||