| 1 | package Perl::Critic::Policy::Community::PackageMatchesFilename; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||||
| 3 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 4 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||||||||||||||||
| 6 | 1 | ▶ | use Perl::Critic::Utils qw(:severities :classification :ppi); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 7 | 1 | ▶ | use Path::Tiny 'path'; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 8 | 1 | ▶ | use parent 'Perl::Critic::Policy'; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||||||||||||||||
| 10 | 1 | our $VERSION = 'v1.0.4'; | |||||||||||||||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||||||||||||||||
| 12 | 1 | ▶ | use constant DESC => 'No package matching the module filename'; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 13 | 1 | ▶ | use constant EXPL => 'A Perl module file is expected to contain a matching package name, so it can be used after loading it from the filesystem. A module file that doesn\'t contain a matching package name usually indicates an error.'; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||||||||||||||||
| 15 | 1 | ▶ | sub default_severity { $SEVERITY_HIGH } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
default_severity: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 16 | 0 | ▶ | sub default_themes { 'community' } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
default_themes: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 17 | 4 | ▶ | sub applies_to { 'PPI::Document' } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
applies_to: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||||||||||||||||
| 19 | 4 | ▶ | sub violates { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
violates: called CC=9 SCAR=22.2
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 20 | 4 | my ($self, $elem, $doc) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 21 | 4 | ▶ | return () unless $doc->is_module and $doc->filename and $doc->filename =~ m/\.pm\z/; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%3/6
$doc->is_module and $doc->filename and $doc->filename =~ /\.pm\z/
$doc->is_module and $doc->filename
MC/DC33%1/3
$doc->is_module and $doc->filename and $doc->filename =~ /\.pm\z/
$doc->is_module
$doc->filename
$doc->filename =~ /\.pm\z/
Truth table50%2/4
A and B and C
A= $doc->is_moduleB= $doc->filenameC= $doc->filename =~ /\.pm\z/
| |||||||||||||||||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||||||||||||||||
| 23 | 3 | ▶ | my $packages = $elem->find('PPI::Statement::Package') || []; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$elem->find('PPI::Statement::Package') || []
MC/DC0%0/1
$elem->find('PPI::Statement::Package') || []
$elem->find('PPI::Statement::Package')
Truth table50%1/2
A
A= $elem->find('PPI::Statement::Package')
| |||||||||||||||||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||||||||||||||||
| 25 | 3 | my $filepath = path($doc->filename)->realpath; | |||||||||||||||||||||||||||||||||||||||||
| 26 | 3 | my $basename = $filepath->basename(qr/\.pm/); | |||||||||||||||||||||||||||||||||||||||||
| 27 | 3 | $filepath = $filepath->parent->child($basename); | |||||||||||||||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||||||||||||||||
| 29 | 3 | my $found_match; | |||||||||||||||||||||||||||||||||||||||||
| 30 | 3 | PKG: foreach my $package (@$packages) { | |||||||||||||||||||||||||||||||||||||||||
| 31 | 3 | my $namespace = $package->namespace; | |||||||||||||||||||||||||||||||||||||||||
| 32 | 3 | my $path_copy = $filepath; | |||||||||||||||||||||||||||||||||||||||||
| 33 | 3 | foreach my $part (reverse split /::/, $namespace) { | |||||||||||||||||||||||||||||||||||||||||
| 34 | 4 | ▶ | next PKG unless $part eq $path_copy->basename; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 35 | 3 | $path_copy = $path_copy->parent; | |||||||||||||||||||||||||||||||||||||||||
| 36 | } | ||||||||||||||||||||||||||||||||||||||||||
| 37 | 2 | $found_match = 1; | |||||||||||||||||||||||||||||||||||||||||
| 38 | 2 | last; | |||||||||||||||||||||||||||||||||||||||||
| 39 | } | ||||||||||||||||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||||||||||||||||
| 41 | 3 | ▶ | return () if $found_match; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 42 | 1 | return $self->violation(DESC, EXPL, $elem); | |||||||||||||||||||||||||||||||||||||||||
| 43 | } | ||||||||||||||||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||||||||||||||||
| 45 | 1 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||||||||||||||||
| 47 | =head1 NAME | ||||||||||||||||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||||||||||||||||
| 49 | Perl::Critic::Policy::Community::PackageMatchesFilename - Module files should | ||||||||||||||||||||||||||||||||||||||||||
| 50 | declare a package matching the filename | ||||||||||||||||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||||||||||||||||
| 52 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||||||||||||||||
| 54 | Perl modules are normally loaded by C<require> (possibly via C<use> or C<no>). | ||||||||||||||||||||||||||||||||||||||||||
| 55 | When given a module name, C<require> will translate this into a filename and | ||||||||||||||||||||||||||||||||||||||||||
| 56 | then load whatever that file contains. The file doesn't need to actually | ||||||||||||||||||||||||||||||||||||||||||
| 57 | contain a package matching the module name initially given to C<require>, but | ||||||||||||||||||||||||||||||||||||||||||
| 58 | this can be confusing if later operations (including C<import> as called by | ||||||||||||||||||||||||||||||||||||||||||
| 59 | C<use>) expect the package to exist. Furthermore, the absence of such a package | ||||||||||||||||||||||||||||||||||||||||||
| 60 | is usually an indicator of a typo in the package name. | ||||||||||||||||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||||||||||||||||
| 62 | ## in file My/Module.pm | ||||||||||||||||||||||||||||||||||||||||||
| 63 | package My::Module; | ||||||||||||||||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||||||||||||||||
| 65 | This policy is similar to the core policy | ||||||||||||||||||||||||||||||||||||||||||
| 66 | L<Perl::Critic::Policy::Modules::RequireFilenameMatchesPackage>, but only | ||||||||||||||||||||||||||||||||||||||||||
| 67 | requires that one package name within a module file matches the filename. | ||||||||||||||||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||||||||||||||||
| 69 | =head1 AFFILIATION | ||||||||||||||||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||||||||||||||||
| 71 | This policy is part of L<Perl::Critic::Community>. | ||||||||||||||||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||||||||||||||||
| 73 | =head1 CONFIGURATION | ||||||||||||||||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||||||||||||||||
| 75 | This policy is not configurable except for the standard options. | ||||||||||||||||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||||||||||||||||
| 77 | =head1 AUTHOR | ||||||||||||||||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||||||||||||||||
| 79 | Dan Book, C<dbook@cpan.org> | ||||||||||||||||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||||||||||||||||
| 81 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||||||||||||||||
| 83 | Copyright 2015, Dan Book. | ||||||||||||||||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||||||||||||||||
| 85 | This library is free software; you may redistribute it and/or modify it under | ||||||||||||||||||||||||||||||||||||||||||
| 86 | the terms of the Artistic License version 2.0. | ||||||||||||||||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||||||||||||||||
| 88 | =head1 SEE ALSO | ||||||||||||||||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||||||||||||||||
| 90 | L<Perl::Critic> | ||||||||||||||||||||||||||||||||||||||||||