Policy/Documentation/RequireEndBeforeLastPod.pm

filter: Statementstmt 100% 44 / 44 Branchbran 100% 8 / 8 Conditioncond 85.7% 6 / 7 MC/DCmcdc 75.0% 3 / 4 Subroutinesub 100% 11 / 11 Podpod 100% 1 / 1 totaltotal 97.3% 73 / 75 SCAR 20.8
CC
8
Cov
98%
violates
8.0
BEGIN
1.0
BEGIN
1.0
CRAP 8.0
« Policy/Modules/ProhibitUseQuotedVersion.pm ↑ Summary Policy/Compatibility/ProhibitUnixDevNull.pm »
1 # Copyright 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2019, 2021 Kevin Rydepod
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 package Perl::Critic::Policy::Documentation::RequireEndBeforeLastPod;
20 40 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 40 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 40 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 40 ▶use base 'Perl::Critic::Policy';
Subroutine BEGIN: called CC=1 SCAR=0.0
24 40 ▶use Perl::Critic::Utils;
Subroutine BEGIN: called CC=1 SCAR=0.0
25  
26 40 our $VERSION = 99;
27  
28 40 ▶use constant supported_parameters => ();
Subroutine BEGIN: called CC=1 SCAR=0.0
29 40 ▶use constant default_severity => $Perl::Critic::Utils::SEVERITY_LOW;
Subroutine BEGIN: called CC=1 SCAR=0.0
30 40 ▶use constant default_themes => qw(pulp cosmetic);
Subroutine BEGIN: called CC=1 SCAR=0.0
31 40 ▶use constant applies_to => 'PPI::Document';
Subroutine BEGIN: called CC=1 SCAR=0.0
32  
33 # only ever gives one violation
34 40 ▶use constant default_maximum_violations_per_document => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
35  
36 sub violates {
37 14 ▶ my ($self, $elem, $document) = @_;
Subroutine violates: called CC=8 SCAR=20.8
Pod documented
38  
39 14 ▶ $elem = $elem->last_element
Condition50%1/2
$elem->last_element || (return)
l!l
140
MC/DC0%0/1
$elem->last_element || (return)
$elem->last_element
Truth table50%1/2
A
A= $elem->last_element
Aresult
11
00
40 || return; # empty file
41  
42 14 ▶ if ($elem->isa('PPI::Statement::End')
Branch: 1 branch
BranchTrueFalseTotal
if ($elem->isa("PPI::Statement::End") or $elem->isa("PPI::Statement::Data")) 5 9 14
Condition100%3/3
$elem->isa("PPI::Statement::End") or $elem->isa("PPI::Statement::Data")
l!l&&r!l&&!r
419
MC/DC100%2/2
$elem->isa("PPI::Statement::End") or $elem->isa("PPI::Statement::Data")
$elem->isa("PPI::Statement::End") $elem->isa("PPI::Statement::Data")
Truth table100%3/3
A or B
A= $elem->isa("PPI::Statement::End")B= $elem->isa("PPI::Statement::Data")
ABresult
1X1
011
000
43 || $elem->isa('PPI::Statement::Data')) {
44 # $document ends with __END__, ok
45 # or ends with __DATA__, in which case you can't use __END__ after last
46 # code, so ok
47 5 return;
48 }
49  
50 9 for (;;) {
51 17 ▶ if ($elem->significant) {
Branch: 1 branch
BranchTrueFalseTotal
if ($elem->significant) 2 15 17
52 # document ends with code, ie. no pod after the last code, so ok
53 2 return;
54 }
55 15 ▶ if ($elem->isa('PPI::Token::Pod')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($elem->isa("PPI::Token::Pod")) 6 9 15
56 # found the last pod
57 6 last;
58 }
59 # otherwise skip PPI::Token::Comment and possibly PPI::Token::Whitespace
60 9 ▶ $elem = $elem->previous_sibling
Condition100%2/2
$elem->previous_sibling || (return)
l!l
81
MC/DC100%1/1
$elem->previous_sibling || (return)
$elem->previous_sibling
Truth table100%2/2
A
A= $elem->previous_sibling
Aresult
11
00
61 || return; # $document is empty, or only comments and whitespace, so ok
62 }
63  
64 6 ▶ if (! $elem->sprevious_sibling) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($elem->sprevious_sibling) 2 4 6
65 # there's no code before the last pod, either a pod-only file, or pod
66 # plus comments etc, so ok
67 2 return;
68 }
69  
70 4 return $self->violation
71 ('Put __END__ before POD at the end of a file.',
72 '',
73 $elem);
74 }
75  
76 40 1;
77 __END__
78  
79 =for stopwords ok SelfLoader Ryde
80  
81 =head1 NAME
82  
83 Perl::Critic::Policy::Documentation::RequireEndBeforeLastPod - require __END__ before POD at end of file
84  
85 =head1 DESCRIPTION
86  
87 This policy is part of the L<C<Perl::Critic::Pulp>|Perl::Critic::Pulp>
88 add-on. It requires that you put an C<__END__> before POD which is at the
89 end of a file. For example,
90  
91 program_code();
92 1;
93 __END__ # good
94  
95 =head1 NAME
96 ...
97  
98 and not merely
99  
100 program_code();
101 1; # bad
102  
103 =head1 NAME
104 ...
105  
106 This is primarily a matter of personal preference, so the policy is low
107 severity and only under the "cosmetic" theme (see L<Perl::Critic/POLICY
108 THEMES>). An C<__END__> like this has no effect on execution, but it's a
109 fairly common convention since it's a good human indication you mean the
110 code to end there, and it stops Perl parsing through the POD which may save
111 a few nanoseconds.
112  
113 This policy is looser than C<Documentation::RequirePodAtEnd>. This policy
114 allows POD to be anywhere in among the code, the requirement is only that if
115 the file ends with POD then you should have an C<__END__> between the last
116 code and last POD.
117  
118 A file of all POD, or all code, or which ends with code, is ok. Ending with
119 code is usual if you write your POD at the start of the file or in among the
120 functions etc,
121  
122 =pod
123  
124 And that's all.
125  
126 =cut
127  
128 cleanup ();
129 exit 0; # good
130  
131 A file using C<__DATA__> is always ok, since you can't have C<__END__>
132 followed by C<__DATA__>, wherever you want your POD. If the C<__DATA__> is
133 in fact C<SelfLoader> code then it can helpfully have an C<__END__> within
134 it, but as of C<perlcritic> version 1.092 no checks at all are applied to
135 SelfLoader sections.
136  
137 =head2 Disabling
138  
139 As always if you don't care about C<__END__> you can disable
140 C<RequireEndBeforeLastPod> from your F<.perlcriticrc> in the usual way (see
141 L<Perl::Critic/CONFIGURATION>),
142  
143 [-Documentation::RequireEndBeforeLastPod]
144  
145 =head1 SEE ALSO
146  
147 L<Perl::Critic::Pulp>,
148 L<Perl::Critic>,
149 L<Perl::Critic::Policy::Documentation::RequirePodAtEnd>
150  
151 =head1 HOME PAGE
152  
153 L<http://user42.tuxfamily.org/perl-critic-pulp/index.html>
154  
155 =head1 COPYRIGHT
156  
157 Copyright 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2019, 2021 Kevin Ryde
158  
159 Perl-Critic-Pulp is free software; you can redistribute it and/or modify it
160 under the terms of the GNU General Public License as published by the Free
161 Software Foundation; either version 3, or (at your option) any later
162 version.
163  
164 Perl-Critic-Pulp is distributed in the hope that it will be useful, but
165 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
166 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
167 more details.
168  
169 You should have received a copy of the GNU General Public License along with
170 Perl-Critic-Pulp. If not, see <http://www.gnu.org/licenses/>.
171  
172 =cut
« Policy/Modules/ProhibitUseQuotedVersion.pm ↑ Summary Policy/Compatibility/ProhibitUnixDevNull.pm »