Perl/Lint/Policy/Variables/RequireNegativeIndices.pm

filter: Statementstmt 26.7% 19 / 71 Branchbran 0.0% 0 / 44 Conditioncond 0.0% 0 / 24 MC/DCmcdc 0.0% 0 / 13 Subroutinesub 83.3% 5 / 6 Podpod 0.0% 0 / 1 totaltotal 15.0% 24 / 159 CC 31 SCAR 67.5
CC
31
Cov
14%
evaluate
992.0
BEGIN
1.0
BEGIN
1.0
CRAP 854.4
« Perl/Lint/Policy/Variables/ProhibitEvilVariables.pm ↑ Summary Perl/Lint/Policy/NamingConventions/ProhibitAmbiguousNames.pm »
1 package Perl::Lint::Policy::Variables::RequireNegativeIndices;
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 Perl::Lint::Constants::Type;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use parent "Perl::Lint::Policy";
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 2 ▶use constant {
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 DESC => 'Negative array index should be used',
9 EXPL => [88],
10 2 };
11  
12 2 my %var_token_types = (
13 &VAR => 1,
14 &GLOBAL_VAR => 1,
15 );
16  
17 2 my %array_dereference_token_types = (
18 &ARRAY_DEREFERENCE => 1,
19 &ARRAY_SIZE_DEREFERENCE => 1,
20 );
21  
22 2 my %array_var_token_types = (
23 &ARRAY_VAR => 1,
24 &GLOBAL_ARRAY_VAR => 1,
25 );
26  
27 0 ▶sub evaluate {
Subroutine evaluate: not called CC=31 SCAR=69.0
Pod undocumented
28 0 my ($class, $file, $tokens, $src, $args) = @_;
29  
30 0 my @violations;
31 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
32 0 $token_type = $token->{type};
33 0 $token_data = $token->{data};
34  
35 0 my $is_reference = 0;
36 0 ▶ if ($var_token_types{$token_type} || $token_type == SHORT_SCALAR_DEREFERENCE) {
Branch: 1 branch
BranchTrueFalseTotal
if ($var_token_types{$token_type} or $token_type == 117) 0 0 0
Condition0%0/3
$var_token_types{$token_type} or $token_type == 117
l!l&&r!l&&!r
000
MC/DC0%0/2
$var_token_types{$token_type} or $token_type == 117
$var_token_types{$token_type} $token_type == 117
Truth table0%0/3
A or B
A= $var_token_types{$token_type}B= $token_type == 117
ABresult
1X1
011
000
37 0 my $var_name;
38 0 ▶ if ($token_type == SHORT_SCALAR_DEREFERENCE) {
Branch: 1 branch
BranchTrueFalseTotal
if ($token_type == 117) { } 0 0 0
39 0 $token = $tokens->[++$i];
40 0 ▶ last if !$token;
Branch: 1 branch
BranchTrueFalseTotal
unless $token 0 0 0
41  
42 0 $var_name = $token->{data};
43 0 $is_reference = 1;
44 }
45 else {
46 0 $var_name = substr($token->{data}, 1);
47 }
48  
49 0 $token = $tokens->[++$i];
50 0 ▶ last if !$token;
Branch: 1 branch
BranchTrueFalseTotal
unless $token 0 0 0
51  
52 0 ▶ if ($token->{type} == POINTER) {
Branch: 1 branch
BranchTrueFalseTotal
if ($token->{"type"} == 125) 0 0 0
53 0 $is_reference = 1;
54 0 $token = $tokens->[++$i];
55 0 ▶ last if !$token;
Branch: 1 branch
BranchTrueFalseTotal
unless $token 0 0 0
56 }
57  
58 0 ▶ if ($token->{type} == LEFT_BRACKET) {
Branch: 1 branch
BranchTrueFalseTotal
if ($token->{"type"} == 111) 0 0 0
59 0 my $nlbracket = 1;
60 0 ▶ for ($i++; $token = $tokens->[$i]; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($token = $tokens->[$i]) 0 0 0
61 0 $token_type = $token->{type};
62  
63 0 ▶ if ($token_type == LEFT_BRACKET) {
Branch: 1 branch
BranchTrueFalseTotal
if ($token_type == 111) 0 0 0
64 0 $nlbracket++;
65 0 next;
66 }
67  
68 0 ▶ if ($token_type == RIGHT_BRACKET) {
Branch: 1 branch
BranchTrueFalseTotal
if ($token_type == 112) 0 0 0
69 0 ▶ last if --$nlbracket <= 0;
Branch: 1 branch
BranchTrueFalseTotal
if --$nlbracket <= 0 0 0 0
70 0 next;
71 }
72  
73 0 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if ($token_type == 66 or $is_reference and $token_type == 118) 0 0 0
Condition0%0/6
$token_type == 66 or $is_reference and $token_type == 118
l!l&&r!l&&!r
000
$is_reference and $token_type == 118
!ll&&!rl&&r
000
MC/DC0%0/3
$token_type == 66 or $is_reference and $token_type == 118
$token_type == 66 $is_reference $token_type == 118
Truth table0%0/4
A or B and C
A= $token_type == 66B= $is_referenceC= $token_type == 118
ABCresult
1XX1
0111
00X0
0100
74 $token_type == ARRAY_SIZE ||
75 ($is_reference && $token_type == SHORT_ARRAY_DEREFERENCE)
76 ) {
77 0 $token = $tokens->[++$i];
78 0 ▶ last if !$token;
Branch: 1 branch
BranchTrueFalseTotal
unless $token 0 0 0
79  
80 0 $token_type = $token->{type};
81  
82 0 my $array_size_data;
83 0 ▶ if ($is_reference && $var_token_types{$token_type}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($is_reference and $var_token_types{$token_type}) { } 0 0 0
elsif ($token->{'type'} == 122) { } 0 0 0
Condition0%0/3
$is_reference and $var_token_types{$token_type}
!ll&&!rl&&r
000
MC/DC0%0/2
$is_reference and $var_token_types{$token_type}
$is_reference $var_token_types{$token_type}
Truth table0%0/3
A and B
A= $is_referenceB= $var_token_types{$token_type}
ABresult
0X0
100
111
84 0 $array_size_data = substr $token->{data}, 1;
85 }
86 elsif ($token->{type} == KEY) {
87 0 $array_size_data = $token->{data};
88 }
89  
90 0 ▶ if ($array_size_data) {
Branch: 1 branch
BranchTrueFalseTotal
if ($array_size_data) 0 0 0
91 0 $array_size_data =~ s/\W.*\Z//; # XXX workaround
92 # ref: https://github.com/goccy/p5-Compiler-Lexer/issues/48
93 0 ▶ if ($array_size_data eq $var_name) {
Branch: 1 branch
BranchTrueFalseTotal
if ($array_size_data eq $var_name) 0 0 0
94 push @violations, {
95 filename => $file,
96 line => $token->{line},
97 0 description => DESC,
98 explanation => EXPL,
99 policy => __PACKAGE__,
100 };
101 }
102 }
103  
104 0 next;
105 }
106  
107 0 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if ($token_type == 113 or $is_reference and $token_type == 121) 0 0 0
Condition0%0/6
$token_type == 113 or $is_reference and $token_type == 121
l!l&&r!l&&!r
000
$is_reference and $token_type == 121
!ll&&!rl&&r
000
MC/DC0%0/3
$token_type == 113 or $is_reference and $token_type == 121
$token_type == 113 $is_reference $token_type == 121
Truth table0%0/4
A or B and C
A= $token_type == 113B= $is_referenceC= $token_type == 121
ABCresult
1XX1
0111
00X0
0100
108 $token_type == ARRAY_DEREFERENCE ||
109 ($is_reference && $token_type == ARRAY_SIZE_DEREFERENCE)
110 ) {
111 0 $token = $tokens->[++$i];
112 0 ▶ last if !$token;
Branch: 1 branch
BranchTrueFalseTotal
unless $token 0 0 0
113 } # fall through
114 0 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if (not $is_reference and $array_var_token_types{$token_type} or $array_dereference_token_types{$token_type}) 0 0 0
Condition0%0/6
not $is_reference and $array_var_token_types{$token_type} or $array_dereference_token_types{$token_type}
l!l&&r!l&&!r
000
not $is_reference and $array_var_token_types{$token_type}
!ll&&!rl&&r
000
MC/DC0%0/3
not $is_reference and $array_var_token_types{$token_type} or $array_dereference_token_types{$token_type}
not $is_reference $array_var_token_types{$token_type} $array_dereference_token_types{$token_type}
Truth table0%0/5
not(A) and B or C
A= not $is_referenceB= $array_var_token_types{$token_type}C= $array_dereference_token_types{$token_type}
ABCresult
11X1
0X11
1011
0X00
1000
115 (!$is_reference && $array_var_token_types{$token_type}) ||
116 $array_dereference_token_types{$token_type}
117 ) {
118 0 ($token_data = substr $token->{data}, 1) =~ s/\W.*\Z//; # XXX workaround
119 # ref: https://github.com/goccy/p5-Compiler-Lexer/issues/48
120 0 ▶ if ($token_data eq $var_name) {
Branch: 1 branch
BranchTrueFalseTotal
if ($token_data eq $var_name) 0 0 0
121 push @violations, {
122 filename => $file,
123 line => $token->{line},
124 0 description => DESC,
125 explanation => EXPL,
126 policy => __PACKAGE__,
127 };
128 }
129 0 next;
130 }
131 }
132 }
133 }
134  
135 }
136  
137 0 return \@violations;
138 }
139  
140 2 1;
141  
« Perl/Lint/Policy/Variables/ProhibitEvilVariables.pm ↑ Summary Perl/Lint/Policy/NamingConventions/ProhibitAmbiguousNames.pm »