Benchmark-Perl-Formance-Cargo

 view release on metacpan or  search on metacpan

share/PerlCritic/Critic/Policy/Modules/RequireFilenameMatchesPackage.pm  view on Meta::CPAN

##############################################################################
#      $URL: http://perlcritic.tigris.org/svn/perlcritic/trunk/distributions/Perl-Critic/lib/Perl/Critic/Policy/Modules/RequireFilenameMatchesPackage.pm $
#     $Date: 2010-06-22 16:14:07 -0400 (Tue, 22 Jun 2010) $
#   $Author: clonezone $
# $Revision: 3843 $
##############################################################################

package # hide from indexer
  Perl::Critic::Policy::Modules::RequireFilenameMatchesPackage;

use 5.006001;
use strict;
use warnings;
use Readonly;

use File::Spec;

use Perl::Critic::Utils qw{ :characters :severities };

use base 'Perl::Critic::Policy';

our $VERSION = '1.108';

#-----------------------------------------------------------------------------

Readonly::Scalar my $DESC => q{Package declaration must match filename};
Readonly::Scalar my $EXPL => q{Correct the filename or package}.q{ statement};

#-----------------------------------------------------------------------------

sub supported_parameters { return ()                }
sub default_severity     { return $SEVERITY_HIGHEST }
sub default_themes       { return qw(core bugs)     }
sub applies_to           { return 'PPI::Document'   }

#-----------------------------------------------------------------------------

sub prepare_to_scan_document {
    my ( $self, $document ) = @_;
    return $document->is_module();   # Must be a library or module.
}

#-----------------------------------------------------------------------------

sub violates {
    my ($self, $elem, $doc) = @_;

    # 'Foo::Bar' -> ('Foo', 'Bar')
    my $pkg_node = $doc->find_first('PPI::Statement::Package');
    return if not $pkg_node;
    my $pkg = $pkg_node->namespace();
    return if $pkg eq 'main';
    my @pkg_parts = split m/(?:\'|::)/xms, $pkg;


    # 'lib/Foo/Bar.pm' -> ('lib', 'Foo', 'Bar')
    my $filename = $pkg_node->logical_filename() || $doc->filename();
    return if not $filename;

    my @path = File::Spec->splitpath($filename);
    $filename = $path[2];
    $filename =~ s/ [.] \w+ \z //xms;
    my @path_parts =
        grep {$_ ne $EMPTY} File::Spec->splitdir($path[1]), $filename;


    # To succeed, at least the lastmost must match
    # Beyond that, the search terminates if a dirname is an impossible package name
    my $matched_any;
    while (@pkg_parts && @path_parts) {
        my $pkg_part = pop @pkg_parts;
        my $path_part = pop @path_parts;
        if ($pkg_part eq $path_part) {
            $matched_any = 1;
            next;
        }

        # if it's a path that's not a possible package (like 'Foo-Bar-1.00'), that's OK
        last if ($path_part =~ m/\W/xms);

        # Mismatched name
        return $self->violation( $DESC, $EXPL, $pkg_node );
    }

    return if $matched_any;
    return $self->violation( $DESC, $EXPL, $pkg_node );
}

1;

#-----------------------------------------------------------------------------

__END__

=pod

=head1 NAME

Perl::Critic::Policy::Modules::RequireFilenameMatchesPackage - Package declaration must match filename.


=head1 AFFILIATION

This Policy is part of the core L<Perl::Critic|Perl::Critic>
distribution.


=head1 DESCRIPTION

The package declaration should always match the name of the file that contains
it.  For example, C<package Foo::Bar;> should be in a file called C<Bar.pm>.
This makes it easier for developers to figure out which file a symbol comes
from when they see it in your code.  For instance, when you see C<<



( run in 0.351 second using v1.01-cache-2.11-cpan-d7a12ab2c7f )