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 CC 13 SCAR 13.8
CC
13
Cov
90%
command
9.2
check_last
4.2
textblock
2.1
CRAP 4.0
« Policy/Documentation/RequireEndBeforeLastPod.pm ↑ Summary Policy/Documentation/RequireLinkedURLs.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 = 100;
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 7 ▶sub violates {
Subroutine violates: called CC=1 SCAR=0.0
Pod documented
48 7 my ($self, $elem, $document) = @_;
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 7 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
64 7 my $class = shift;
65 ### new() ...
66 7 return $class->SUPER::new (last_text => '',
67 last_command => '',
68 @_);
69 }
70 14 ▶sub command {
Subroutine command: called CC=9 SCAR=22.2
71 14 my ($self, $command, $text, $linenum, $paraobj) = @_;
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") 11 3 14
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 10 ▶sub textblock {
Subroutine textblock: called CC=2 SCAR=7.2
97 10 my ($self, $text, $linenum, $paraobj) = @_;
98 ### textblock(): $text
99 10 $self->check_last;
100 # sometimes $text=undef from Pod::Parser
101 0 ▶ 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 3 ▶sub verbatim {
Subroutine verbatim: called CC=1 SCAR=0.0
106 3 my ($self, $text, $linenum, $paraobj) = @_;
107 # anything before a verbatim is ok
108 3 $self->{'last_text'} = '';
109 }
110  
111 19 ▶sub check_last {
Subroutine check_last: called CC=4 SCAR=14.5
112 19 my ($self) = @_;
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__
« Policy/Documentation/RequireEndBeforeLastPod.pm ↑ Summary Policy/Documentation/RequireLinkedURLs.pm »