File Coverage

blib/lib/Perl/Critic/Policy/Documentation/RequirePackageMatchesPodName.pm
Criterion Covered Total %
statement 45 46 97.8
branch 9 10 90.0
condition n/a
subroutine 13 13 100.0
pod 5 6 83.3
total 72 75 96.0


line stmt bran cond sub pod time code
1             package Perl::Critic::Policy::Documentation::RequirePackageMatchesPodName;
2              
3 40     40   27111 use 5.010001;
  40         161  
4              
5 40     40   230 use strict;
  40         103  
  40         892  
6 40     40   272 use warnings;
  40         351  
  40         1082  
7              
8 40     40   218 use Readonly;
  40         113  
  40         2365  
9 40     40   279 use English qw{ -no_match_vars };
  40         88  
  40         224  
10 40     40   15344 use Perl::Critic::Utils qw{ :severities :classification };
  40         102  
  40         1941  
11 40     40   13588 use parent 'Perl::Critic::Policy';
  40         117  
  40         271  
12              
13             our $VERSION = '1.148';
14              
15             #-----------------------------------------------------------------------------
16              
17             Readonly::Scalar my $PKG_RX => qr{ [[:alpha:]](?:[\w:\']*\w)? }xms;
18             Readonly::Scalar my $DESC =>
19             q{Pod NAME on line %d does not match the package declaration};
20             Readonly::Scalar my $EXPL => q{};
21              
22             #-----------------------------------------------------------------------------
23              
24 105     105 0 1653 sub supported_parameters { return () }
25 78     78 1 380 sub default_severity { return $SEVERITY_LOWEST }
26 84     84 1 389 sub default_themes { return qw( core cosmetic ) }
27 44     44 1 153 sub applies_to { return 'PPI::Document' }
28              
29             #-----------------------------------------------------------------------------
30              
31             sub prepare_to_scan_document {
32 46     46 1 180 my ( $self, $document ) = @_;
33              
34             # idea: force NAME to match the file name in programs?
35 46         206 return $document->is_module(); # mismatch is normal in program entry points
36             }
37              
38             sub violates {
39 44     44 1 213 my ( $self, $elem, $doc ) = @_;
40              
41             # No POD means no violation
42 44         155 my $pods_ref = $doc->find('PPI::Token::Pod');
43 44 100       254 return if !$pods_ref;
44              
45 16         35 for my $pod (@{$pods_ref}) {
  16         49  
46 16         52 my $content = $pod->content;
47              
48 16 100       153 next if $content !~ m{^=head1 [ \t]+ NAME [ \t]*$ \s*}cgxms;
49              
50 13         52 my $line_number = $pod->line_number() + (
51             substr( $content, 0, $LAST_MATCH_START[0] + 1 ) =~ tr/\n/\n/ );
52              
53 13         346 my ($pod_pkg) = $content =~ m{\G (\S+) }cgxms;
54              
55 13 50       42 if (!$pod_pkg) {
56 0         0 return $self->violation( sprintf( $DESC, $line_number ),
57             q{Empty name declaration}, $pod );
58             }
59              
60             # idea: worry about POD escapes?
61 13         50 $pod_pkg =~ s{\A [BCIL]<(.*)>\z}{$1}gxms; # unwrap
62 13         31 $pod_pkg =~ s{\'}{::}gxms; # perl4 -> perl5
63              
64 13 100       22 foreach my $stmt ( @{ $doc->find('PPI::Statement::Package') || [] } ) {
  13         41  
65 13         46 my $pkg = $stmt->namespace();
66 13         380 $pkg =~ s{\'}{::}gxms;
67 13 100       70 return if $pkg eq $pod_pkg;
68             }
69              
70 4         48 return $self->violation( sprintf( $DESC, $line_number ),
71             $EXPL, $pod );
72             }
73              
74 3         14 return; # no NAME section found
75             }
76              
77             1;
78              
79             __END__
80              
81             #-----------------------------------------------------------------------------
82              
83             =pod
84              
85             =head1 NAME
86              
87             Perl::Critic::Policy::Documentation::RequirePackageMatchesPodName - The C<=head1 NAME> section should match the package.
88              
89              
90             =head1 AFFILIATION
91              
92             This Policy is part of the core L<Perl::Critic|Perl::Critic> distribution.
93              
94              
95             =head1 DESCRIPTION
96              
97              
98             =head1 CONFIGURATION
99              
100             This Policy is not configurable except for the standard options.
101              
102              
103             =head1 AUTHOR
104              
105             Chris Dolan <cdolan@cpan.org>
106              
107              
108             =head1 COPYRIGHT
109              
110             Copyright (c) 2008-2011 Chris Dolan
111              
112             This program is free software; you can redistribute it and/or modify
113             it under the same terms as Perl itself. The full text of this license
114             can be found in the LICENSE file included with this module
115              
116             =cut
117              
118             # Local Variables:
119             # mode: cperl
120             # cperl-indent-level: 4
121             # fill-column: 78
122             # indent-tabs-mode: nil
123             # c-indentation-style: bsd
124             # End:
125             # ex: set ts=8 sts=4 sw=4 tw=78 ft=perl expandtab shiftround :