File Coverage

blib/lib/Perl/Critic/Policy/Documentation/ProhibitLinkToSelf.pm
Criterion Covered Total %
statement 70 72 97.2
branch 16 20 80.0
condition 2 6 33.3
subroutine 18 18 100.0
pod 1 1 100.0
total 107 117 91.4


line stmt bran cond sub pod time code
1             # Copyright 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2019, 2021 Kevin Ryde
2              
3             # This file is part of Perl-Critic-Pulp.
4              
5             # Perl-Critic-Pulp is free software; you can redistribute it and/or modify
6             # it under the terms of the GNU General Public License as published by the
7             # Free Software Foundation; either version 3, or (at your option) any later
8             # version.
9             #
10             # Perl-Critic-Pulp is distributed in the hope that it will be useful, but
11             # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12             # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13             # for more details.
14             #
15             # You should have received a copy of the GNU General Public License along
16             # with Perl-Critic-Pulp. If not, see <http://www.gnu.org/licenses/>.
17              
18              
19             # perlcritic -s ProhibitLinkToSelf ProhibitLinkToSelf.pm
20              
21              
22             package Perl::Critic::Policy::Documentation::ProhibitLinkToSelf;
23 40     40   35170 use 5.006;
  40         238  
24 40     40   241 use strict;
  40         88  
  40         809  
25 40     40   204 use warnings;
  40         93  
  40         1117  
26 40     40   217 use base 'Perl::Critic::Policy';
  40         94  
  40         4602  
27 40     40   184139 use Perl::Critic::Utils;
  40         108  
  40         747  
28              
29             # uncomment this to run the ### lines
30             #use Devel::Comments;
31              
32             our $VERSION = 99;
33              
34 40     40   37070 use constant supported_parameters => ();
  40         122  
  40         2735  
35 40     40   312 use constant default_severity => $Perl::Critic::Utils::SEVERITY_LOW;
  40         126  
  40         2404  
36 40     40   261 use constant default_themes => qw(pulp cosmetic);
  40         84  
  40         2482  
37 40     40   360 use constant applies_to => 'PPI::Document';
  40         87  
  40         5562  
38              
39             sub violates {
40 6     6 1 554631 my ($self, $elem, $document) = @_;
41             # ### ProhibitLinkToSelf on: $elem->content
42              
43 6         228 my $parser = Perl::Critic::Pulp::PodParser::ProhibitLinkToSelf->new
44             (policy => $self);
45 6         44 $parser->parse_from_elem ($elem);
46 6         45 return $parser->violations;
47             }
48              
49             package Perl::Critic::Pulp::PodParser::ProhibitLinkToSelf;
50 40     40   305 use strict;
  40         176  
  40         1232  
51 40     40   259 use warnings;
  40         138  
  40         1352  
52 40     40   771 use Pod::ParseLink;
  40         1016  
  40         2241  
53 40     40   268 use base 'Perl::Critic::Pulp::PodParser';
  40         162  
  40         32304  
54              
55             my %command_non_text = (for => 1,
56             begin => 1,
57             end => 1,
58             # cut => 1, # not seen unless -process_cut_cmd
59             );
60             sub command {
61 8     8   1005 my $self = shift;
62 8         31 my ($command, $text, $linenum, $paraobj) = @_;
63 8         63 $self->SUPER::command(@_); # maintain 'in_begin'
64              
65             # if ($command eq 'for'
66             # && $text =~ /^ProhibitLinkToSelf\b\s*(.*)/) {
67             # my $directive = $1;
68             # ### $directive
69             # if ($directive =~ /^allow next( (\d+))?/) {
70             # # numbered "allow next 5" means up to that many following,
71             # # unnumbered "allow next" means one following
72             # $self->{'allow_next'} = (defined $2 ? $2 : 1);
73             # }
74              
75 8 100       37 if ($command eq 'head1') {
76 7         46 $self->{'in_name'} = ($text =~ /^\s*NAME\b/);
77 7         46 $self->{'in_see_also'} = ($text =~ /^\s*SEE\s+ALSO\b/);
78             ### in_name now: $self->{'in_name'}
79             ### in_see_also: $self->{'in_see_also'}
80             }
81              
82 8 50       39 unless ($command_non_text{$command}) {
83             # padded for the column number right, the leading spaces do no harm here
84 8         55 _check_text ($self,
85             (' ' x (length($command)+1)) . $text,
86             $linenum,
87             $paraobj);
88             }
89              
90 8         177 return '';
91             }
92              
93             sub textblock {
94 11     11   791 my ($self, $text, $linenum, $paraobj) = @_;
95             ### textblock(): "linenum=$linenum"
96             ### $text
97              
98             # "=begin :foo" is markup, check it. Other =begin is not markup.
99 11 50 33     59 unless ($self->{'in_begin'} eq '' || $self->{'in_begin'} =~ /^:/) {
100 0         0 return '';
101             }
102              
103 11         41 my $str = _check_text ($self, $text, $linenum, $paraobj);
104             ### interpolated: $str
105 11 100       56 if ($self->{'in_name'}) {
106 10 100       62 if ($str =~ /^\s*([[:word:]:]+)\s*-/) {
107             ### add own package name: $1
108 6         36 $self->{'own_package_names'}->{$1} = 1;
109             }
110             }
111 11         257 return '';
112             }
113              
114             sub _check_text {
115 19     19   54 my ($self, $text, $linenum, $paraobj) = @_;
116             ### _check_text() ...
117             ### $linenum
118 19         1601 return $self->interpolate($text, $linenum);
119             }
120              
121             sub interior_sequence {
122 7     7   46 my ($self, $cmd, $text, $paraobj) = @_;
123             ### interior_sequence() ...
124              
125 7 50       51 if ($cmd eq 'X') {
    100          
126             # index entry, no text output, but keep newlines for linenum
127 0         0 $text =~ tr/\n//cd;
128              
129             } elsif ($cmd eq 'L') {
130 6         46 my ($display, $inferred, $name, $section, $type)
131             = Pod::ParseLink::parselink ($text);
132             ### $display
133             ### $inferred
134             ### $name
135              
136 6 50 33     452 if (defined $name && $self->{'own_package_names'}->{$name}) {
137 6         39 $text =~ /(\s*)$/;
138 6         25 my $pos = length($text) - length($1); # end of $text
139             ### $pos
140 6         55 (undef, my $linenum) = $paraobj->file_line;
141              
142             $self->violation_at_linenum_and_textpos
143 6 100       60 (($self->{'in_see_also'}
144             ? "L<> link to this POD itself in \"SEE ALSO\" section, probable typo"
145             : "L<> link to this POD itself, suggest just C<> markup is enough"),
146             $linenum, $text, $pos);
147             }
148 6 100       278 return (defined $display ? $display : $name);
149             }
150 1         42 return $text;
151             }
152              
153             1;
154             __END__
155              
156             =for stopwords Ryde clickable one's formatters filename
157              
158             =head1 NAME
159              
160             Perl::Critic::Policy::Documentation::ProhibitLinkToSelf - don't LE<lt>E<gt> link to own POD
161              
162             =head1 DESCRIPTION
163              
164             This policy is part of the L<C<Perl::Critic::Pulp>|Perl::Critic::Pulp>
165             add-on. It asks you not to use C<< LE<lt>E<gt> >> markup to refer to a POD
166             document itself.
167              
168             =for ProhibitVerbatimMarkup allow next 6
169              
170             =head1 NAME
171              
172             My::Package - something
173              
174             =head1 DESCRIPTION
175              
176             L<My::Package> does blah blah ... # bad
177              
178             =head1 SEE ALSO
179              
180             L<My::Package> # bad
181              
182             The idea is that it doesn't make sense to link to a document from within
183             itself. If rendered as a clickable link then it may suggest there's
184             somewhere else to go to read about the module when in fact you're already
185             looking at it.
186              
187             This is only a minor thing though, so this policy is low severity and under
188             the C<cosmetic> theme (see L<Perl::Critic/POLICY THEMES>).
189              
190             In ordinary text the suggestion is plain C<< CE<lt>E<gt> >> or similar for
191             one's own module name,
192              
193             =for ProhibitVerbatimMarkup allow next
194              
195             C<My::Package> does something something ... # ok
196              
197             In a "SEE ALSO" a link to self in very likely a typo, or too much cut and
198             paste, or at least pretty unnecessary since there's no need to "see also"
199             what you've just read.
200              
201             If linking to a particular section within one's own document then use
202             C<< LE<lt>E<gt> >> with just the section part. This will probably give
203             better looking output from the formatters too,
204              
205             =for ProhibitVerbatimMarkup allow next 2
206              
207             L<My::Package/SECTION> # bad
208              
209             L</SECTION> # ok
210              
211             For this policy the name of the POD is picked out of the "=head1 NAME"
212             section, so doesn't depend on the filename or directory where C<perlcritic>
213             is run. In the current code multiple names can be given in man-page style.
214             Not sure if that's a good idea.
215              
216             =head1 NAME
217              
218             My::Package -- blah
219              
220             My::Package::Parser -- and its parser
221              
222             =head1 DESCRIPTION
223              
224             It's always possible an C<< LE<lt>E<gt> >> is right and in fact the "NAME"
225             appearing is wrong. A violation on the C<< LE<lt>E<gt> >> will at least
226             show there's something fishy in the one or the other.
227              
228             =head2 Disabling
229              
230             If you don't care about this then you can always disable
231             C<ProhibitLinkToSelf> from your F<.perlcriticrc> file in the usual way (see
232             L<Perl::Critic/CONFIGURATION>),
233              
234             [-Documentation::ProhibitLinkToSelf]
235              
236             If you like to almost always put C<< LE<lt>E<gt> >> on module names,
237             including in the module's own POD, then disable this policy. Maybe an
238             option in the future could allow links to self in ordinary text but prohibit
239             in "SEE ALSO".
240              
241             =head1 SEE ALSO
242              
243             L<Perl::Critic::Pulp>,
244             L<Perl::Critic>
245              
246             L<Perl::Critic::Policy::Documentation::RequirePackageMatchesPodName>,
247             L<Perl::Critic::Policy::Documentation::ProhibitAdjacentLinks>
248              
249             =head1 HOME PAGE
250              
251             L<http://user42.tuxfamily.org/perl-critic-pulp/index.html>
252              
253             =head1 COPYRIGHT
254              
255             Copyright 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2019, 2021 Kevin Ryde
256              
257             Perl-Critic-Pulp is free software; you can redistribute it and/or modify it
258             under the terms of the GNU General Public License as published by the Free
259             Software Foundation; either version 3, or (at your option) any later
260             version.
261              
262             Perl-Critic-Pulp is distributed in the hope that it will be useful, but
263             WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
264             or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
265             more details.
266              
267             You should have received a copy of the GNU General Public License along with
268             Perl-Critic-Pulp. If not, see <http://www.gnu.org/licenses/>.
269              
270             =cut