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