Policy/Documentation/RequireFinalCut.pm

filter: Statementstmt 94.8% 74 / 78 Branchbran 81.2% 13 / 16 Conditioncond 100% 6 / 6 MC/DCmcdc 100% 4 / 4 Subroutinesub 100% 21 / 21 Podpod 100% 1 / 1 totaltotal 94.4% 119 / 126 SCAR 24.0
CC
11
Cov
93%
verbatim
6.1
command
6.0
end_input
3.0
CRAP 11.0
« Policy/CodeLayout/ProhibitIfIfSameLine.pm ↑ Summary Policy/Documentation/RequireLinkedURLs.pm »
1 # Copyright 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 RequireFinalCut RequireFinalCut.pm
20 # perlcritic -s RequireFinalCut /usr/share/perl5/Class/InsideOut.pm
21 # perlcritic -s RequireFinalCut /usr/share/perl5/Lingua/Any/Numbers.pm
22  
23  
24 package Perl::Critic::Policy::Documentation::RequireFinalCut;
25 40 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 40 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 40 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
28 40 ▶use base 'Perl::Critic::Policy';
Subroutine BEGIN: called CC=1 SCAR=0.0
29 40 ▶use Perl::Critic::Utils;
Subroutine BEGIN: called CC=1 SCAR=0.0
30  
31 # uncomment this to run the ### lines
32 # use Smart::Comments;
33  
34 40 our $VERSION = 99;
35  
36 40 ▶use constant supported_parameters => ();
Subroutine BEGIN: called CC=1 SCAR=0.0
37 40 ▶use constant default_severity => $Perl::Critic::Utils::SEVERITY_LOWEST;
Subroutine BEGIN: called CC=1 SCAR=0.0
38 40 ▶use constant default_themes => qw(pulp cosmetic);
Subroutine BEGIN: called CC=1 SCAR=0.0
39 40 ▶use constant applies_to => 'PPI::Document';
Subroutine BEGIN: called CC=1 SCAR=0.0
40  
41 sub violates {
42 31 ▶ my ($self, $elem, $document) = @_;
Subroutine violates: called CC=1 SCAR=0.0
Pod documented
43 ### RequireFinalCut on: $elem->content
44  
45 31 my $parser = Perl::Critic::Pulp::PodParser::RequireFinalCut->new
46 (policy => $self);
47 31 $parser->parse_from_elem ($elem);
48 31 return $parser->violations;
49 }
50  
51 package Perl::Critic::Pulp::PodParser::RequireFinalCut;
52 40 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
53 40 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
54 40 ▶use base 'Perl::Critic::Pulp::PodParser';
Subroutine BEGIN: called CC=1 SCAR=0.0
55  
56 sub new {
57 31 ▶ my $class = shift;
Subroutine new: called CC=1 SCAR=0.0
58 31 my $self = $class->SUPER::new (@_);
59 31 $self->parseopts(-process_cut_cmd => 1);
60 31 return $self;
61 }
62  
63 # Pod::Parser doesn't hold the current line number except in a local
64 # variable, so have to note it here for use in end_input().
65 #
66 sub begin_input {
67 28 ▶ my $self = shift;
Subroutine begin_input: called CC=1 SCAR=0.0
68 28 $self->SUPER::begin_input(@_);
69 28 $self->{'last_linenum'} = 0;
70 }
71 sub preprocess_line {
72 113 ▶ my ($self, $line, $linenum) = @_;
Subroutine preprocess_line: called CC=1 SCAR=0.0
73 ### preprocess_line(): "linenum=$linenum"
74 113 $self->{'last_linenum'} = $linenum;
75 113 return $line;
76 }
77  
78 sub end_input {
79 28 ▶ my $self = shift;
Subroutine end_input: called CC=3 SCAR=11.0
80 28 $self->SUPER::begin_input(@_);
81 28 ▶ if ($self->{'in_pod'}
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"in_pod"} and not $self->{"saw_cut_in_text"}) 7 21 28
Condition100%3/3
$self->{"in_pod"} and not $self->{"saw_cut_in_text"}
!ll&&!rl&&r
1477
MC/DC100%2/2
$self->{"in_pod"} and not $self->{"saw_cut_in_text"}
$self->{"in_pod"} not $self->{"saw_cut_in_text"}
Truth table100%3/3
A and not(B)
A= $self->{"in_pod"}B= not $self->{"saw_cut_in_text"}
ABresult
0X0
100
111
82 && ! $self->{'saw_cut_in_text'}) {
83 $self->violation_at_linenum_and_textpos
84 ("POD doesn't end with =cut directive",
85 7 $self->{'last_linenum'} + 1, # end of file as the position
86 '',
87 0);
88 }
89 }
90  
91 sub command {
92 49 ▶ my $self = shift;
Subroutine command: called CC=6 SCAR=17.9
93 49 $self->SUPER::command(@_);
94 49 my ($command, $text, $linenum, $paraobj) = @_;
95 ### $command
96 ### $text
97  
98 49 ▶ if ($command eq 'cut') {
Branch: 3 branches
BranchTrueFalseTotal
if ($command eq 'cut') { } 10 39 49
elsif ($command eq 'end' or $command eq 'for') { } 11 28 39
elsif ($command eq 'pod') { } 15 13 28
99 10 $self->{'in_pod'} = 0;
100  
101 ▶ } elsif ($command eq 'end' || $command eq 'for') {
Condition100%3/3
$command eq "end" or $command eq "for"
l!l&&r!l&&!r
9228
MC/DC100%2/2
$command eq "end" or $command eq "for"
$command eq "end" $command eq "for"
Truth table100%3/3
A or B
A= $command eq "end"B= $command eq "for"
ABresult
1X1
011
000
102  
103 } elsif ($command eq 'pod') {
104 15 $self->{'in_pod'} = 1;
105  
106 } else {
107 13 ▶ unless ($self->{'in_begin'}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"in_begin"}) 3 10 13
108 3 $self->{'in_pod'} = 1;
109 }
110 }
111 ### now in_pod: $self->{'in_pod'}
112  
113 49 $self->my_notice_cut($text);
114 49 return '';
115 }
116  
117 sub verbatim {
118 4 ▶ my ($self, $text, $linenum, $paraobj) = @_;
Subroutine verbatim: called CC=3 SCAR=18.1
119 ### verbatim ...
120  
121 # ignore entirely whitespace runs of blank lines
122 4 ▶ return '' if $text =~ /^\s*$/;
Branch: 1 branch
BranchTrueFalseTotal
if $text =~ /^\s*$/ 4 0 4
123  
124 0 ▶ unless ($self->{'in_begin'}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"in_begin"}) 0 0 0
125 0 $self->{'in_pod'} = 1;
126 }
127 0 $self->my_notice_cut($text);
128 0 return '';
129 }
130  
131 sub textblock {
132 13 ▶ my ($self, $text, $linenum, $paraobj) = @_;
Subroutine textblock: called CC=2 SCAR=6.9
133 ### textblock ...
134 ### $text
135  
136 13 ▶ unless ($self->{'in_begin'}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"in_begin"}) 5 8 13
137 5 $self->{'in_pod'} = 1;
138 }
139 13 $self->my_notice_cut($text);
140 13 return '';
141 }
142  
143 sub my_notice_cut {
144 62 ▶ my ($self, $text) = @_;
Subroutine my_notice_cut: called CC=1 SCAR=0.0
145 62 $self->{'saw_cut_in_text'} = ($text =~ /\n=cut\b[^\n]*/);
146 }
147  
148 40 1;
149 __END__
150  
151 =for stopwords Ryde
152  
153 =head1 NAME
154  
155 Perl::Critic::Policy::Documentation::RequireFinalCut - end POD with =cut directive
156  
157 =head1 DESCRIPTION
158  
159 This policy is part of the L<C<Perl::Critic::Pulp>|Perl::Critic::Pulp>
160 add-on. It asks you to end POD with a C<=cut> directive at the end of a
161 file.
162  
163 =head1 DOCO
164  
165 Some text.
166  
167 =cut # ok
168  
169 The idea is to have a definite end indication for human readers. Perl and
170 the POD processors don't require a final C<=cut>. On that basis this policy
171 is lowest severity and under the "cosmetic" theme (see L<Perl::Critic/POLICY
172 THEMES>).
173  
174 If there's no POD in the file then a C<=cut> is not required. Or if the
175 file ends with code rather than POD then a C<=cut> after that code is not
176 required.
177  
178 =head2 About foo
179  
180 =cut
181  
182 sub foo {
183 } # ok, file ends with code not POD
184  
185 If there's POD at end of file but consists only of C<=begin/=end> blocks
186 then a C<=cut> is not required. It's reckoned the C<=end> is enough in this
187 case.
188  
189 =begin wikidoc
190  
191 Entire document in wiki style.
192  
193 =end wikidoc # ok, =cut not required
194  
195 If the file ends with a mixture of ordinary POD and C<=begin> blocks then a
196 is still required. The special allowance is when only C<=begin> blocks,
197 presumably destined for some other markup system.
198  
199 =head2 Blank Line
200  
201 Generally a C<=cut> should have a blank line before it, the same as other
202 POD commands. But Perl execution doesn't enforce that and the same
203 looseness is permitted here,
204  
205 =pod
206  
207 Blah blah blah
208 =cut # ok without preceding newline
209  
210 A check for blanks around POD commands is left to other policies. The
211 C<podchecker> program reports this (L<Pod::Checker>).
212  
213 =cut
214  
215 # The POD parsers vary a little in their treatment of this sort of thing.
216 # C<Pod::Parser> takes it as part of the paragraph, C<Pod::Simple> takes it as
217 # a command but may issue warnings.
218  
219 =pod
220  
221 =head2 Disabling
222  
223 If you don't care about a final C<=cut> you can disable C<RequireFinalCut>
224 from your F<.perlcriticrc> in the usual way (see
225 L<Perl::Critic/CONFIGURATION>),
226  
227 [-Documentation::RequireFinalCut]
228  
229 =head1 SEE ALSO
230  
231 L<Perl::Critic::Pulp>, L<Perl::Critic>
232  
233 L<Perl::Critic::Policy::Documentation::RequireEndBeforeLastPod>,
234 L<Perl::Critic::Policy::Documentation::RequirePodAtEnd>
235  
236 =head1 HOME PAGE
237  
238 L<http://user42.tuxfamily.org/perl-critic-pulp/index.html>
239  
240 =head1 COPYRIGHT
241  
242 Copyright 2012, 2013, 2014, 2015, 2016, 2017, 2019, 2021 Kevin Ryde
243  
244 Perl-Critic-Pulp is free software; you can redistribute it and/or modify it
245 under the terms of the GNU General Public License as published by the Free
246 Software Foundation; either version 3, or (at your option) any later
247 version.
248  
249 Perl-Critic-Pulp is distributed in the hope that it will be useful, but
250 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
251 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
252 more details.
253  
254 You should have received a copy of the GNU General Public License along with
255 Perl-Critic-Pulp. If not, see <http://www.gnu.org/licenses/>.
256  
257 =cut
« Policy/CodeLayout/ProhibitIfIfSameLine.pm ↑ Summary Policy/Documentation/RequireLinkedURLs.pm »