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

filter: Statementstmt 76.6% 59 / 77 Branchbran 35.7% 15 / 42 Conditioncond 13.1% 5 / 38 MC/DCmcdc 0.0% 0 / 18 Subroutinesub 87.5% 14 / 16 Podpod 80.0% 4 / 5 totaltotal 49.4% 97 / 196 CC 36 SCAR 36.2
CC
36
Cov
50%
_is_special_string_number_addion
90.0
violates
58.9
_have_stringy_x
30.0
CRAP 37.4
« blib/lib/Perl/Critic/Policy/RegularExpressions/ProhibitComplexRegexes.pm ↑ Summary blib/lib/Perl/Critic/Policy/Variables/ProhibitPunctuationVars.pm »
1 package Perl::Critic::Policy::ValuesAndExpressions::ProhibitMismatchedOperators;
2 40 ▶use 5.010001;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 40 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 40 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 40 ▶use Readonly;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 40 ▶use Perl::Critic::Utils qw{ :booleans :severities hashify };
Subroutine BEGIN: called CC=1 SCAR=0.0
8 40 ▶use parent 'Perl::Critic::Policy';
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 40 our $VERSION = '1.150';
11  
12 #-----------------------------------------------------------------------------
13  
14 40 Readonly::Scalar my $DESC => q<Mismatched operator>;
15 40 Readonly::Scalar my $EXPL => q<Numeric/string operators and operands should match>;
16  
17 # token compatibility [ numeric, string ]
18 40 Readonly::Hash my %TOKEN_COMPATIBILITY => (
19 'PPI::Token::Number' => [$TRUE, $FALSE],
20 'PPI::Token::Symbol' => [$TRUE, $TRUE ],
21 'PPI::Token::Quote' => [$FALSE, $TRUE ],
22 );
23  
24 Readonly::Hash my %FILE_OPERATOR_COMPATIBILITY =>
25 40 map {; "-$_" => [$TRUE, $FALSE] }
26 qw< r w x o R W X O e z s f d l p S b c t u g k T B M A >;
27  
28 40 Readonly::Scalar my $TOKEN_COMPATIBILITY_INDEX_NUMERIC => 0;
29 40 Readonly::Scalar my $TOKEN_COMPATIBILITY_INDEX_STRING => 1;
30  
31 Readonly::Hash my %OPERATOR_TYPES => (
32 # numeric
33 (
34 560 map { $_ => $TOKEN_COMPATIBILITY_INDEX_NUMERIC }
35 qw[ == != > >= < <= + - * / += -= *= /= ]
36 ),
37 # string
38 40 map { $_ => $TOKEN_COMPATIBILITY_INDEX_STRING }
39 qw< eq ne lt gt le ge . .= >,
40 );
41  
42 40 Readonly::Scalar my $TOKEN_COMPATIBILITY_SPECIAL_STRING_OPERATOR => qw{+};
43 40 Readonly::Hash my %SPECIAL_STRING_VALUES =>
44 hashify( qw('nan' 'inf' '-inf' '+inf') );
45  
46 #-----------------------------------------------------------------------------
47  
48 90 ▶sub supported_parameters { return () }
Subroutine supported_parameters: called CC=1 SCAR=0.0
Pod undocumented
49 75 ▶sub default_severity { return $SEVERITY_MEDIUM }
Subroutine default_severity: called CC=1 SCAR=0.0
Pod documented
50 74 ▶sub default_themes { return qw< core bugs certrule > }
Subroutine default_themes: called CC=1 SCAR=0.0
Pod documented
51 31 ▶sub applies_to { return 'PPI::Token::Operator' }
Subroutine applies_to: called CC=1 SCAR=0.0
Pod documented
52  
53 #-----------------------------------------------------------------------------
54  
55 sub violates {
56 135 ▶ my ($self, $elem) = @_;
Subroutine violates: called CC=15 SCAR=40.8
Pod documented
57  
58 135 my $elem_text = $elem->content();
59  
60 135 ▶ return if not exists $OPERATOR_TYPES{$elem_text};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $OPERATOR_TYPES{$elem_text} 118 17 135
61  
62 17 ▶ my $leading_operator = $self->_get_potential_leading_operator($elem)
Branch: 1 branch
BranchTrueFalseTotal
my $leading_operator = $self->_get_potential_leading_operator($elem) or return 0 17 17
63 or return;
64  
65 17 ▶ my $next_elem = $elem->snext_sibling() or return;
Branch: 1 branch
BranchTrueFalseTotal
my $next_elem = $elem->snext_sibling or return 0 17 17
66  
67 17 ▶ if ( $next_elem->isa('PPI::Token::Operator') ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($next_elem->isa("PPI::Token::Operator")) 0 17 17
68 0 $elem_text .= $next_elem->content();
69 0 $next_elem = $next_elem->snext_sibling();
70 }
71  
72 17 ▶ return if not exists $OPERATOR_TYPES{$elem_text};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $OPERATOR_TYPES{$elem_text} 0 17 17
73 17 my $operator_type = $OPERATOR_TYPES{$elem_text};
74  
75 17 my $leading_operator_compatibility =
76 $self->_get_token_compatibility($leading_operator);
77 17 my $next_compatibility = $self->_get_token_compatibility($next_elem);
78  
79 return if
80 (
81 17 ▶ ! defined $leading_operator_compatibility
Branch: 1 branch
BranchTrueFalseTotal
if !defined($leading_operator_compatibility) || $leading_operator_compatibility->[$operator_type] and !defined($next_compatibility) || $next_compatibility->[$operator_type] 17 0 17
Condition33%3/9
!defined($leading_operator_compatibility) || $leading_operator_compatibility->[$operator_type] and !defined($next_compatibility) || $next_compatibility->[$operator_type]
!ll&&!rl&&r
0017
not defined $leading_operator_compatibility or $leading_operator_compatibility->[$operator_type]
l!l&&r!l&&!r
0170
not defined $next_compatibility or $next_compatibility->[$operator_type]
l!l&&r!l&&!r
0170
MC/DC0%0/4
!defined($leading_operator_compatibility) || $leading_operator_compatibility->[$operator_type] and !defined($next_compatibility) || $next_compatibility->[$operator_type]
not defined $leading_operator_compatibility $leading_operator_compatibility->[$operator_type] not defined $next_compatibility $next_compatibility->[$operator_type]
Truth table14%1/7
not(A) or B and not(C) or D
A= not defined $leading_operator_compatibilityB= $leading_operator_compatibility->[$operator_type]C= not defined $next_compatibilityD= $next_compatibility->[$operator_type]
ABCDresult
00XX0
1X000
01000
1X1X1
1X011
011X1
01011
82 || $leading_operator_compatibility->[$operator_type]
83 )
84 && (
85 ! defined $next_compatibility
86 || $next_compatibility->[$operator_type]
87 );
88  
89 return if
90 0 ▶ $operator_type
Branch: 1 branch
BranchTrueFalseTotal
if $operator_type and defined $leading_operator_compatibility and not $leading_operator_compatibility->[$operator_type] and $self->_have_stringy_x($leading_operator) 0 0 0
Condition0%0/9
$operator_type and defined $leading_operator_compatibility and not $leading_operator_compatibility->[$operator_type] and $self->_have_stringy_x($leading_operator)
!ll&&!rl&&r
000
$operator_type and defined $leading_operator_compatibility and not $leading_operator_compatibility->[$operator_type]
!ll&&!rl&&r
000
$operator_type and defined $leading_operator_compatibility
!ll&&!rl&&r
000
MC/DC0%0/4
$operator_type and defined $leading_operator_compatibility and not $leading_operator_compatibility->[$operator_type] and $self->_have_stringy_x($leading_operator)
$operator_type defined $leading_operator_compatibility not $leading_operator_compatibility->[$operator_type] $self->_have_stringy_x($leading_operator)
Truth table0%0/5
A and B and not(C) and D
A= $operator_typeB= defined $leading_operator_compatibilityC= not $leading_operator_compatibility->[$operator_type]D= $self->_have_stringy_x($leading_operator)
ABCDresult
0XXX0
10XX0
110X0
11100
11111
91 && defined $leading_operator_compatibility
92 && ! $leading_operator_compatibility->[$operator_type]
93 && $self->_have_stringy_x($leading_operator); # RT 54524
94  
95 0 ▶ return if $self->_is_special_string_number_addion($elem_text, $leading_operator, $next_elem);
Branch: 1 branch
BranchTrueFalseTotal
if $self->_is_special_string_number_addion($elem_text, $leading_operator, $next_elem) 0 0 0
96  
97 0 return $self->violation($DESC, $EXPL, $elem);
98 }
99  
100 #-----------------------------------------------------------------------------
101  
102 sub _get_token_compatibility {
103 51 ▶ my ($self, $elem) = @_;
Subroutine _get_token_compatibility: called CC=4 SCAR=14.3
104  
105 51 ▶ return $FILE_OPERATOR_COMPATIBILITY{ $elem->content() }
Branch: 1 branch
BranchTrueFalseTotal
if $self->_is_file_operator($elem) 0 51 51
106 if $self->_is_file_operator($elem);
107  
108 51 for my $class (keys %TOKEN_COMPATIBILITY) {
109 81 ▶ return $TOKEN_COMPATIBILITY{$class} if $elem->isa($class);
Branch: 1 branch
BranchTrueFalseTotal
if $elem->isa($class) 51 30 81
110 }
111  
112 0 return;
113 }
114  
115 #-----------------------------------------------------------------------------
116  
117 sub _have_stringy_x {
118 0 ▶ my ($self, $elem) = @_;
Subroutine _have_stringy_x: not called CC=5 SCAR=34.0
119  
120 0 ▶ return if not $elem;
Branch: 1 branch
BranchTrueFalseTotal
unless $elem 0 0 0
121  
122 0 ▶ my $prev_oper = $elem->sprevious_sibling() or return;
Branch: 1 branch
BranchTrueFalseTotal
my $prev_oper = $elem->sprevious_sibling or return 0 0 0
123  
124 0 ▶ return if not $prev_oper->isa('PPI::Token::Operator');
Branch: 1 branch
BranchTrueFalseTotal
unless $prev_oper->isa("PPI::Token::Operator") 0 0 0
125 0 ▶ return if 'x' ne $prev_oper->content();
Branch: 1 branch
BranchTrueFalseTotal
if "x" ne $prev_oper->content 0 0 0
126  
127 0 return !! $prev_oper->sprevious_sibling();
128 }
129  
130 #-----------------------------------------------------------------------------
131  
132 sub _get_potential_leading_operator {
133 17 ▶ my ($self, $elem) = @_;
Subroutine _get_potential_leading_operator: called CC=5 SCAR=17.5
134  
135 17 ▶ my $previous_element = $elem->sprevious_sibling() or return;
Branch: 1 branch
BranchTrueFalseTotal
my $previous_element = $elem->sprevious_sibling or return 0 17 17
136  
137 17 ▶ if ( $self->_get_token_compatibility($previous_element) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_get_token_compatibility($previous_element)) 17 0 17
138 17 my $previous_sibling = $previous_element->sprevious_sibling();
139 17 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if ($previous_sibling and $self->_is_file_operator($previous_sibling)) 0 17 17
Condition66%2/3
$previous_sibling and $self->_is_file_operator($previous_sibling)
!ll&&!rl&&r
2150
MC/DC0%0/2
$previous_sibling and $self->_is_file_operator($previous_sibling)
$previous_sibling $self->_is_file_operator($previous_sibling)
Truth table66%2/3
A and B
A= $previous_siblingB= $self->_is_file_operator($previous_sibling)
ABresult
0X0
100
111
140 $previous_sibling and $self->_is_file_operator($previous_sibling)
141 ) {
142 0 $previous_element = $previous_sibling;
143 }
144 }
145  
146 17 return $previous_element;
147 }
148  
149 #-----------------------------------------------------------------------------
150  
151 sub _is_file_operator {
152 66 ▶ my ($self, $elem) = @_;
Subroutine _is_file_operator: called CC=3 SCAR=12.5
153  
154 66 ▶ return if not $elem;
Branch: 1 branch
BranchTrueFalseTotal
unless $elem 0 66 66
155 66 ▶ return if not $elem->isa('PPI::Token::Operator');
Branch: 1 branch
BranchTrueFalseTotal
unless $elem->isa("PPI::Token::Operator") 66 0 66
156 0 return !! $FILE_OPERATOR_COMPATIBILITY{ $elem->content() };
157 }
158  
159 #-----------------------------------------------------------------------------
160  
161 sub _is_special_string_number_addion {
162 0 ▶ my ($self, $elem_operator, $element_1, $element_2, $check_recursive) = @_;
Subroutine _is_special_string_number_addion: not called CC=9 SCAR=45.0
163  
164 return 1 if $elem_operator
165 && $elem_operator eq $TOKEN_COMPATIBILITY_SPECIAL_STRING_OPERATOR
166 0 ▶ && $SPECIAL_STRING_VALUES{lc($element_1->content()//0)}
Branch: 1 branch
BranchTrueFalseTotal
if $elem_operator and $elem_operator eq $TOKEN_COMPATIBILITY_SPECIAL_STRING_OPERATOR and $SPECIAL_STRING_VALUES{lc($element_1->content // 0)} and $element_2->isa("PPI::Token::Number") and $element_2->content == 0 0 0 0
Condition0%0/14
$elem_operator and $elem_operator eq $TOKEN_COMPATIBILITY_SPECIAL_STRING_OPERATOR and $SPECIAL_STRING_VALUES{lc($element_1->content // 0)} and $element_2->isa("PPI::Token::Number") and $element_2->content == 0
!ll&&!rl&&r
000
$elem_operator and $elem_operator eq $TOKEN_COMPATIBILITY_SPECIAL_STRING_OPERATOR and $SPECIAL_STRING_VALUES{lc($element_1->content // 0)} and $element_2->isa("PPI::Token::Number")
!ll&&!rl&&r
000
$elem_operator and $elem_operator eq $TOKEN_COMPATIBILITY_SPECIAL_STRING_OPERATOR and $SPECIAL_STRING_VALUES{lc($element_1->content // 0)}
!ll&&!rl&&r
000
$elem_operator and $elem_operator eq $TOKEN_COMPATIBILITY_SPECIAL_STRING_OPERATOR
!ll&&!rl&&r
000
$element_1->content // 0
l!l
00
MC/DC0%0/5
$elem_operator and $elem_operator eq $TOKEN_COMPATIBILITY_SPECIAL_STRING_OPERATOR and $SPECIAL_STRING_VALUES{lc($element_1->content // 0)} and $element_2->isa("PPI::Token::Number") and $element_2->content == 0
$elem_operator $elem_operator eq $TOKEN_COMPATIBILITY_SPECIAL_STRING_OPERATOR $SPECIAL_STRING_VALUES{lc($element_1->content // 0)} $element_2->isa("PPI::Token::Number") $element_2->content == 0
MC/DC0%0/1
$element_1->content // 0
$element_1->content
Truth table0%0/6
A and B and C and D and E
A= $elem_operatorB= $elem_operator eq $TOKEN_COMPATIBILITY_SPECIAL_STRING_OPERATORC= $SPECIAL_STRING_VALUES{lc($element_1->content // 0)}D= $element_2->isa("PPI::Token::Number")E= $element_2->content == 0
ABCDEresult
0XXXX0
10XXX0
110XX0
1110X0
111100
111111
Truth table0%0/2
A
A= $element_1->content
Aresult
11
00
167 && $element_2->isa('PPI::Token::Number')
168 && $element_2->content() == 0;
169 0 ▶ return 1 if !$check_recursive && $self->_is_special_string_number_addion($elem_operator, $element_2, $element_1, 1);
Branch: 1 branch
BranchTrueFalseTotal
if not $check_recursive and $self->_is_special_string_number_addion($elem_operator, $element_2, $element_1, 1) 0 0 0
Condition0%0/3
not $check_recursive and $self->_is_special_string_number_addion($elem_operator, $element_2, $element_1, 1)
!ll&&!rl&&r
000
MC/DC0%0/2
not $check_recursive and $self->_is_special_string_number_addion($elem_operator, $element_2, $element_1, 1)
not $check_recursive $self->_is_special_string_number_addion($elem_operator, $element_2, $element_1, 1)
Truth table0%0/3
not(A) and B
A= not $check_recursiveB= $self->_is_special_string_number_addion($elem_operator, $element_2, $element_1, 1)
ABresult
0X0
100
111
170  
171 0 return;
172 }
173  
174 40 1;
175  
176 __END__
177  
178 #-----------------------------------------------------------------------------
179  
180 =pod
181  
182 =head1 NAME
183  
184 Perl::Critic::Policy::ValuesAndExpressions::ProhibitMismatchedOperators - Don't mix numeric operators with string operands, or vice-versa.
185  
186  
187 =head1 AFFILIATION
188  
189 This Policy is part of the core L<Perl::Critic|Perl::Critic>
190 distribution.
191  
192  
193 =head1 DESCRIPTION
194  
195 Using the wrong operator type for a value can obscure coding intent
196 and possibly lead to subtle errors. An example of this is mixing a
197 string equality operator with a numeric value, or vice-versa.
198  
199 if ($foo == 'bar') {} #not ok
200 if ($foo eq 'bar') {} #ok
201 if ($foo eq 123) {} #not ok
202 if ($foo == 123) {} #ok
203  
204  
205 =head1 CONFIGURATION
206  
207 This Policy is not configurable except for the standard options.
208  
209 =for stopwords NaN struct
210  
211 =head1 NOTES
212  
213 If L<warnings|warnings> are enabled, the Perl interpreter usually
214 warns you about using mismatched operators at run-time. This Policy
215 does essentially the same thing, but at author-time. That way, you
216 can find out about them sooner.
217  
218 Perl handles the strings 'NaN' and 'inf' as special numbers and creates an NV struct when compared with a numeric operator.
219 Although not necessary it is allowed to write code such as:
220 my $i = 'inf'+0;
221 This pattern helps others understand that the variable is indeed the Infinite or NaN numbers as Perl interprets them.
222 Only these two special string numbers are allowed to have the '+' operator which would otherwise be allowed only for strings.
223  
224  
225 =head1 AUTHOR
226  
227 Peter Guzis <pguzis@cpan.org>
228  
229  
230 =head1 COPYRIGHT
231  
232 Copyright (c) 2006-2023 Peter Guzis. All rights reserved.
233  
234 This program is free software; you can redistribute it and/or modify
235 it under the same terms as Perl itself. The full text of this license
236 can be found in the LICENSE file included with this module.
237  
238 =cut
239  
240 # Local Variables:
241 # mode: cperl
242 # cperl-indent-level: 4
243 # fill-column: 78
244 # indent-tabs-mode: nil
245 # c-indentation-style: bsd
246 # End:
247 # ex: set ts=8 sts=4 sw=4 tw=78 ft=perl expandtab shiftround :
« blib/lib/Perl/Critic/Policy/RegularExpressions/ProhibitComplexRegexes.pm ↑ Summary blib/lib/Perl/Critic/Policy/Variables/ProhibitPunctuationVars.pm »