Policy/Documentation/ProhibitDuplicateHeadings.pm

filter: Statementstmt 99.0% 101 / 102 Branchbran 88.2% 30 / 34 Conditioncond 80.0% 4 / 5 MC/DCmcdc 66.6% 2 / 3 Subroutinesub 100% 15 / 15 Podpod 100% 1 / 1 totaltotal 95.6% 153 / 160 CC 24 SCAR 23.8
CC
24
Cov
96%
command
19.0
_parse_uniqueness
6.0
BEGIN
1.0
CRAP 10.8
« Policy/CodeLayout/RequireTrailingCommaAtNewline.pm ↑ Summary Policy/Compatibility/Gtk2Constants.pm »
1 # Copyright 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 ProhibitDuplicateHeadings ProhibitDuplicateHeadings.pm
20 #
21 # duplicate BUGS
22 # perlcritic -s ProhibitDuplicateHeadings /usr/share/perl5/Acme/Tie/Eleet.pm
23  
24 # duplicate toplevel CLASS METHODS
25 # perlcritic -s ProhibitDuplicateHeadings /usr/share/perl5/Games/Euchre/Trick.pm
26  
27 # duplicate =head2 serialise
28 # perlcritic -s ProhibitDuplicateHeadings /usr/share/perl5/SVG/Extension.pm
29  
30  
31 package Perl::Critic::Policy::Documentation::ProhibitDuplicateHeadings;
32 40 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
33 40 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
34 40 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
35 40 ▶use base 'Perl::Critic::Policy';
Subroutine BEGIN: called CC=1 SCAR=0.0
36 40 ▶use Perl::Critic::Utils;
Subroutine BEGIN: called CC=1 SCAR=0.0
37  
38 # uncomment this to run the ### lines
39 # use Smart::Comments;
40  
41 40 our $VERSION = 100;
42  
43 40 ▶use constant supported_parameters =>
Subroutine BEGIN: called CC=1 SCAR=0.0
44 ({ name => 'uniqueness',
45 description => 'The scope for headings names, meaning to what extent they must not be duplicates. Choices nested, all.',
46 behavior => 'string',
47 default_string => 'default',
48 parser => \&_parse_uniqueness,
49 40 });
50 40 ▶use constant default_severity => $Perl::Critic::Utils::SEVERITY_MEDIUM;
Subroutine BEGIN: called CC=1 SCAR=0.0
51 40 ▶use constant default_themes => qw(pulp bugs);
Subroutine BEGIN: called CC=1 SCAR=0.0
52 40 ▶use constant applies_to => 'PPI::Document';
Subroutine BEGIN: called CC=1 SCAR=0.0
53  
54 45 ▶sub violates {
Subroutine violates: called CC=1 SCAR=0.0
Pod documented
55 45 my ($self, $elem, $document) = @_;
56 ### ProhibitDuplicateHeadings ...
57 ### _uniqueness: $self->{'_uniqueness'}
58 # ### content: $elem->content
59  
60 45 my $parser = Perl::Critic::Pulp::PodParser::ProhibitDuplicateHeadings->new
61 (policy => $self);
62 45 $parser->parse_from_elem ($elem);
63  
64 ### violations return: [ $parser->violations ]
65 45 return $parser->violations;
66 }
67  
68 40 my %known_uniqueness = ('' => 1, # for trailing comma
69 all => 1,
70 ancestor => 1,
71 sibling => 1,
72 adjacent => 1,
73 default => 1,
74 );
75 40 my %uniqueness_expand = (default => [ 'ancestor', 'sibling', 'adjacent' ],
76 );
77 52 ▶sub _parse_uniqueness {
Subroutine _parse_uniqueness: called CC=6 SCAR=18.0
78 52 my ($self, $parameter, $str) = @_;
79 ### _parse_uniqueness ...
80 ### $parameter
81 ### $str
82  
83 52 ▶ if (! defined $str) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $str) 46 6 52
84 46 $str = $parameter->get_default_string;
85 ### default: $str
86 }
87  
88 52 my %uhash;
89 52 foreach my $key (split /,/, $str) {
90 53 $key =~ s/^\s+//;
91 53 $key =~ s/\s+$//;
92 53 ▶ if (! $known_uniqueness{$key}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($known_uniqueness{$key}) 0 53 53
93 0 $self->throw_parameter_value_exception
94 ($parameter->get_name,
95 $str,
96 undef, # source
97 'unrecognised uniqueness');
98 }
99 53 ▶ if (my $aref = $uniqueness_expand{$key}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $aref = $uniqueness_expand{$key}) { } 47 6 53
100 47 foreach my $key (@$aref) {
101 141 $uhash{$key} = 1;
102 }
103 } else {
104 6 $uhash{$key} = 1;
105 }
106 }
107  
108 ### %uhash
109 52 $self->__set_parameter_value ($parameter, \%uhash);
110 }
111  
112 #------------------------------------------------------------------------------
113 package Perl::Critic::Pulp::PodParser::ProhibitDuplicateHeadings;
114 40 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
115 40 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
116 40 ▶use base 'Perl::Critic::Pulp::PodParser';
Subroutine BEGIN: called CC=1 SCAR=0.0
117  
118 140 ▶sub command {
Subroutine command: called CC=19 SCAR=29.5
119 140 my $self = shift;
120 140 my ($command, $text, $linenum, $paraobj) = @_;
121 ### $command
122  
123 140 ▶ if ($command =~ /^head(\d*)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($command =~ /^head(\d*)$/) 140 0 140
124 140 ▶ my $level = $1 || 0;
Condition50%1/2
$1 || 0
l!l
1400
MC/DC0%0/1
$1 || 0
$1
Truth table50%1/2
A
A= $1
Aresult
11
00
125 140 $text =~ s/^\s+//; # leading whitespace
126 140 $text =~ s/\s+$//; # trailing whitespace
127 140 $text =~ s/\s+/ /; # collapse whitespace to single space each
128 ### $text
129 ### $level
130  
131 140 my $uniqueness = $self->{'policy'}->{'_uniqueness'};
132 140 my $seen_linenum;
133 my $seen_type;
134  
135 140 ▶ if ($uniqueness->{'all'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($uniqueness->{"all"}) 28 112 140
136 28 ▶ unless (defined $seen_linenum) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $seen_linenum) 28 0 28
137 28 $seen_linenum = $self->{'seen_all'}->{$text};
138 28 $seen_type = ' ';
139 }
140  
141 28 $self->{'seen_all'}->{$text} = $linenum;
142 }
143  
144 140 ▶ if ($uniqueness->{'adjacent'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($uniqueness->{"adjacent"}) 53 87 140
145 53 ▶ unless (defined $seen_linenum) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $seen_linenum) 53 0 53
146 53 ▶ if (defined $self->{'seen_adjacent'}
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"seen_adjacent"} and $text eq $self->{"seen_adjacent"}) 16 37 53
Condition100%3/3
defined $self->{"seen_adjacent"} and $text eq $self->{"seen_adjacent"}
!ll&&!rl&&r
172016
MC/DC100%2/2
defined $self->{"seen_adjacent"} and $text eq $self->{"seen_adjacent"}
defined $self->{"seen_adjacent"} $text eq $self->{"seen_adjacent"}
Truth table100%3/3
A and B
A= defined $self->{"seen_adjacent"}B= $text eq $self->{"seen_adjacent"}
ABresult
0X0
100
111
147 && $text eq $self->{'seen_adjacent'}) {
148 16 $seen_linenum = $self->{'seen_adjacent_linenum'};
149 16 $seen_type = ' adjacent ';
150 }
151 }
152 53 $self->{'seen_adjacent'} = $text;
153 53 $self->{'seen_adjacent_linenum'} = $linenum;
154 }
155  
156 140 ▶ if ($uniqueness->{'sibling'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($uniqueness->{"sibling"}) 59 81 140
157 ### seen_sibling: $self->{'seen_sibling'}
158 59 ▶ unless (defined $seen_linenum) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $seen_linenum) 50 9 59
159 50 $seen_linenum = $self->{'seen_sibling'}->{$level}->{$text};
160 50 $seen_type = ' sibling ';
161 }
162  
163 # discard anything > $level
164 59 foreach my $l (keys %{$self->{'seen_sibling'}}) {
165 91 ▶ if ($l > $level) {
Branch: 1 branch
BranchTrueFalseTotal
if ($l > $level) 15 76 91
166 15 delete $self->{'seen_sibling'}->{$l};
167 }
168 }
169 59 $self->{'seen_sibling'}->{$level}->{$text} = $linenum;
170 }
171  
172 140 ▶ if ($uniqueness->{'ancestor'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($uniqueness->{"ancestor"}) 59 81 140
173 59 foreach my $l (sort {$a<=>$b} # biggest to smallest
174 59 keys %{$self->{'seen_ancestor'}}) {
175 55 ▶ if ($l < $level) {
Branch: 1 branch
BranchTrueFalseTotal
if ($l < $level) { } 21 34 55
176 21 ▶ if ($text eq $self->{'seen_ancestor'}->{$l}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($text eq $self->{"seen_ancestor"}{$l}) 5 16 21
177 5 ▶ unless (defined $seen_linenum) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $seen_linenum) 3 2 5
178 3 $seen_linenum = $self->{'seen_ancestor_linenum'}->{$l};
179 3 $seen_type = ' ancestor ';
180 }
181 }
182 } else {
183 34 delete $self->{'seen_ancestor'}->{$l};
184 }
185 }
186 59 $self->{'seen_ancestor'}->{$level} = $text;
187 59 $self->{'seen_ancestor_linenum'}->{$level} = $linenum;
188 }
189  
190 ### $seen_linenum
191 ### $seen_type
192 140 ▶ if (defined $seen_linenum) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $seen_linenum) 38 102 140
193 38 $self->violation_at_linenum
194 ("Duplicate$seen_type=head \"$text\", previously seen at line $seen_linenum",
195 $linenum);
196 ### violation at line: $linenum
197 }
198 }
199 140 return '';
200 }
201  
202 40 1;
203 __END__
« Policy/CodeLayout/RequireTrailingCommaAtNewline.pm ↑ Summary Policy/Compatibility/Gtk2Constants.pm »