Policy/References/ProhibitUnnecessaryRefs.pm

filter: Statementstmt 93.3% 251 / 269 Branchbran 86.9% 120 / 138 Conditioncond 84.9% 79 / 93 MC/DCmcdc 71.4% 40 / 56 Subroutinesub 96.6% 29 / 30 Podpod 28.5% 6 / 21 totaltotal 86.4% 525 / 607 CC 110 SCAR 24.7
CC
110
Cov
90%
isBasicCast
22.4
analyze
20.2
step
16.0
CRAP 11.8
« Policy/References/ProhibitRefChecks.pm ↑ Summary Policy/Variables/ProhibitTopicIterator.pm »
1 package Perl::Critic::Policy::References::ProhibitUnnecessaryRefs;
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{Use Perl basic types.};
15 11 Readonly::Scalar my $EXPL=>undef;
16  
17 9 ▶sub supported_parameters {
Subroutine supported_parameters: called CC=1 SCAR=0.0
Pod undocumented
18 return (
19 {
20 9 name => 'self',
21 description => 'Report instances of $self.',
22 default_string => '0',
23 behavior => 'boolean',
24 },
25 );
26 }
27  
28 62 ▶sub applies_to { return qw/PPI::Statement::Variable PPI::Token::Operator/ }
Subroutine applies_to: called CC=1 SCAR=0.0
Pod documented
29 53 ▶sub default_severity { return $SEVERITY_LOW }
Subroutine default_severity: called CC=1 SCAR=0.0
Pod documented
30 0 ▶sub default_themes { return qw/cosmetic/ }
Subroutine default_themes: not called CC=1 SCAR=6.9
Pod documented
31  
32 # When a subscript is encountered, what is it's sigiled type?
33 11 my %subscriptToType=(
34 '[]'=>'@',
35 '{}'=>'%',
36 '()'=>'&',
37 );
38  
39 # For these sigils, any subscript must be checked.
40 # EG $$var[] versus $$var{}
41 # EG @$var[] versus @$var{}
42 11 my %subscriptTypes=map {$_=>1} ('$','@');
43  
44 # The following operators are considered when tracking variable assignments
45 11 my %assignmentOperator=map {$_=>1} qw{ = &&= ||= //= };
46  
47 # Create a randomly-named cache inside the policy instance.
48 11 my $cachekey;
49 9 ▶sub initialize_if_enabled {
Subroutine initialize_if_enabled: called CC=4 SCAR=16.0
Pod documented
50 9 my ($self,$config)=@_;
51 9 my $name='__PROHIBITUNNEEDEDREF';
52 9 my $suffix=int(rand(1e9));
53 9 my $retry=3;
54 0 ▶ while($retry&&exists($$self{"$name$suffix"})) { $suffix=int(rand(1e9)); $retry-- }
Branch: 1 branch
BranchTrueFalseTotal
if ($retry and exists $$self{"$name$suffix"}) 0 9 9
Condition33%1/3
$retry and exists $$self{"$name$suffix"}
!ll&&!rl&&r
090
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
55 0 ▶ if(exists($$self{"$name$suffix"})) { warn 'RequireConsistentReturn unable to build subroutine cache' }
Branch: 1 branch
BranchTrueFalseTotal
if (exists $$self{"$name$suffix"}) { } 0 9 9
56 9 else { $cachekey="$name$suffix" }
57 9 return 1;
58 }
59  
60 65 ▶sub initCache {
Subroutine initCache: called CC=2 SCAR=7.6
Pod undocumented
61 65 my ($self)=@_;
62 0 ▶ if(!$cachekey) { return }
Branch: 1 branch
BranchTrueFalseTotal
unless ($cachekey) 0 65 65
63 65 $$self{$cachekey}={ # Note that all stacks are push/pop based, so [-1] is the top of the stack
64 pending=>{
65 # 'lockey'=>[{type=>'stmt var', ...}, ...]
66 },
67 scope=>[
68 # node (Document, Statement::Block, ...)
69 ],
70 scopes=>{
71 # 'lockey of scope node'=>1 # present if we're still within self-or-descendant of that scope node
72 },
73 symbol=>{
74 # "$symbol"=>[
75 # {key=>"lockey $symbol", scope=>'lockey of scope node at declaration'}
76 # ]
77 },
78 var=>{
79 # 'lockey $symbol'=>{
80 # usage=>{
81 # 'x'=>1, for x=$@%&*
82 # }
83 # ...
84 # decl=>{node=>node} # maybe not needed, unsure
85 # }
86 },
87 };
88 }
89  
90 3 ▶sub fetchCache {
Subroutine fetchCache: called CC=2 SCAR=7.1
Pod undocumented
91 3 my ($self)=@_;
92 3 ▶ return %{$$self{$cachekey}//{}};
Condition50%1/2
$self->{$cachekey} // {}
l!l
30
MC/DC0%0/1
$self->{$cachekey} // {}
$self->{$cachekey}
Truth table50%1/2
A
A= $self->{$cachekey}
Aresult
11
00
93 }
94  
95 1291 ▶sub lockey { my ($node)=@_; return join(q{ },map {$_//'u'} @{$node->location()//[]}) }
Subroutine lockey: called CC=3 SCAR=11.0
Pod undocumented
Condition75%3/4
$_ // "u"
l!l
51641291
$node->location // []
l!l
12910
MC/DC100%1/1
$_ // "u"
$_
MC/DC0%0/1
$node->location // []
$node->location
Truth table100%2/2
A
A= $_
Aresult
11
00
Truth table50%1/2
A
A= $node->location
Aresult
11
00
96 337 ▶sub symkey { my ($node,$symbol)=@_; return sprintf('%s %s',lockey($node),$symbol) }
Subroutine symkey: called CC=1 SCAR=0.0
Pod undocumented
97  
98 141 ▶sub handlePending {
Subroutine handlePending: called CC=2 SCAR=7.0
Pod undocumented
99 141 my ($self,%event)=@_;
100 141 ▶ if($event{type} eq 'recordAssignment') { $self->recordAssignment(@event{qw/node symbol exempt/}) }
Branch: 1 branch
BranchTrueFalseTotal
if ($event{"type"} eq "recordAssignment") 141 0 141
101 141 return $self;
102 }
103  
104 # Consider the statement node S for "$x=f(x)".
105 # The LHS node("$x") has parent=S, which means the scoping action to be performed occurs when S is entered (not when "$x" is left).
106 # This only happens when there's no next sibling, so there's no pending processing for non-terminal nodes.
107 # When the top of document is reached, there is no parent and the step returns undefined.
108 1034 ▶sub step {
Subroutine step: called CC=16 SCAR=27.7
Pod undocumented
109 1034 my ($self,$node)=@_;
110 1034 my $res;
111 1034 ▶ if($node->isa('PPI::Node')&&($res=$node->schild(0))) { return $res }
Branch: 1 branch
BranchTrueFalseTotal
if ($node->isa("PPI::Node") and $res = $node->schild(0)) 389 645 1034
Condition100%3/3
$node->isa("PPI::Node") and $res = $node->schild(0)
!ll&&!rl&&r
59352389
MC/DC100%2/2
$node->isa("PPI::Node") and $res = $node->schild(0)
$node->isa("PPI::Node") $res = $node->schild(0)
Truth table100%3/3
A and B
A= $node->isa("PPI::Node")B= $res = $node->schild(0)
ABresult
0X0
100
111
112 645 ▶ if($node->isa('PPI::Element')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($node->isa("PPI::Element")) 645 0 645
113 645 my ($parent,$snext)=($node,$node->snext_sibling());
114 645 ▶ while($parent&&!$snext) {
Branch: 1 branch
BranchTrueFalseTotal
if ($parent and not $snext) 543 645 1188
Condition100%3/3
$parent and not $snext
!ll&&!rl&&r
65580543
MC/DC100%2/2
$parent and not $snext
$parent not $snext
Truth table100%3/3
A and not(B)
A= $parentB= not $snext
ABresult
0X0
100
111
115 543 $parent=$parent->parent();
116 543 ▶ $snext=$parent?$parent->snext_sibling():undef;
Branch: 1 branch
BranchTrueFalseTotal
$parent ? : 478 65 543
117 543 ▶ if($parent) {
Branch: 1 branch
BranchTrueFalseTotal
if ($parent) 478 65 543
118 478 my $loc=lockey($parent);
119 478 ▶ if(my $things=delete($$self{$cachekey}{pending}{$loc})) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $things = delete $self->{$cachekey}{"pending"}{$loc}) 104 374 478
120 104 foreach my $thing (@$things) {
121 141 $self->handlePending(%$thing)
122 } }
123 478 ▶ while(!$$self{$cachekey}{scope}[-1]->ancestor_of($snext?$snext:$parent)) {
Branch: 2 branches
BranchTrueFalseTotal
unless ($self->{$cachekey}{"scope"}[-1]->ancestor_of($snext ? $snext : $parent)) 42 478 520
$snext ? : 204 316 520
124 42 my $locscope=lockey($$self{$cachekey}{scope}[-1]);
125 42 foreach my $varscopes (values %{$$self{$cachekey}{symbol}}) {
126 72 ▶ while(($#$varscopes>-1)&&($locscope eq $$varscopes[-1]{scope})) {
Branch: 1 branch
BranchTrueFalseTotal
if ($varscopes->$#* > -1 and $locscope eq $varscopes->[-1]{"scope"}) 21 72 93
Condition100%3/3
$varscopes->$#* > -1 and $locscope eq $varscopes->[-1]{"scope"}
!ll&&!rl&&r
165621
MC/DC100%2/2
$varscopes->$#* > -1 and $locscope eq $varscopes->[-1]{"scope"}
$varscopes->$#* > -1 $locscope eq $varscopes->[-1]{"scope"}
Truth table100%3/3
A and B
A= $varscopes->$#* > -1B= $locscope eq $varscopes->[-1]{"scope"}
ABresult
0X0
100
111
127 21 pop(@$varscopes) } }
128 42 delete($$self{$cachekey}{scopes}{lockey($$self{$cachekey}{scope}[-1])});
129 42 pop @{$$self{$cachekey}{scope}};
130 }
131 }
132 }
133 645 ▶ if($parent) { return $snext }
Branch: 1 branch
BranchTrueFalseTotal
if ($parent) 580 65 645
134 65 return;
135 }
136 0 return;
137 }
138  
139 # If the $node is a basic casting operator, return
140 # {
141 # type =>one of $@%&*
142 # symbol=>'$var',
143 # node =>$varnode,
144 # next =>$aftervarnode, # after the var, possibly a subscript
145 # }
146 #
147 # $$var
148 # @$var
149 # %$var
150 # *$var
151 # &$var
152 # ${$var}
153 # @{$var}
154 # %{$var}
155 # *{$var}
156 # &{$var}
157 # $$var[idx]
158 # @$var[idxs]
159 # $$var{key}
160 # @$var{keys}
161 # %$var{keys}
162 # ${$var}[idx]
163 # @{$var}[idxs]
164 # ${$var}{key}
165 # @{$var}{keys}
166 # %{$var}{keys}
167 # *$globref{THING}
168 # *{$globref}{THING}
169 #
170 1064 ▶sub isBasicCast {
Subroutine isBasicCast: called CC=22 SCAR=31.1
Pod undocumented
171 1064 my ($node)=@_;
172 1064 ▶ if(!$node) { return }
Branch: 1 branch
BranchTrueFalseTotal
unless ($node) 1 1063 1064
173 1063 ▶ if(!$node->isa('PPI::Token::Cast')) { return }
Branch: 1 branch
BranchTrueFalseTotal
unless ($node->isa("PPI::Token::Cast")) 960 103 1063
174 103 my $sigil=$node->content();
175 103 ▶ if($sigil eq '\\') { return }
Branch: 1 branch
BranchTrueFalseTotal
if ($sigil eq "\\") 1 102 103
176 102 my $next=$node->snext_sibling();
177 0 ▶ if(!$next) { warn('Found casting operator applied to nothing'); return }
Branch: 1 branch
BranchTrueFalseTotal
unless ($next) 0 102 102
178 102 ▶ if($next->isa('PPI::Token::Symbol')) {
Branch: 2 branches
BranchTrueFalseTotal
if ($next->isa('PPI::Token::Symbol')) { } 82 20 102
elsif ($next->isa('PPI::Structure::Block')) { } 18 2 20
179 82 my $nnext=$next->snext_sibling();
180 82 my $type=$sigil;
181 82 my $symbol=$next->symbol();
182 82 ▶ if($subscriptTypes{$sigil}&&$nnext&&$nnext->isa('PPI::Structure::Subscript')) { $type=$subscriptToType{$nnext->braces()}//'?' }
Branch: 1 branch
BranchTrueFalseTotal
if ($subscriptTypes{$sigil} and $nnext and $nnext->isa("PPI::Structure::Subscript")) 25 57 82
Condition87%7/8
$subscriptTypes{$sigil} and $nnext and $nnext->isa("PPI::Structure::Subscript")
!ll&&!rl&&r
381925
$subscriptTypes{$sigil} and $nnext
!ll&&!rl&&r
251344
$subscriptToType{$nnext->braces} // "?"
l!l
250
MC/DC100%3/3
$subscriptTypes{$sigil} and $nnext and $nnext->isa("PPI::Structure::Subscript")
$subscriptTypes{$sigil} $nnext $nnext->isa("PPI::Structure::Subscript")
MC/DC0%0/1
$subscriptToType{$nnext->braces} // "?"
$subscriptToType{$nnext->braces}
Truth table100%4/4
A and B and C
A= $subscriptTypes{$sigil}B= $nnextC= $nnext->isa("PPI::Structure::Subscript")
ABCresult
0XX0
10X0
1100
1111
Truth table50%1/2
A
A= $subscriptToType{$nnext->braces}
Aresult
11
00
183 82 ▶ if($sigil eq '*') { $symbol=$next->content() }
Branch: 1 branch
BranchTrueFalseTotal
if ($sigil eq "*") 3 79 82
184 82 return (type=>$type,symbol=>$symbol,node=>$next,next=>$nnext);
185 }
186 elsif($next->isa('PPI::Structure::Block')) {
187 18 my @children=$next->schildren();
188 18 ▶ if($#children>0) { return } # this is a non-simple/evaluative block
Branch: 1 branch
BranchTrueFalseTotal
if ($#children > 0) 1 17 18
189 17 my $nnext=$children[0];
190 0 ▶ if(!$nnext->isa('PPI::Statement')) { return } # type guard
Branch: 1 branch
BranchTrueFalseTotal
unless ($nnext->isa("PPI::Statement")) 0 17 17
191 17 @children=$nnext->schildren();
192 17 ▶ if($#children>0) { return } # this is a compound etc.
Branch: 1 branch
BranchTrueFalseTotal
if ($#children > 0) 4 13 17
193 13 my $nnnext=$children[0];
194 13 ▶ if($nnnext->isa('PPI::Token::Symbol')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($nnnext->isa("PPI::Token::Symbol")) 12 1 13
195 12 my $subnext=$next->snext_sibling();
196 12 my $type=$sigil;
197 12 ▶ if($subscriptTypes{$sigil}&&$subnext) {
Branch: 1 branch
BranchTrueFalseTotal
if ($subscriptTypes{$sigil} and $subnext) 4 8 12
Condition100%3/3
$subscriptTypes{$sigil} and $subnext
!ll&&!rl&&r
624
MC/DC100%2/2
$subscriptTypes{$sigil} and $subnext
$subscriptTypes{$sigil} $subnext
Truth table100%3/3
A and B
A= $subscriptTypes{$sigil}B= $subnext
ABresult
0X0
100
111
198 4 ▶ if($subnext->isa('PPI::Structure::Subscript')) { $type=$subscriptToType{$subnext->braces()}//'?' }
Branch: 1 branch
BranchTrueFalseTotal
if ($subnext->isa("PPI::Structure::Subscript")) 2 2 4
Condition50%1/2
$subscriptToType{$subnext->braces} // "?"
l!l
20
MC/DC0%0/1
$subscriptToType{$subnext->braces} // "?"
$subscriptToType{$subnext->braces}
Truth table50%1/2
A
A= $subscriptToType{$subnext->braces}
Aresult
11
00
199 4 ▶ if($subnext->isa('PPI::Structure::Constructor')) { $type=$subscriptToType{$subnext->braces()}//'?' } # PPI bug?
Branch: 1 branch
BranchTrueFalseTotal
if ($subnext->isa("PPI::Structure::Constructor")) 2 2 4
Condition50%1/2
$subscriptToType{$subnext->braces} // "?"
l!l
20
MC/DC0%0/1
$subscriptToType{$subnext->braces} // "?"
$subscriptToType{$subnext->braces}
Truth table50%1/2
A
A= $subscriptToType{$subnext->braces}
Aresult
11
00
200 }
201 12 return (type=>$type,
202 symbol=>$nnnext->symbol(),
203 node=>$nnnext,
204 next=>$subnext,
205 );
206 }
207 1 return;
208 }
209 else {
210 2 return;
211 }
212 }
213  
214 # If the $node dereferences a scalar variable, return
215 # {
216 # type =>one of $@%&*
217 # symbol=>'$var',
218 # node =>$varnode,
219 # next =>$aftervarnode,
220 # }
221 #
222 974 ▶sub isSubscriptedSymbol {
Subroutine isSubscriptedSymbol: called CC=10 SCAR=23.2
Pod undocumented
223 974 my ($node)=@_;
224 0 ▶ if(!$node) { return }
Branch: 1 branch
BranchTrueFalseTotal
unless ($node) 0 974 974
225 974 ▶ if(!$node->isa('PPI::Token::Symbol')) { return }
Branch: 1 branch
BranchTrueFalseTotal
unless ($node->isa("PPI::Token::Symbol")) 892 82 974
226 82 my ($next,$prev)=($node->snext_sibling(),$node->sprevious_sibling());
227 82 ▶ if($next&&$next->isa('PPI::Token::Operator')&&($next->content() eq '->')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($next and $next->isa("PPI::Token::Operator") and $next->content eq "->") 27 55 82
Condition100%6/6
$next and $next->isa("PPI::Token::Operator") and $next->content eq "->"
!ll&&!rl&&r
292627
$next and $next->isa("PPI::Token::Operator")
!ll&&!rl&&r
161353
MC/DC100%3/3
$next and $next->isa("PPI::Token::Operator") and $next->content eq "->"
$next $next->isa("PPI::Token::Operator") $next->content eq "->"
Truth table100%4/4
A and B and C
A= $nextB= $next->isa("PPI::Token::Operator")C= $next->content eq "->"
ABCresult
0XX0
10X0
1100
1111
228 27 my $symbol=$node->symbol();
229 27 my $nnext=$next->snext_sibling();
230 # The returned 'next' node is the '->' arrow, because step() will move onward to the subscript itself, which needs to be separately checked for usages
231 27 ▶ if($nnext->isa('PPI::Structure::Subscript')) { return (type=>$subscriptToType{$nnext->braces()},symbol=>$symbol,node=>$node,next=>$next) }
Branch: 4 branches
BranchTrueFalseTotal
if ($nnext->isa('PPI::Structure::Subscript')) { } 19 8 27
elsif ($nnext->isa('PPI::Structure::List')) { } 2 6 8
elsif ($nnext->isa('PPI::Token::Word')) { } 4 2 6
elsif ($nnext->isa('PPI::Token::Cast')) { } 2 0 2
232 2 elsif($nnext->isa('PPI::Structure::List')) { return (type=>'&',symbol=>$symbol,node=>$node,next=>$next) }
233 4 elsif($nnext->isa('PPI::Token::Word')) { return (type=>'B',symbol=>$symbol,node=>$node,next=>$next) }
234 0 elsif($nnext->isa('PPI::Token::Cast')) { return; warn("Post-deref not yet supported") } # backwards=silence
235 }
236 # isBasicCast already performed a precheck for casting
237 #
238 # A PPI::Token::Symbol that is not resolvable with the above logic is not a "subscripted symbol" for the sake of this check.
239 # For example, it may be the LHS of an assignment so the type cannot be established until usage.
240 55 return;
241 }
242  
243 85 ▶sub recordUsage {
Subroutine recordUsage: called CC=4 SCAR=13.9
Pod undocumented
244 85 my ($self,%data)=@_;
245 85 my $cache=$$self{$cachekey};
246 85 ▶ $$cache{symbol}{$data{symbol}}//=[];
Condition100%2/2
$cache->{"symbol"}{$data{"symbol"}} //= []
l!l
823
MC/DC100%1/1
$cache->{"symbol"}{$data{"symbol"}} //= []
$cache->{"symbol"}{$data{"symbol"}}
Truth table100%2/2
A
A= $cache->{"symbol"}{$data{"symbol"}}
Aresult
11
00
247 85 @{$$cache{symbol}{$data{symbol}}} ||
248 3 push @{$$cache{symbol}{$data{symbol}}}, {
249 key=>symkey($data{node},$data{symbol}),
250 85 ▶ scope=>lockey($$cache{scope}[-1]) };
Branch: 1 branch
BranchTrueFalseTotal
@{$cache->{"symbol"}{$data{"symbol"}};} or push @{$cache->{"symbol"}{$data{"symbol"}};}, {"key", symkey($data{"node"}, $data{"symbol"}), "scope", lockey($cache->{"scope"}[-1])} 82 3 85
251 85 my $symkey=$$cache{symbol}{$data{symbol}}[-1]{key};
252 85 ▶ $$cache{var}{$symkey}//={};
Condition100%2/2
$cache->{"var"}{$symkey} //= {}
l!l
1174
MC/DC100%1/1
$cache->{"var"}{$symkey} //= {}
$cache->{"var"}{$symkey}
Truth table100%2/2
A
A= $cache->{"var"}{$symkey}
Aresult
11
00
253 85 $$cache{var}{$symkey}{usage}{$data{type}}=1;
254 85 return $self;
255 }
256  
257 # node is the location of the assignment operator
258 # symbol is the Token::Symbol that is assigned
259 141 ▶sub recordAssignment {
Subroutine recordAssignment: called CC=4 SCAR=13.9
Pod undocumented
260 141 my ($self,$node,$symbol,$exempt)=@_;
261 141 my $cache=$$self{$cachekey};
262 141 ▶ $$cache{symbol}{$symbol->content()}//=[];
Condition100%2/2
$cache->{"symbol"}{$symbol->content} //= []
l!l
34107
MC/DC100%1/1
$cache->{"symbol"}{$symbol->content} //= []
$cache->{"symbol"}{$symbol->content}
Truth table100%2/2
A
A= $cache->{"symbol"}{$symbol->content}
Aresult
11
00
263 141 my $C=$$cache{symbol}{$symbol->content()};
264 141 my $symkey=symkey($node,$symbol->content());
265 141 push @$C,{key=>$symkey,scope=>lockey($$cache{scope}[-1])};
266 141 my $type=$symbol->symbol_type();
267 0 ▶ if($type ne '$') { $$cache{var}{$symkey}{usage}{$type}=1 }
Branch: 1 branch
BranchTrueFalseTotal
if ($type ne "\$") 0 141 141
268 141 ▶ if($exempt) { $$cache{var}{$symkey}{exempt}=1 }
Branch: 1 branch
BranchTrueFalseTotal
if ($exempt) 5 136 141
269 141 return $self;
270 }
271  
272 # Find all bare scalar symbols on the left of a known assignment node.
273 160 ▶sub assignedVariables {
Subroutine assignedVariables: called CC=2 SCAR=7.0
Pod undocumented
274 160 my ($self,$node)=@_;
275 160 my $parent=$node->parent();
276 0 ▶ if(!$parent) { return }
Branch: 1 branch
BranchTrueFalseTotal
unless ($parent) 0 160 160
277 160 my @res;
278 1170 ▶ $parent->find(sub {
Subroutine __ANON__: called CC=11 SCAR=24.0
279 1170 my (undef,$e)=@_;
280 1170 my ($p,$n)=($e->sprevious_sibling(),$e->snext_sibling());
281 1170 ▶ if($n&&$n->isa('PPI::Structure::Subscript')) { return 0 }
Branch: 1 branch
BranchTrueFalseTotal
if ($n and $n->isa("PPI::Structure::Subscript")) 46 1124 1170
Condition100%3/3
$n and $n->isa("PPI::Structure::Subscript")
!ll&&!rl&&r
39672846
MC/DC100%2/2
$n and $n->isa("PPI::Structure::Subscript")
$n $n->isa("PPI::Structure::Subscript")
Truth table100%3/3
A and B
A= $nB= $n->isa("PPI::Structure::Subscript")
ABresult
0X0
100
111
282 0 ▶ if($p&&$p->isa('PPI::Token::Cast')) { return 0 }
Branch: 1 branch
BranchTrueFalseTotal
if ($p and $p->isa("PPI::Token::Cast")) 0 1124 1124
Condition66%2/3
$p and $p->isa("PPI::Token::Cast")
!ll&&!rl&&r
5525720
MC/DC0%0/2
$p and $p->isa("PPI::Token::Cast")
$p $p->isa("PPI::Token::Cast")
Truth table66%2/3
A and B
A= $pB= $p->isa("PPI::Token::Cast")
ABresult
0X0
100
111
283 1124 ▶ if($e->isa('PPI::Token::Symbol')&&($e->symbol_type() eq '$')) { push @res,$e; return 1 }
Branch: 3 branches
BranchTrueFalseTotal
if ($e->isa('PPI::Token::Symbol') and $e->symbol_type eq '$') { } 168 956 1124
elsif ($e->isa('PPI::Token::Operator') and builtin::refaddr($e) == builtin::refaddr($node)) { } 160 796 956
elsif ($e->isa('PPI::Token::Operator') and $assignmentOperator{$e->content}) { } 36 1464 1500
Condition100%3/3
$e->isa("PPI::Token::Symbol") and $e->symbol_type eq "\$"
!ll&&!rl&&r
9488168
MC/DC100%2/2
$e->isa("PPI::Token::Symbol") and $e->symbol_type eq "\$"
$e->isa("PPI::Token::Symbol") $e->symbol_type eq "\$"
Truth table100%3/3
A and B
A= $e->isa("PPI::Token::Symbol")B= $e->symbol_type eq "\$"
ABresult
0X0
100
111
284 160 ▶ elsif($e->isa('PPI::Token::Operator')&&(refaddr($e)==refaddr($node))) { die 'done searching' }
Condition100%3/3
$e->isa("PPI::Token::Operator") and builtin::refaddr($e) == builtin::refaddr($node)
!ll&&!rl&&r
70492160
MC/DC100%2/2
$e->isa("PPI::Token::Operator") and builtin::refaddr($e) == builtin::refaddr($node)
$e->isa("PPI::Token::Operator") builtin::refaddr($e) == builtin::refaddr($node)
Truth table100%3/3
A and B
A= $e->isa("PPI::Token::Operator")B= builtin::refaddr($e) == builtin::refaddr($node)
ABresult
0X0
100
111
285 ▶ elsif($e->isa('PPI::Token::Operator')&&$assignmentOperator{$e->content()}) {
Condition100%3/3
$e->isa("PPI::Token::Operator") and $assignmentOperator{$e->content}
!ll&&!rl&&r
7045636
MC/DC100%2/2
$e->isa("PPI::Token::Operator") and $assignmentOperator{$e->content}
$e->isa("PPI::Token::Operator") $assignmentOperator{$e->content}
Truth table100%3/3
A and B
A= $e->isa("PPI::Token::Operator")B= $assignmentOperator{$e->content}
ABresult
0X0
100
111
286 # refaddr(e)!=refaddr(node)
287 # This is an assignment to the left of our assignment, and should have been captured using assignedVariables(e).
288 36 @res=();
289 }
290 796 return 0;
291 160 });
292 160 return @res;
293 }
294  
295 65 ▶sub analyze {
Subroutine analyze: called CC=20 SCAR=30.1
Pod undocumented
296 65 my ($self,$doc)=@_;
297 65 my $node=$doc;
298 0 ▶ if(!$node->isa('PPI::Node')) { die "Unable to analyze node ($node)" }
Branch: 1 branch
BranchTrueFalseTotal
unless ($node->isa("PPI::Node")) 0 65 65
299 65 ▶ while($node) {
Branch: 1 branch
BranchTrueFalseTotal
if ($node) 1034 65 1099
300 1034 ▶ if(my %cast=isBasicCast($node)) { $self->recordUsage(%cast); $node=$cast{next}||$cast{node} }
Branch: 5 branches
BranchTrueFalseTotal
if (my(%cast) = isBasicCast($node)) { } 70 964 1034
elsif (my(%subscript) = isSubscriptedSymbol($node)) { } 15 949 964
elsif ($node->isa('PPI::Structure::Block') or $node->isa('PPI::Statement::Compound')) { } 42 907 949
elsif ($node->isa('PPI::Statement::Variable')) { } 89 818 907
elsif ($node->isa('PPI::Token::Operator') and $assignmentOperator{$node->content}) { } 51 1511 1562
Condition66%2/3
$cast{'next'} || $cast{'node'}
l!l&&r!l&&!r
52180
MC/DC0%0/2
$cast{'next'} || $cast{'node'}
$cast{'next'} $cast{'node'}
Truth table66%2/3
A || B
A= $cast{'next'}B= $cast{'node'}
ABresult
1X1
011
000
301 15 ▶ elsif(my %subscript=isSubscriptedSymbol($node)) { $self->recordUsage(%subscript); $node=$subscript{next}||$subscript{node} }
Condition33%1/3
$subscript{'next'} || $subscript{'node'}
l!l&&r!l&&!r
1500
MC/DC0%0/2
$subscript{'next'} || $subscript{'node'}
$subscript{'next'} $subscript{'node'}
Truth table33%1/3
A || B
A= $subscript{'next'}B= $subscript{'node'}
ABresult
1X1
011
000
302 ▶ elsif($node->isa('PPI::Structure::Block')||$node->isa('PPI::Statement::Compound')) {
Condition100%3/3
$node->isa("PPI::Structure::Block") or $node->isa("PPI::Statement::Compound")
l!l&&r!l&&!r
2616907
MC/DC100%2/2
$node->isa("PPI::Structure::Block") or $node->isa("PPI::Statement::Compound")
$node->isa("PPI::Structure::Block") $node->isa("PPI::Statement::Compound")
Truth table100%3/3
A or B
A= $node->isa("PPI::Structure::Block")B= $node->isa("PPI::Statement::Compound")
ABresult
1X1
011
000
303 42 push @{$$self{$cachekey}{scope}},$node;
304 42 $$self{$cachekey}{scopes}{lockey($node)}=1;
305 }
306 elsif($node->isa('PPI::Statement::Variable')) {
307 89 foreach my $symbol (grep {'$' eq $_->symbol_type()} $node->symbols()) {
308 104 ▶ my %exempt; if(($symbol eq '$self')&&!$$self{_self}) { %exempt=(exempt=>1) }
Branch: 1 branch
BranchTrueFalseTotal
if ($symbol eq "\$self" and not $self->{"_self"}) 4 100 104
Condition100%3/3
$symbol eq "\$self" and not $self->{"_self"}
!ll&&!rl&&r
9644
MC/DC100%2/2
$symbol eq "\$self" and not $self->{"_self"}
$symbol eq "\$self" not $self->{"_self"}
Truth table100%3/3
A and not(B)
A= $symbol eq "\$self"B= not $self->{"_self"}
ABresult
0X0
100
111
309 104 push @{$$self{$cachekey}{pending}{lockey($node)}},{type=>'recordAssignment',node=>$node,symbol=>$symbol,%exempt};
310 }
311 89 ▶ if(my $assign=$node->find_first(sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
Branch: 1 branch
BranchTrueFalseTotal
if (my $assign = $node->find_first(sub { my(undef, $e) = @_; unless ($e->isa('PPI::Token::Operator')) { return 0; } ; if ($e->content eq '=') { return 1; } ; return 0; } )) { } 64 25 89
312 534 my (undef,$e)=@_;
313 534 ▶ if(!$e->isa('PPI::Token::Operator')) { return 0 }
Branch: 1 branch
BranchTrueFalseTotal
unless ($e->isa("PPI::Token::Operator")) 433 101 534
314 101 ▶ if($e->content() eq '=') { return 1 }
Branch: 1 branch
BranchTrueFalseTotal
if ($e->content eq "=") 64 37 101
315 37 return 0;
316 64 })) { $node=$assign }
317 25 ▶ else { $node=$node->last_element()||$node }
Condition33%1/3
$node->last_element || $node
l!l&&r!l&&!r
2500
MC/DC0%0/2
$node->last_element || $node
$node->last_element $node
Truth table33%1/3
A || B
A= $node->last_elementB= $node
ABresult
1X1
011
000
318 }
319 ▶ elsif($node->isa('PPI::Token::Operator')&&$assignmentOperator{$node->content()}) {
Condition100%3/3
$node->isa("PPI::Token::Operator") and $assignmentOperator{$node->content}
!ll&&!rl&&r
7442351
MC/DC100%2/2
$node->isa("PPI::Token::Operator") and $assignmentOperator{$node->content}
$node->isa("PPI::Token::Operator") $assignmentOperator{$node->content}
Truth table100%3/3
A and B
A= $node->isa("PPI::Token::Operator")B= $assignmentOperator{$node->content}
ABresult
0X0
100
111
320 51 foreach my $symbol ($self->assignedVariables($node)) {
321 37 ▶ my %exempt; if(($symbol eq '$self')&&!$$self{_self}) { %exempt=(exempt=>1) }
Branch: 1 branch
BranchTrueFalseTotal
if ($symbol eq "\$self" and not $self->{"_self"}) 1 36 37
Condition100%3/3
$symbol eq "\$self" and not $self->{"_self"}
!ll&&!rl&&r
3511
MC/DC100%2/2
$symbol eq "\$self" and not $self->{"_self"}
$symbol eq "\$self" not $self->{"_self"}
Truth table100%3/3
A and not(B)
A= $symbol eq "\$self"B= not $self->{"_self"}
ABresult
0X0
100
111
322 37 push @{$$self{$cachekey}{pending}{lockey($node->parent())}},{type=>'recordAssignment',node=>$node,symbol=>$symbol,%exempt};
323 }
324 }
325 1034 $node=$self->step($node);
326 }
327 65 return $self;
328 }
329  
330 65 ▶sub prepare_to_scan_document {
Subroutine prepare_to_scan_document: called CC=2 SCAR=7.1
Pod documented
331 65 my ($self,$doc)=@_;
332 0 ▶ if(!$cachekey) { return 1 }
Branch: 1 branch
BranchTrueFalseTotal
unless ($cachekey) 0 65 65
333 65 $self->initCache();
334 65 push @{$$self{$cachekey}{scope}},$doc;
335 65 $$self{$cachekey}{scopes}{lockey($doc)}=1;
336 65 $self->analyze($doc);
337 65 return 1;
338 }
339  
340 193 ▶sub symbolUsageViolates {
Subroutine symbolUsageViolates: called CC=7 SCAR=19.5
Pod undocumented
341 193 my (%var)=@_;
342 193 ▶ if($var{exempt}) { return }
Branch: 1 branch
BranchTrueFalseTotal
if ($var{"exempt"}) 5 188 193
343 188 my ($sum,$sumah)=(0,0);
344 188 ▶ while(my ($k,$v)=each %{$var{usage}//{}}) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$var{"usage"} // {};}) 61 186 247
Condition100%2/2
$var{"usage"} // {}
l!l
117130
MC/DC100%1/1
$var{"usage"} // {}
$var{"usage"}
Truth table100%2/2
A
A= $var{"usage"}
Aresult
11
00
345 61 ▶ if($v) { $sum++ }
Branch: 1 branch
BranchTrueFalseTotal
if ($v) 61 0 61
346 61 ▶ if(($k eq '@')||($k eq '%')) { $sumah++ }
Branch: 1 branch
BranchTrueFalseTotal
if ($k eq '@' or $k eq '%') { } 59 2 61
Condition100%3/3
$k eq "\@" or $k eq "%"
l!l&&r!l&&!r
33262
MC/DC100%2/2
$k eq "\@" or $k eq "%"
$k eq "\@" $k eq "%"
Truth table100%3/3
A or B
A= $k eq "\@"B= $k eq "%"
ABresult
1X1
011
000
347 2 else { return }
348 }
349 186 return ($sumah==1);
350 }
351  
352 53 ▶sub invalid {
Subroutine invalid: called CC=3 SCAR=11.3
Pod undocumented
353 53 my ($self,$elem,$note)=@_;
354 53 ▶ $note//='';
Condition50%1/2
$note //= ""
l!l
530
MC/DC0%0/1
$note //= ""
$note
Truth table50%1/2
A
A= $note
Aresult
11
00
355 53 ▶ if($note) { $note=" ($note)" }
Branch: 1 branch
BranchTrueFalseTotal
if ($note) 53 0 53
356 53 return $self->violation(sprintf("%s%s",$DESC,$note),$EXPL,$elem);
357 }
358  
359 252 ▶sub violates {
Subroutine violates: called CC=10 SCAR=23.2
Pod documented
360 252 my ($self,$elem,undef)=@_;
361 0 ▶ if(!$cachekey) { return }
Branch: 1 branch
BranchTrueFalseTotal
unless ($cachekey) 0 252 252
362 252 ▶ if($elem->isa('PPI::Token::Operator') && !$assignmentOperator{$elem->content()}) { return }
Branch: 1 branch
BranchTrueFalseTotal
if ($elem->isa("PPI::Token::Operator") and not $assignmentOperator{$elem->content}) 60 192 252
Condition100%3/3
$elem->isa("PPI::Token::Operator") and not $assignmentOperator{$elem->content}
!ll&&!rl&&r
8310960
MC/DC100%2/2
$elem->isa("PPI::Token::Operator") and not $assignmentOperator{$elem->content}
$elem->isa("PPI::Token::Operator") not $assignmentOperator{$elem->content}
Truth table100%3/3
A and not(B)
A= $elem->isa("PPI::Token::Operator")B= not $assignmentOperator{$elem->content}
ABresult
0X0
100
111
363 192 my %violations;
364 my @vars;
365 192 ▶ if($elem->isa('PPI::Statement::Variable')) { @vars=$elem->variables() }
Branch: 1 branch
BranchTrueFalseTotal
if ($elem->isa('PPI::Statement::Variable')) { } 83 109 192
366 109 else { @vars=$self->assignedVariables($elem) }
367 192 foreach my $var (@vars) {
368 193 ▶ if(my $v=symkey($elem,$var)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $v = symkey($elem, $var)) 193 0 193
369 193 ▶ if(symbolUsageViolates(%{$$self{$cachekey}{var}{$v}//{}})) {
Branch: 1 branch
BranchTrueFalseTotal
if (symbolUsageViolates(%{$self->{$cachekey}{"var"}{$v} // {};})) 53 140 193
Condition100%2/2
$self->{$cachekey}{"var"}{$v} // {}
l!l
63130
MC/DC100%1/1
$self->{$cachekey}{"var"}{$v} // {}
$self->{$cachekey}{"var"}{$v}
Truth table100%2/2
A
A= $self->{$cachekey}{"var"}{$v}
Aresult
11
00
370 53 $violations{$var}=undef } } }
371 192 ▶ if(!%violations) { return }
Branch: 1 branch
BranchTrueFalseTotal
unless (%violations) 139 53 192
372 53 return $self->invalid($elem,join(',',sort keys(%violations)));
373 0 return;
374 }
375  
376 11 1;
377  
378 __END__
« Policy/References/ProhibitRefChecks.pm ↑ Summary Policy/Variables/ProhibitTopicIterator.pm »