blib/lib/Perl/Critic/Policy/Modules/RequireFilenameMatchesPackage.pm

filter: Statementstmt 70.3% 38 / 54 Branchbran 28.5% 4 / 14 Conditioncond 16.6% 1 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 100% 13 / 13 Podpod 83.3% 5 / 6 totaltotal 62.8% 61 / 97 CC 10 SCAR 30.4
CC
10
Cov
58%
violates
45.0
BEGIN
1.0
BEGIN
1.0
CRAP 21.0
« blib/lib/Test/Perl/Critic/Policy.pm ↑ Summary blib/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.150';
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 75 ▶sub default_severity { return $SEVERITY_HIGHEST }
Subroutine default_severity: called CC=1 SCAR=0.0
Pod documented
25 74 ▶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 sub prepare_to_scan_document {
31 37 ▶ my ( $self, $document ) = @_;
Subroutine prepare_to_scan_document: called CC=1 SCAR=0.0
Pod documented
32 37 return $document->is_module(); # Must be a library or module.
33 }
34  
35 #-----------------------------------------------------------------------------
36  
37 sub violates {
38 36 ▶ my ($self, $elem, $doc) = @_;
Subroutine violates: called CC=10 SCAR=38.1
Pod documented
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
BranchTrueFalseTotal
unless $pkg_node 6 30 36
43 30 my $pkg = $pkg_node->namespace();
44 30 ▶ return if $pkg eq 'main';
Branch: 1 branch
BranchTrueFalseTotal
if $pkg eq "main" 0 30 30
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
l!l&&r!l&&!r
0030
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
ABresult
1X1
011
000
50 30 ▶ return if not $filename;
Branch: 1 branch
BranchTrueFalseTotal
unless $filename 30 0 30
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
BranchTrueFalseTotal
if (@pkg_parts and @path_parts) 0 0 0
Condition0%0/3
@pkg_parts and @path_parts
!ll&&!rl&&r
000
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
ABresult
0X0
100
111
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
BranchTrueFalseTotal
if ($pkg_part eq $path_part) 0 0 0
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
BranchTrueFalseTotal
if $path_part =~ /\W/msx 0 0 0
72  
73 # Mismatched name
74 0 return $self->violation( $DESC, $EXPL, $pkg_node );
75 }
76  
77 0 ▶ return if $matched_any;
Branch: 1 branch
BranchTrueFalseTotal
if $matched_any 0 0 0
78 0 return $self->violation( $DESC, $EXPL, $pkg_node );
79 }
80  
81 40 1;
82  
83 #-----------------------------------------------------------------------------
84  
85 __END__
86  
87 =pod
88  
89 =head1 NAME
90  
91 Perl::Critic::Policy::Modules::RequireFilenameMatchesPackage - Package declaration must match filename.
92  
93  
94 =head1 AFFILIATION
95  
96 This Policy is part of the core L<Perl::Critic|Perl::Critic>
97 distribution.
98  
99  
100 =head1 DESCRIPTION
101  
102 The package declaration should always match the name of the file that contains
103 it. For example, C<package Foo::Bar;> should be in a file called C<Bar.pm>.
104 This makes it easier for developers to figure out which file a symbol comes
105 from when they see it in your code. For instance, when you see C<<
106 Foo::Bar->new() >>, you should be able to find the class definition for a
107 C<Foo::Bar> in a file called F<Bar.pm>
108  
109 Therefore, this Policy requires the last component of the first package name
110 declared in the file to match the physical filename. Or if C<#line>
111 directives are used, then it must match the logical filename defined by the
112 prevailing C<#line> directive at the point of the package declaration. Here
113 are some examples:
114  
115 # Any of the following in file "Foo/Bar/Baz.pm":
116 package Foo::Bar::Baz; # ok
117 package Baz; # ok
118 package Nuts; # not ok (doesn't match physical filename)
119  
120 # using #line directives in file "Foo/Bar/Baz.pm":
121 #line 1 Nuts.pm
122 package Nuts; # ok
123 package Baz; # not ok (contradicts #line directive)
124  
125 If the file is not deemed to be a module, then this Policy does not apply.
126 Also, if the first package namespace found in the file is "main" then this
127 Policy does not apply.
128  
129 =head1 CONFIGURATION
130  
131 This Policy is not configurable except for the standard options.
132  
133  
134 =head1 AUTHOR
135  
136 Chris Dolan <cdolan@cpan.org>
137  
138  
139 =head1 COPYRIGHT
140  
141 Copyright (c) 2006-2011 Chris Dolan.
142  
143 This program is free software; you can redistribute it and/or modify
144 it under the same terms as Perl itself.
145  
146 =cut
147  
148 ##############################################################################
149 # Local Variables:
150 # mode: cperl
151 # cperl-indent-level: 4
152 # fill-column: 78
153 # indent-tabs-mode: nil
154 # c-indentation-style: bsd
155 # End:
156 # ex: set ts=8 sts=4 sw=4 tw=78 ft=perl expandtab shiftround :
« blib/lib/Test/Perl/Critic/Policy.pm ↑ Summary blib/lib/Perl/Critic/Policy/BuiltinFunctions/ProhibitUselessTopic.pm »