Pulp/PodParser.pm

filter: Statementstmt 98.5% 70 / 71 Branchbran 81.2% 13 / 16 Conditioncond 54.5% 6 / 11 MC/DCmcdc 33.3% 2 / 6 Subroutinesub 100% 16 / 16 Podpod 0.0% 0 / 9 totaltotal 82.9% 107 / 129 SCAR 26.5
CC
14
Cov
91%
command
6.0
violation_at_linenum
4.5
parse_from_elem
4.0
CRAP 14.2
« Policy/Documentation/ProhibitAdjacentLinks.pm ↑ Summary Policy/Miscellanea/TextDomainUnused.pm »
1 # Copyright 2010, 2011, 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 package Perl::Critic::Pulp::PodParser;
19 40 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 40 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 40 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 40 ▶use Perl::Critic::Pulp::Utils;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 40 ▶use base 'Pod::Parser';
Subroutine BEGIN: called CC=1 SCAR=0.0
24  
25 40 our $VERSION = 99;
26  
27 # uncomment this to run the ### lines
28 # use Smart::Comments;
29  
30  
31 # sub new {
32 # my $class = shift;
33 # ### Pulp-PodParser new()
34 # my $self = $class->SUPER::new (@_);
35 # return $self;
36 # }
37 sub initialize {
38 322 ▶ my ($self) = @_;
Subroutine initialize: called CC=1 SCAR=0.0
Pod undocumented
39 ### initialize() ...
40  
41 # empty violations for violations() to return before a parse
42 322 $self->{'violations'} = [];
43 322 $self->{'in_begin'} = '';
44 322 $self->errorsub ('error_handler'); # method name
45  
46 # Note: The violations list is never cleared. Might like to do so at the
47 # start of a new a pod document, though this parser is only ever used on a
48 # single document and then discarded. begin_input() and begin_pod() are
49 # no good as they're invoked for each chunk fed in by parse_from_elem().
50 }
51  
52 sub error_handler {
53 3 ▶ my ($self, $errmsg) = @_;
Subroutine error_handler: called CC=1 SCAR=0.0
Pod undocumented
54 ### error_handler() ...
55 3 return 1; # error handled
56  
57 # Don't think it's the place of this policy to report pod parse errors.
58 # Maybe within sections a policy is operating on, on the basis that could
59 # affect the goodness of its checks, but better leave it all to podchecker
60 # or other perlcritic policies.
61 #
62 # my $policy = $self->{'policy'};
63 # my $elem = $self->{'elem'};
64 # push @{$self->{'violations'}},
65 # $policy->violation ("Pod::Parser $errmsg", '', $elem);
66 }
67  
68 sub parse_from_elem {
69 322 ▶ my ($self, $elem) = @_;
Subroutine parse_from_elem: called CC=4 SCAR=13.9
Pod undocumented
70 ### Pulp-PodParser parse_from_elem(): ref($elem)
71  
72 322 ▶ my $elems = ($elem->can('find')
Branch: 1 branch
BranchTrueFalseTotal
$elem->can('find') ? : 322 0 322
Condition100%2/2
($elem->can('find') ? $elem->find('PPI::Token::Pod') : [$elem]) || (return)
l!l
3193
MC/DC100%1/1
($elem->can('find') ? $elem->find('PPI::Token::Pod') : [$elem]) || (return)
($elem->can('find') ? $elem->find('PPI::Token::Pod') : [$elem])
Truth table100%2/2
A
A= ($elem->can('find') ? $elem->find('PPI::Token::Pod') : [$elem])
Aresult
11
00
73 ? $elem->find ('PPI::Token::Pod')
74 : [ $elem ])
75 || return; # find() returns false if nothing found
76 319 foreach my $pod (@$elems) {
77 ### pod chunk at linenum: $pod->line_number
78 325 $self->{'elem'} = $pod;
79 325 $self->parse_from_string ($pod->content);
80 }
81 }
82  
83 # this is generic except for holding onto $str ready for violation override
84 sub parse_from_string {
85 325 ▶ my ($self, $str) = @_;
Subroutine parse_from_string: called CC=1 SCAR=0.0
Pod undocumented
86 325 $self->{'str'} = $str;
87 325 require IO::String;
88 325 my $fh = IO::String->new ($str);
89 325 $self->parse_from_filehandle ($fh);
90 }
91  
92 sub command {
93 323 ▶ my ($self, $command, $text, $linenum) = @_;
Subroutine command: called CC=6 SCAR=18.0
Pod undocumented
94 323 ▶ if ($command eq 'begin') {
Branch: 2 branches
BranchTrueFalseTotal
if ($command eq 'begin') { } 31 292 323
elsif ($command eq 'end') { } 21 271 292
95 31 push @{$self->{'in_begin_stack'}}, $self->{'in_begin'};
96 31 ▶ if ($text =~ /^:/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($text =~ /^:/) { } 6 25 31
elsif ($text =~ /(\w+)/) { } 24 1 25
97 # "=begin :foo" is ordinary POD
98 6 $self->{'in_begin'} = '';
99 } elsif ($text =~ /(\w+)/) {
100 24 $self->{'in_begin'} = $1; # first word only
101 } else {
102 # "=begin" with no word chars ...
103 1 $self->{'in_begin'} = '';
104 }
105 ### in_begin: $self->{'in_begin'}
106  
107 } elsif ($command eq 'end') {
108 21 $self->{'in_begin'} = pop @{$self->{'in_begin_stack'}};
109 21 ▶ if (! defined $self->{'in_begin'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $self->{"in_begin"}) 0 21 21
110 0 $self->{'in_begin'} = '';
111 }
112 ### pop to in_begin: $self->{'in_begin'}
113 }
114 }
115 40 ▶use constant verbatim => '';
Subroutine BEGIN: called CC=1 SCAR=0.0
116 40 ▶use constant textblock => '';
Subroutine BEGIN: called CC=1 SCAR=0.0
117  
118 sub violation_at_linenum {
119 182 ▶ my ($self, $message, $linenum) = @_;
Subroutine violation_at_linenum: called CC=4 SCAR=15.0
Pod undocumented
120 ### violation on elem: ref($self->{'elem'})
121  
122 182 my $policy = $self->{'policy'};
123 ### policy: ref($policy)
124 182 my $violation = $policy->violation ($message, '', $self->{'elem'});
125  
126 # fix dodgy Perl::Critic::Policy 1.108 violation() ending up with caller
127 # package not given $policy
128 182 ▶ if ($violation->policy eq __PACKAGE__
Branch: 1 branch
BranchTrueFalseTotal
if ($violation->policy eq __PACKAGE__ and defined $violation->{"_policy"} and $violation->{"_policy"} eq __PACKAGE__) 182 0 182
Condition33%2/6
$violation->policy eq __PACKAGE__ and defined $violation->{"_policy"} and $violation->{"_policy"} eq __PACKAGE__
!ll&&!rl&&r
00182
$violation->policy eq __PACKAGE__ and defined $violation->{"_policy"}
!ll&&!rl&&r
00182
MC/DC0%0/3
$violation->policy eq __PACKAGE__ and defined $violation->{"_policy"} and $violation->{"_policy"} eq __PACKAGE__
$violation->policy eq __PACKAGE__ defined $violation->{"_policy"} $violation->{"_policy"} eq __PACKAGE__
Truth table25%1/4
A and B and C
A= $violation->policy eq __PACKAGE__B= defined $violation->{"_policy"}C= $violation->{"_policy"} eq __PACKAGE__
ABCresult
0XX0
10X0
1100
1111
129 && defined $violation->{'_policy'}
130 && $violation->{'_policy'} eq __PACKAGE__) {
131 182 $violation->{'_policy'} = ref($policy);
132 }
133  
134 Perl::Critic::Pulp::Utils::_violation_override_linenum
135 182 ($violation, $self->{'str'}, $linenum);
136 ### $violation
137 182 push @{$self->{'violations'}}, $violation;
138 }
139  
140 sub violation_at_linenum_and_textpos {
141 136 ▶ my ($self, $message, $linenum, $text, $pos) = @_;
Subroutine violation_at_linenum_and_textpos: called CC=1 SCAR=0.0
Pod undocumented
142 ### violation_at_linenum_and_textpos()
143 ### $message
144 ### $linenum
145 ### $pos
146  
147 136 my $part = substr($text,0,$pos);
148 136 $linenum += ($part =~ tr/\n//);
149 136 $self->violation_at_linenum ($message, $linenum);
150 }
151  
152 # return list of violation objects (possibly empty)
153 sub violations {
154 322 ▶ my ($self) = @_;
Subroutine violations: called CC=1 SCAR=0.0
Pod undocumented
155 322 return @{$self->{'violations'}};
156 }
157  
158 #------------------------------------------------------------------------------
159 # This not documented yet. Might prefer to split it out for separate use too.
160 #
161 # Not sure about padding to make the column right. Usually good, but
162 # perhaps not always. Maybe should offset a column by examining
163 # $paraobj->cmd_prefix() and $paraobj->cmd_name().
164  
165 {
166 40 my %command_non_text = (for => 1,
167 begin => 1,
168 end => 1,
169 cut => 1);
170  
171 # The parameters are as per the command() method of Pod::Parser.
172 # If $command contains text style markup then call $self->textblock() on
173 # its text.
174 # All commands except =for, =begin, =end and =cut have marked-up text.
175 # Eg. =head2 C<blah blah>
176 #
177 sub command_as_textblock {
178 203 ▶ my ($self, $command, $text, $linenum, $paraobj) = @_;
Subroutine command_as_textblock: called CC=3 SCAR=11.0
Pod undocumented
179 ### command: $command
180 ### $text
181  
182 # $text can be undef if =foo with no newline at end-of-file
183 203 ▶ if (defined $text && ! $command_non_text{$command}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $text and not $command_non_text{$command}) 179 24 203
Condition66%2/3
defined $text and not $command_non_text{$command}
!ll&&!rl&&r
024179
MC/DC50%1/2
defined $text and not $command_non_text{$command}
defined $text not $command_non_text{$command}
Truth table66%2/3
A and not(B)
A= defined $textB= not $command_non_text{$command}
ABresult
0X0
100
111
184 # padded to make the column number right, the leading spaces do no harm
185 # for this policy
186 179 $self->textblock ((' ' x (length($command)+1)) . $text,
187 $linenum,
188 $paraobj);
189 }
190 203 return '';
191 }
192 }
193  
194 40 1;
195 __END__
196  
197 =for stopwords perlcritic Ryde
198  
199 =head1 NAME
200  
201 Perl::Critic::Pulp::PodParser - shared POD parsing code for the Pulp perlcritic add-on
202  
203 =head1 SYNOPSIS
204  
205 use base 'Perl::Critic::Pulp::PodParser';
206  
207 =head1 DESCRIPTION
208  
209 This is only meant for internal use yet.
210  
211 It's some shared parse-from-element, error suppression, no output, violation
212 accumulation and violation line number things for POD parsing in policies.
213  
214 =head1 SEE ALSO
215  
216 L<Perl::Critic::Pulp>
217  
218 =head1 HOME PAGE
219  
220 L<http://user42.tuxfamily.org/perl-critic-pulp/index.html>
221  
222 =head1 COPYRIGHT
223  
224 Copyright 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2019, 2021 Kevin Ryde
225  
226 Perl-Critic-Pulp is free software; you can redistribute it and/or modify it
227 under the terms of the GNU General Public License as published by the Free
228 Software Foundation; either version 3, or (at your option) any later
229 version.
230  
231 Perl-Critic-Pulp is distributed in the hope that it will be useful, but
232 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
233 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
234 more details.
235  
236 You should have received a copy of the GNU General Public License along with
237 Perl-Critic-Pulp. If not, see <http://www.gnu.org/licenses/>.
238  
239 =cut
« Policy/Documentation/ProhibitAdjacentLinks.pm ↑ Summary Policy/Miscellanea/TextDomainUnused.pm »