line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Perl::Critic::Policy::Freenode::PackageMatchesFilename; |
2
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
645
|
use strict; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
32
|
|
4
|
1
|
|
|
1
|
|
5
|
use warnings; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
28
|
|
5
|
|
|
|
|
|
|
|
6
|
1
|
|
|
1
|
|
5
|
use Perl::Critic::Utils qw(:severities :classification :ppi); |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
50
|
|
7
|
1
|
|
|
1
|
|
1242
|
use Path::Tiny 'path'; |
|
1
|
|
|
|
|
11375
|
|
|
1
|
|
|
|
|
63
|
|
8
|
1
|
|
|
1
|
|
8
|
use parent 'Perl::Critic::Policy'; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
7
|
|
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
our $VERSION = '0.033'; |
11
|
|
|
|
|
|
|
|
12
|
1
|
|
|
1
|
|
75
|
use constant DESC => 'No package matching the module filename'; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
55
|
|
13
|
1
|
|
|
1
|
|
7
|
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
|
|
|
|
|
378
|
|
14
|
|
|
|
|
|
|
|
15
|
1
|
|
|
1
|
1
|
18
|
sub default_severity { $SEVERITY_HIGH } |
16
|
0
|
|
|
0
|
1
|
0
|
sub default_themes { 'freenode' } |
17
|
4
|
|
|
4
|
1
|
39237
|
sub applies_to { 'PPI::Document' } |
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
sub violates { |
20
|
4
|
|
|
4
|
1
|
50
|
my ($self, $elem, $doc) = @_; |
21
|
4
|
100
|
33
|
|
|
17
|
return () unless $doc->is_module and $doc->filename and $doc->filename =~ m/\.pm\z/; |
|
|
|
66
|
|
|
|
|
22
|
|
|
|
|
|
|
|
23
|
3
|
|
50
|
|
|
114
|
my $packages = $elem->find('PPI::Statement::Package') || []; |
24
|
|
|
|
|
|
|
|
25
|
3
|
|
|
|
|
41
|
my $filepath = path($doc->filename)->realpath; |
26
|
3
|
|
|
|
|
915
|
my $basename = $filepath->basename(qr/\.pm/); |
27
|
3
|
|
|
|
|
140
|
$filepath = $filepath->parent->child($basename); |
28
|
|
|
|
|
|
|
|
29
|
3
|
|
|
|
|
215
|
my $found_match; |
30
|
3
|
|
|
|
|
10
|
PKG: foreach my $package (@$packages) { |
31
|
3
|
|
|
|
|
14
|
my $namespace = $package->namespace; |
32
|
3
|
|
|
|
|
91
|
my $path_copy = $filepath; |
33
|
3
|
|
|
|
|
14
|
foreach my $part (reverse split '::', $namespace) { |
34
|
4
|
100
|
|
|
|
53
|
next PKG unless $part eq $path_copy->basename; |
35
|
3
|
|
|
|
|
74
|
$path_copy = $path_copy->parent; |
36
|
|
|
|
|
|
|
} |
37
|
2
|
|
|
|
|
81
|
$found_match = 1; |
38
|
2
|
|
|
|
|
7
|
last; |
39
|
|
|
|
|
|
|
} |
40
|
|
|
|
|
|
|
|
41
|
3
|
100
|
|
|
|
40
|
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> |