« lib/Test/Perl/Critic/Policy.pm
↑ Summary
lib/Perl/Critic/Policy/BuiltinFunctions/ProhibitUselessTopic.pm »
| 1 | package Perl::Critic::Policy::Modules::RequireFilenameMatchesPackage; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 40 | ▶ | use 5.010001; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 40 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 40 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 40 | ▶ | use Readonly; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 40 | ▶ | use File::Spec; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | 40 | ▶ | use Perl::Critic::Utils qw{ :characters :severities }; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | 40 | ▶ | use parent 'Perl::Critic::Policy'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | 40 | our $VERSION = '1.156'; | |||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | #----------------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | 40 | Readonly::Scalar my $DESC => q{Package declaration must match filename}; | |||||||||||||||||||||||||||
| 19 | 40 | Readonly::Scalar my $EXPL => q{Correct the filename or package statement}; | |||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | #----------------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | 90 | ▶ | sub supported_parameters { return () } | ||||||||||||||||||||||||||
|
Subroutine
supported_parameters: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 24 | 74 | ▶ | sub default_severity { return $SEVERITY_HIGHEST } | ||||||||||||||||||||||||||
|
Subroutine
default_severity: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 25 | 73 | ▶ | sub default_themes { return qw(core bugs) } | ||||||||||||||||||||||||||
|
Subroutine
default_themes: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 26 | 36 | ▶ | sub applies_to { return 'PPI::Document' } | ||||||||||||||||||||||||||
|
Subroutine
applies_to: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | #----------------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | 37 | ▶ | sub prepare_to_scan_document { | ||||||||||||||||||||||||||
|
Subroutine
prepare_to_scan_document: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 31 | 37 | my ( undef, $document ) = @_; | |||||||||||||||||||||||||||
| 32 | 37 | return $document->is_module(); # Must be a library or module. | |||||||||||||||||||||||||||
| 33 | } | ||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | #----------------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | 36 | ▶ | sub violates { | ||||||||||||||||||||||||||
|
Subroutine
violates: called CC=10 SCAR=38.1
Pod
documented
| |||||||||||||||||||||||||||||
| 38 | 36 | my ($self, undef, $doc) = @_; | |||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | # 'Foo::Bar' -> ('Foo', 'Bar') | ||||||||||||||||||||||||||||
| 41 | 36 | my $pkg_node = $doc->find_first('PPI::Statement::Package'); | |||||||||||||||||||||||||||
| 42 | 36 | ▶ | return if not $pkg_node; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 43 | 30 | my $pkg = $pkg_node->namespace(); | |||||||||||||||||||||||||||
| 44 | 30 | ▶ | return if $pkg eq 'main'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 45 | 30 | my @pkg_parts = split m/(?:\'|::)/xms, $pkg; | |||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | # 'lib/Foo/Bar.pm' -> ('lib', 'Foo', 'Bar') | ||||||||||||||||||||||||||||
| 49 | 30 | ▶ | my $filename = $pkg_node->logical_filename() || $doc->filename(); | ||||||||||||||||||||||||||
|
Condition33%1/3
$pkg_node->logical_filename || $doc->filename
MC/DC0%0/2
$pkg_node->logical_filename || $doc->filename
$pkg_node->logical_filename
$doc->filename
Truth table33%1/3
A || B
A= $pkg_node->logical_filenameB= $doc->filename
| |||||||||||||||||||||||||||||
| 50 | 30 | ▶ | return if not $filename; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | 0 | my @path = File::Spec->splitpath($filename); | |||||||||||||||||||||||||||
| 53 | 0 | $filename = $path[2]; | |||||||||||||||||||||||||||
| 54 | 0 | $filename =~ s/ [.] \w+ \z //xms; | |||||||||||||||||||||||||||
| 55 | my @path_parts = | ||||||||||||||||||||||||||||
| 56 | 0 | grep {$_ ne $EMPTY} File::Spec->splitdir($path[1]), $filename; | |||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | # To succeed, at least the lastmost must match | ||||||||||||||||||||||||||||
| 60 | # Beyond that, the search terminates if a dirname is an impossible package name | ||||||||||||||||||||||||||||
| 61 | 0 | my $matched_any; | |||||||||||||||||||||||||||
| 62 | 0 | ▶ | while (@pkg_parts && @path_parts) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
@pkg_parts and @path_parts
MC/DC0%0/2
@pkg_parts and @path_parts
@pkg_parts
@path_parts
Truth table0%0/3
A and B
A= @pkg_partsB= @path_parts
| |||||||||||||||||||||||||||||
| 63 | 0 | my $pkg_part = pop @pkg_parts; | |||||||||||||||||||||||||||
| 64 | 0 | my $path_part = pop @path_parts; | |||||||||||||||||||||||||||
| 65 | 0 | ▶ | if ($pkg_part eq $path_part) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 66 | 0 | $matched_any = 1; | |||||||||||||||||||||||||||
| 67 | 0 | next; | |||||||||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | # if it's a path that's not a possible package (like 'Foo-Bar-1.00'), that's OK | ||||||||||||||||||||||||||||
| 71 | 0 | ▶ | last if ($path_part =~ m/\W/xms); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | # Mismatched name | ||||||||||||||||||||||||||||
| 74 | 0 | return $self->violation( $DESC, $EXPL, $pkg_node ); | |||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | 0 | ▶ | return if $matched_any; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 78 | 0 | return $self->violation( $DESC, $EXPL, $pkg_node ); | |||||||||||||||||||||||||||
| 79 | } | ||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | 40 | 1; | |||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | #----------------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | __END__ | ||||||||||||||||||||||||||||