Perl/Lint/Policy/ValuesAndExpressions/ProhibitMismatchedOperators.pm

filter: Statementstmt 26.3% 19 / 72 Branchbran 0.0% 0 / 42 Conditioncond 0.0% 0 / 63 MC/DCmcdc 0.0% 0 / 30 Subroutinesub 85.7% 6 / 7 Podpod 0.0% 0 / 1 totaltotal 11.6% 25 / 215 CC 43 SCAR 74.1
CC
43
Cov
11%
evaluate
1892.0
BEGIN
1.0
BEGIN
1.0
CRAP 1660.4
« Perl/Lint/Policy/ValuesAndExpressions/ProhibitComplexVersion.pm ↑ Summary Perl/Lint/Policy/Modules/ProhibitConditionalUseStatements.pm »
1 package Perl::Lint::Policy::ValuesAndExpressions::ProhibitMismatchedOperators;
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 Perl::Lint::Constants::Kind;
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 => 'Mismatched operator',
10 EXPL => 'Numeric/string operators and operands should match',
11 2 };
12  
13 0 ▶sub evaluate {
Subroutine evaluate: not called CC=43 SCAR=75.5
Pod undocumented
14 0 my ($class, $file, $tokens, $args) = @_;
15  
16 0 my %numeric_ops = (
17 '==' => 1, '!=' => 1, '>' => 1,
18 '>=' => 1, '<' => 1, '<=' => 1,
19 '+' => 1, '-' => 1, '*' => 1,
20 '/' => 1, '+=' => 1, '-=' => 1,
21 '*=' => 1, '/=' => 1,
22 );
23  
24 0 my %string_ops = (
25 'eq' => 1, 'ne' => 1, 'lt' => 1,
26 'gt' => 1, 'le' => 1, 'ge' => 1,
27 '.' => 1, '.=' => 1,
28 );
29  
30 0 my %file_operators = (
31 '-r' => 1, '-w' => 1, '-x' => 1,
32 '-o' => 1, '-R' => 1, '-W' => 1,
33 '-X' => 1, '-O' => 1, '-e' => 1,
34 '-z' => 1, '-s' => 1, '-f' => 1,
35 '-d' => 1, '-l' => 1, '-p' => 1,
36 '-S' => 1, '-b' => 1, '-c' => 1,
37 '-t' => 1, '-u' => 1, '-g' => 1,
38 '-k' => 1, '-T' => 1, '-B' => 1,
39 '-M' => 1, '-A' => 1,
40 );
41  
42 0 my @violations;
43 0 ▶ for (my $i = 0, my $token_type, my $token_data, my $token_kind; my $token = $tokens->[$i]; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $token = $tokens->[$i]) 0 0 0
44 0 $token_type = $token->{type};
45 0 $token_kind = $token->{kind};
46 0 $token_data = $token->{data};
47  
48 0 ▶ if ($token_kind == KIND_OP || $token_kind == KIND_ASSIGN) {
Branch: 2 branches
BranchTrueFalseTotal
if ($token_kind == 1 or $token_kind == 2) { } 0 0 0
elsif ($token_type == 89) { } 0 0 0
Condition0%0/3
$token_kind == 1 or $token_kind == 2
l!l&&r!l&&!r
000
MC/DC0%0/2
$token_kind == 1 or $token_kind == 2
$token_kind == 1 $token_kind == 2
Truth table0%0/3
A or B
A= $token_kind == 1B= $token_kind == 2
ABresult
1X1
011
000
49 0 my $is_in_numeric_context = 0;
50 0 my $is_in_string_context = 0;
51 0 ▶ if ($numeric_ops{$token_data}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($numeric_ops{$token_data}) { } 0 0 0
elsif ($string_ops{$token_data}) { } 0 0 0
52 0 $is_in_numeric_context = 1;
53 }
54 elsif ($string_ops{$token_data}) {
55 0 $is_in_string_context = 1;
56 }
57  
58 0 ▶ if (!$is_in_numeric_context && !$is_in_string_context) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($is_in_numeric_context or $is_in_string_context) 0 0 0
Condition0%0/3
$is_in_numeric_context or $is_in_string_context
l!l&&r!l&&!r
000
MC/DC0%0/2
$is_in_numeric_context or $is_in_string_context
$is_in_numeric_context $is_in_string_context
Truth table0%0/3
A or B
A= $is_in_numeric_contextB= $is_in_string_context
ABresult
1X1
011
000
59 # Not target operator
60 0 next;
61 }
62  
63 0 my $before_token = $tokens->[$i-1];
64 0 my $next_token = $tokens->[$i+1];
65  
66 0 my $before_token_type = $before_token->{type};
67 0 my $next_token_type = $next_token->{type};
68  
69 0 my $is_before_token_variable = 0;
70 0 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if ($before_token_type == 166 or $before_token_type == 168 or $before_token_type == 169 or $before_token_type == 194 or $before_token_type == 195 or $before_token_type == 196) 0 0 0
Condition0%0/15
$before_token_type == 166 or $before_token_type == 168 or $before_token_type == 169 or $before_token_type == 194 or $before_token_type == 195 or $before_token_type == 196
l!l&&r!l&&!r
000
$before_token_type == 166 or $before_token_type == 168 or $before_token_type == 169 or $before_token_type == 194 or $before_token_type == 195
l!l&&r!l&&!r
000
$before_token_type == 166 or $before_token_type == 168 or $before_token_type == 169 or $before_token_type == 194
l!l&&r!l&&!r
000
$before_token_type == 166 or $before_token_type == 168 or $before_token_type == 169
l!l&&r!l&&!r
000
$before_token_type == 166 or $before_token_type == 168
l!l&&r!l&&!r
000
MC/DC0%0/6
$before_token_type == 166 or $before_token_type == 168 or $before_token_type == 169 or $before_token_type == 194 or $before_token_type == 195 or $before_token_type == 196
$before_token_type == 166 $before_token_type == 168 $before_token_type == 169 $before_token_type == 194 $before_token_type == 195 $before_token_type == 196
Truth table0%0/7
A or B or C or D or E or F
A= $before_token_type == 166B= $before_token_type == 168C= $before_token_type == 169D= $before_token_type == 194E= $before_token_type == 195F= $before_token_type == 196
ABCDEFresult
1XXXXX1
01XXXX1
001XXX1
0001XX1
00001X1
0000011
0000000
71 # XXX enough?
72 $before_token_type == VAR ||
73 $before_token_type == ARRAY_VAR ||
74 $before_token_type == HASH_VAR ||
75 $before_token_type == GLOBAL_VAR ||
76 $before_token_type == GLOBAL_ARRAY_VAR ||
77 $before_token_type == GLOBAL_HASH_VAR
78 ) {
79 0 $is_before_token_variable = 1;
80 }
81  
82 0 my $is_next_token_variable = 0;
83 0 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if ($next_token_type == 166 or $next_token_type == 168 or $next_token_type == 169 or $next_token_type == 194 or $next_token_type == 195 or $next_token_type == 196) 0 0 0
Condition0%0/15
$next_token_type == 166 or $next_token_type == 168 or $next_token_type == 169 or $next_token_type == 194 or $next_token_type == 195 or $next_token_type == 196
l!l&&r!l&&!r
000
$next_token_type == 166 or $next_token_type == 168 or $next_token_type == 169 or $next_token_type == 194 or $next_token_type == 195
l!l&&r!l&&!r
000
$next_token_type == 166 or $next_token_type == 168 or $next_token_type == 169 or $next_token_type == 194
l!l&&r!l&&!r
000
$next_token_type == 166 or $next_token_type == 168 or $next_token_type == 169
l!l&&r!l&&!r
000
$next_token_type == 166 or $next_token_type == 168
l!l&&r!l&&!r
000
MC/DC0%0/6
$next_token_type == 166 or $next_token_type == 168 or $next_token_type == 169 or $next_token_type == 194 or $next_token_type == 195 or $next_token_type == 196
$next_token_type == 166 $next_token_type == 168 $next_token_type == 169 $next_token_type == 194 $next_token_type == 195 $next_token_type == 196
Truth table0%0/7
A or B or C or D or E or F
A= $next_token_type == 166B= $next_token_type == 168C= $next_token_type == 169D= $next_token_type == 194E= $next_token_type == 195F= $next_token_type == 196
ABCDEFresult
1XXXXX1
01XXXX1
001XXX1
0001XX1
00001X1
0000011
0000000
84 # XXX enough?
85 $next_token_type == VAR ||
86 $next_token_type == ARRAY_VAR ||
87 $next_token_type == HASH_VAR ||
88 $next_token_type == GLOBAL_VAR ||
89 $next_token_type == GLOBAL_ARRAY_VAR ||
90 $next_token_type == GLOBAL_HASH_VAR
91 ) {
92 0 $is_next_token_variable = 1;
93 }
94  
95 0 ▶ if ($is_before_token_variable && $is_next_token_variable) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_before_token_variable and $is_next_token_variable) 0 0 0
Condition0%0/3
$is_before_token_variable and $is_next_token_variable
!ll&&!rl&&r
000
MC/DC0%0/2
$is_before_token_variable and $is_next_token_variable
$is_before_token_variable $is_next_token_variable
Truth table0%0/3
A and B
A= $is_before_token_variableB= $is_next_token_variable
ABresult
0X0
100
111
96 # when both of lvalue and rvalue are variable
97 # e.g
98 # $foo > $bar
99 0 $i++;
100 0 next;
101 }
102  
103 0 my $is_before_token_numeric = 0;
104 0 ▶ if ($before_token_type == INT || $before_token_type == DOUBLE) {
Branch: 1 branch
BranchTrueFalseTotal
if ($before_token_type == 170 or $before_token_type == 171) 0 0 0
Condition0%0/3
$before_token_type == 170 or $before_token_type == 171
l!l&&r!l&&!r
000
MC/DC0%0/2
$before_token_type == 170 or $before_token_type == 171
$before_token_type == 170 $before_token_type == 171
Truth table0%0/3
A or B
A= $before_token_type == 170B= $before_token_type == 171
ABresult
1X1
011
000
105 0 $is_before_token_numeric = 1;
106 0 ▶ if ($tokens->[$i-2]->{type} == STRING_MUL) {
Branch: 1 branch
BranchTrueFalseTotal
if ($$tokens[$i - 2]{"type"} == 19) 0 0 0
107 0 $is_before_token_numeric = 0;
108 }
109 }
110  
111 0 my $is_next_token_numeric = 0;
112 0 ▶ if ($next_token_type == INT || $next_token_type == DOUBLE) {
Branch: 1 branch
BranchTrueFalseTotal
if ($next_token_type == 170 or $next_token_type == 171) 0 0 0
Condition0%0/3
$next_token_type == 170 or $next_token_type == 171
l!l&&r!l&&!r
000
MC/DC0%0/2
$next_token_type == 170 or $next_token_type == 171
$next_token_type == 170 $next_token_type == 171
Truth table0%0/3
A or B
A= $next_token_type == 170B= $next_token_type == 171
ABresult
1X1
011
000
113 0 $is_next_token_numeric = 1;
114 }
115  
116 0 ▶ if ($is_in_numeric_context) {
Branch: 2 branches
BranchTrueFalseTotal
if ($is_in_numeric_context) { } 0 0 0
elsif ($is_in_string_context) { } 0 0 0
117 0 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
unless ($is_before_token_numeric || $is_before_token_variable and $is_next_token_numeric || $is_next_token_variable) 0 0 0
Condition0%0/9
$is_before_token_numeric || $is_before_token_variable and $is_next_token_numeric || $is_next_token_variable
!ll&&!rl&&r
000
$is_before_token_numeric or $is_before_token_variable
l!l&&r!l&&!r
000
$is_next_token_numeric or $is_next_token_variable
l!l&&r!l&&!r
000
MC/DC0%0/4
$is_before_token_numeric || $is_before_token_variable and $is_next_token_numeric || $is_next_token_variable
$is_before_token_numeric $is_before_token_variable $is_next_token_numeric $is_next_token_variable
Truth table0%0/7
A or B and C or D
A= $is_before_token_numericB= $is_before_token_variableC= $is_next_token_numericD= $is_next_token_variable
ABCDresult
00XX0
1X000
01000
1X1X1
1X011
011X1
01011
118 (!$is_before_token_numeric && !$is_before_token_variable) ||
119 (!$is_next_token_numeric && !$is_next_token_variable)
120 ) {
121 push @violations, {
122 filename => $file,
123 line => $token->{line},
124 0 description => DESC,
125 explanation => EXPL,
126 policy => __PACKAGE__,
127 };
128 }
129 }
130 elsif ($is_in_string_context) {
131 0 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if ($is_before_token_numeric and not $is_before_token_variable or $is_next_token_numeric and not $is_next_token_variable) 0 0 0
Condition0%0/9
$is_before_token_numeric and not $is_before_token_variable or $is_next_token_numeric and not $is_next_token_variable
l!l&&r!l&&!r
000
$is_before_token_numeric and not $is_before_token_variable
!ll&&!rl&&r
000
$is_next_token_numeric and not $is_next_token_variable
!ll&&!rl&&r
000
MC/DC0%0/4
$is_before_token_numeric and not $is_before_token_variable or $is_next_token_numeric and not $is_next_token_variable
$is_before_token_numeric not $is_before_token_variable $is_next_token_numeric not $is_next_token_variable
Truth table0%0/7
A and not(B) or C and not(D)
A= $is_before_token_numericB= not $is_before_token_variableC= $is_next_token_numericD= not $is_next_token_variable
ABCDresult
11XX1
0X111
10111
0X0X0
0X100
100X0
10100
132 ($is_before_token_numeric && !$is_before_token_variable) ||
133 ($is_next_token_numeric && !$is_next_token_variable)
134 ) {
135 push @violations, {
136 filename => $file,
137 line => $token->{line},
138 0 description => DESC,
139 explanation => EXPL,
140 policy => __PACKAGE__,
141 };
142 }
143 }
144 }
145 elsif ($token_type == HANDLE) {
146 0 ▶ if ($file_operators{$token_data}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($file_operators{$token_data}) 0 0 0
147 0 ▶ for ($i++; $token = $tokens->[$i]; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($token = $tokens->[$i]) 0 0 0
148 0 $token_type = $token->{type};
149 0 $token_kind = $token->{kind};
150 0 ▶ if ($token_kind == KIND_OP) {
Branch: 2 branches
BranchTrueFalseTotal
if ($token_kind == 1) { } 0 0 0
elsif ($token_type == 106) { } 0 0 0
151 0 ▶ if ($string_ops{$token->{data}}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($string_ops{$token->{"data"}}) 0 0 0
152 push @violations, {
153 filename => $file,
154 line => $token->{line},
155 0 description => DESC,
156 explanation => EXPL,
157 policy => __PACKAGE__,
158 };
159 }
160 0 last;
161 }
162 elsif ($token_type == SEMI_COLON) {
163 0 last; # fail safe
164 }
165 }
166 }
167 }
168 }
169  
170 0 return \@violations;
171 }
172  
173 2 1;
174  
« Perl/Lint/Policy/ValuesAndExpressions/ProhibitComplexVersion.pm ↑ Summary Perl/Lint/Policy/Modules/ProhibitConditionalUseStatements.pm »