| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package Perl::Critic::Policy::Freenode::PackageMatchesFilename; | 
| 2 |  |  |  |  |  |  |  | 
| 3 | 1 |  |  | 1 |  | 516 | use strict; | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 24 |  | 
| 4 | 1 |  |  | 1 |  | 4 | use warnings; | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 24 |  | 
| 5 |  |  |  |  |  |  |  | 
| 6 | 1 |  |  | 1 |  | 4 | use Perl::Critic::Utils qw(:severities :classification :ppi); | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 41 |  | 
| 7 | 1 |  |  | 1 |  | 924 | use Path::Tiny 'path'; | 
|  | 1 |  |  |  |  | 8951 |  | 
|  | 1 |  |  |  |  | 57 |  | 
| 8 | 1 |  |  | 1 |  | 8 | use parent 'Perl::Critic::Policy'; | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 6 |  | 
| 9 |  |  |  |  |  |  |  | 
| 10 |  |  |  |  |  |  | our $VERSION = '0.032'; | 
| 11 |  |  |  |  |  |  |  | 
| 12 | 1 |  |  | 1 |  | 67 | use constant DESC => 'No package matching the module filename'; | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 64 |  | 
| 13 | 1 |  |  | 1 |  | 6 | 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 |  |  |  |  | 299 |  | 
| 14 |  |  |  |  |  |  |  | 
| 15 | 1 |  |  | 1 | 1 | 16 | sub default_severity { $SEVERITY_HIGH } | 
| 16 | 0 |  |  | 0 | 1 | 0 | sub default_themes { 'freenode' } | 
| 17 | 4 |  |  | 4 | 1 | 50261 | sub applies_to { 'PPI::Document' } | 
| 18 |  |  |  |  |  |  |  | 
| 19 |  |  |  |  |  |  | sub violates { | 
| 20 | 4 |  |  | 4 | 1 | 49 | my ($self, $elem, $doc) = @_; | 
| 21 | 4 | 100 | 33 |  |  | 18 | return () unless $doc->is_module and $doc->filename and $doc->filename =~ m/\.pm\z/; | 
|  |  |  | 66 |  |  |  |  | 
| 22 |  |  |  |  |  |  |  | 
| 23 | 3 |  | 50 |  |  | 106 | my $packages = $elem->find('PPI::Statement::Package') || []; | 
| 24 |  |  |  |  |  |  |  | 
| 25 | 3 |  |  |  |  | 34 | my $filepath = path($doc->filename)->realpath; | 
| 26 | 3 |  |  |  |  | 1176 | my $basename = $filepath->basename(qr/\.pm/); | 
| 27 | 3 |  |  |  |  | 122 | $filepath = $filepath->parent->child($basename); | 
| 28 |  |  |  |  |  |  |  | 
| 29 | 3 |  |  |  |  | 184 | my $found_match; | 
| 30 | 3 |  |  |  |  | 9 | PKG: foreach my $package (@$packages) { | 
| 31 | 3 |  |  |  |  | 15 | my $namespace = $package->namespace; | 
| 32 | 3 |  |  |  |  | 116 | my $path_copy = $filepath; | 
| 33 | 3 |  |  |  |  | 13 | foreach my $part (reverse split '::', $namespace) { | 
| 34 | 4 | 100 |  |  |  | 47 | next PKG unless $part eq $path_copy->basename; | 
| 35 | 3 |  |  |  |  | 67 | $path_copy = $path_copy->parent; | 
| 36 |  |  |  |  |  |  | } | 
| 37 | 2 |  |  |  |  | 67 | $found_match = 1; | 
| 38 | 2 |  |  |  |  | 7 | last; | 
| 39 |  |  |  |  |  |  | } | 
| 40 |  |  |  |  |  |  |  | 
| 41 | 3 | 100 |  |  |  | 36 | return () if $found_match; | 
| 42 | 1 |  |  |  |  | 13 | return $self->violation(DESC, EXPL, $elem); | 
| 43 |  |  |  |  |  |  | } | 
| 44 |  |  |  |  |  |  |  | 
| 45 |  |  |  |  |  |  | 1; | 
| 46 |  |  |  |  |  |  |  | 
| 47 |  |  |  |  |  |  | =head1 NAME | 
| 48 |  |  |  |  |  |  |  | 
| 49 |  |  |  |  |  |  | Perl::Critic::Policy::Freenode::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::Freenode>. | 
| 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> |