File Coverage

blib/lib/Perl/Critic/Policy/Community/PackageMatchesFilename.pm
Criterion Covered Total %
statement 40 41 97.5
branch 6 6 100.0
condition 4 8 50.0
subroutine 10 11 90.9
pod 4 4 100.0
total 64 70 91.4


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