| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 21 | 120 | return; # don't descend into other structures | |||||||||||||||||||||||||||
| 22 | 15 | }); | |||||||||||||||||||||||||||
| 23 | 15 | ▶ | return if not $block; # postfix loop | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 28 | 60 | ▶ | die if $s_elem->isa('PPI::Structure::List'); # no iterator, halt search | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 29 | 15 | }); | |||||||||||||||||||||||||||
| 30 | 15 | ▶ | return if not $iterator; # checking $_ is unreliable | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | 15 | my $subscripts_ref = $block->find('PPI::Structure::Subscript'); | |||||||||||||||||||||||||||
| 33 | 15 | ▶ | return if not $subscripts_ref; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
Condition66%2/3
$source->isa("PPI::Token::Operator") and $source eq "->"
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 "->"
| |||||||||||||||||||||||||||||
| 39 | 48 | $source = $source->sprevious_sibling(); # reference subscript | |||||||||||||||||||||||||||
| 40 | } | ||||||||||||||||||||||||||||
| 41 | 60 | ▶ | next if not $source->isa('PPI::Token::Symbol') # variable | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$source->isa("PPI::Token::Symbol") or $source->isa("PPI::Token::Word")
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")
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | # skip delete statements since they require keys | ||||||||||||||||||||||||||||
| 48 | 59 | ▶ | next if _is_delete_arg($source); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 54 | next # only check subscripts utilising the current iterator | ||||||||||||||||||||||||||||
| 55 | 59 | ▶ | if not $source_is_iterator | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$source_is_iterator or $sub_value eq $iterator
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
| |||||||||||||||||||||||||||||
| 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
Condition100%3/3
$used_cnt and $used_cnt > 2
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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | 57 | ▶ | if (@children == 1) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 84 | 53 | my $child = $children[0]; | |||||||||||||||||||||||||||
| 85 | 53 | ▶ | return $child->literal if $child->isa('PPI::Token::QuoteLike::Words'); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 92 | 12 | ▶ | if ($child->isa('PPI::Token::Operator') and $child eq ',') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$child->isa("PPI::Token::Operator") and $child eq ","
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 ","
| |||||||||||||||||||||||||||||
| 93 | 3 | push @values, []; | |||||||||||||||||||||||||||
| 94 | 3 | next; | |||||||||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||||||||
| 96 | 9 | ▶ | push @{ $values[-1] }, | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 109 | 40 | my $expr = $elem->parent(); | |||||||||||||||||||||||||||
| 110 | 40 | ▶ | return if not $expr or not $expr->isa('PPI::Statement'); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$expr and $expr->isa("PPI::Statement")
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")
| |||||||||||||||||||||||||||||
| 111 | 40 | my $parens = $expr->parent(); | |||||||||||||||||||||||||||
| 112 | 40 | ▶ | return if not $parens or not $parens->isa('PPI::Structure::List'); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$parens and $parens->isa("PPI::Structure::List")
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")
| |||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | 14 | $maybe_del = $parens->sprevious_sibling(); | |||||||||||||||||||||||||||
| 115 | } | ||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | 33 | ▶ | return if not $maybe_del; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 118 | 33 | ▶ | return if not $maybe_del->isa('PPI::Token::Word'); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 119 | 20 | return $maybe_del eq 'delete'; | |||||||||||||||||||||||||||
| 120 | } | ||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | 2 | 1; | |||||||||||||||||||||||||||
| 123 | __END__ | ||||||||||||||||||||||||||||