| 1 | package Perl::Critic::Policy::ValuesAndExpressions::RequireSlices; | ||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||
| 3 | 11 | ▶ | use 5.010001; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 4 | 11 | ▶ | use strict; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 5 | 11 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 6 | 11 | ▶ | use Readonly; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 7 | 11 | ▶ | use Scalar::Util qw/refaddr/; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||||||||
| 9 | 11 | ▶ | use Perl::Critic::Utils qw/:severities/; # :classification | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 10 | 11 | ▶ | use base 'Perl::Critic::Policy'; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||||||||
| 12 | 11 | our $VERSION = '0.1.4'; | |||||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||||||
| 14 | 11 | Readonly::Scalar my $DESC => q{Write chains of array/hash access as slices}; | |||||||||||||||||||||||||||||||||
| 15 | 11 | Readonly::Scalar my $EXPL => undef; | |||||||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||||||||
| 17 | #----------------------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||||||||
| 19 | 8 | ▶ | sub supported_parameters { | ||||||||||||||||||||||||||||||||
|
Subroutine
supported_parameters: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||
| 20 | return ( | ||||||||||||||||||||||||||||||||||
| 21 | { | ||||||||||||||||||||||||||||||||||
| 22 | 8 | name => 'minimum', | |||||||||||||||||||||||||||||||||
| 23 | description => 'Subscript repetition needed to raise a violation (only 2 is supported).', | ||||||||||||||||||||||||||||||||||
| 24 | default_string => '2', | ||||||||||||||||||||||||||||||||||
| 25 | behavior => 'integer', | ||||||||||||||||||||||||||||||||||
| 26 | integer_minimum => 1, | ||||||||||||||||||||||||||||||||||
| 27 | }, | ||||||||||||||||||||||||||||||||||
| 28 | ); | ||||||||||||||||||||||||||||||||||
| 29 | } | ||||||||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||||||||
| 31 | 167 | ▶ | sub applies_to { return qw/PPI::Structure::Subscript/ } | ||||||||||||||||||||||||||||||||
|
Subroutine
applies_to: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||
| 32 | 88 | ▶ | sub default_severity { return $SEVERITY_LOW } | ||||||||||||||||||||||||||||||||
|
Subroutine
default_severity: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||
| 33 | 0 | ▶ | sub default_themes { return qw/cosmetic/ } | ||||||||||||||||||||||||||||||||
|
Subroutine
default_themes: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||||||||
| 35 | #----------------------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||||||||
| 37 | 88 | ▶ | sub invalid { | ||||||||||||||||||||||||||||||||
|
Subroutine
invalid: called CC=3 SCAR=12.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||
| 38 | 88 | my ($self,$elem,$note)=@_; | |||||||||||||||||||||||||||||||||
| 39 | 88 | ▶ | $note//=''; | ||||||||||||||||||||||||||||||||
|
Condition50%1/2
$note //= ""
MC/DC0%0/1
$note //= ""
$note
Truth table50%1/2
A
A= $note
| |||||||||||||||||||||||||||||||||||
| 40 | 0 | ▶ | if($note) { $note=" ($note)" } | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 41 | 88 | return $self->violation(sprintf("%s%s",$DESC,$note),$EXPL,$elem); | |||||||||||||||||||||||||||||||||
| 42 | } | ||||||||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||||||||
| 44 | 206 | ▶ | sub parseStatement { | ||||||||||||||||||||||||||||||||
|
Subroutine
parseStatement: called CC=17 SCAR=28.3
Pod
undocumented
| |||||||||||||||||||||||||||||||||||
| 45 | 206 | my ($statement)=@_; | |||||||||||||||||||||||||||||||||
| 46 | 206 | my $skiptocomma=0; | |||||||||||||||||||||||||||||||||
| 47 | 206 | my %boot=(violates=>0,path=>'',node=>undef); | |||||||||||||||||||||||||||||||||
| 48 | 206 | my @res=({%boot}); | |||||||||||||||||||||||||||||||||
| 49 | 206 | foreach my $node ($statement->children()) { | |||||||||||||||||||||||||||||||||
| 50 | 1247 | ▶ | if($skiptocomma) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 51 | 60 | ▶ | if($node->isa('PPI::Token::Operator') && ($node->content()=~/\s*,\s*/)) { push @res,{%boot}; $skiptocomma=0 } | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$node->isa("PPI::Token::Operator") and $node->content =~ /\s*,\s*/
MC/DC100%2/2
$node->isa("PPI::Token::Operator") and $node->content =~ /\s*,\s*/
$node->isa("PPI::Token::Operator")
$node->content =~ /\s*,\s*/
Truth table100%3/3
A and B
A= $node->isa("PPI::Token::Operator")B= $node->content =~ /\s*,\s*/
| |||||||||||||||||||||||||||||||||||
| 52 | 60 | next; | |||||||||||||||||||||||||||||||||
| 53 | } | ||||||||||||||||||||||||||||||||||
| 54 | 1187 | ▶ | if($node->isa('PPI::Token::Symbol')) { $res[-1]{path}=$node->content() } | ||||||||||||||||||||||||||||||||
|
Branch: 7 branches
| |||||||||||||||||||||||||||||||||||
| 55 | elsif($node->isa('PPI::Structure::Subscript')) { | ||||||||||||||||||||||||||||||||||
| 56 | 442 | ▶ | if($res[-1]{node}) { $res[-1]{path}.=','.$res[-1]{node}->content() } | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 57 | else { | ||||||||||||||||||||||||||||||||||
| 58 | 362 | my $type=$node->start()->content(); | |||||||||||||||||||||||||||||||||
| 59 | 362 | $res[-1]{path}.=$type; | |||||||||||||||||||||||||||||||||
| 60 | 362 | ▶ | if ($type eq '[') { $res[-1]{path}=~s/^[\@\$]+/@/ } # ] | ||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||
| 61 | 196 | elsif($type eq '{') { $res[-1]{path}=~s/^[\@\$]+/%/ } # } | |||||||||||||||||||||||||||||||||
| 62 | } | ||||||||||||||||||||||||||||||||||
| 63 | 442 | $res[-1]{node}=$node; | |||||||||||||||||||||||||||||||||
| 64 | } | ||||||||||||||||||||||||||||||||||
| 65 | 182 | ▶ | elsif($node->isa('PPI::Token::Operator') && ($node->content()=~/\s*,\s*/)) { push @res,{%boot} } | ||||||||||||||||||||||||||||||||
|
Condition100%3/3
$node->isa("PPI::Token::Operator") and $node->content =~ /\s*,\s*/
MC/DC100%2/2
$node->isa("PPI::Token::Operator") and $node->content =~ /\s*,\s*/
$node->isa("PPI::Token::Operator")
$node->content =~ /\s*,\s*/
Truth table100%3/3
A and B
A= $node->isa("PPI::Token::Operator")B= $node->content =~ /\s*,\s*/
| |||||||||||||||||||||||||||||||||||
| 66 | 108 | ▶ | elsif($node->isa('PPI::Token::Operator') && ($node->content()=~/\s*->\s*/)) { 1 } | ||||||||||||||||||||||||||||||||
|
Condition100%3/3
$node->isa("PPI::Token::Operator") and $node->content =~ /\s*->\s*/
MC/DC100%2/2
$node->isa("PPI::Token::Operator") and $node->content =~ /\s*->\s*/
$node->isa("PPI::Token::Operator")
$node->content =~ /\s*->\s*/
Truth table100%3/3
A and B
A= $node->isa("PPI::Token::Operator")B= $node->content =~ /\s*->\s*/
| |||||||||||||||||||||||||||||||||||
| 67 | 2 | elsif($node->isa('PPI::Token::Operator')) { %{$res[-1]}=%boot; $skiptocomma=1 } | |||||||||||||||||||||||||||||||||
| 68 | 0 | elsif($node->isa('PPI::Structure::List')) { 1 } | |||||||||||||||||||||||||||||||||
| 69 | 25 | elsif($node->isa('PPI::Token::Word')) { %{$res[-1]}=%boot; $skiptocomma=1 } | |||||||||||||||||||||||||||||||||
| 70 | 60 | else { $res[-1]={%boot} } | |||||||||||||||||||||||||||||||||
| 71 | } | ||||||||||||||||||||||||||||||||||
| 72 | 206 | return @res; | |||||||||||||||||||||||||||||||||
| 73 | } | ||||||||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||||||||
| 75 | 206 | ▶ | sub countViolations { | ||||||||||||||||||||||||||||||||
|
Subroutine
countViolations: called CC=8 SCAR=20.8
Pod
undocumented
| |||||||||||||||||||||||||||||||||||
| 76 | 206 | my ($M,$P)=@_; | |||||||||||||||||||||||||||||||||
| 77 | 206 | my $idx=$#$P-$M+1; | |||||||||||||||||||||||||||||||||
| 78 | 206 | ▶ | while($idx>=0) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 79 | 190 | my $violates=0; | |||||||||||||||||||||||||||||||||
| 80 | 190 | ▶ | if($$P[$idx]{node}) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 81 | 156 | $violates=1; | |||||||||||||||||||||||||||||||||
| 82 | 156 | foreach my $off (1..($M-1)) { | |||||||||||||||||||||||||||||||||
| 83 | 159 | ▶ | if(!$$P[$off+$idx]{node} || ($$P[$idx]{path} ne $$P[$off+$idx]{path})) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
not $$P[$off + $idx]{"node"} or $P->[$idx]{"path"} ne $$P[$off + $idx]{"path"}
MC/DC100%2/2
not $$P[$off + $idx]{"node"} or $P->[$idx]{"path"} ne $$P[$off + $idx]{"path"}
not $$P[$off + $idx]{"node"}
$P->[$idx]{"path"} ne $$P[$off + $idx]{"path"}
Truth table100%3/3
not(A) or B
A= not $$P[$off + $idx]{"node"}B= $P->[$idx]{"path"} ne $$P[$off + $idx]{"path"}
| |||||||||||||||||||||||||||||||||||
| 84 | 50 | $violates=0 } } } | |||||||||||||||||||||||||||||||||
| 85 | 190 | ▶ | if($violates) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 86 | 106 | $$P[$idx]{violates}=1; | |||||||||||||||||||||||||||||||||
| 87 | 106 | foreach my $off (1..($M-1)) { $$P[$off+$idx]{violates}=0 } | |||||||||||||||||||||||||||||||||
| 88 | } | ||||||||||||||||||||||||||||||||||
| 89 | 190 | $idx--; | |||||||||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||||||||
| 91 | 206 | return; | |||||||||||||||||||||||||||||||||
| 92 | } | ||||||||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||||||||
| 94 | 11 | my $cachekey; | |||||||||||||||||||||||||||||||||
| 95 | 450 | ▶ | sub stmtViolates { | ||||||||||||||||||||||||||||||||
|
Subroutine
stmtViolates: called CC=10 SCAR=23.4
Pod
undocumented
| |||||||||||||||||||||||||||||||||||
| 96 | 450 | my ($self,$elem,$statement)=@_; | |||||||||||||||||||||||||||||||||
| 97 | 450 | my $nodes; | |||||||||||||||||||||||||||||||||
| 98 | 450 | ▶ | if($cachekey) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 99 | 450 | my ($cache,$recent)=@{$$self{$cachekey}}{qw/cache recent/}; | |||||||||||||||||||||||||||||||||
| 100 | 450 | ▶ | my $skey=join(',',map {$_//'U'} @{$statement->location()}); | ||||||||||||||||||||||||||||||||
|
Condition100%2/2
$_ // "U"
MC/DC100%1/1
$_ // "U"
$_
Truth table100%2/2
A
A= $_
| |||||||||||||||||||||||||||||||||||
| 101 | 450 | ▶ | if($$cache{$skey}&&($$cache{$skey}[0]<$#$recent)) { | ||||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition100%3/3
$cache->{$skey} and $cache->{$skey}[0] < $recent->$#*
MC/DC100%2/2
$cache->{$skey} and $cache->{$skey}[0] < $recent->$#*
$cache->{$skey}
$cache->{$skey}[0] < $recent->$#*
Truth table100%3/3
A and B
A= $cache->{$skey}B= $cache->{$skey}[0] < $recent->$#*
| |||||||||||||||||||||||||||||||||||
| 102 | 2 | push @$recent,$skey; | |||||||||||||||||||||||||||||||||
| 103 | 2 | splice(@$recent,$$cache{$skey}[0],1); | |||||||||||||||||||||||||||||||||
| 104 | 2 | $$cache{$skey}[0]=$#$recent; | |||||||||||||||||||||||||||||||||
| 105 | } | ||||||||||||||||||||||||||||||||||
| 106 | elsif(!$$cache{$skey}) { | ||||||||||||||||||||||||||||||||||
| 107 | 206 | push @$recent,$skey; | |||||||||||||||||||||||||||||||||
| 108 | 206 | my @parsed=parseStatement($statement); | |||||||||||||||||||||||||||||||||
| 109 | 206 | countViolations($$self{_minimum},\@parsed); | |||||||||||||||||||||||||||||||||
| 110 | 206 | $$cache{$skey}=[$#$recent,\@parsed]; | |||||||||||||||||||||||||||||||||
| 111 | 206 | ▶ | while($#$recent>63) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 112 | 0 | my $rkey=shift(@$recent); | |||||||||||||||||||||||||||||||||
| 113 | 0 | delete($$cache{$rkey}); | |||||||||||||||||||||||||||||||||
| 114 | } | ||||||||||||||||||||||||||||||||||
| 115 | } | ||||||||||||||||||||||||||||||||||
| 116 | # else the cache key is defined and the most recent, do nothing | ||||||||||||||||||||||||||||||||||
| 117 | 450 | $nodes=$$cache{$skey}[1]; | |||||||||||||||||||||||||||||||||
| 118 | } | ||||||||||||||||||||||||||||||||||
| 119 | else { # no caching support | ||||||||||||||||||||||||||||||||||
| 120 | 0 | my @parsed=parseStatement($statement); | |||||||||||||||||||||||||||||||||
| 121 | 0 | countViolations($$self{_minimum},\@parsed); | |||||||||||||||||||||||||||||||||
| 122 | 0 | $nodes=\@parsed; | |||||||||||||||||||||||||||||||||
| 123 | } | ||||||||||||||||||||||||||||||||||
| 124 | 450 | foreach my $node (@$nodes) { | |||||||||||||||||||||||||||||||||
| 125 | 963 | ▶ | if($$node{violates} && (refaddr($$node{node})==refaddr($elem))) { return $self->invalid($elem) } | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$node->{"violates"} and builtin::refaddr($node->{"node"}) == builtin::refaddr($elem)
MC/DC100%2/2
$node->{"violates"} and builtin::refaddr($node->{"node"}) == builtin::refaddr($elem)
$node->{"violates"}
builtin::refaddr($node->{"node"}) == builtin::refaddr($elem)
Truth table100%3/3
A and B
A= $node->{"violates"}B= builtin::refaddr($node->{"node"}) == builtin::refaddr($elem)
| |||||||||||||||||||||||||||||||||||
| 126 | } | ||||||||||||||||||||||||||||||||||
| 127 | 362 | return; | |||||||||||||||||||||||||||||||||
| 128 | } | ||||||||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||||||||
| 130 | # Create a statement cache inside the policy instance. While unlikely, this permits | ||||||||||||||||||||||||||||||||||
| 131 | # parallel scans launched by perlcritic, and prevents bleed across scanned documents. | ||||||||||||||||||||||||||||||||||
| 132 | 8 | ▶ | sub initialize_if_enabled { | ||||||||||||||||||||||||||||||||
|
Subroutine
initialize_if_enabled: called CC=4 SCAR=15.7
Pod
documented
| |||||||||||||||||||||||||||||||||||
| 133 | 8 | my ($self,$config)=@_; | |||||||||||||||||||||||||||||||||
| 134 | 8 | my $name='__REQUIRESLICES__'; | |||||||||||||||||||||||||||||||||
| 135 | 8 | my $suffix=int(rand(1e9)); | |||||||||||||||||||||||||||||||||
| 136 | 8 | my $retry=3; | |||||||||||||||||||||||||||||||||
| 137 | 0 | ▶ | while($retry&&exists($$self{"$name$suffix"})) { $suffix=int(rand(1e9)); $retry-- } | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$retry and exists $$self{"$name$suffix"}
MC/DC0%0/2
$retry and exists $$self{"$name$suffix"}
$retry
exists $$self{"$name$suffix"}
Truth table33%1/3
A and B
A= $retryB= exists $$self{"$name$suffix"}
| |||||||||||||||||||||||||||||||||||
| 138 | 0 | ▶ | if(exists($$self{"$name$suffix"})) { warn 'RequireSlices unable to build statement cache' } | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 139 | else { | ||||||||||||||||||||||||||||||||||
| 140 | 8 | $cachekey="$name$suffix"; | |||||||||||||||||||||||||||||||||
| 141 | 8 | $$self{$cachekey}={cache=>{},recent=>[]}; | |||||||||||||||||||||||||||||||||
| 142 | } | ||||||||||||||||||||||||||||||||||
| 143 | 8 | return 1; | |||||||||||||||||||||||||||||||||
| 144 | } | ||||||||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||||||||
| 146 | # Reset the cache between documents | ||||||||||||||||||||||||||||||||||
| 147 | 167 | ▶ | sub prepare_to_scan_document { | ||||||||||||||||||||||||||||||||
|
Subroutine
prepare_to_scan_document: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||||||||||||||||||||||
| 148 | 167 | my ($self,$doc)=@_; | |||||||||||||||||||||||||||||||||
| 149 | 167 | ▶ | if($cachekey) { $$self{$cachekey}={cache=>{},recent=>[]} } | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 150 | 167 | return 1; | |||||||||||||||||||||||||||||||||
| 151 | } | ||||||||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||||||||
| 153 | 450 | ▶ | sub violates { | ||||||||||||||||||||||||||||||||
|
Subroutine
violates: called CC=4 SCAR=14.6
Pod
documented
| |||||||||||||||||||||||||||||||||||
| 154 | 450 | my ($self,$elem,undef)=@_; | |||||||||||||||||||||||||||||||||
| 155 | 0 | ▶ | if(!$elem->isa('PPI::Structure::Subscript')) { return } | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 156 | 450 | my $parent=$elem->parent(); | |||||||||||||||||||||||||||||||||
| 157 | 450 | ▶ | if(ref($parent) eq 'PPI::Statement') { return $self->stmtViolates($elem,$parent) } | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 158 | 111 | ▶ | if(ref($parent) eq 'PPI::Statement::Expression') { return $self->stmtViolates($elem,$parent) } | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 159 | 0 | return; | |||||||||||||||||||||||||||||||||
| 160 | } | ||||||||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||||||||
| 162 | #----------------------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||||||||
| 164 | 11 | 1; | |||||||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||||||||
| 166 | __END__ | ||||||||||||||||||||||||||||||||||