Policy/Documentation/ProhibitParagraphEndComma.pm

filter: Statementstmt 98.4% 63 / 64 Branchbran 75.0% 9 / 12 Conditioncond 72.2% 13 / 18 MC/DCmcdc 44.4% 4 / 9 Subroutinesub 100% 18 / 18 Podpod 100% 1 / 1 totaltotal 88.5% 108 / 122 SCAR 25.8
CC
13
Cov
90%
command
9.2
check_last
4.2
textblock
2.1
CRAP 13.1
« Policy/Miscellanea/TextDomainUnused.pm ↑ Summary Policy/Documentation/ProhibitLinkToSelf.pm »
1 # Copyright 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2019, 2020, 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 # maybe allow comma for
20 # =head1 Foo
21 # And something,
22 # =head2 Item
23  
24 # perlcritic -s ProhibitParagraphEndComma ProhibitParagraphEndComma.pm
25 # perlcritic -s ProhibitParagraphEndComma /usr/share/perl5/IO/Socket/INET6.pm
26 # perlcritic -s ProhibitParagraphEndComma /usr/share/perl5/MIME/Body.pm /usr/share/perl5/XML/Twig.pm
27  
28  
29 package Perl::Critic::Policy::Documentation::ProhibitParagraphEndComma;
30 40 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
31 40 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
32 40 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
33 40 ▶use base 'Perl::Critic::Policy';
Subroutine BEGIN: called CC=1 SCAR=0.0
34 40 ▶use Perl::Critic::Utils;
Subroutine BEGIN: called CC=1 SCAR=0.0
35  
36 # uncomment this to run the ### lines
37 # use Smart::Comments;
38  
39 40 our $VERSION = 99;
40  
41  
42 40 ▶use constant supported_parameters => ();
Subroutine BEGIN: called CC=1 SCAR=0.0
43 40 ▶use constant default_severity => $Perl::Critic::Utils::SEVERITY_LOWEST;
Subroutine BEGIN: called CC=1 SCAR=0.0
44 40 ▶use constant default_themes => qw(pulp cosmetic);
Subroutine BEGIN: called CC=1 SCAR=0.0
45 40 ▶use constant applies_to => 'PPI::Document';
Subroutine BEGIN: called CC=1 SCAR=0.0
46  
47 sub violates {
48 7 ▶ my ($self, $elem, $document) = @_;
Subroutine violates: called CC=1 SCAR=0.0
Pod documented
49 ### ProhibitParagraphEndComma on: $elem->content
50  
51 7 my $parser = Perl::Critic::Pulp::PodParser::ProhibitParagraphEndComma->new
52 (policy => $self);
53 7 $parser->parse_from_elem ($elem);
54 7 $parser->check_last;
55 7 return $parser->violations;
56 }
57  
58 package Perl::Critic::Pulp::PodParser::ProhibitParagraphEndComma;
59 40 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
60 40 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
61 40 ▶use base 'Perl::Critic::Pulp::PodParser';
Subroutine BEGIN: called CC=1 SCAR=0.0
62  
63 sub new {
64 7 ▶ my $class = shift;
Subroutine new: called CC=1 SCAR=0.0
65 ### new() ...
66 7 return $class->SUPER::new (last_text => '',
67 last_command => '',
68 @_);
69 }
70 sub command {
71 14 ▶ my ($self, $command, $text, $linenum, $paraobj) = @_;
Subroutine command: called CC=9 SCAR=22.2
72 ### command(): $command
73  
74 # "=begin :foo" means pod markup continues. Ignore the =begin and
75 # continue processing POD within it. Any other begin is a new block
76 # something and preceding comma not allowed.
77 #
78 14 ▶ if ($command eq 'for'
Branch: 1 branch
BranchTrueFalseTotal
if ($command eq "for" or $command eq "pod" or $command eq "begin" and $text =~ /^\s*:/ or $command eq "end") 3 3 6
Condition91%11/12
$command eq "for" or $command eq "pod" or $command eq "begin" and $text =~ /^\s*:/ or $command eq "end"
l!l&&r!l&&!r
923
$command eq "for" or $command eq "pod" or $command eq "begin" and $text =~ /^\s*:/
l!l&&r!l&&!r
815
$command eq "for" or $command eq "pod"
l!l&&r!l&&!r
086
$command eq "begin" and $text =~ /^\s*:/
!ll&&!rl&&r
411
MC/DC80%4/5
$command eq "for" or $command eq "pod" or $command eq "begin" and $text =~ /^\s*:/ or $command eq "end"
$command eq "for" $command eq "pod" $command eq "begin" $text =~ /^\s*:/ $command eq "end"
Truth table71%5/7
A or B or C and D or E
A= $command eq "for"B= $command eq "pod"C= $command eq "begin"D= $text =~ /^\s*:/E= $command eq "end"
ABCDEresult
1XXXX1
01XXX1
0011X1
000X11
001011
000X00
001000
79 || $command eq 'pod'
80 || ($command eq 'begin' && $text =~ /^\s*:/)
81 || $command eq 'end') {
82 11 return; # ignore these completely
83 }
84  
85 3 ▶ if ($command eq 'item' && $self->{'last_command'} eq 'item') {
Branch: 2 branches
BranchTrueFalseTotal
if ($command eq 'item' and $self->{'last_command'} eq 'item') { } 0 3 3
elsif ($command eq 'over') { } 1 2 3
Condition33%1/3
$command eq "item" and $self->{"last_command"} eq "item"
!ll&&!rl&&r
300
MC/DC0%0/2
$command eq "item" and $self->{"last_command"} eq "item"
$command eq "item" $self->{"last_command"} eq "item"
Truth table33%1/3
A and B
A= $command eq "item"B= $self->{"last_command"} eq "item"
ABresult
0X0
100
111
86 # Paragraphs in =item list can end in successive commas.
87  
88 } elsif ($command eq 'over') {
89  
90 } else {
91 2 $self->check_last;
92 }
93 3 $self->{'last_text'} = '';
94 3 $self->{'last_command'} = $command;
95 }
96 sub textblock {
97 10 ▶ my ($self, $text, $linenum, $paraobj) = @_;
Subroutine textblock: called CC=2 SCAR=7.2
98 ### textblock(): $text
99 10 $self->check_last;
100 # sometimes $text=undef from Pod::Parser
101 10 ▶ if (! defined $text) { $text = ''; }
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $text) 0 10 10
102 10 $self->{'last_linenum'} = $linenum;
103 10 $self->{'last_text'} = $text;
104 }
105 sub verbatim {
106 3 ▶ my ($self, $text, $linenum, $paraobj) = @_;
Subroutine verbatim: called CC=1 SCAR=0.0
107 # anything before a verbatim is ok
108 3 $self->{'last_text'} = '';
109 }
110  
111 sub check_last {
112 19 ▶ my ($self) = @_;
Subroutine check_last: called CC=4 SCAR=14.5
113 ### check_last() ...
114 ### in_begin: $self->{'in_begin'}
115  
116 19 ▶ if ($self->{'in_begin'} && $self->{'in_begin'} !~ /^:/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->{'in_begin'} and not $self->{'in_begin'} =~ /^:/) { } 0 19 19
elsif ($self->{'last_text'} =~ /(,\s*)$/s) { } 3 16 19
Condition33%1/3
$self->{"in_begin"} and not $self->{"in_begin"} =~ /^:/
!ll&&!rl&&r
1900
MC/DC0%0/2
$self->{"in_begin"} and not $self->{"in_begin"} =~ /^:/
$self->{"in_begin"} not $self->{"in_begin"} =~ /^:/
Truth table33%1/3
A and not(B)
A= $self->{"in_begin"}B= not $self->{"in_begin"} =~ /^:/
ABresult
0X0
100
111
117 # =begin block of non-: means not pod markup
118  
119 } elsif ($self->{'last_text'} =~ /(,\s*)$/s) {
120 ### last_text ends comma ...
121 3 my $pos = length($self->{'last_text'}) - length($1); # position of comma
122 $self->violation_at_linenum_and_textpos
123 ("Paragraph ends with comma",
124 3 $self->{'last_linenum'}, $self->{'last_text'}, $pos);
125 }
126 19 $self->{'last_text'} = '';
127 }
128  
129 40 1;
130 __END__
131  
132 =for stopwords Ryde
133  
134 =head1 NAME
135  
136 Perl::Critic::Policy::Documentation::ProhibitParagraphEndComma - avoid comma at end of section
137  
138 =head1 DESCRIPTION
139  
140 This policy is part of the L<C<Perl::Critic::Pulp>|Perl::Critic::Pulp>
141 add-on. It asks you not to end a POD paragraph with a comma.
142  
143 Some text, # bad, meant to be a full-stop?
144  
145 Some more text.
146  
147 Usually such a comma is meant to be a full-stop, or perhaps omitted at the
148 end of a "SEE ALSO" list
149  
150 =for ProhibitVerbatimMarkup allow next 2
151  
152 =head1 SEE ALSO
153  
154 L<Foo>,
155 L<Bar>, # bad, meant to be omitted?
156  
157 A paragraph is allowed to end with a comma when before an C<=over> or a
158 verbatim block, that being taken as introducing a quotation or example,
159  
160 For example, # ok, introduce an example
161  
162 foo(1+2+3)
163  
164 Or one of, # ok, introduce an itemized list
165  
166 =over
167  
168 =item Foo
169  
170 =head2 Disabling
171  
172 If you don't care about this you can disable C<ProhibitParagraphEndComma>
173 from your F<.perlcriticrc> in the usual way (see
174 L<Perl::Critic/CONFIGURATION>),
175  
176 [-Documentation::ProhibitParagraphEndComma]
177  
178 =head1 SEE ALSO
179  
180 L<Perl::Critic::Pulp>, L<Perl::Critic>
181  
182 L<Perl::Critic::Policy::Documentation::ProhibitParagraphTwoDots>
183  
184 =head1 HOME PAGE
185  
186 http://user42.tuxfamily.org/perl-critic-pulp/index.html
187  
188 =head1 COPYRIGHT
189  
190 Copyright 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2019, 2020, 2021 Kevin Ryde
191  
192 Perl-Critic-Pulp is free software; you can redistribute it and/or modify it
193 under the terms of the GNU General Public License as published by the Free
194 Software Foundation; either version 3, or (at your option) any later
195 version.
196  
197 Perl-Critic-Pulp is distributed in the hope that it will be useful, but
198 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
199 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
200 more details.
201  
202 You should have received a copy of the GNU General Public License along with
203 Perl-Critic-Pulp. If not, see <http://www.gnu.org/licenses/>.
204  
205 =cut
« Policy/Miscellanea/TextDomainUnused.pm ↑ Summary Policy/Documentation/ProhibitLinkToSelf.pm »