Policy/ControlStructures/ProhibitMultipleSubscripts.pm

filter: Statementstmt 98.8% 87 / 88 Branchbran 83.9% 47 / 56 Conditioncond 76.1% 16 / 21 MC/DCmcdc 64.2% 9 / 14 Subroutinesub 93.3% 14 / 15 Podpod 100% 4 / 4 totaltotal 89.3% 177 / 198 CC 39 SCAR 23.3
CC
39
Cov
91%
violates
18.2
_extract_values
11.0
_is_delete_arg
8.4
CRAP 10.2
« Policy/CodeLayout/RequireKRBracing.pm ↑ Summary Policy/CodeLayout/RequireKRParens.pm »
1 package Perl::Critic::Policy::ControlStructures::ProhibitMultipleSubscripts;
2 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use parent qw[ Perl::Critic::Policy ];
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use Perl::Critic::Utils qw[ :severities :booleans ];
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use Data::Alias;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 2 ▶use constant PBP_PAGE => 103;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 10 ▶sub default_severity { return $SEVERITY_MEDIUM }
Subroutine default_severity: called CC=1 SCAR=0.0
Pod documented
11 0 ▶sub default_themes { return qw[ pbp maintenance ] }
Subroutine default_themes: not called CC=1 SCAR=6.9
Pod documented
12 12 ▶sub applies_to { return 'PPI::Statement::Compound' }
Subroutine applies_to: called CC=1 SCAR=0.0
Pod documented
13  
14 21 ▶sub violates {
Subroutine violates: called CC=18 SCAR=29.0
Pod documented
15 21 my ($self, $elem, $doc) = @_;
16 21 ▶ return if $elem->type ne 'foreach';
Branch: 1 branch
BranchTrueFalseTotal
if $elem->type ne "foreach" 6 15 21
17  
18 135 ▶ my $block = $elem->find_first(sub { # do a flat search for a PPI::Structure::Block
Subroutine __ANON__: called CC=2 SCAR=6.9
19 135 my ($s_doc, $s_elem) = @_;
20 135 ▶ return $TRUE if $s_elem->isa('PPI::Structure::Block');
Branch: 1 branch
BranchTrueFalseTotal
if $s_elem->isa("PPI::Structure::Block") 15 120 135
21 120 return; # don't descend into other structures
22 15 });
23 15 ▶ return if not $block; # postfix loop
Branch: 1 branch
BranchTrueFalseTotal
unless $block 0 15 15
24  
25 75 ▶ my $iterator = $elem->find_first(sub {
Subroutine __ANON__: called CC=3 SCAR=11.1
26 75 my ($s_doc, $s_elem) = @_;
27 75 ▶ return $TRUE if $s_elem->isa('PPI::Token::Symbol');
Branch: 1 branch
BranchTrueFalseTotal
if $s_elem->isa("PPI::Token::Symbol") 15 60 75
28 60 ▶ die if $s_elem->isa('PPI::Structure::List'); # no iterator, halt search
Branch: 1 branch
BranchTrueFalseTotal
if $s_elem->isa("PPI::Structure::List") 0 60 60
29 15 });
30 15 ▶ return if not $iterator; # checking $_ is unreliable
Branch: 1 branch
BranchTrueFalseTotal
unless $iterator 0 15 15
31  
32 15 my $subscripts_ref = $block->find('PPI::Structure::Subscript');
33 15 ▶ return if not $subscripts_ref;
Branch: 1 branch
BranchTrueFalseTotal
unless $subscripts_ref 1 14 15
34  
35 14 my (%used, @violations);
36 14 foreach my $subscript (@$subscripts_ref) {
37 60 my $source = $subscript->sprevious_sibling();
38 60 ▶ if ($source->isa('PPI::Token::Operator') and $source eq '->') {
Branch: 1 branch
BranchTrueFalseTotal
if ($source->isa("PPI::Token::Operator") and $source eq "->") 48 12 60
Condition66%2/3
$source->isa("PPI::Token::Operator") and $source eq "->"
!ll&&!rl&&r
12048
MC/DC50%1/2
$source->isa("PPI::Token::Operator") and $source eq "->"
$source->isa("PPI::Token::Operator") $source eq "->"
Truth table66%2/3
A and B
A= $source->isa("PPI::Token::Operator")B= $source eq "->"
ABresult
0X0
100
111
39 48 $source = $source->sprevious_sibling(); # reference subscript
40 }
41 60 ▶ next if not $source->isa('PPI::Token::Symbol') # variable
Branch: 1 branch
BranchTrueFalseTotal
unless $source->isa("PPI::Token::Symbol") or $source->isa("PPI::Token::Word") 1 59 60
Condition66%2/3
$source->isa("PPI::Token::Symbol") or $source->isa("PPI::Token::Word")
l!l&&r!l&&!r
5901
MC/DC50%1/2
$source->isa("PPI::Token::Symbol") or $source->isa("PPI::Token::Word")
$source->isa("PPI::Token::Symbol") $source->isa("PPI::Token::Word")
Truth table66%2/3
A or B
A= $source->isa("PPI::Token::Symbol")B= $source->isa("PPI::Token::Word")
ABresult
1X1
011
000
42 and not $source->isa('PPI::Token::Word'); # constant
43  
44 # skip the topic variable since it can easily reference different things
45 59 ▶ next if _eq_symbol($source, '$_');
Branch: 1 branch
BranchTrueFalseTotal
if _eq_symbol($source, "\$_") 0 59 59
46  
47 # skip delete statements since they require keys
48 59 ▶ next if _is_delete_arg($source);
Branch: 1 branch
BranchTrueFalseTotal
if _is_delete_arg($source) 2 57 59
49  
50 57 my $source_is_iterator = _eq_symbol($source, $iterator);
51 57 my $sub_expr = $subscript->find_first('PPI::Statement::Expression');
52 57 foreach my $sub_value (_extract_values($sub_expr)) {
53 61 ▶ next if $sub_value eq '$_';
Branch: 1 branch
BranchTrueFalseTotal
if $sub_value eq "\$_" 2 59 61
54 next # only check subscripts utilising the current iterator
55 59 ▶ if not $source_is_iterator
Branch: 1 branch
BranchTrueFalseTotal
unless $source_is_iterator or $sub_value eq $iterator 3 56 59
Condition100%3/3
$source_is_iterator or $sub_value eq $iterator
l!l&&r!l&&!r
4793
MC/DC100%2/2
$source_is_iterator or $sub_value eq $iterator
$source_is_iterator $sub_value eq $iterator
Truth table100%3/3
A or B
A= $source_is_iteratorB= $sub_value eq $iterator
ABresult
1X1
011
000
56 and not $sub_value eq $iterator;
57  
58 56 alias my $used_cnt = $used{$source}{$sub_value};
59 56 ▶ if ($used_cnt and $used_cnt > 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($used_cnt and $used_cnt > 2) 10 46 56
Condition100%3/3
$used_cnt and $used_cnt > 2
!ll&&!rl&&r
242210
MC/DC100%2/2
$used_cnt and $used_cnt > 2
$used_cnt $used_cnt > 2
Truth table100%3/3
A and B
A= $used_cntB= $used_cnt > 2
ABresult
0X0
100
111
60 10 my $braced = $subscript->start . $sub_value . $subscript->finish;
61 10 my $desc = "Subscript $braced of $source used multiple times in a block";
62 10 push @violations, $self->violation($desc, PBP_PAGE, $subscript);
63 }
64 56 $used_cnt++;
65 }
66 }
67  
68 14 return @violations;
69 }
70  
71 116 ▶sub _eq_symbol {
Subroutine _eq_symbol: called CC=2 SCAR=7.1
72 116 my ($elem, $symbol) = @_;
73 116 ▶ return if not $elem->isa('PPI::Token::Symbol');
Branch: 1 branch
BranchTrueFalseTotal
unless $elem->isa("PPI::Token::Symbol") 0 116 116
74 116 return $elem eq $symbol;
75 }
76  
77 57 ▶sub _extract_values {
Subroutine _extract_values: called CC=11 SCAR=24.0
78 57 my ($expr) = @_;
79  
80 57 my @children = $expr->children;
81 57 ▶ return if not @children;
Branch: 1 branch
BranchTrueFalseTotal
unless @children 0 57 57
82  
83 57 ▶ if (@children == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@children == 1) 53 4 57
84 53 my $child = $children[0];
85 53 ▶ return $child->literal if $child->isa('PPI::Token::QuoteLike::Words');
Branch: 1 branch
BranchTrueFalseTotal
if $child->isa("PPI::Token::QuoteLike::Words") 1 52 53
86 52 return $child;
87 }
88  
89 4 my @values = ([]);
90 4 foreach my $child (@children) {
91 17 ▶ next if $child->isa('PPI::Token::Whitespace');
Branch: 1 branch
BranchTrueFalseTotal
if $child->isa("PPI::Token::Whitespace") 5 12 17
92 12 ▶ if ($child->isa('PPI::Token::Operator') and $child eq ',') {
Branch: 1 branch
BranchTrueFalseTotal
if ($child->isa("PPI::Token::Operator") and $child eq ",") 3 9 12
Condition100%3/3
$child->isa("PPI::Token::Operator") and $child eq ","
!ll&&!rl&&r
813
MC/DC100%2/2
$child->isa("PPI::Token::Operator") and $child eq ","
$child->isa("PPI::Token::Operator") $child eq ","
Truth table100%3/3
A and B
A= $child->isa("PPI::Token::Operator")B= $child eq ","
ABresult
0X0
100
111
93 3 push @values, [];
94 3 next;
95 }
96 9 ▶ push @{ $values[-1] },
Branch: 2 branches
BranchTrueFalseTotal
$child->isa('PPI::Token::QuoteLike::Words') ? : 0 9 9
$child->isa('PPI::Token::Quote') ? : 7 2 9
97 $child->isa('PPI::Token::QuoteLike::Words') ? $child->literal
98 : $child->isa('PPI::Token::Quote') ? $child->string
99 : $child;
100 }
101 4 ▶ return map { join '', map { ref() ? $_->content : $_ } @$_ } @values;
Branch: 1 branch
BranchTrueFalseTotal
ref $_ ? : 2 7 9
102 }
103  
104 59 ▶sub _is_delete_arg {
Subroutine _is_delete_arg: called CC=8 SCAR=21.3
105 59 my ($elem) = @_;
106  
107 59 my $maybe_del = $elem->sprevious_sibling();
108 59 ▶ if (not $maybe_del) { # might still be a delete() with parentheses
Branch: 1 branch
BranchTrueFalseTotal
unless ($maybe_del) 40 19 59
109 40 my $expr = $elem->parent();
110 40 ▶ return if not $expr or not $expr->isa('PPI::Statement');
Branch: 1 branch
BranchTrueFalseTotal
unless $expr and $expr->isa("PPI::Statement") 0 40 40
Condition33%1/3
$expr and $expr->isa("PPI::Statement")
!ll&&!rl&&r
0040
MC/DC0%0/2
$expr and $expr->isa("PPI::Statement")
$expr $expr->isa("PPI::Statement")
Truth table33%1/3
A and B
A= $exprB= $expr->isa("PPI::Statement")
ABresult
0X0
100
111
111 40 my $parens = $expr->parent();
112 40 ▶ return if not $parens or not $parens->isa('PPI::Structure::List');
Branch: 1 branch
BranchTrueFalseTotal
unless $parens and $parens->isa("PPI::Structure::List") 26 14 40
Condition66%2/3
$parens and $parens->isa("PPI::Structure::List")
!ll&&!rl&&r
02614
MC/DC50%1/2
$parens and $parens->isa("PPI::Structure::List")
$parens $parens->isa("PPI::Structure::List")
Truth table66%2/3
A and B
A= $parensB= $parens->isa("PPI::Structure::List")
ABresult
0X0
100
111
113  
114 14 $maybe_del = $parens->sprevious_sibling();
115 }
116  
117 33 ▶ return if not $maybe_del;
Branch: 1 branch
BranchTrueFalseTotal
unless $maybe_del 0 33 33
118 33 ▶ return if not $maybe_del->isa('PPI::Token::Word');
Branch: 1 branch
BranchTrueFalseTotal
unless $maybe_del->isa("PPI::Token::Word") 13 20 33
119 20 return $maybe_del eq 'delete';
120 }
121  
122 2 1;
123 __END__
« Policy/CodeLayout/RequireKRBracing.pm ↑ Summary Policy/CodeLayout/RequireKRParens.pm »