line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Perl::Lint::Policy::ControlStructures::ProhibitCascadingIfElse; |
2
|
133
|
|
|
133
|
|
69410
|
use strict; |
|
133
|
|
|
|
|
199
|
|
|
133
|
|
|
|
|
3229
|
|
3
|
133
|
|
|
133
|
|
3771
|
use warnings; |
|
133
|
|
|
|
|
188
|
|
|
133
|
|
|
|
|
2875
|
|
4
|
133
|
|
|
133
|
|
6038
|
use Perl::Lint::Constants::Type; |
|
133
|
|
|
|
|
186
|
|
|
133
|
|
|
|
|
60453
|
|
5
|
133
|
|
|
133
|
|
979
|
use Perl::Lint::Constants::Kind; |
|
133
|
|
|
|
|
188
|
|
|
133
|
|
|
|
|
6410
|
|
6
|
133
|
|
|
133
|
|
477
|
use parent "Perl::Lint::Policy"; |
|
133
|
|
|
|
|
158
|
|
|
133
|
|
|
|
|
586
|
|
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
use constant { |
9
|
133
|
|
|
|
|
29780
|
DESC => 'Cascading if-elsif chain', |
10
|
|
|
|
|
|
|
EXPL => [117, 118], |
11
|
133
|
|
|
133
|
|
6558
|
}; |
|
133
|
|
|
|
|
197
|
|
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
sub evaluate { |
14
|
4
|
|
|
4
|
0
|
11
|
my ($class, $file, $tokens, $src, $args) = @_; |
15
|
|
|
|
|
|
|
|
16
|
4
|
|
100
|
|
|
24
|
my $max_elsif = $args->{prohibit_cascading_if_else}->{max_elsif} || 2; |
17
|
|
|
|
|
|
|
|
18
|
4
|
|
|
|
|
5
|
my @violations; |
19
|
4
|
|
|
|
|
6
|
my $is_chained = 0; |
20
|
4
|
|
|
|
|
4
|
my $cascading_num = 0; |
21
|
4
|
|
|
|
|
8
|
my $top_of_conditional_branch_line_num = 0; |
22
|
4
|
|
|
|
|
17
|
for (my $i = 0; my $token = $tokens->[$i]; $i++) { |
23
|
159
|
|
|
|
|
104
|
my $token_type = $token->{type}; |
24
|
|
|
|
|
|
|
|
25
|
159
|
100
|
66
|
|
|
354
|
if ($token_type == IF_STATEMENT || $token_type == UNLESS_STATEMENT) { |
26
|
10
|
|
|
|
|
8
|
$top_of_conditional_branch_line_num = $token->{line}; |
27
|
10
|
|
|
|
|
24
|
next; |
28
|
|
|
|
|
|
|
} |
29
|
|
|
|
|
|
|
|
30
|
149
|
100
|
|
|
|
161
|
if ($token_type == ELSIF_STATEMENT) { |
31
|
22
|
|
|
|
|
16
|
$cascading_num++; |
32
|
|
|
|
|
|
|
|
33
|
22
|
100
|
100
|
|
|
50
|
if ($is_chained && $cascading_num > $max_elsif) { |
34
|
7
|
|
|
|
|
27
|
push @violations, { |
35
|
|
|
|
|
|
|
filename => $file, |
36
|
|
|
|
|
|
|
line => $top_of_conditional_branch_line_num, |
37
|
|
|
|
|
|
|
description => DESC, |
38
|
|
|
|
|
|
|
explanation => EXPL, |
39
|
|
|
|
|
|
|
policy => __PACKAGE__, |
40
|
|
|
|
|
|
|
}; |
41
|
|
|
|
|
|
|
} |
42
|
|
|
|
|
|
|
|
43
|
22
|
|
|
|
|
19
|
my $left_brace_num = 0; |
44
|
22
|
|
|
|
|
38
|
for ($i++; my $token = $tokens->[$i]; $i++) { |
45
|
154
|
|
|
|
|
99
|
my $token_type = $token->{type}; |
46
|
154
|
100
|
|
|
|
279
|
if ($token_type == LEFT_BRACE) { |
|
|
100
|
|
|
|
|
|
47
|
22
|
|
|
|
|
30
|
$left_brace_num++; |
48
|
|
|
|
|
|
|
} |
49
|
|
|
|
|
|
|
elsif ($token_type == RIGHT_BRACE) { |
50
|
22
|
50
|
|
|
|
35
|
last if --$left_brace_num <= 0; |
51
|
|
|
|
|
|
|
} |
52
|
|
|
|
|
|
|
} |
53
|
22
|
|
|
|
|
17
|
$is_chained = 1; |
54
|
22
|
|
|
|
|
30
|
next; |
55
|
|
|
|
|
|
|
} |
56
|
|
|
|
|
|
|
|
57
|
127
|
|
|
|
|
76
|
$cascading_num = 0; |
58
|
127
|
|
|
|
|
170
|
$is_chained = 0; |
59
|
|
|
|
|
|
|
} |
60
|
|
|
|
|
|
|
|
61
|
4
|
|
|
|
|
17
|
return \@violations; |
62
|
|
|
|
|
|
|
} |
63
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
1; |
65
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
|