Policy/Documentation/ProhibitUnbalancedParens.pm

filter: Statementstmt 99.0% 99 / 100 Branchbran 94.1% 32 / 34 Conditioncond 78.5% 11 / 14 MC/DCmcdc 66.6% 6 / 9 Subroutinesub 100% 17 / 17 Podpod 100% 1 / 1 totaltotal 94.8% 166 / 175 CC 26 SCAR 22.9
CC
26
Cov
96%
textblock
19.0
command
5.0
interior_sequence_as_displayed_noncode_text
4.0
CRAP 9.9
« Policy/Compatibility/Gtk2Constants.pm ↑ Summary Policy/Documentation/ProhibitAdjacentLinks.pm »
1 # Copyright 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  
19 # perlcritic -s ProhibitUnbalancedParens ProhibitUnbalancedParens.pm
20  
21 # unclosed:
22 # perlcritic -s ProhibitUnbalancedParens /usr/share/perl/5.12/CGI.pm
23  
24 # smiley close:
25 # perlcritic -s ProhibitUnbalancedParens /usr/share/perl5/accessors.pm
26  
27  
28 package Perl::Critic::Policy::Documentation::ProhibitUnbalancedParens;
29 40 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
30 40 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
31 40 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
32 40 ▶use base 'Perl::Critic::Policy';
Subroutine BEGIN: called CC=1 SCAR=0.0
33 40 ▶use Perl::Critic::Utils;
Subroutine BEGIN: called CC=1 SCAR=0.0
34  
35 # uncomment this to run the ### lines
36 # use Smart::Comments;
37  
38 40 our $VERSION = 100;
39  
40 40 ▶use constant supported_parameters => ();
Subroutine BEGIN: called CC=1 SCAR=0.0
41 40 ▶use constant default_severity => $Perl::Critic::Utils::SEVERITY_LOW;
Subroutine BEGIN: called CC=1 SCAR=0.0
42 40 ▶use constant default_themes => qw(pulp cosmetic);
Subroutine BEGIN: called CC=1 SCAR=0.0
43 40 ▶use constant applies_to => 'PPI::Document';
Subroutine BEGIN: called CC=1 SCAR=0.0
44  
45 75 ▶sub violates {
Subroutine violates: called CC=1 SCAR=0.0
Pod documented
46 75 my ($self, $elem, $document) = @_;
47 ### ProhibitUnbalancedParens on: $elem->content
48  
49 75 my $parser = Perl::Critic::Pulp::PodParser::ProhibitUnbalancedParens->new
50 (policy => $self);
51 75 $parser->parse_from_elem ($elem);
52 75 return $parser->violations;
53 }
54  
55 package Perl::Critic::Pulp::PodParser::ProhibitUnbalancedParens;
56 40 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
57 40 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
58 40 ▶use Pod::ParseLink;
Subroutine BEGIN: called CC=1 SCAR=0.0
59 40 ▶use base 'Perl::Critic::Pulp::PodParser';
Subroutine BEGIN: called CC=1 SCAR=0.0
60  
61 79 ▶sub command {
Subroutine command: called CC=5 SCAR=16.2
62 79 my $self = shift;
63 79 my ($command, $text, $linenum, $paraobj) = @_;
64 79 $self->SUPER::command(@_); # maintain 'in_begin'
65  
66 79 ▶ if ($command eq 'for'
Branch: 1 branch
BranchTrueFalseTotal
if ($command eq "for" and $text =~ /^ProhibitUnbalancedParens\b\s*(.*)/) 4 75 79
Condition66%2/3
$command eq "for" and $text =~ /^ProhibitUnbalancedParens\b\s*(.*)/
!ll&&!rl&&r
7504
MC/DC50%1/2
$command eq "for" and $text =~ /^ProhibitUnbalancedParens\b\s*(.*)/
$command eq "for" $text =~ /^ProhibitUnbalancedParens\b\s*(.*)/
Truth table66%2/3
A and B
A= $command eq "for"B= $text =~ /^ProhibitUnbalancedParens\b\s*(.*)/
ABresult
0X0
100
111
67 && $text =~ /^ProhibitUnbalancedParens\b\s*(.*)/) {
68 4 my $directive = $1;
69 ### $directive
70 4 ▶ if ($directive =~ /^allow next( (\d+))?/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($directive =~ /^allow next( (\d+))?/) 4 0 4
71 # numbered "allow next 5" means up to that many following
72 # unnumbered "allow next" means one following
73 4 ▶ $self->{'allow_next'} = (defined $2 ? $2 : 1);
Branch: 1 branch
BranchTrueFalseTotal
defined $2 ? : 2 2 4
74 }
75 }
76 79 return $self->command_as_textblock(@_);
77 }
78  
79 40 my %open_to_close = ('(' => ')',
80 '[' => ']',
81 '{' => '}');
82 40 my %close_to_open = reverse %open_to_close;
83  
84 152 ▶sub textblock {
Subroutine textblock: called CC=19 SCAR=29.5
85 152 my ($self, $text, $linenum, $paraobj) = @_;
86 ### textblock: "linenum=$linenum"
87  
88 152 ▶ if (($self->{'allow_next'}||0) > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (($self->{"allow_next"} || 0) > 0) 6 146 152
Condition100%2/2
$self->{'allow_next'} || 0
l!l
6146
MC/DC100%1/1
$self->{'allow_next'} || 0
$self->{'allow_next'}
Truth table100%2/2
A
A= $self->{'allow_next'}
Aresult
11
00
89 6 $self->{'allow_next'}--;
90 6 return '';
91 }
92  
93 # process outside =begin, and inside =begin which is ":" markup
94 146 ▶ unless ($self->{'in_begin'} eq '' || $self->{'in_begin'} =~ /^:/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"in_begin"} eq "" or $self->{"in_begin"} =~ /^:/) 1 145 146
Condition66%2/3
$self->{"in_begin"} eq "" or $self->{"in_begin"} =~ /^:/
l!l&&r!l&&!r
14501
MC/DC50%1/2
$self->{"in_begin"} eq "" or $self->{"in_begin"} =~ /^:/
$self->{"in_begin"} eq "" $self->{"in_begin"} =~ /^:/
Truth table66%2/3
A or B
A= $self->{"in_begin"} eq ""B= $self->{"in_begin"} =~ /^:/
ABresult
1X1
011
000
95 1 return '';
96 }
97  
98 145 my $interpolated = $self->interpolate($text, $linenum);
99 ### $text
100 ### $interpolated
101  
102 145 my @parens;
103 145 ▶ while ($interpolated
Branch: 1 branch
BranchTrueFalseTotal
if ($interpolated =~ / ([][({})]) # $1 open or close |([:;]-?\) # $2 smiley face optional close |\b[a-zA-Z1-9]\) # "middle a) or 1) item" |(?<!\$)\$\) # perlvar $), and not $$ ) |(["'])[][(){}]+\3 # $3 "(" quoted |[:;]-?[(] # smiley face not an open |(?<!\$)\$\$ # perlvar $$ consumed |\$\(\w*\) # makefile var $(abc) |\$\[\w*\] # perhaps template $[abc] |(?<!\$)\$[][(] # perlvars $[, $(, $], and not $$ |^\s*(\d+|[A-Za-z])\.?\) # initial "1.5) something" /gx) 117 145 262
104 =~ m/
105 ([][({})]) # $1 open or close
106 |([:;]-?\) # $2 smiley face optional close
107 |\b[a-zA-Z1-9]\) # "middle a) or 1) item"
108 |(?<!\$)\$\) # perlvar $), and not $$
109 )
110 |(["'])[][(){}]+\3 # $3 "(" quoted
111 |[:;]-?[(] # smiley face not an open
112 |(?<!\$)\$\$ # perlvar $$ consumed
113 |\$\(\w*\) # makefile var $(abc)
114 |\$\[\w*\] # perhaps template $[abc]
115 |(?<!\$)\$[][(] # perlvars $[, $(, $], and not $$
116 |^\s*(\d+|[A-Za-z])\.?\) # initial "1.5) something"
117 /xg) {
118 ### match: $&
119 ### $1
120 ### $2
121 ### $3
122 117 ▶ if (defined $1) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $1) { } 65 52 117
elsif (defined $2) { } 25 27 52
123 65 push @parens, { char => $1,
124 pos => pos($interpolated)-1,
125 };
126  
127 } elsif (defined $2) {
128 25 push @parens, { char => ')',
129 pos => pos($interpolated)-1,
130 optional => 1,
131 };
132 }
133 }
134 ### @parens
135  
136 # sort optional closes to after hard closes
137 {
138 145 my @optional;
139 my @new;
140 145 foreach my $p (@parens) {
141 90 ▶ if (@optional && $optional[0]->{'char'} ne $p->{'char'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (@optional and $optional[0]{"char"} ne $p->{"char"}) 5 85 90
Condition100%3/3
@optional and $optional[0]{"char"} ne $p->{"char"}
!ll&&!rl&&r
8145
MC/DC100%2/2
@optional and $optional[0]{"char"} ne $p->{"char"}
@optional $optional[0]{"char"} ne $p->{"char"}
Truth table100%3/3
A and B
A= @optionalB= $optional[0]{"char"} ne $p->{"char"}
ABresult
0X0
100
111
142 5 push @new, splice @optional;
143 }
144 90 ▶ if ($p->{'optional'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($p->{'optional'}) { } 25 65 90
145 25 push @optional, $p;
146 } else {
147 65 push @new, $p;
148 }
149 }
150 145 @parens = (@new, @optional);
151 }
152 ### sorted: @parens
153  
154 145 my @opens;
155 145 foreach my $p (@parens) {
156 ### $p
157 90 my $char = $p->{'char'};
158 90 ▶ if (my $want_openchar = $close_to_open{$char}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $want_openchar = $close_to_open{$char}) { } 42 48 90
159 # a close
160 42 ▶ if (my $o = pop @opens) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $o = pop @opens) { } 28 14 42
161 28 my $openchar = $o->{'char'};
162 28 ▶ if ($openchar ne $want_openchar) {
Branch: 1 branch
BranchTrueFalseTotal
if ($openchar ne $want_openchar) 2 26 28
163 2 ▶ if ($p->{'optional'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($p->{"optional"}) 2 0 2
164 ### mismatched optional close, skip
165 2 push @opens, $o;
166 2 next;
167 }
168 $self->violation_at_linenum_and_textpos
169 ("Mismatched closing paren \"$char\" expected \"$open_to_close{$openchar}\"",
170 0 $linenum, $interpolated, $p->{'pos'});
171 }
172  
173 } else {
174 14 ▶ if ($p->{'optional'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($p->{"optional"}) 12 2 14
175 ### unopened optional close, skip
176 12 next;
177 }
178 $self->violation_at_linenum_and_textpos
179 ("Unopened close paren \"$char\"",
180 2 $linenum, $interpolated, $p->{'pos'});
181 }
182  
183 } else {
184 # an open
185 48 push @opens, $p;
186 }
187 }
188 145 foreach my $p (@opens) {
189 $self->violation_at_linenum_and_textpos
190 ("Unclosed paren \"$p->{'char'}\"",
191 22 $linenum, $interpolated, $p->{'pos'});
192 }
193 145 return '';
194 }
195  
196 40 *interior_sequence = \&interior_sequence_as_displayed_noncode_text;
197  
198 12 ▶sub interior_sequence_as_displayed_noncode_text {
Subroutine interior_sequence_as_displayed_noncode_text: called CC=4 SCAR=13.9
199 12 my ($self, $cmd, $text, $pod_seq) = @_;
200  
201 12 ▶ if ($cmd eq 'X' || $cmd eq 'C') {
Branch: 2 branches
BranchTrueFalseTotal
if ($cmd eq 'X' or $cmd eq 'C') { } 7 5 12
elsif ($cmd eq 'L') { } 3 2 5
Condition66%2/3
$cmd eq "X" or $cmd eq "C"
l!l&&r!l&&!r
075
MC/DC50%1/2
$cmd eq "X" or $cmd eq "C"
$cmd eq "X" $cmd eq "C"
Truth table66%2/3
A or B
A= $cmd eq "X"B= $cmd eq "C"
ABresult
1X1
011
000
202 ### $cmd
203 ### X,C keep only the newlines: $text
204 7 $text =~ tr/\n//cd;
205  
206 } elsif ($cmd eq 'L') {
207 3 my ($display, $inferred, $name, $section, $type)
208 = Pod::ParseLink::parselink ($text);
209 ### $text
210 ### $display
211 ### $inferred
212 ### $name
213 3 return $inferred; # the display part, or the name part if no display
214 }
215 9 return $text;
216 }
217  
218 40 1;
219 __END__
« Policy/Compatibility/Gtk2Constants.pm ↑ Summary Policy/Documentation/ProhibitAdjacentLinks.pm »