blib/lib/Perl/Lint/Policy/ValuesAndExpressions/ProhibitMismatchedOperators.pm

filter: Statementstmt 77.7% 7 / 9 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 3 / 3 Podpod n/a totaltotal 83.3% 10 / 12 SCAR 0.1
CC
1
Cov
78%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
« blib/lib/Perl/Lint/Policy/ValuesAndExpressions/ProhibitLongChainsOfMethodCalls.pm ↑ Summary blib/lib/Perl/Lint/Policy/ValuesAndExpressions/ProhibitMixedBooleanOperators.pm »
1 package Perl::Lint::Policy::ValuesAndExpressions::ProhibitMismatchedOperators;
2 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use Perl::Lint::Constants::Type;
Subroutine BEGIN: called CC=1 SCAR=2.6
5 use Perl::Lint::Constants::Kind;
6 use parent "Perl::Lint::Policy";
7  
8 use constant {
9 DESC => 'Mismatched operator',
10 EXPL => 'Numeric/string operators and operands should match',
11 };
12  
13 sub evaluate {
14 my ($class, $file, $tokens, $args) = @_;
15  
16 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 my %string_ops = (
25 'eq' => 1, 'ne' => 1, 'lt' => 1,
26 'gt' => 1, 'le' => 1, 'ge' => 1,
27 '.' => 1, '.=' => 1,
28 );
29  
30 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 my @violations;
43 for (my $i = 0, my $token_type, my $token_data, my $token_kind; my $token = $tokens->[$i]; $i++) {
44 $token_type = $token->{type};
45 $token_kind = $token->{kind};
46 $token_data = $token->{data};
47  
48 if ($token_kind == KIND_OP || $token_kind == KIND_ASSIGN) {
49 my $is_in_numeric_context = 0;
50 my $is_in_string_context = 0;
51 if ($numeric_ops{$token_data}) {
52 $is_in_numeric_context = 1;
53 }
54 elsif ($string_ops{$token_data}) {
55 $is_in_string_context = 1;
56 }
57  
58 if (!$is_in_numeric_context && !$is_in_string_context) {
59 # Not target operator
60 next;
61 }
62  
63 my $before_token = $tokens->[$i-1];
64 my $next_token = $tokens->[$i+1];
65  
66 my $before_token_type = $before_token->{type};
67 my $next_token_type = $next_token->{type};
68  
69 my $is_before_token_variable = 0;
70 if (
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 $is_before_token_variable = 1;
80 }
81  
82 my $is_next_token_variable = 0;
83 if (
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 $is_next_token_variable = 1;
93 }
94  
95 if ($is_before_token_variable && $is_next_token_variable) {
96 # when both of lvalue and rvalue are variable
97 # e.g
98 # $foo > $bar
99 $i++;
100 next;
101 }
102  
103 my $is_before_token_numeric = 0;
104 if ($before_token_type == INT || $before_token_type == DOUBLE) {
105 $is_before_token_numeric = 1;
106 if ($tokens->[$i-2]->{type} == STRING_MUL) {
107 $is_before_token_numeric = 0;
108 }
109 }
110  
111 my $is_next_token_numeric = 0;
112 if ($next_token_type == INT || $next_token_type == DOUBLE) {
113 $is_next_token_numeric = 1;
114 }
115  
116 if ($is_in_numeric_context) {
117 if (
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 description => DESC,
125 explanation => EXPL,
126 policy => __PACKAGE__,
127 };
128 }
129 }
130 elsif ($is_in_string_context) {
131 if (
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 description => DESC,
139 explanation => EXPL,
140 policy => __PACKAGE__,
141 };
142 }
143 }
144 }
145 elsif ($token_type == HANDLE) {
146 if ($file_operators{$token_data}) {
147 for ($i++; $token = $tokens->[$i]; $i++) {
148 $token_type = $token->{type};
149 $token_kind = $token->{kind};
150 if ($token_kind == KIND_OP) {
151 if ($string_ops{$token->{data}}) {
152 push @violations, {
153 filename => $file,
154 line => $token->{line},
155 description => DESC,
156 explanation => EXPL,
157 policy => __PACKAGE__,
158 };
159 }
160 last;
161 }
162 elsif ($token_type == SEMI_COLON) {
163 last; # fail safe
164 }
165 }
166 }
167 }
168 }
169  
170 return \@violations;
171 }
172  
173 1;
174  
« blib/lib/Perl/Lint/Policy/ValuesAndExpressions/ProhibitLongChainsOfMethodCalls.pm ↑ Summary blib/lib/Perl/Lint/Policy/ValuesAndExpressions/ProhibitMixedBooleanOperators.pm »