Policy/Documentation/ProhibitLinkToSelf.pm

filter: Statementstmt 97.3% 73 / 75 Branchbran 80.0% 16 / 20 Conditioncond 33.3% 2 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 100% 18 / 18 Podpod 100% 1 / 1 totaltotal 88.7% 110 / 124 SCAR 25.8
CC
13
Cov
90%
interior_sequence
7.4
textblock
5.3
command
3.0
CRAP 13.2
« Policy/Documentation/ProhibitParagraphEndComma.pm ↑ Summary Policy/ValuesAndExpressions/ProhibitEmptyCommas.pm »
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 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 40 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 40 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 40 ▶use base 'Perl::Critic::Policy';
Subroutine BEGIN: called CC=1 SCAR=0.0
27 40 ▶use Perl::Critic::Utils;
Subroutine BEGIN: called CC=1 SCAR=0.0
28  
29 # uncomment this to run the ### lines
30 #use Devel::Comments;
31  
32 40 our $VERSION = 99;
33  
34 40 ▶use constant supported_parameters => ();
Subroutine BEGIN: called CC=1 SCAR=0.0
35 40 ▶use constant default_severity => $Perl::Critic::Utils::SEVERITY_LOW;
Subroutine BEGIN: called CC=1 SCAR=0.0
36 40 ▶use constant default_themes => qw(pulp cosmetic);
Subroutine BEGIN: called CC=1 SCAR=0.0
37 40 ▶use constant applies_to => 'PPI::Document';
Subroutine BEGIN: called CC=1 SCAR=0.0
38  
39 sub violates {
40 6 ▶ my ($self, $elem, $document) = @_;
Subroutine violates: called CC=1 SCAR=0.0
Pod documented
41 # ### ProhibitLinkToSelf on: $elem->content
42  
43 6 my $parser = Perl::Critic::Pulp::PodParser::ProhibitLinkToSelf->new
44 (policy => $self);
45 6 $parser->parse_from_elem ($elem);
46 6 return $parser->violations;
47 }
48  
49 package Perl::Critic::Pulp::PodParser::ProhibitLinkToSelf;
50 40 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
51 40 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
52 40 ▶use Pod::ParseLink;
Subroutine BEGIN: called CC=1 SCAR=0.0
53 40 ▶use base 'Perl::Critic::Pulp::PodParser';
Subroutine BEGIN: called CC=1 SCAR=0.0
54  
55 40 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 ▶ my $self = shift;
Subroutine command: called CC=3 SCAR=11.0
62 8 my ($command, $text, $linenum, $paraobj) = @_;
63 8 $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 ▶ if ($command eq 'head1') {
Branch: 1 branch
BranchTrueFalseTotal
if ($command eq "head1") 7 1 8
76 7 $self->{'in_name'} = ($text =~ /^\s*NAME\b/);
77 7 $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 ▶ unless ($command_non_text{$command}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($command_non_text{$command}) 8 0 8
83 # padded for the column number right, the leading spaces do no harm here
84 8 _check_text ($self,
85 (' ' x (length($command)+1)) . $text,
86 $linenum,
87 $paraobj);
88 }
89  
90 8 return '';
91 }
92  
93 sub textblock {
94 11 ▶ my ($self, $text, $linenum, $paraobj) = @_;
Subroutine textblock: called CC=5 SCAR=16.7
95 ### textblock(): "linenum=$linenum"
96 ### $text
97  
98 # "=begin :foo" is markup, check it. Other =begin is not markup.
99 11 ▶ unless ($self->{'in_begin'} eq '' || $self->{'in_begin'} =~ /^:/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"in_begin"} eq "" or $self->{"in_begin"} =~ /^:/) 0 11 11
Condition33%1/3
$self->{"in_begin"} eq "" or $self->{"in_begin"} =~ /^:/
l!l&&r!l&&!r
1100
MC/DC0%0/2
$self->{"in_begin"} eq "" or $self->{"in_begin"} =~ /^:/
$self->{"in_begin"} eq "" $self->{"in_begin"} =~ /^:/
Truth table33%1/3
A or B
A= $self->{"in_begin"} eq ""B= $self->{"in_begin"} =~ /^:/
ABresult
1X1
011
000
100 0 return '';
101 }
102  
103 11 my $str = _check_text ($self, $text, $linenum, $paraobj);
104 ### interpolated: $str
105 11 ▶ if ($self->{'in_name'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"in_name"}) 10 1 11
106 10 ▶ if ($str =~ /^\s*([[:word:]:]+)\s*-/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($str =~ /^\s*([[:word:]:]+)\s*-/) 6 4 10
107 ### add own package name: $1
108 6 $self->{'own_package_names'}->{$1} = 1;
109 }
110 }
111 11 return '';
112 }
113  
114 sub _check_text {
115 19 ▶ my ($self, $text, $linenum, $paraobj) = @_;
Subroutine _check_text: called CC=1 SCAR=0.0
116 ### _check_text() ...
117 ### $linenum
118 19 return $self->interpolate($text, $linenum);
119 }
120  
121 sub interior_sequence {
122 7 ▶ my ($self, $cmd, $text, $paraobj) = @_;
Subroutine interior_sequence: called CC=7 SCAR=20.1
123 ### interior_sequence() ...
124  
125 7 ▶ if ($cmd eq 'X') {
Branch: 2 branches
BranchTrueFalseTotal
if ($cmd eq 'X') { } 0 7 7
elsif ($cmd eq 'L') { } 6 1 7
126 # index entry, no text output, but keep newlines for linenum
127 0 $text =~ tr/\n//cd;
128  
129 } elsif ($cmd eq 'L') {
130 6 my ($display, $inferred, $name, $section, $type)
131 = Pod::ParseLink::parselink ($text);
132 ### $display
133 ### $inferred
134 ### $name
135  
136 6 ▶ if (defined $name && $self->{'own_package_names'}->{$name}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $name and $self->{"own_package_names"}{$name}) 6 0 6
Condition33%1/3
defined $name and $self->{"own_package_names"}{$name}
!ll&&!rl&&r
006
MC/DC0%0/2
defined $name and $self->{"own_package_names"}{$name}
defined $name $self->{"own_package_names"}{$name}
Truth table33%1/3
A and B
A= defined $nameB= $self->{"own_package_names"}{$name}
ABresult
0X0
100
111
137 6 $text =~ /(\s*)$/;
138 6 my $pos = length($text) - length($1); # end of $text
139 ### $pos
140 6 (undef, my $linenum) = $paraobj->file_line;
141  
142 $self->violation_at_linenum_and_textpos
143 6 ▶ (($self->{'in_see_also'}
Branch: 1 branch
BranchTrueFalseTotal
$self->{'in_see_also'} ? : 1 5 6
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 ▶ return (defined $display ? $display : $name);
Branch: 1 branch
BranchTrueFalseTotal
defined $display ? : 1 5 6
149 }
150 1 return $text;
151 }
152  
153 40 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
« Policy/Documentation/ProhibitParagraphEndComma.pm ↑ Summary Policy/ValuesAndExpressions/ProhibitEmptyCommas.pm »