Perl/Lint/Policy/Variables/ProhibitPackageVars.pm

filter: Statementstmt 13.6% 19 / 139 Branchbran 0.0% 0 / 114 Conditioncond 0.0% 0 / 65 MC/DCmcdc 0.0% 0 / 31 Subroutinesub 60.0% 6 / 10 Podpod 0.0% 0 / 1 totaltotal 6.9% 25 / 360 CC 82 SCAR 87.2
CC
82
Cov
6%
evaluate
6806.0
__ANON__
2.0
__ANON__
2.0
CRAP 6133.0
« Perl/Lint/Policy/ControlStructures/ProhibitMutatingListFunctions.pm ↑ Summary Perl/Lint/Policy/NamingConventions/Capitalization.pm »
1 package Perl::Lint::Policy::Variables::ProhibitPackageVars;
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 List::Util qw/any/;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use Perl::Lint::Constants::Type;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use parent "Perl::Lint::Policy";
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 2 ▶use constant {
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 DESC => 'Package variable declared or used',
10 EXPL => [73, 75],
11 2 };
12  
13 0 ▶sub evaluate {
Subroutine evaluate: not called CC=82 SCAR=88.3
Pod undocumented
14 0 my ($class, $file, $tokens, $src, $args) = @_;
15  
16 0 my @allowed_packages = qw/Data::Dumper File::Find FindBin Log::Log4perl/;
17 0 ▶ if (my $this_policies_arg = $args->{prohibit_package_vars}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $this_policies_arg = $args->{"prohibit_package_vars"}) 0 0 0
18 0 ▶ if (my $add_packages = $this_policies_arg->{add_packages}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $add_packages = $this_policies_arg->{"add_packages"}) 0 0 0
19 0 push @allowed_packages, split /\s+/, $add_packages;
20 }
21 }
22  
23 0 my @violations;
24 0 ▶ for (my $i = 0, my $token_type, my $token_data; my $token = $tokens->[$i]; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $token = $tokens->[$i]) 0 0 0
25 0 $token_type = $token->{type};
26 0 $token_data = $token->{data};
27  
28 0 ▶ if ($token_type == OUR_DECL) {
Branch: 5 branches
BranchTrueFalseTotal
if ($token_type == 91) { } 0 0 0
elsif ($token_type == 90) { } 0 0 0
elsif ($token_type == 194 or $token_type == 195 or $token_type == 196 or $token_type == 166 or $token_type == 168 or $token_type == 169) { } 0 0 0
elsif ($token_type == 137 and $token_data eq '$:') { } 0 0 0
elsif ($token_type == 94 and $token_data eq 'vars') { } 0 0 0
29 0 $token = $tokens->[++$i];
30 0 $token_type = $token->{type};
31 0 ▶ if ($token_type == LEFT_PAREN) {
Branch: 2 branches
BranchTrueFalseTotal
if ($token_type == 107) { } 0 0 0
elsif ($token_type == 194 or $token_type == 195 or $token_type == 196 or $token_type == 166 or $token_type == 168 or $token_type == 169) { } 0 0 0
32 0 my $violation;
33 0 my $left_paren_num = 1;
34 0 ▶ for ($i++; $token = $tokens->[$i]; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($token = $tokens->[$i]) 0 0 0
35 0 $token_type = $token->{type};
36 0 ▶ if ($token_type == LEFT_PAREN) {
Branch: 3 branches
BranchTrueFalseTotal
if ($token_type == 107) { } 0 0 0
elsif ($token_type == 108) { } 0 0 0
elsif ($token_type == 194 or $token_type == 195 or $token_type == 196 or $token_type == 166 or $token_type == 168 or $token_type == 169) { } 0 0 0
37 0 $left_paren_num++;
38 }
39 elsif ($token_type == RIGHT_PAREN) {
40 0 ▶ if (--$left_paren_num <= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (--$left_paren_num <= 0) 0 0 0
41 0 ▶ if ($violation) {
Branch: 1 branch
BranchTrueFalseTotal
if ($violation) 0 0 0
42 0 push @violations, $violation;
43 0 undef $violation;
44 }
45 0 last;
46 }
47 }
48 ▶ elsif (
Condition0%0/15
$token_type == 194 or $token_type == 195 or $token_type == 196 or $token_type == 166 or $token_type == 168 or $token_type == 169
l!l&&r!l&&!r
000
$token_type == 194 or $token_type == 195 or $token_type == 196 or $token_type == 166 or $token_type == 168
l!l&&r!l&&!r
000
$token_type == 194 or $token_type == 195 or $token_type == 196 or $token_type == 166
l!l&&r!l&&!r
000
$token_type == 194 or $token_type == 195 or $token_type == 196
l!l&&r!l&&!r
000
$token_type == 194 or $token_type == 195
l!l&&r!l&&!r
000
MC/DC0%0/6
$token_type == 194 or $token_type == 195 or $token_type == 196 or $token_type == 166 or $token_type == 168 or $token_type == 169
$token_type == 194 $token_type == 195 $token_type == 196 $token_type == 166 $token_type == 168 $token_type == 169
Truth table0%0/7
A or B or C or D or E or F
A= $token_type == 194B= $token_type == 195C= $token_type == 196D= $token_type == 166E= $token_type == 168F= $token_type == 169
ABCDEFresult
1XXXXX1
01XXXX1
001XXX1
0001XX1
00001X1
0000011
0000000
49 $token_type == GLOBAL_VAR ||
50 $token_type == GLOBAL_ARRAY_VAR ||
51 $token_type == GLOBAL_HASH_VAR ||
52 $token_type == VAR ||
53 $token_type == ARRAY_VAR ||
54 $token_type == HASH_VAR
55 ) {
56 0 ▶ if ($token->{data} !~ /\A.[A-Z0-9_]+\Z/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($token->{"data"} =~ /\A.[A-Z0-9_]+\Z/) 0 0 0
57 $violation ||= +{
58 filename => $file,
59 line => $token->{line},
60 0 ▶ description => DESC,
Condition0%0/2
$violation ||= {"filename", $file, "line", $token->{"line"}, "description", "Package variable declared or used", "explanation", [73,75], "policy", __PACKAGE__}
l!l
00
MC/DC0%0/1
$violation ||= {"filename", $file, "line", $token->{"line"}, "description", "Package variable declared or used", "explanation", [73,75], "policy", __PACKAGE__}
$violation
Truth table0%0/2
A
A= $violation
Aresult
11
00
61 explanation => EXPL,
62 policy => __PACKAGE__,
63 };
64 }
65 }
66 }
67 }
68 ▶ elsif (
Condition0%0/15
$token_type == 194 or $token_type == 195 or $token_type == 196 or $token_type == 166 or $token_type == 168 or $token_type == 169
l!l&&r!l&&!r
000
$token_type == 194 or $token_type == 195 or $token_type == 196 or $token_type == 166 or $token_type == 168
l!l&&r!l&&!r
000
$token_type == 194 or $token_type == 195 or $token_type == 196 or $token_type == 166
l!l&&r!l&&!r
000
$token_type == 194 or $token_type == 195 or $token_type == 196
l!l&&r!l&&!r
000
$token_type == 194 or $token_type == 195
l!l&&r!l&&!r
000
MC/DC0%0/6
$token_type == 194 or $token_type == 195 or $token_type == 196 or $token_type == 166 or $token_type == 168 or $token_type == 169
$token_type == 194 $token_type == 195 $token_type == 196 $token_type == 166 $token_type == 168 $token_type == 169
Truth table0%0/7
A or B or C or D or E or F
A= $token_type == 194B= $token_type == 195C= $token_type == 196D= $token_type == 166E= $token_type == 168F= $token_type == 169
ABCDEFresult
1XXXXX1
01XXXX1
001XXX1
0001XX1
00001X1
0000011
0000000
69 $token_type == GLOBAL_VAR ||
70 $token_type == GLOBAL_ARRAY_VAR ||
71 $token_type == GLOBAL_HASH_VAR ||
72 $token_type == VAR ||
73 $token_type == ARRAY_VAR ||
74 $token_type == HASH_VAR
75 ) {
76 0 ▶ if ($token->{data} !~ /\A.[A-Z0-9_]+\Z/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($token->{"data"} =~ /\A.[A-Z0-9_]+\Z/) 0 0 0
77 push @violations, {
78 filename => $file,
79 line => $token->{line},
80 0 description => DESC,
81 explanation => EXPL,
82 policy => __PACKAGE__,
83 };
84 }
85 }
86 }
87 elsif ($token_type == LOCAL_DECL) {
88 0 $token = $tokens->[++$i];
89 0 $token_type = $token->{type};
90 0 ▶ if ($token_type == LEFT_PAREN) {
Branch: 1 branch
BranchTrueFalseTotal
if ($token_type == 107) { } 0 0 0
91 0 my $violation;
92 0 my $left_paren_num = 1;
93 0 my $does_exist_namespace_resolver = 0;
94  
95 0 my @namespaces;
96  
97 my @packages;
98 0 my @var_names;
99 0 ▶ for ($i++; $token = $tokens->[$i]; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($token = $tokens->[$i]) 0 0 0
100 0 $token_type = $token->{type};
101 0 ▶ if ($token_type == LEFT_PAREN) {
Branch: 4 branches
BranchTrueFalseTotal
if ($token_type == 107) { } 0 0 0
elsif ($token_type == 108) { } 0 0 0
elsif ($token_type == 104) { } 0 0 0
elsif ($token_type == 126) { } 0 0 0
102 0 $left_paren_num++;
103 }
104 elsif ($token_type == RIGHT_PAREN) {
105 0 push @var_names, pop @namespaces;
106 0 push @packages, join '::', @namespaces;
107 0 ▶ if (--$left_paren_num <= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (--$left_paren_num <= 0) 0 0 0
108 0 last;
109 }
110 0 @namespaces = ();
111 }
112 elsif ($token_type == COMMA) {
113 0 push @var_names, pop @namespaces;
114 0 push @packages, join '::', @namespaces;
115 0 @namespaces = ();
116 }
117 elsif ($token_type == NAMESPACE_RESOLVER) {
118 0 $does_exist_namespace_resolver = 1;
119 }
120 else {
121 0 push @namespaces, $token->{data};
122 }
123 }
124  
125 0 ▶ if ($does_exist_namespace_resolver) {
Branch: 1 branch
BranchTrueFalseTotal
if ($does_exist_namespace_resolver) 0 0 0
126 0 $token = $tokens->[++$i];
127 0 ▶ if ($token->{type} == ASSIGN) {
Branch: 1 branch
BranchTrueFalseTotal
if ($token->{"type"} == 65) 0 0 0
128 0 my $is_violated = 0;
129 0 for my $package (@packages) {
130 0 ▶ if (!any {$package =~ /\A[\$\@\%]$_/} @allowed_packages) {
Subroutine __ANON__: not called CC=1 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
unless (&any(sub { $package =~ /\A[\$\@\%]$_/; } , @allowed_packages)) 0 0 0
131 0 $is_violated = 1;
132 }
133 }
134  
135 # TODO check @var_names ?
136  
137 0 ▶ if ($is_violated) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_violated) 0 0 0
138 push @violations, {
139 filename => $file,
140 line => $token->{line},
141 0 description => DESC,
142 explanation => EXPL,
143 policy => __PACKAGE__,
144 };
145 }
146 }
147 }
148 }
149 else {
150 0 my $does_exist_namespace_resolver = 0;
151 0 my $is_assigned = 0;
152 0 my @namespaces = ($token->{data});
153 0 ▶ for ($i++; $token = $tokens->[$i]; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($token = $tokens->[$i]) 0 0 0
154 0 $token_type = $token->{type};
155 0 $token_data = $token->{data};
156 0 ▶ if ($token_type == NAMESPACE) {
Branch: 4 branches
BranchTrueFalseTotal
if ($token_type == 127) { } 0 0 0
elsif ($token_type == 126) { } 0 0 0
elsif ($token_type == 65) { } 0 0 0
elsif ($token_type == 106) { } 0 0 0
157 0 push @namespaces, $token_data;
158 }
159 elsif ($token_type == NAMESPACE_RESOLVER) {
160 0 $does_exist_namespace_resolver = 1;
161 }
162 elsif ($token_type == ASSIGN) {
163 0 $is_assigned = 1;
164 0 last;
165 }
166 elsif ($token_type == SEMI_COLON) {
167 0 last;
168 }
169 }
170  
171 0 ▶ if ($does_exist_namespace_resolver && $is_assigned) {
Branch: 1 branch
BranchTrueFalseTotal
if ($does_exist_namespace_resolver and $is_assigned) 0 0 0
Condition0%0/3
$does_exist_namespace_resolver and $is_assigned
!ll&&!rl&&r
000
MC/DC0%0/2
$does_exist_namespace_resolver and $is_assigned
$does_exist_namespace_resolver $is_assigned
Truth table0%0/3
A and B
A= $does_exist_namespace_resolverB= $is_assigned
ABresult
0X0
100
111
172 0 pop @namespaces; # throw variable name away
173 0 my $package_name = join '::', @namespaces;
174 0 ▶ if (any {$package_name =~ /\A[\$\@\%]$_/} @allowed_packages) {
Subroutine __ANON__: not called CC=1 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
if (&any(sub { $package_name =~ /\A[\$\@\%]$_/; } , @allowed_packages)) 0 0 0
175 0 next;
176 }
177  
178 # TODO check the var name
179 push @violations, {
180 filename => $file,
181 line => $token->{line},
182 0 description => DESC,
183 explanation => EXPL,
184 policy => __PACKAGE__,
185 };
186 }
187 }
188 }
189 ▶ elsif (
Condition0%0/15
$token_type == 194 or $token_type == 195 or $token_type == 196 or $token_type == 166 or $token_type == 168 or $token_type == 169
l!l&&r!l&&!r
000
$token_type == 194 or $token_type == 195 or $token_type == 196 or $token_type == 166 or $token_type == 168
l!l&&r!l&&!r
000
$token_type == 194 or $token_type == 195 or $token_type == 196 or $token_type == 166
l!l&&r!l&&!r
000
$token_type == 194 or $token_type == 195 or $token_type == 196
l!l&&r!l&&!r
000
$token_type == 194 or $token_type == 195
l!l&&r!l&&!r
000
MC/DC0%0/6
$token_type == 194 or $token_type == 195 or $token_type == 196 or $token_type == 166 or $token_type == 168 or $token_type == 169
$token_type == 194 $token_type == 195 $token_type == 196 $token_type == 166 $token_type == 168 $token_type == 169
Truth table0%0/7
A or B or C or D or E or F
A= $token_type == 194B= $token_type == 195C= $token_type == 196D= $token_type == 166E= $token_type == 168F= $token_type == 169
ABCDEFresult
1XXXXX1
01XXXX1
001XXX1
0001XX1
00001X1
0000011
0000000
190 $token_type == GLOBAL_VAR ||
191 $token_type == GLOBAL_ARRAY_VAR ||
192 $token_type == GLOBAL_HASH_VAR ||
193 $token_type == VAR ||
194 $token_type == ARRAY_VAR ||
195 $token_type == HASH_VAR
196 ) {
197 0 my @namespaces = ($token->{data});
198  
199 0 ▶ my $does_exist_namespace_resolver = $tokens->[$i+1]->{type} == NAMESPACE_RESOLVER ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$$tokens[$i + 1]{'type'} == 126 ? : 0 0 0
200  
201 0 ▶ for ($i++; $token = $tokens->[$i]; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($token = $tokens->[$i]) 0 0 0
202 0 $token_type = $token->{type};
203 0 ▶ if ($token_type == ASSIGN || $token_type == SEMI_COLON) {
Branch: 2 branches
BranchTrueFalseTotal
if ($token_type == 65 or $token_type == 106) { } 0 0 0
elsif ($token_type == 127) { } 0 0 0
Condition0%0/3
$token_type == 65 or $token_type == 106
l!l&&r!l&&!r
000
MC/DC0%0/2
$token_type == 65 or $token_type == 106
$token_type == 65 $token_type == 106
Truth table0%0/3
A or B
A= $token_type == 65B= $token_type == 106
ABresult
1X1
011
000
204 0 last;
205 }
206 elsif ($token_type == NAMESPACE) {
207 0 push @namespaces, $token->{data};
208 }
209 }
210  
211 0 ▶ if ($does_exist_namespace_resolver) {
Branch: 1 branch
BranchTrueFalseTotal
if ($does_exist_namespace_resolver) 0 0 0
212 0 my $var_name = pop @namespaces;
213  
214 0 my $package_name = join '::', @namespaces;
215 0 ▶ if (any {$package_name =~ /\A[\$\@\%]$_/} @allowed_packages) {
Subroutine __ANON__: not called CC=1 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
if (&any(sub { $package_name =~ /\A[\$\@\%]$_/; } , @allowed_packages)) 0 0 0
216 0 next;
217 }
218  
219 0 ▶ if ($var_name !~ /\A.[A-Z0-9_]+\Z/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($var_name =~ /\A.[A-Z0-9_]+\Z/) 0 0 0
220 push @violations, {
221 filename => $file,
222 line => $token->{line},
223 0 description => DESC,
224 explanation => EXPL,
225 policy => __PACKAGE__,
226 };
227 }
228 }
229 }
230 ▶ elsif ($token_type == SPECIFIC_VALUE && $token_data eq '$:') {
Condition0%0/3
$token_type == 137 and $token_data eq "\$:"
!ll&&!rl&&r
000
MC/DC0%0/2
$token_type == 137 and $token_data eq "\$:"
$token_type == 137 $token_data eq "\$:"
Truth table0%0/3
A and B
A= $token_type == 137B= $token_data eq "\$:"
ABresult
0X0
100
111
231 0 $token = $tokens->[++$i];
232 0 ▶ my $does_exist_namespace_resolver = $token->{type} == COLON ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$token->{'type'} == 105 ? : 0 0 0
233  
234 0 my $var_token;
235 0 ▶ for ($i++; $token = $tokens->[$i]; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($token = $tokens->[$i]) 0 0 0
236 0 $token_type = $token->{type};
237 0 ▶ if ($token_type == ASSIGN) {
Branch: 2 branches
BranchTrueFalseTotal
if ($token_type == 65) { } 0 0 0
elsif ($token_type == 106) { } 0 0 0
238 0 $var_token = $tokens->[$i-1];
239 }
240 elsif ($token_type == SEMI_COLON) { # XXX skip to the edge
241 0 last;
242 }
243 }
244  
245 0 ▶ if ($does_exist_namespace_resolver && $var_token->{data} !~ /\A.[A-Z0-9_]+\Z/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($does_exist_namespace_resolver and not $var_token->{"data"} =~ /\A.[A-Z0-9_]+\Z/) 0 0 0
Condition0%0/3
$does_exist_namespace_resolver and not $var_token->{"data"} =~ /\A.[A-Z0-9_]+\Z/
!ll&&!rl&&r
000
MC/DC0%0/2
$does_exist_namespace_resolver and not $var_token->{"data"} =~ /\A.[A-Z0-9_]+\Z/
$does_exist_namespace_resolver not $var_token->{"data"} =~ /\A.[A-Z0-9_]+\Z/
Truth table0%0/3
A and not(B)
A= $does_exist_namespace_resolverB= not $var_token->{"data"} =~ /\A.[A-Z0-9_]+\Z/
ABresult
0X0
100
111
246 push @violations, {
247 filename => $file,
248 line => $token->{line},
249 0 description => DESC,
250 explanation => EXPL,
251 policy => __PACKAGE__,
252 };
253 }
254 }
255 ▶ elsif ($token_type == USED_NAME && $token_data eq 'vars') {
Condition0%0/3
$token_type == 94 and $token_data eq "vars"
!ll&&!rl&&r
000
MC/DC0%0/2
$token_type == 94 and $token_data eq "vars"
$token_type == 94 $token_data eq "vars"
Truth table0%0/3
A and B
A= $token_type == 94B= $token_data eq "vars"
ABresult
0X0
100
111
256 0 my $is_used_package_var = 0;
257 0 ▶ for ($i++; $token = $tokens->[$i]; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($token = $tokens->[$i]) 0 0 0
258 0 $token_type = $token->{type};
259 0 $token_data = $token->{data};
260  
261 0 ▶ if ($token_type == REG_EXP) {
Branch: 3 branches
BranchTrueFalseTotal
if ($token_type == 187) { } 0 0 0
elsif ($token_type == 172 or $token_type == 173) { } 0 0 0
elsif ($token_type == 106) { } 0 0 0
262 0 for my $elem (split /\s+/, $token_data) {
263 0 ▶ if ($elem =~ /\A[\$\@\%](.*)\Z/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($elem =~ /\A[\$\@\%](.*)\Z/) 0 0 0
264 0 ▶ if ($1 !~ /\A[A-Z0-9_]+\Z/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($1 =~ /\A[A-Z0-9_]+\Z/) 0 0 0
265 0 $is_used_package_var = 1;
266 }
267 }
268 }
269 }
270 ▶ elsif ($token_type == STRING || $token_type == RAW_STRING) {
Condition0%0/3
$token_type == 172 or $token_type == 173
l!l&&r!l&&!r
000
MC/DC0%0/2
$token_type == 172 or $token_type == 173
$token_type == 172 $token_type == 173
Truth table0%0/3
A or B
A= $token_type == 172B= $token_type == 173
ABresult
1X1
011
000
271 0 ▶ if ($token_data =~ /\A[\$\@\%](.*)\Z/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($token_data =~ /\A[\$\@\%](.*)\Z/) 0 0 0
272 0 ▶ if ($1 !~ /\A[A-Z0-9_]+\Z/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($1 =~ /\A[A-Z0-9_]+\Z/) 0 0 0
273 0 $is_used_package_var = 1;
274 }
275 }
276 }
277 elsif ($token_type == SEMI_COLON) {
278 0 last;
279 }
280 }
281 0 ▶ if ($is_used_package_var) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_used_package_var) 0 0 0
282 push @violations, {
283 filename => $file,
284 line => $token->{line},
285 0 description => DESC,
286 explanation => EXPL,
287 policy => __PACKAGE__,
288 };
289 }
290 }
291 }
292  
293 0 return \@violations;
294 }
295  
296 2 1;
297  
« Perl/Lint/Policy/ControlStructures/ProhibitMutatingListFunctions.pm ↑ Summary Perl/Lint/Policy/NamingConventions/Capitalization.pm »