Policy/Documentation/RequireFilenameMarkup.pm

filter: Statementstmt 94.6% 71 / 75 Branchbran 75.0% 9 / 12 Conditioncond 40.0% 2 / 5 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 100% 18 / 18 Podpod 100% 1 / 1 totaltotal 88.6% 101 / 114 SCAR 22.1
CC
9
Cov
89%
textblock
7.3
interior_sequence
4.0
BEGIN
1.0
CRAP 9.1
« Policy/Documentation/ProhibitVerbatimMarkup.pm ↑ Summary Policy/Modules/ProhibitUseQuotedVersion.pm »
1 # Copyright 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 RequireFilenameMarkup RequireFilenameMarkup.pm
20  
21 # unmarked /usr/local
22 # perlcritic -s RequireFilenameMarkup /usr/share/perl5/XML/Twig.pm
23  
24  
25 package Perl::Critic::Policy::Documentation::RequireFilenameMarkup;
26 40 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 40 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
28 40 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
29 40 ▶use base 'Perl::Critic::Policy';
Subroutine BEGIN: called CC=1 SCAR=0.0
30 40 ▶use Perl::Critic::Utils;
Subroutine BEGIN: called CC=1 SCAR=0.0
31 40 ▶use Pod::Escapes;
Subroutine BEGIN: called CC=1 SCAR=0.0
32  
33 # uncomment this to run the ### lines
34 # use Smart::Comments;
35  
36 40 our $VERSION = 99;
37  
38 40 ▶use constant supported_parameters => ();
Subroutine BEGIN: called CC=1 SCAR=0.0
39 40 ▶use constant default_severity => $Perl::Critic::Utils::SEVERITY_LOW;
Subroutine BEGIN: called CC=1 SCAR=0.0
40 40 ▶use constant default_themes => qw(pulp cosmetic);
Subroutine BEGIN: called CC=1 SCAR=0.0
41 40 ▶use constant applies_to => 'PPI::Document';
Subroutine BEGIN: called CC=1 SCAR=0.0
42  
43 sub violates {
44 41 ▶ my ($self, $elem, $document) = @_;
Subroutine violates: called CC=1 SCAR=0.0
Pod documented
45 ### RequireFilenameMarkup on: $elem->content
46  
47 41 my $parser = Perl::Critic::Pulp::PodParser::RequireFilenameMarkup->new
48 (policy => $self);
49 41 $parser->parse_from_elem ($elem);
50 41 return $parser->violations;
51 }
52  
53 package Perl::Critic::Pulp::PodParser::RequireFilenameMarkup;
54 40 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
55 40 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
56 40 ▶use Pod::ParseLink;
Subroutine BEGIN: called CC=1 SCAR=0.0
57 40 ▶use base 'Perl::Critic::Pulp::PodParser';
Subroutine BEGIN: called CC=1 SCAR=0.0
58  
59 sub command {
60 41 ▶ my $self = shift;
Subroutine command: called CC=1 SCAR=0.0
61 41 $self->SUPER::command(@_); # for $self->{'in_begin'}
62 41 $self->command_as_textblock(@_);
63 41 return '';
64 }
65  
66 sub textblock {
67 79 ▶ my ($self, $text, $linenum, $pod_para) = @_;
Subroutine textblock: called CC=6 SCAR=19.9
68 ### textblock: "linenum=$linenum"
69  
70 79 ▶ if (($self->{'allow_next'}||0) > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (($self->{"allow_next"} || 0) > 0) 0 79 79
Condition50%1/2
$self->{'allow_next'} || 0
l!l
079
MC/DC0%0/1
$self->{'allow_next'} || 0
$self->{'allow_next'}
Truth table50%1/2
A
A= $self->{'allow_next'}
Aresult
11
00
71 0 $self->{'allow_next'}--;
72 0 return '';
73 }
74  
75 # process outside =begin, and inside =begin which is ":" markup
76 79 ▶ unless ($self->{'in_begin'} eq '' || $self->{'in_begin'} =~ /^:/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"in_begin"} eq "" or $self->{"in_begin"} =~ /^:/) 0 79 79
Condition33%1/3
$self->{"in_begin"} eq "" or $self->{"in_begin"} =~ /^:/
l!l&&r!l&&!r
7900
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
77 0 return '';
78 }
79  
80 79 my $interpolated = $self->interpolate($text, $linenum);
81 ### $text
82 ### $interpolated
83  
84 79 ▶ while ($interpolated =~ m{(^ | (?<=[\([:space:]])) # BOL or preceding space
Branch: 1 branch
BranchTrueFalseTotal
if ($interpolated =~ m[(^ | (?<=[\([:space:]])) # BOL or preceding space ( /(bin|etc|dev|opt|proc|tmp|usr|var) ($ # EOL |(?=[)[:space:]]) # or following space |/\S*) # or /chars | [cC]:\\\S* ) ]gmx) 36 79 115
85 (
86 /(bin|etc|dev|opt|proc|tmp|usr|var)
87 ($ # EOL
88 |(?=[)[:space:]]) # or following space
89 |/\S*) # or /chars
90 |
91 [cC]:\\\S*
92 )
93 }mgx) {
94 36 my $before = $1;
95 36 my $match = $2;
96 36 $match =~ s/[.,;:]+$//;
97 36 my $pos = pos($interpolated) - length($match);
98  
99 36 $self->violation_at_linenum_and_textpos
100 ("Filename without F<> or other markup \"$match\"\n",
101 $linenum, $interpolated, $pos);
102 }
103 }
104  
105 sub interior_sequence {
106 12 ▶ my ($self, $cmd, $text, $pod_seq) = @_;
Subroutine interior_sequence: called CC=4 SCAR=13.9
107 ### $cmd
108 ### $text
109  
110 12 ▶ if ($cmd eq 'E') {
Branch: 1 branch
BranchTrueFalseTotal
if ($cmd eq "E") 3 9 12
111 3 my $char = Pod::Escapes::e2char($text);
112 3 ▶ if (! defined $char) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $char) 0 3 3
113 ### oops, unrecognised E<> ...
114 0 return 'X';
115 }
116 3 return $char;
117 }
118 9 ▶ if ($cmd eq 'L') {
Branch: 1 branch
BranchTrueFalseTotal
if ($cmd eq "L") 5 4 9
119 5 my ($display, $inferred, $name, $section, $type)
120 = Pod::ParseLink::parselink ($text);
121 ### $display
122 ### $inferred
123 ### $name
124 5 return $inferred; # the display part, or the name part if no display
125 }
126  
127 ### X,C keep only the newlines: $text
128 4 $text =~ tr/\n//cd;
129 4 return $text;
130 }
131  
132 40 1;
133 __END__
134  
135 =for stopwords Ryde filenames filename Filenames
136  
137 =head1 NAME
138  
139 Perl::Critic::Policy::Documentation::RequireFilenameMarkup - markup /foo filenames
140  
141 =head1 DESCRIPTION
142  
143 This policy is part of the L<C<Perl::Critic::Pulp>|Perl::Critic::Pulp>
144 add-on. It asks you to use C<FE<lt>E<gt>> or other markup on filenames.
145  
146 =for ProhibitVerbatimMarkup allow next 2
147  
148 /usr/bin # bad
149  
150 F</usr/bin> # ok
151 C</bin/sh> # ok
152  
153 C<FE<lt>E<gt>> lets the formatters show filenames in a usual way, such as
154 italics in man pages. This can help human readability but is a minor matter
155 and on that basis this policy is lowest severity and under the "cosmetic"
156 theme (see L<Perl::Critic/POLICY THEMES>).
157  
158 Filenames in text are identified by likely forms. Currently words starting
159 as follows are considered filenames. F</usr> and F</etc> are the most
160 common.
161  
162 /bin
163 /dev
164 /etc
165 /opt # some proprietary Unix
166 /proc
167 /tmp
168 /usr
169 /var
170 C:\ # MS-DOS
171  
172 Any markup on a filename satisfies this policy. C<FE<lt>E<gt>> is usual,
173 but C<CE<lt>E<gt>> might suit for instance C<CE<lt>/bin/shE<gt>> to show
174 it's a command with path rather than a file as such.
175  
176 C<=begin :foo> blocks with <:> POD type are checked since they can have
177 markup. "Verbatim" paragraphs are ignored since of course they cannot have
178 markup.
179  
180 =head2 Disabling
181  
182 If you don't care about filename markup you can disable
183 C<RequireFilenameMarkup> from your F<.perlcriticrc> in the usual way (see
184 L<Perl::Critic/CONFIGURATION>),
185  
186 [-Documentation::RequireFilenameMarkup]
187  
188 =head1 SEE ALSO
189  
190 L<Perl::Critic::Pulp>,
191 L<Perl::Critic>
192  
193 =head1 HOME PAGE
194  
195 L<http://user42.tuxfamily.org/perl-critic-pulp/index.html>
196  
197 =head1 COPYRIGHT
198  
199 Copyright 2013, 2014, 2015, 2016, 2017, 2019, 2021 Kevin Ryde
200  
201 Perl-Critic-Pulp is free software; you can redistribute it and/or modify it
202 under the terms of the GNU General Public License as published by the Free
203 Software Foundation; either version 3, or (at your option) any later
204 version.
205  
206 Perl-Critic-Pulp is distributed in the hope that it will be useful, but
207 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
208 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
209 more details.
210  
211 You should have received a copy of the GNU General Public License along with
212 Perl-Critic-Pulp. If not, see <http://www.gnu.org/licenses/>.
213  
214 =cut
215  
216 # /usr/local
217 # /opt.
218 # /tmp
219 # /dev/null
220 # /dev/
221 # /dev.
222 # blah/option
223 #
224 # /option
225 #
226 # blah/blah/etc
227 #
228 # E<sol>dev
« Policy/Documentation/ProhibitVerbatimMarkup.pm ↑ Summary Policy/Modules/ProhibitUseQuotedVersion.pm »