Perl/Lint/Policy/ValuesAndExpressions/ProhibitMagicNumbers.pm

filter: Statementstmt 28.7% 58 / 202 Branchbran 11.1% 19 / 170 Conditioncond 17.8% 13 / 73 MC/DCmcdc 7.1% 3 / 42 Subroutinesub 70.0% 7 / 10 Podpod 0.0% 0 / 1 totaltotal 20.0% 100 / 498 CC 114 SCAR 74.0
CC
114
Cov
20%
evaluate
2531.1
_scan
506.0
_validate_int_token
182.0
CRAP 1637.2
« Perl/Lint/Policy/Modules/ProhibitConditionalUseStatements.pm ↑ Summary Perl/Lint/Policy/TestingAndDebugging/RequireTestLabels.pm »
1 package Perl::Lint::Policy::ValuesAndExpressions::ProhibitMagicNumbers;
2 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶no warnings qw/numeric/;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use Perl::Lint::Constants::Type;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use parent "Perl::Lint::Policy";
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 3 ▶use constant {
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 DESC => 'Unnamed numeric literals make code less maintainable', # TODO
10 EXPL => 'Unnamed numeric literals make code less maintainable',
11 3 };
12  
13 3 my $file;
14 my $tokens;
15  
16 3 my %allowed_values;
17 3 my %allowed_types;
18 3 my %constant_creator_subroutines;
19 3 my $allow_to_the_right_of_a_fat_comma;
20 3 my $is_readonly_array1_ctx;
21  
22 2 ▶sub evaluate {
Subroutine evaluate: called CC=74 SCAR=78.4
Pod undocumented
23 2 my $class = shift;
24 2 $file = shift;
25 2 $tokens = shift;
26 2 my ($src, $args) = @_;
27  
28 2 %allowed_values = (
29 0 => 1,
30 1 => 1,
31 2 => 1,
32 );
33  
34 2 %allowed_types = (
35 Int => 1,
36 Float => 1,
37 );
38  
39 2 %constant_creator_subroutines = (
40 plan => 1,
41 Readonly => 1,
42 const => 1,
43 );
44  
45 2 $allow_to_the_right_of_a_fat_comma = 1;
46  
47 # initializing
48 2 ▶ if (my $this_policies_arg = $args->{prohibit_magic_numbers}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $this_policies_arg = $args->{"prohibit_magic_numbers"}) 0 2 2
49 $allow_to_the_right_of_a_fat_comma =
50 0 ▶ $this_policies_arg->{allow_to_the_right_of_a_fat_comma} // 1;
Condition0%0/2
$this_policies_arg->{"allow_to_the_right_of_a_fat_comma"} // 1
l!l
00
MC/DC0%0/1
$this_policies_arg->{"allow_to_the_right_of_a_fat_comma"} // 1
$this_policies_arg->{"allow_to_the_right_of_a_fat_comma"}
Truth table0%0/2
A
A= $this_policies_arg->{"allow_to_the_right_of_a_fat_comma"}
Aresult
11
00
51  
52 0 my $allowed_values = $this_policies_arg->{allowed_values};
53 0 ▶ if (defined $allowed_values) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $allowed_values) 0 0 0
54 0 delete $allowed_values{2}; # remove `2` from allowed list when allowed_values is specified
55  
56 0 for my $allowed_value (split /\s+/, $allowed_values) {
57 0 my ($begin, $end) = split /[.][.]/, $allowed_value; # for range notation (e.g. `1..42`)
58 0 ▶ if (defined $begin && defined $end) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $begin and defined $end) { } 0 0 0
Condition0%0/3
defined $begin and defined $end
!ll&&!rl&&r
000
MC/DC0%0/2
defined $begin and defined $end
defined $begin defined $end
Truth table0%0/3
A and B
A= defined $beginB= defined $end
ABresult
0X0
100
111
59 # used range notation
60 0 my ($delta) = $end =~ /:by [(] (.+) [)] \z/x; # for range notation with by (e.g. `-2.0..2.0:by(0.5)`)
61 0 ▶ $delta //= 1; # default delta
Condition0%0/2
$delta //= 1
l!l
00
MC/DC0%0/1
$delta //= 1
$delta
Truth table0%0/2
A
A= $delta
Aresult
11
00
62  
63 0 ▶ for (my $num = $begin; $num <= $end; $num += $delta) {
Branch: 1 branch
BranchTrueFalseTotal
if ($num <= $end) 0 0 0
64 0 $allowed_values{$num} = 1;
65 }
66 }
67 else {
68 # not used range notation
69 0 $allowed_values{$allowed_value} = 1;
70 }
71 }
72 }
73  
74 0 my $allowed_types = $this_policies_arg->{allowed_types};
75 0 ▶ if (defined $allowed_types) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $allowed_types) 0 0 0
76 0 delete $allowed_types{Float}; # remove `Float` from allowed types list when allowed_types is specified
77  
78 0 for my $allowed_type (split /\s+/, $allowed_types) {
79 0 $allowed_types{$allowed_type} = 1;
80 }
81 }
82  
83 0 my $constant_creator_subroutines = $this_policies_arg->{constant_creator_subroutines};
84 0 ▶ if (defined $constant_creator_subroutines) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $constant_creator_subroutines) 0 0 0
85 0 for my $sub (split /\s+/, $constant_creator_subroutines) {
86 0 $constant_creator_subroutines{$sub} = 1;
87 }
88 }
89 }
90  
91 2 my @violations;
92 2 ▶ for (my $i = 0, my $token_type, my $token_data; my $token = $tokens->[$i]; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $token = $tokens->[$i]) 21 2 23
93 21 $is_readonly_array1_ctx = 0;
94  
95 21 $token_type = $token->{type};
96 21 $token_data = $token->{data};
97  
98 21 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if ($token_type == 93 or $token_type == 71 or $token_type == 122 and $constant_creator_subroutines{$token_data}) 8 13 21
Condition55%5/9
$token_type == 93 or $token_type == 71 or $token_type == 122 and $constant_creator_subroutines{$token_data}
l!l&&r!l&&!r
8013
$token_type == 93 or $token_type == 71
l!l&&r!l&&!r
8013
$token_type == 122 and $constant_creator_subroutines{$token_data}
!ll&&!rl&&r
1300
MC/DC25%1/4
$token_type == 93 or $token_type == 71 or $token_type == 122 and $constant_creator_subroutines{$token_data}
$token_type == 93 $token_type == 71 $token_type == 122 $constant_creator_subroutines{$token_data}
Truth table40%2/5
A or B or C and D
A= $token_type == 93B= $token_type == 71C= $token_type == 122D= $constant_creator_subroutines{$token_data}
ABCDresult
1XXX1
01XX1
00111
000X0
00100
99 $token_type == USE_DECL ||
100 $token_type == REQUIRE_DECL ||
101 ($token_type == KEY && $constant_creator_subroutines{$token_data})
102 ) {
103 8 ▶ for ($i++; $token = $tokens->[$i]; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($token = $tokens->[$i]) 24 0 24
104 24 $token_type = $token->{type};
105 24 ▶ if ($token_type == SEMI_COLON) {
Branch: 1 branch
BranchTrueFalseTotal
if ($token_type == 106) 8 16 24
106 8 last;
107 }
108 }
109 8 next;
110 }
111  
112 13 ▶ if ($token_type == NAMESPACE && $token_data eq 'Readonly') {
Branch: 1 branch
BranchTrueFalseTotal
if ($token_type == 127 and $token_data eq "Readonly") 0 13 13
Condition33%1/3
$token_type == 127 and $token_data eq "Readonly"
!ll&&!rl&&r
1300
MC/DC0%0/2
$token_type == 127 and $token_data eq "Readonly"
$token_type == 127 $token_data eq "Readonly"
Truth table33%1/3
A and B
A= $token_type == 127B= $token_data eq "Readonly"
ABresult
0X0
100
111
113 0 ▶ $token = $tokens->[++$i] or last;
Branch: 1 branch
BranchTrueFalseTotal
$token = $$tokens[++$i] or last 0 0 0
114  
115 0 ▶ if ($token->{type} == NAMESPACE_RESOLVER) {
Branch: 1 branch
BranchTrueFalseTotal
if ($token->{"type"} == 126) 0 0 0
116 0 ▶ $token = $tokens->[++$i] or last;
Branch: 1 branch
BranchTrueFalseTotal
$token = $$tokens[++$i] or last 0 0 0
117 0 $token_data = $token->{data};
118  
119 0 ▶ if ($token->{type} == NAMESPACE) {
Branch: 1 branch
BranchTrueFalseTotal
if ($token->{"type"} == 127) 0 0 0
120 0 ▶ if ($token_data eq 'Scalar' || $token_data eq 'Array') {
Branch: 2 branches
BranchTrueFalseTotal
if ($token_data eq 'Scalar' or $token_data eq 'Array') { } 0 0 0
elsif ($token_data eq 'Array1') { } 0 0 0
Condition0%0/3
$token_data eq "Scalar" or $token_data eq "Array"
l!l&&r!l&&!r
000
MC/DC0%0/2
$token_data eq "Scalar" or $token_data eq "Array"
$token_data eq "Scalar" $token_data eq "Array"
Truth table0%0/3
A or B
A= $token_data eq "Scalar"B= $token_data eq "Array"
ABresult
1X1
011
000
121 # when `Readonly::Scalar` or `Readonly::Array`,
122 # skip tokens to semi colon (means don't evaluate).
123 0 ▶ for ($i++; $token = $tokens->[$i]; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($token = $tokens->[$i]) 0 0 0
124 0 $token_type = $token->{type};
125 0 ▶ if ($token_type == SEMI_COLON) {
Branch: 1 branch
BranchTrueFalseTotal
if ($token_type == 106) 0 0 0
126 0 last;
127 }
128 }
129 0 next;
130 }
131 elsif ($token_data eq 'Array1') {
132 # when `Readonly::Array1`
133 0 $i += 2; # skip to assigning token
134  
135 0 $token = $tokens->[$i];
136 0 $token_type = $token->{type};
137 0 $token_data = $token->{data};
138  
139 0 $is_readonly_array1_ctx = 1;
140  
141 # no break!
142 }
143 }
144 }
145 }
146  
147 # for the $VERSION variable
148 13 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if ($token_data eq "\$VERSION" and $token_type == 166 || $token_type == 194) 0 13 13
Condition16%1/6
$token_data eq "\$VERSION" and $token_type == 166 || $token_type == 194
!ll&&!rl&&r
1300
$token_type == 166 or $token_type == 194
l!l&&r!l&&!r
000
MC/DC0%0/3
$token_data eq "\$VERSION" and $token_type == 166 || $token_type == 194
$token_data eq "\$VERSION" $token_type == 166 $token_type == 194
Truth table25%1/4
A and B or C
A= $token_data eq "\$VERSION"B= $token_type == 166C= $token_type == 194
ABCresult
0XX0
1000
11X1
1011
149 $token_data eq '$VERSION' &&
150 ($token_type == VAR || $token_type == GLOBAL_VAR)
151 ) {
152 # skip to end of line. Don't evaluate it.
153 0 ▶ for ($i++; $token = $tokens->[$i]; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($token = $tokens->[$i]) 0 0 0
154 0 ▶ last if $token->{type} == SEMI_COLON;
Branch: 1 branch
BranchTrueFalseTotal
if $token->{"type"} == 106 0 0 0
155 }
156 0 next;
157 }
158  
159 13 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if ($token_type == 65 or $token_type == 124 and !$allow_to_the_right_of_a_fat_comma || $is_readonly_array1_ctx) 0 13 13
Condition22%2/9
$token_type == 65 or $token_type == 124 and !$allow_to_the_right_of_a_fat_comma || $is_readonly_array1_ctx
l!l&&r!l&&!r
0013
$token_type == 124 and !$allow_to_the_right_of_a_fat_comma || $is_readonly_array1_ctx
!ll&&!rl&&r
1300
not $allow_to_the_right_of_a_fat_comma or $is_readonly_array1_ctx
l!l&&r!l&&!r
000
MC/DC0%0/4
$token_type == 65 or $token_type == 124 and !$allow_to_the_right_of_a_fat_comma || $is_readonly_array1_ctx
$token_type == 65 $token_type == 124 not $allow_to_the_right_of_a_fat_comma $is_readonly_array1_ctx
Truth table20%1/5
A or B and not(C) or D
A= $token_type == 65B= $token_type == 124C= not $allow_to_the_right_of_a_fat_commaD= $is_readonly_array1_ctx
ABCDresult
1XXX1
011X1
01011
00XX0
01000
160 $token_type == ASSIGN ||
161 ($token_type == ARROW && (!$allow_to_the_right_of_a_fat_comma || $is_readonly_array1_ctx))
162 ) {
163 0 push @violations, @{$class->_scan(\$i)};
164 0 next;
165 }
166  
167 13 ▶ if ($token_type == FUNCTION) {
Branch: 1 branch
BranchTrueFalseTotal
if ($token_type == 203) 0 13 13
168 0 $token = $tokens->[++$i];
169  
170 0 my $statement = [];
171 0 my @statements = ();
172  
173 0 my $lbnum = 1;
174 0 ▶ for ($i++; $token = $tokens->[$i]; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($token = $tokens->[$i]) 0 0 0
175 0 $token_type = $token->{type};
176  
177 0 ▶ if ($token_type == LEFT_BRACE) {
Branch: 3 branches
BranchTrueFalseTotal
if ($token_type == 109) { } 0 0 0
elsif ($token_type == 110) { } 0 0 0
elsif ($token_type == 106) { } 0 0 0
178 0 $lbnum++;
179 }
180 elsif ($token_type == RIGHT_BRACE) {
181 0 ▶ last if --$lbnum <= 0;
Branch: 1 branch
BranchTrueFalseTotal
if --$lbnum <= 0 0 0 0
182 }
183 elsif ($token_type == SEMI_COLON) {
184 0 push @statements, $statement;
185 0 $statement = [];
186 }
187 else {
188 0 push @$statement, $token;
189 }
190 }
191  
192 0 ▶ if (scalar @statements > 1) { # when exists multiple statements in function
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @statements > 1) 0 0 0
193 0 my $last_statement = pop @statements;
194  
195 0 ▶ my $return_value_token = pop @$last_statement or next;
Branch: 1 branch
BranchTrueFalseTotal
my $return_value_token = pop @$last_statement or next 0 0 0
196 0 ▶ if ($return_value_token->{type} == RETURN) {
Branch: 1 branch
BranchTrueFalseTotal
if ($return_value_token->{"type"} == 0) 0 0 0
197 0 ▶ $return_value_token = pop @$last_statement or next;
Branch: 1 branch
BranchTrueFalseTotal
$return_value_token = pop @$last_statement or next 0 0 0
198 }
199  
200 0 my $invalid_token;
201 0 ▶ if ($return_value_token->{type} == INT) {
Branch: 2 branches
BranchTrueFalseTotal
if ($return_value_token->{'type'} == 170) { } 0 0 0
elsif ($return_value_token->{'type'} == 171) { } 0 0 0
202 0 $invalid_token = $class->_validate_int_token($return_value_token);
203 }
204 elsif ($return_value_token->{type} == DOUBLE) {
205 0 $invalid_token = $class->_validate_doble_token($return_value_token);
206 }
207  
208 0 ▶ if ($invalid_token) {
Branch: 1 branch
BranchTrueFalseTotal
if ($invalid_token) 0 0 0
209 push @violations, {
210 filename => $file,
211 line => $token->{line},
212 0 description => DESC,
213 explanation => EXPL,
214 policy => __PACKAGE__,
215 };
216 }
217 }
218  
219 0 next;
220 }
221  
222 13 ▶ if ($token_type == FOR_STATEMENT || $token_type == FOREACH_STATEMENT) {
Branch: 1 branch
BranchTrueFalseTotal
if ($token_type == 133 or $token_type == 134) 1 12 13
Condition66%2/3
$token_type == 133 or $token_type == 134
l!l&&r!l&&!r
1012
MC/DC50%1/2
$token_type == 133 or $token_type == 134
$token_type == 133 $token_type == 134
Truth table66%2/3
A or B
A= $token_type == 133B= $token_type == 134
ABresult
1X1
011
000
223 1 ▶ for ($i++; $token = $tokens->[$i]; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($token = $tokens->[$i]) 6 0 6
224 6 $token_type = $token->{type};
225  
226 6 ▶ if ($token_type == SEMI_COLON || $token_type == LEFT_BRACE) {
Branch: 2 branches
BranchTrueFalseTotal
if ($token_type == 106 or $token_type == 109) { } 1 5 6
elsif ($token_type == 59) { } 0 5 5
Condition66%2/3
$token_type == 106 or $token_type == 109
l!l&&r!l&&!r
105
MC/DC50%1/2
$token_type == 106 or $token_type == 109
$token_type == 106 $token_type == 109
Truth table66%2/3
A or B
A= $token_type == 106B= $token_type == 109
ABresult
1X1
011
000
227 1 last;
228 }
229 elsif ($token_type == SLICE) { # e.g. for my $foo (1..42)
230 0 ▶ my $begin = $tokens->[$i-1] or last;
Branch: 1 branch
BranchTrueFalseTotal
my $begin = $$tokens[$i - 1] or last 0 0 0
231 0 ▶ my $end = $tokens->[$i+1] or last;
Branch: 1 branch
BranchTrueFalseTotal
my $end = $$tokens[$i + 1] or last 0 0 0
232 0 ▶ if ($begin->{type} == INT && $end->{type} == INT) {
Branch: 1 branch
BranchTrueFalseTotal
if ($begin->{"type"} == 170 and $end->{"type"} == 170) 0 0 0
Condition0%0/3
$begin->{"type"} == 170 and $end->{"type"} == 170
!ll&&!rl&&r
000
MC/DC0%0/2
$begin->{"type"} == 170 and $end->{"type"} == 170
$begin->{"type"} == 170 $end->{"type"} == 170
Truth table0%0/3
A and B
A= $begin->{"type"} == 170B= $end->{"type"} == 170
ABresult
0X0
100
111
233 0 ▶ if (!$allowed_values{$begin->{data}} || !$allowed_values{$end->{data}}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($allowed_values{$begin->{"data"}} and $allowed_values{$end->{"data"}}) 0 0 0
Condition0%0/3
$allowed_values{$begin->{"data"}} and $allowed_values{$end->{"data"}}
!ll&&!rl&&r
000
MC/DC0%0/2
$allowed_values{$begin->{"data"}} and $allowed_values{$end->{"data"}}
$allowed_values{$begin->{"data"}} $allowed_values{$end->{"data"}}
Truth table0%0/3
A and B
A= $allowed_values{$begin->{"data"}}B= $allowed_values{$end->{"data"}}
ABresult
0X0
100
111
234 push @violations, {
235 filename => $file,
236 line => $token->{line},
237 0 description => DESC,
238 explanation => EXPL,
239 policy => __PACKAGE__,
240 };
241 }
242 }
243 }
244 }
245 1 next;
246 }
247  
248 # for index of array
249 12 ▶ if ($token_type == LEFT_BRACKET) {
Branch: 1 branch
BranchTrueFalseTotal
if ($token_type == 111) 0 12 12
250 0 ▶ my $next_token = $tokens->[$i+1] or last;
Branch: 1 branch
BranchTrueFalseTotal
my $next_token = $$tokens[$i + 1] or last 0 0 0
251 0 ▶ if ($next_token->{type} == INT) {
Branch: 1 branch
BranchTrueFalseTotal
if ($next_token->{"type"} == 170) 0 0 0
252 0 my $int_token = $next_token;
253 0 ▶ $next_token = $tokens->[$i+2] or last;
Branch: 1 branch
BranchTrueFalseTotal
$next_token = $$tokens[$i + 2] or last 0 0 0
254  
255 0 my $invalid_token;
256 0 ▶ if ($next_token->{type} == RIGHT_BRACKET) {
Branch: 2 branches
BranchTrueFalseTotal
if ($next_token->{'type'} == 112) { } 0 0 0
elsif ($next_token->{'type'} != 104) { } 0 0 0
257 0 my $num = $int_token->{data} + 0;
258 0 ▶ if (!$allowed_values{$num} && $num ne -1) { # -1 is allowed specially when it is used as index of array
Branch: 1 branch
BranchTrueFalseTotal
if (not $allowed_values{$num} and $num ne -1) 0 0 0
Condition0%0/3
not $allowed_values{$num} and $num ne -1
!ll&&!rl&&r
000
MC/DC0%0/2
not $allowed_values{$num} and $num ne -1
not $allowed_values{$num} $num ne -1
Truth table0%0/3
not(A) and B
A= not $allowed_values{$num}B= $num ne -1
ABresult
0X0
100
111
259 0 $invalid_token = $int_token;
260 }
261 }
262 elsif ($next_token->{type} != COMMA) { # if it is not enumeration (probably it is any handling for index of array)
263 0 $invalid_token = $next_token;
264 }
265  
266 0 ▶ if ($invalid_token) {
Branch: 1 branch
BranchTrueFalseTotal
if ($invalid_token) 0 0 0
267 push @violations, {
268 filename => $file,
269 line => $invalid_token->{line},
270 0 description => DESC,
271 explanation => EXPL,
272 policy => __PACKAGE__,
273 };
274 }
275 }
276 0 next;
277 }
278 }
279  
280 2 return \@violations;
281 }
282  
283 3 my $is_in_assigning_context;
284 0 ▶sub _scan {
Subroutine _scan: not called CC=22 SCAR=62.3
285 0 my ($class, $i) = @_;
286  
287 0 ▶ my $token = $tokens->[$$i] or return;
Branch: 1 branch
BranchTrueFalseTotal
my $token = $$tokens[$$i] or return 0 0 0
288 0 my $token_type = $token->{type};
289 0 my $token_data = $token->{data};
290  
291 0 ▶ if ($token_type == ASSIGN) {
Branch: 2 branches
BranchTrueFalseTotal
if ($token_type == 65) { } 0 0 0
elsif ($token_type == 124) { } 0 0 0
292 0 $is_in_assigning_context = 1;
293  
294 0 ▶ $token = $tokens->[++$$i] or return;
Branch: 1 branch
BranchTrueFalseTotal
$token = $$tokens[++$$i] or return 0 0 0
295 0 $token_type = $token->{type};
296 0 $token_data = $token->{data};
297 }
298 elsif ($token_type == ARROW) {
299 0 $is_in_assigning_context = 0;
300 0 ▶ if (!$allow_to_the_right_of_a_fat_comma || $is_readonly_array1_ctx) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $allow_to_the_right_of_a_fat_comma or $is_readonly_array1_ctx) 0 0 0
Condition0%0/3
not $allow_to_the_right_of_a_fat_comma or $is_readonly_array1_ctx
l!l&&r!l&&!r
000
MC/DC0%0/2
not $allow_to_the_right_of_a_fat_comma or $is_readonly_array1_ctx
not $allow_to_the_right_of_a_fat_comma $is_readonly_array1_ctx
Truth table0%0/3
not(A) or B
A= not $allow_to_the_right_of_a_fat_commaB= $is_readonly_array1_ctx
ABresult
1X1
011
000
301 0 $is_in_assigning_context = 1;
302 }
303  
304 0 ▶ $token = $tokens->[++$$i] or return;
Branch: 1 branch
BranchTrueFalseTotal
$token = $$tokens[++$$i] or return 0 0 0
305 0 $token_type = $token->{type};
306 0 $token_data = $token->{data};
307 }
308  
309 0 my $invalid_token;
310  
311 my @violations;
312 0 ▶ if ($token_type == DOUBLE) {
Branch: 4 branches
BranchTrueFalseTotal
if ($token_type == 171) { } 0 0 0
elsif ($token_type == 170) { } 0 0 0
elsif ($token_type == 107) { } 0 0 0
elsif ($token_type == 111) { } 0 0 0
313 0 $invalid_token = $class->_validate_doble_token($token, $$i);
314 }
315 elsif ($token_type == INT) {
316 0 $invalid_token = $class->_validate_int_token($token);
317 }
318 elsif ($token_type == LEFT_PAREN) {
319 0 my $lpnum = 1;
320 0 ▶ for ($$i++; $token = $tokens->[$$i]; $$i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($token = $$tokens[$$i]) 0 0 0
321 0 $token_type = $token->{type};
322 0 ▶ if ($token_type == LEFT_PAREN) {
Branch: 2 branches
BranchTrueFalseTotal
if ($token_type == 107) { } 0 0 0
elsif ($token_type == 108) { } 0 0 0
323 0 $lpnum++;
324 }
325 elsif ($token_type == RIGHT_PAREN) {
326 0 ▶ last if --$lpnum <= 0;
Branch: 1 branch
BranchTrueFalseTotal
if --$lpnum <= 0 0 0 0
327 }
328 else {
329 0 push @violations, @{$class->_scan($i)};
330 }
331 }
332 }
333 elsif ($token_type == LEFT_BRACKET) {
334 0 my $lbnum = 1;
335  
336 0 ▶ for ($$i++; $token = $tokens->[$$i]; $$i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($token = $$tokens[$$i]) 0 0 0
337 0 $token_type = $token->{type};
338 0 ▶ if ($token_type == LEFT_BRACKET) {
Branch: 2 branches
BranchTrueFalseTotal
if ($token_type == 111) { } 0 0 0
elsif ($token_type == 112) { } 0 0 0
339 0 $lbnum++;
340 }
341 elsif ($token_type == RIGHT_BRACKET) {
342 0 ▶ last if --$lbnum <= 0;
Branch: 1 branch
BranchTrueFalseTotal
if --$lbnum <= 0 0 0 0
343 }
344 else {
345 0 push @violations, @{$class->_scan($i)};
346 }
347 }
348 }
349  
350 0 ▶ if ($is_in_assigning_context && $invalid_token) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_in_assigning_context and $invalid_token) 0 0 0
Condition0%0/3
$is_in_assigning_context and $invalid_token
!ll&&!rl&&r
000
MC/DC0%0/2
$is_in_assigning_context and $invalid_token
$is_in_assigning_context $invalid_token
Truth table0%0/3
A and B
A= $is_in_assigning_contextB= $invalid_token
ABresult
0X0
100
111
351 push @violations, {
352 filename => $file,
353 line => $invalid_token->{line},
354 0 description => DESC,
355 explanation => EXPL,
356 policy => __PACKAGE__,
357 };
358 }
359  
360 0 return \@violations;
361 }
362  
363 0 ▶sub _validate_int_token {
Subroutine _validate_int_token: not called CC=13 SCAR=52.0
364 0 my ($class, $token) = @_;
365  
366 0 my $token_data = $token->{data};
367  
368 0 ▶ if (my ($base_type) = $token_data =~ /\A[0-9]([b0xe]).+\z/) {
Branch: 1 branch
BranchTrueFalseTotal
if (my($base_type) = $token_data =~ /\A[0-9]([b0xe]).+\z/) 0 0 0
369 0 ▶ if ($1 eq 'b') {
Branch: 4 branches
BranchTrueFalseTotal
if ($1 eq 'b') { } 0 0 0
elsif ($1 eq '0') { } 0 0 0
elsif ($1 eq 'x') { } 0 0 0
elsif ($1 eq 'e') { } 0 0 0
370 0 ▶ return $token if !$allowed_types{Binary};
Branch: 1 branch
BranchTrueFalseTotal
unless $allowed_types{"Binary"} 0 0 0
371 }
372 elsif ($1 eq '0') {
373 0 ▶ return $token if !$allowed_types{Octal};
Branch: 1 branch
BranchTrueFalseTotal
unless $allowed_types{"Octal"} 0 0 0
374 }
375 elsif ($1 eq 'x') {
376 0 ▶ return $token if !$allowed_types{Hex};
Branch: 1 branch
BranchTrueFalseTotal
unless $allowed_types{"Hex"} 0 0 0
377 }
378 elsif ($1 eq 'e') {
379 0 ▶ return $token if !$allowed_types{Exp};
Branch: 1 branch
BranchTrueFalseTotal
unless $allowed_types{"Exp"} 0 0 0
380 }
381 }
382  
383 0 ▶ if (!$allowed_types{Int}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($allowed_types{"Int"}) 0 0 0
384 0 return $token;
385 }
386  
387 0 ▶ if (!$allowed_values{all_integers} && !$allowed_values{$token_data+0}) { # `+0` to convert to number
Branch: 1 branch
BranchTrueFalseTotal
unless ($allowed_values{"all_integers"} or $allowed_values{$token_data + 0}) 0 0 0
Condition0%0/3
$allowed_values{"all_integers"} or $allowed_values{$token_data + 0}
l!l&&r!l&&!r
000
MC/DC0%0/2
$allowed_values{"all_integers"} or $allowed_values{$token_data + 0}
$allowed_values{"all_integers"} $allowed_values{$token_data + 0}
Truth table0%0/3
A or B
A= $allowed_values{"all_integers"}B= $allowed_values{$token_data + 0}
ABresult
1X1
011
000
388 0 return $token;
389 }
390  
391 0 return;
392 }
393  
394 0 ▶sub _validate_doble_token {
Subroutine _validate_doble_token: not called CC=8 SCAR=42.8
395 0 my ($class, $token, $i) = @_;
396  
397 0 my $token_data = $token->{data};
398  
399 0 ▶ if ($i && $allowed_types{Float} && $allowed_values{$token_data+0}) { # `+0` to convert to number
Branch: 2 branches
BranchTrueFalseTotal
if ($i and $allowed_types{'Float'} and $allowed_values{$token_data + 0}) { } 0 0 0
elsif (not $allowed_values{'all_integers'} && $token_data =~ /[.]0+\z/) { } 0 0 0
Condition0%0/6
$i and $allowed_types{"Float"} and $allowed_values{$token_data + 0}
!ll&&!rl&&r
000
$i and $allowed_types{"Float"}
!ll&&!rl&&r
000
MC/DC0%0/3
$i and $allowed_types{"Float"} and $allowed_values{$token_data + 0}
$i $allowed_types{"Float"} $allowed_values{$token_data + 0}
Truth table0%0/4
A and B and C
A= $iB= $allowed_types{"Float"}C= $allowed_values{$token_data + 0}
ABCresult
0XX0
10X0
1100
1111
400 0 my $next_token = $tokens->[$i+1];
401 0 ▶ if ($next_token && $next_token->{type} == DOUBLE) {
Branch: 1 branch
BranchTrueFalseTotal
if ($next_token and $next_token->{"type"} == 171) 0 0 0
Condition0%0/3
$next_token and $next_token->{"type"} == 171
!ll&&!rl&&r
000
MC/DC0%0/2
$next_token and $next_token->{"type"} == 171
$next_token $next_token->{"type"} == 171
Truth table0%0/3
A and B
A= $next_tokenB= $next_token->{"type"} == 171
ABresult
0X0
100
111
402 0 return $next_token;
403 }
404 }
405 ▶ elsif (!$allowed_values{all_integers} || $token_data !~ /[.]0+\z/) {
Condition0%0/3
$allowed_values{'all_integers'} && $token_data =~ /[.]0+\z/
!ll&&!rl&&r
000
MC/DC0%0/2
$allowed_values{'all_integers'} && $token_data =~ /[.]0+\z/
$allowed_values{'all_integers'} $token_data =~ /[.]0+\z/
Truth table0%0/3
A && B
A= $allowed_values{'all_integers'}B= $token_data =~ /[.]0+\z/
ABresult
0X0
100
111
406 0 return $token;
407 }
408  
409 0 return;
410 }
411  
412 3 1;
413  
« Perl/Lint/Policy/Modules/ProhibitConditionalUseStatements.pm ↑ Summary Perl/Lint/Policy/TestingAndDebugging/RequireTestLabels.pm »