Policy/ValuesAndExpressions/RequireSlices.pm

filter: Statementstmt 90.1% 119 / 132 Branchbran 82.1% 46 / 56 Conditioncond 88.0% 22 / 25 MC/DCmcdc 81.2% 13 / 16 Subroutinesub 94.4% 17 / 18 Podpod 54.5% 6 / 11 totaltotal 86.4% 223 / 258 CC 42 SCAR 21.8
CC
42
Cov
88%
parseStatement
17.0
stmtViolates
10.4
countViolations
8.0
CRAP 8.8
« Policy/Subroutines/RequireConsistentReturn.pm ↑ Summary Policy/Generic/External.pm »
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 //= ""
l!l
088
MC/DC0%0/1
$note //= ""
$note
Truth table50%1/2
A
A= $note
Aresult
11
00
40 0 ▶ if($note) { $note=" ($note)" }
Branch: 1 branch
BranchTrueFalseTotal
if ($note) 0 88 88
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
BranchTrueFalseTotal
if ($skiptocomma) 60 1187 1247
51 60 ▶ if($node->isa('PPI::Token::Operator') && ($node->content()=~/\s*,\s*/)) { push @res,{%boot}; $skiptocomma=0 }
Branch: 1 branch
BranchTrueFalseTotal
if ($node->isa("PPI::Token::Operator") and $node->content =~ /\s*,\s*/) 18 42 60
Condition100%3/3
$node->isa("PPI::Token::Operator") and $node->content =~ /\s*,\s*/
!ll&&!rl&&r
37518
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*/
ABresult
0X0
100
111
52 60 next;
53 }
54 1187 ▶ if($node->isa('PPI::Token::Symbol')) { $res[-1]{path}=$node->content() }
Branch: 7 branches
BranchTrueFalseTotal
if ($node->isa('PPI::Token::Symbol')) { } 368 819 1187
elsif ($node->isa('PPI::Structure::Subscript')) { } 442 377 819
elsif ($node->isa('PPI::Token::Operator') and $node->content =~ /\s*,\s*/) { } 182 195 377
elsif ($node->isa('PPI::Token::Operator') and $node->content =~ /\s*->\s*/) { } 108 87 195
elsif ($node->isa('PPI::Token::Operator')) { } 2 85 87
elsif ($node->isa('PPI::Structure::List')) { } 0 85 85
elsif ($node->isa('PPI::Token::Word')) { } 25 60 85
55 elsif($node->isa('PPI::Structure::Subscript')) {
56 442 ▶ if($res[-1]{node}) { $res[-1]{path}.=','.$res[-1]{node}->content() }
Branch: 1 branch
BranchTrueFalseTotal
if ($res[-1]{'node'}) { } 80 362 442
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
BranchTrueFalseTotal
if ($type eq '[') { } 166 196 362
elsif ($type eq '{') { } 196 0 196
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*/
!ll&&!rl&&r
85110182
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*/
ABresult
0X0
100
111
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*/
!ll&&!rl&&r
852108
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*/
ABresult
0X0
100
111
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
BranchTrueFalseTotal
if ($idx >= 0) 190 206 396
79 190 my $violates=0;
80 190 ▶ if($$P[$idx]{node}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($P->[$idx]{"node"}) 156 34 190
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
BranchTrueFalseTotal
if (not $$P[$off + $idx]{"node"} or $P->[$idx]{"path"} ne $$P[$off + $idx]{"path"}) 50 109 159
Condition100%3/3
not $$P[$off + $idx]{"node"} or $P->[$idx]{"path"} ne $$P[$off + $idx]{"path"}
l!l&&r!l&&!r
3713109
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"}
ABresult
1X1
011
000
84 50 $violates=0 } } }
85 190 ▶ if($violates) {
Branch: 1 branch
BranchTrueFalseTotal
if ($violates) 106 84 190
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
BranchTrueFalseTotal
if ($cachekey) { } 450 0 450
99 450 my ($cache,$recent)=@{$$self{$cachekey}}{qw/cache recent/};
100 450 ▶ my $skey=join(',',map {$_//'U'} @{$statement->location()});
Condition100%2/2
$_ // "U"
l!l
1800450
MC/DC100%1/1
$_ // "U"
$_
Truth table100%2/2
A
A= $_
Aresult
11
00
101 450 ▶ if($$cache{$skey}&&($$cache{$skey}[0]<$#$recent)) {
Branch: 2 branches
BranchTrueFalseTotal
if ($cache->{$skey} and $cache->{$skey}[0] < $recent->$#*) { } 2 448 450
elsif (not $cache->{$skey}) { } 206 242 448
Condition100%3/3
$cache->{$skey} and $cache->{$skey}[0] < $recent->$#*
!ll&&!rl&&r
2062422
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->$#*
ABresult
0X0
100
111
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
BranchTrueFalseTotal
if ($recent->$#* > 63) 0 206 206
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
BranchTrueFalseTotal
if ($node->{"violates"} and builtin::refaddr($node->{"node"}) == builtin::refaddr($elem)) 88 875 963
Condition100%3/3
$node->{"violates"} and builtin::refaddr($node->{"node"}) == builtin::refaddr($elem)
!ll&&!rl&&r
70517088
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)
ABresult
0X0
100
111
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
BranchTrueFalseTotal
if ($retry and exists $$self{"$name$suffix"}) 0 8 8
Condition33%1/3
$retry and exists $$self{"$name$suffix"}
!ll&&!rl&&r
080
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"}
ABresult
0X0
100
111
138 0 ▶ if(exists($$self{"$name$suffix"})) { warn 'RequireSlices unable to build statement cache' }
Branch: 1 branch
BranchTrueFalseTotal
if (exists $$self{"$name$suffix"}) { } 0 8 8
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
BranchTrueFalseTotal
if ($cachekey) 167 0 167
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
BranchTrueFalseTotal
unless ($elem->isa("PPI::Structure::Subscript")) 0 450 450
156 450 my $parent=$elem->parent();
157 450 ▶ if(ref($parent) eq 'PPI::Statement') { return $self->stmtViolates($elem,$parent) }
Branch: 1 branch
BranchTrueFalseTotal
if (ref $parent eq "PPI::Statement") 339 111 450
158 111 ▶ if(ref($parent) eq 'PPI::Statement::Expression') { return $self->stmtViolates($elem,$parent) }
Branch: 1 branch
BranchTrueFalseTotal
if (ref $parent eq "PPI::Statement::Expression") 111 0 111
159 0 return;
160 }
161  
162 #-----------------------------------------------------------------------------
163  
164 11 1;
165  
166 __END__
« Policy/Subroutines/RequireConsistentReturn.pm ↑ Summary Policy/Generic/External.pm »