Policy/ValuesAndExpressions/ProhibitDuplicateHashKeys.pm

filter: Statementstmt 90.1% 110 / 122 Branchbran 75.8% 44 / 58 Conditioncond 63.8% 23 / 36 MC/DCmcdc 50.0% 10 / 20 Subroutinesub 100% 18 / 18 Podpod 100% 1 / 1 totaltotal 80.7% 206 / 255 CC 44 SCAR 34.1
CC
44
Cov
82%
_elem_is_assigned_to_hash
75.6
violates
26.3
_take_to_comma
4.0
CRAP 30.4
« Policy/ValuesAndExpressions/ProhibitUnknownBackslash.pm ↑ Summary Policy/ValuesAndExpressions/NotWithCompare.pm »
1 # Copyright 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 package Perl::Critic::Policy::ValuesAndExpressions::ProhibitDuplicateHashKeys;
20 40 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 40 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 40 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 40 ▶use base 'Perl::Critic::Policy';
Subroutine BEGIN: called CC=1 SCAR=0.0
24 40 ▶use Perl::Critic::Utils;
Subroutine BEGIN: called CC=1 SCAR=0.0
25  
26 40 ▶use Perl::Critic::Policy::CodeLayout::RequireFinalSemicolon;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 40 ▶use Perl::Critic::Policy::Miscellanea::TextDomainPlaceholders;
Subroutine BEGIN: called CC=1 SCAR=0.0
28 40 ▶use Perl::Critic::Policy::ValuesAndExpressions::ConstantBeforeLt;
Subroutine BEGIN: called CC=1 SCAR=0.0
29 40 ▶use Perl::Critic::Pulp::Utils 'elem_is_comma_operator';
Subroutine BEGIN: called CC=1 SCAR=0.0
30  
31 # uncomment this to run the ### lines
32 #use Smart::Comments;
33  
34  
35 40 our $VERSION = 100;
36  
37 40 ▶use constant supported_parameters => ();
Subroutine BEGIN: called CC=1 SCAR=0.0
38 40 ▶use constant default_severity => $Perl::Critic::Utils::SEVERITY_MEDIUM;
Subroutine BEGIN: called CC=1 SCAR=0.0
39 40 ▶use constant default_themes => qw(pulp bugs);
Subroutine BEGIN: called CC=1 SCAR=0.0
40 40 ▶use constant applies_to => ('PPI::Structure::Constructor',
Subroutine BEGIN: called CC=1 SCAR=0.0
41 'PPI::Structure::List',
42 # this policy is not for blocks, but PPI
43 # mis-reports some anonymous hashref
44 # constructors as blocks, so look at them
45 40 'PPI::Structure::Block');
46  
47 63 ▶sub violates {
Subroutine violates: called CC=26 SCAR=32.7
Pod documented
48 63 my ($self, $elem, $document) = @_;
49 ### ProhibitDuplicateHashKeys violates() ...
50  
51 ### consider: (ref $elem)." $elem"
52  
53 63 ▶ if ($elem->isa('PPI::Structure::Constructor')) {
Branch: 2 branches
BranchTrueFalseTotal
if ($elem->isa('PPI::Structure::Constructor')) { } 9 54 63
elsif ($elem->isa('PPI::Structure::Block')) { } 11 43 54
54 ### constructor ...
55 9 ▶ unless ($elem->start eq '{') {
Branch: 1 branch
BranchTrueFalseTotal
unless ($elem->start eq "{") 0 9 9
56 ### constructor is not a hash ...
57 0 return;
58 }
59  
60 } elsif ($elem->isa('PPI::Structure::Block')) {
61 ### block ...
62 11 ▶ if (Perl::Critic::Policy::CodeLayout::RequireFinalSemicolon::_block_is_hash_constructor($elem) == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (Perl::Critic::Policy::CodeLayout::RequireFinalSemicolon::_block_is_hash_constructor($elem) == 1) { } 6 5 11
63 ### block is a hash, continue ...
64 } else {
65 ### block is a block, or not certain, stop ...
66 5 return;
67 }
68  
69 } else { # PPI::Structure::List
70 43 ▶ _elem_is_assigned_to_hash($elem) || return;
Branch: 1 branch
BranchTrueFalseTotal
_elem_is_assigned_to_hash($elem) or return 39 4 43
71 }
72  
73 54 ▶ $elem = $elem->schild(0) || return;
Condition100%2/2
$elem->schild(0) || (return)
l!l
531
MC/DC100%1/1
$elem->schild(0) || (return)
$elem->schild(0)
Truth table100%2/2
A
A= $elem->schild(0)
Aresult
11
00
74 53 ▶ if ($elem->isa('PPI::Statement')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($elem->isa("PPI::Statement")) 53 0 53
75 53 ▶ $elem = $elem->schild(0) || return;
Condition50%1/2
$elem->schild(0) || (return)
l!l
530
MC/DC0%0/1
$elem->schild(0) || (return)
$elem->schild(0)
Truth table50%1/2
A
A= $elem->schild(0)
Aresult
11
00
76 }
77 ### first elem: (ref $elem)." $elem"
78  
79 53 my @elems = Perl::Critic::Policy::ValuesAndExpressions::ConstantBeforeLt::_elem_and_ssiblings($elem);
80 ### elems len: scalar(@elems)
81  
82 53 @elems = map {_expand_qw($_)} @elems;
83 ### expanded len: scalar(@elems)
84  
85 53 my $state = 'key';
86 53 my @violations;
87 my %seen_key;
88  
89 53 ▶ while (@elems) {
Branch: 1 branch
BranchTrueFalseTotal
if (@elems) 228 53 281
90 ### $state
91 228 my ($comma, @arg) = _take_to_comma(\@elems);
92  
93 228 ▶ if (! @arg) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@arg) 2 226 228
94 ### consecutive commas ...
95 2 next;
96 }
97  
98 226 $elem = $arg[0];
99 ### first of arg: (ref $elem)." $elem"
100 ### arg elem count: scalar(@arg)
101  
102 226 ▶ if ($elem->isa('PPI::Token::Cast') && $elem eq '%') {
Branch: 1 branch
BranchTrueFalseTotal
if ($elem->isa("PPI::Token::Cast") and $elem eq "%") 2 224 226
Condition66%2/3
$elem->isa("PPI::Token::Cast") and $elem eq "%"
!ll&&!rl&&r
22402
MC/DC50%1/2
$elem->isa("PPI::Token::Cast") and $elem eq "%"
$elem->isa("PPI::Token::Cast") $elem eq "%"
Truth table66%2/3
A and B
A= $elem->isa("PPI::Token::Cast")B= $elem eq "%"
ABresult
0X0
100
111
103 ### skip cast % even num elements ...
104 2 $state = 'key';
105 2 next;
106 }
107 # %$foo is an even number of things
108 224 ▶ if (@arg == 1
Branch: 1 branch
BranchTrueFalseTotal
if (@arg == 1 and $elem->isa("PPI::Token::Symbol") and $elem->raw_type eq "%") 3 221 224
Condition100%6/6
@arg == 1 and $elem->isa("PPI::Token::Symbol") and $elem->raw_type eq "%"
!ll&&!rl&&r
22013
@arg == 1 and $elem->isa("PPI::Token::Symbol")
!ll&&!rl&&r
122084
MC/DC100%3/3
@arg == 1 and $elem->isa("PPI::Token::Symbol") and $elem->raw_type eq "%"
@arg == 1 $elem->isa("PPI::Token::Symbol") $elem->raw_type eq "%"
Truth table100%4/4
A and B and C
A= @arg == 1B= $elem->isa("PPI::Token::Symbol")C= $elem->raw_type eq "%"
ABCresult
0XX0
10X0
1100
1111
109 && $elem->isa('PPI::Token::Symbol')
110 && $elem->raw_type eq '%') {
111 ### skip hash var even num elements ...
112 3 $state = 'key';
113 3 next;
114 }
115  
116 221 ▶ if ($state eq 'unknown' && $comma eq '=>') {
Branch: 1 branch
BranchTrueFalseTotal
if ($state eq "unknown" and $comma eq "=>") 5 216 221
Condition100%3/3
$state eq "unknown" and $comma eq "=>"
!ll&&!rl&&r
21245
MC/DC100%2/2
$state eq "unknown" and $comma eq "=>"
$state eq "unknown" $comma eq "=>"
Truth table100%3/3
A and B
A= $state eq "unknown"B= $comma eq "=>"
ABresult
0X0
100
111
117 5 $state = 'key';
118 }
119  
120 221 ▶ if ($state eq 'key') {
Branch: 2 branches
BranchTrueFalseTotal
if ($state eq 'key') { } 111 110 221
elsif ($state eq 'value') { } 106 4 110
121 111 my $str;
122 my $any_vars;
123 111 ▶ if ($elem->isa('Perl::Critic::Pulp::ProhibitDuplicateHashKeys::Qword')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($elem->isa('Perl::Critic::Pulp::ProhibitDuplicateHashKeys::Qword')) { } 5 106 111
124 ### qword ...
125 5 $str = $elem->{'word'};
126 5 $any_vars = 0;
127 5 $elem = $elem->{'elem'};
128 } else {
129 106 ($str, $any_vars) = Perl::Critic::Policy::Miscellanea::TextDomainPlaceholders::_arg_string(\@arg, $document);
130 }
131  
132 ### $str
133 111 ▶ if (defined $str
Branch: 1 branch
BranchTrueFalseTotal
if (defined $str and not $any_vars and $seen_key{$str}++) 40 71 111
Condition83%5/6
defined $str and not $any_vars and $seen_key{$str}++
!ll&&!rl&&r
76440
defined $str and not $any_vars
!ll&&!rl&&r
07104
MC/DC66%2/3
defined $str and not $any_vars and $seen_key{$str}++
defined $str not $any_vars $seen_key{$str}++
Truth table75%3/4
A and not(B) and C
A= defined $strB= not $any_varsC= $seen_key{$str}++
ABCresult
0XX0
10X0
1100
1111
134 && ! $any_vars
135 && $seen_key{$str}++) {
136 ### found duplicate ...
137 40 push @violations, $self->violation ("Duplicate hash key \"$str\"",
138 '',
139 $elem);
140 }
141  
142 111 ▶ if ($any_vars >= 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($any_vars >= 2) { } 5 106 111
143 ### expression, go to unknown ...
144 5 $state = 'unknown';
145 } else {
146 106 $state = 'value';
147 }
148  
149 } elsif ($state eq 'value') {
150 106 ▶ if ($comma eq '=>') {
Branch: 1 branch
BranchTrueFalseTotal
if ($comma eq '=>') { } 0 106 106
151 ### hmm, something like a=>b=>..., assume next is a value still ...
152 0 $state = 'value';
153 } else {
154 106 $state = 'key';
155 }
156 }
157 }
158  
159 ### done ...
160 53 return @violations;
161 }
162  
163 419 ▶sub _expand_qw {
Subroutine _expand_qw: called CC=2 SCAR=6.9
164 419 my ($elem) = @_;
165 419 ▶ if (! $elem->isa('PPI::Token::QuoteLike::Words')) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($elem->isa("PPI::Token::QuoteLike::Words")) 416 3 419
166 416 return $elem;
167 }
168 3 my @words = $elem->literal;
169 ### @words
170  
171 return map {
172 3 Perl::Critic::Pulp::ProhibitDuplicateHashKeys::Qword->new
173 (word => $_,
174 elem => $elem);
175 } @words;
176 }
177  
178 228 ▶sub _take_to_comma {
Subroutine _take_to_comma: called CC=4 SCAR=13.9
179 228 my ($aref) = @_;
180 228 my @ret;
181 228 ▶ while (@$aref) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$aref) 425 48 473
182 425 my $elem = shift @$aref;
183 425 ▶ if ($elem->isa('Perl::Critic::Pulp::ProhibitDuplicateHashKeys::Qword')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($elem->isa("Perl::Critic::Pulp::ProhibitDuplicateHashKeys::Qword")) 9 416 425
184 9 push @ret, $elem;
185 9 return ',', @ret;
186 }
187 416 ▶ if (elem_is_comma_operator($elem)) {
Branch: 1 branch
BranchTrueFalseTotal
if (elem_is_comma_operator($elem)) 171 245 416
188 171 return $elem, @ret; # found a comma
189 }
190 245 push @ret, $elem; # not a comma
191 }
192 48 return '', @ret; # no final comma
193 }
194  
195 # $elem is any PPI::Element
196 # return true if it's assigned to a hash,
197 # %foo = ELEM
198 # %$foo = ELEM
199 # %{expr()} = ELEM
200 #
201 43 ▶sub _elem_is_assigned_to_hash {
Subroutine _elem_is_assigned_to_hash: called CC=15 SCAR=43.3
202 43 my ($elem) = @_;
203 ### _elem_is_assigned_to_hash() ...
204  
205 43 ▶ $elem = $elem->sprevious_sibling || return 0;
Condition50%1/2
$elem->sprevious_sibling || (return 0)
l!l
430
MC/DC0%0/1
$elem->sprevious_sibling || (return 0)
$elem->sprevious_sibling
Truth table50%1/2
A
A= $elem->sprevious_sibling
Aresult
11
00
206  
207 43 ▶ ($elem->isa('PPI::Token::Operator') && $elem eq '=')
Branch: 1 branch
BranchTrueFalseTotal
$elem->isa("PPI::Token::Operator") and $elem eq "=" or return 0 39 4 43
Condition66%2/3
$elem->isa("PPI::Token::Operator") and $elem eq "="
!ll&&!rl&&r
4039
MC/DC50%1/2
$elem->isa("PPI::Token::Operator") and $elem eq "="
$elem->isa("PPI::Token::Operator") $elem eq "="
Truth table66%2/3
A and B
A= $elem->isa("PPI::Token::Operator")B= $elem eq "="
ABresult
0X0
100
111
208 or return 0;
209  
210 39 ▶ $elem = $elem->sprevious_sibling || return 0;
Condition50%1/2
$elem->sprevious_sibling || (return 0)
l!l
390
MC/DC0%0/1
$elem->sprevious_sibling || (return 0)
$elem->sprevious_sibling
Truth table50%1/2
A
A= $elem->sprevious_sibling
Aresult
11
00
211 ### assign to: "$elem"
212  
213 # %{expr} = () deref
214 39 ▶ if ($elem->isa('PPI::Structure::Block')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($elem->isa("PPI::Structure::Block")) 0 39 39
215 0 ▶ $elem = $elem->sprevious_sibling || return 0;
Condition0%0/2
$elem->sprevious_sibling || (return 0)
l!l
00
MC/DC0%0/1
$elem->sprevious_sibling || (return 0)
$elem->sprevious_sibling
Truth table0%0/2
A
A= $elem->sprevious_sibling
Aresult
11
00
216 ### cast hash ...
217 0 ▶ return ($elem->isa('PPI::Token::Cast') && $elem eq '%');
Condition0%0/3
$elem->isa('PPI::Token::Cast') && $elem eq '%'
!ll&&!rl&&r
000
MC/DC0%0/2
$elem->isa('PPI::Token::Cast') && $elem eq '%'
$elem->isa('PPI::Token::Cast') $elem eq '%'
Truth table0%0/3
A && B
A= $elem->isa('PPI::Token::Cast')B= $elem eq '%'
ABresult
0X0
100
111
218 }
219  
220 39 ▶ if ($elem->isa('PPI::Token::Symbol')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($elem->isa("PPI::Token::Symbol")) 39 0 39
221 39 ▶ if ($elem->symbol_type eq '%') {
Branch: 1 branch
BranchTrueFalseTotal
if ($elem->symbol_type eq "%") 39 0 39
222 ### yes, %foo ...
223 39 return 1;
224 }
225 0 ▶ if ($elem->symbol_type eq '$') {
Branch: 1 branch
BranchTrueFalseTotal
if ($elem->symbol_type eq "\$") 0 0 0
226 ### symbol scalar ...
227 # %$x=() or %$$$x=() deref
228 0 for (;;) {
229 0 ▶ $elem = $elem->sprevious_sibling || return 0;
Condition0%0/2
$elem->sprevious_sibling || (return 0)
l!l
00
MC/DC0%0/1
$elem->sprevious_sibling || (return 0)
$elem->sprevious_sibling
Truth table0%0/2
A
A= $elem->sprevious_sibling
Aresult
11
00
230 ### prev: (ref $elem)." $elem"
231 0 ▶ if ($elem->isa('PPI::Token::Magic')) {
Branch: 2 branches
BranchTrueFalseTotal
if ($elem->isa('PPI::Token::Magic')) { } 0 0 0
elsif ($elem->isa('PPI::Token::Cast')) { } 0 0 0
232 # PPI 1.215 mistakes %$$$r as magic variable $$
233 } elsif ($elem->isa('PPI::Token::Cast')) {
234 0 ▶ if ($elem ne '$') {
Branch: 1 branch
BranchTrueFalseTotal
if ($elem ne "\$") 0 0 0
235 ### cast hash: ($elem eq '%')
236 0 return ($elem eq '%');
237 }
238 } else {
239 0 return 0;
240 }
241 }
242 }
243 }
244  
245 ### no ...
246 0 return 0;
247 }
248  
249 {
250 package Perl::Critic::Pulp::ProhibitDuplicateHashKeys::Qword;
251 9 ▶ sub new {
Subroutine new: called CC=1 SCAR=0.0
252 9 my ($class, %self) = @_;
253 9 return bless \%self, $class;
254 }
255 }
256  
257 40 1;
258 __END__
« Policy/ValuesAndExpressions/ProhibitUnknownBackslash.pm ↑ Summary Policy/ValuesAndExpressions/NotWithCompare.pm »