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
« lib/Perl/Critic/Policy/RegularExpressions/ProhibitComplexRegexes.pm ↑ Summary 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.156';
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 74 ▶sub default_severity { return $SEVERITY_MEDIUM }
Subroutine default_severity: called CC=1 SCAR=0.0
Pod documented
50 73 ▶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 135 ▶sub violates {
Subroutine violates: called CC=15 SCAR=40.8
Pod documented
56 135 my ($self, $elem) = @_;
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 = _get_potential_leading_operator($elem)
Branch: 1 branch
BranchTrueFalseTotal
my $leading_operator = _get_potential_leading_operator($elem) or return 17 0 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 17 0 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 = _get_token_compatibility($leading_operator);
76 17 my $next_compatibility = _get_token_compatibility($next_elem);
77  
78 return if
79 (
80 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
81 || $leading_operator_compatibility->[$operator_type]
82 )
83 && (
84 ! defined $next_compatibility
85 || $next_compatibility->[$operator_type]
86 );
87  
88 return if
89 0 ▶ $operator_type
Branch: 1 branch
BranchTrueFalseTotal
if $operator_type and defined $leading_operator_compatibility and not $leading_operator_compatibility->[$operator_type] and _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 _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 _have_stringy_x($leading_operator)
$operator_type defined $leading_operator_compatibility not $leading_operator_compatibility->[$operator_type] _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= _have_stringy_x($leading_operator)
ABCDresult
0XXX0
10XX0
110X0
11100
11111
90 && defined $leading_operator_compatibility
91 && ! $leading_operator_compatibility->[$operator_type]
92 && _have_stringy_x($leading_operator); # RT 54524
93  
94 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
95  
96 0 return $self->violation($DESC, $EXPL, $elem);
97 }
98  
99 #-----------------------------------------------------------------------------
100  
101 51 ▶sub _get_token_compatibility {
Subroutine _get_token_compatibility: called CC=4 SCAR=14.3
102 51 my ($elem) = @_;
103  
104 51 ▶ return $FILE_OPERATOR_COMPATIBILITY{ $elem->content() }
Branch: 1 branch
BranchTrueFalseTotal
if _is_file_operator($elem) 0 51 51
105 if _is_file_operator($elem);
106  
107 51 for my $class (keys %TOKEN_COMPATIBILITY) {
108 123 ▶ return $TOKEN_COMPATIBILITY{$class} if $elem->isa($class);
Branch: 1 branch
BranchTrueFalseTotal
if $elem->isa($class) 51 72 123
109 }
110  
111 0 return;
112 }
113  
114 #-----------------------------------------------------------------------------
115  
116 0 ▶sub _have_stringy_x {
Subroutine _have_stringy_x: not called CC=5 SCAR=34.0
117 0 my ($elem) = @_;
118  
119 0 ▶ return if not $elem;
Branch: 1 branch
BranchTrueFalseTotal
unless $elem 0 0 0
120  
121 0 ▶ my $prev_oper = $elem->sprevious_sibling() or return;
Branch: 1 branch
BranchTrueFalseTotal
my $prev_oper = $elem->sprevious_sibling or return 0 0 0
122  
123 0 ▶ return if not $prev_oper->isa('PPI::Token::Operator');
Branch: 1 branch
BranchTrueFalseTotal
unless $prev_oper->isa("PPI::Token::Operator") 0 0 0
124 0 ▶ return if 'x' ne $prev_oper->content();
Branch: 1 branch
BranchTrueFalseTotal
if "x" ne $prev_oper->content 0 0 0
125  
126 0 return !! $prev_oper->sprevious_sibling();
127 }
128  
129 #-----------------------------------------------------------------------------
130  
131 17 ▶sub _get_potential_leading_operator {
Subroutine _get_potential_leading_operator: called CC=5 SCAR=17.5
132 17 my ($elem) = @_;
133  
134 17 ▶ my $previous_element = $elem->sprevious_sibling() or return;
Branch: 1 branch
BranchTrueFalseTotal
my $previous_element = $elem->sprevious_sibling or return 17 0 17
135  
136 17 ▶ if ( _get_token_compatibility($previous_element) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (_get_token_compatibility($previous_element)) 17 0 17
137 17 my $previous_sibling = $previous_element->sprevious_sibling();
138 17 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if ($previous_sibling and _is_file_operator($previous_sibling)) 0 17 17
Condition66%2/3
$previous_sibling and _is_file_operator($previous_sibling)
!ll&&!rl&&r
2150
MC/DC0%0/2
$previous_sibling and _is_file_operator($previous_sibling)
$previous_sibling _is_file_operator($previous_sibling)
Truth table66%2/3
A and B
A= $previous_siblingB= _is_file_operator($previous_sibling)
ABresult
0X0
100
111
139 $previous_sibling and _is_file_operator($previous_sibling)
140 ) {
141 0 $previous_element = $previous_sibling;
142 }
143 }
144  
145 17 return $previous_element;
146 }
147  
148 #-----------------------------------------------------------------------------
149  
150 66 ▶sub _is_file_operator {
Subroutine _is_file_operator: called CC=3 SCAR=12.5
151 66 my ($elem) = @_;
152  
153 66 ▶ return if not $elem;
Branch: 1 branch
BranchTrueFalseTotal
unless $elem 0 66 66
154 66 ▶ return if not $elem->isa('PPI::Token::Operator');
Branch: 1 branch
BranchTrueFalseTotal
unless $elem->isa("PPI::Token::Operator") 66 0 66
155 0 return !! $FILE_OPERATOR_COMPATIBILITY{ $elem->content() };
156 }
157  
158 #-----------------------------------------------------------------------------
159  
160 0 ▶sub _is_special_string_number_addion {
Subroutine _is_special_string_number_addion: not called CC=9 SCAR=45.0
161 0 my ($self, $elem_operator, $element_1, $element_2, $check_recursive) = @_;
162  
163 return 1 if $elem_operator
164 && $elem_operator eq $TOKEN_COMPATIBILITY_SPECIAL_STRING_OPERATOR
165 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
166 && $element_2->isa('PPI::Token::Number')
167 && $element_2->content() == 0;
168 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
169  
170 0 return;
171 }
172  
173 40 1;
174  
175 __END__
« lib/Perl/Critic/Policy/RegularExpressions/ProhibitComplexRegexes.pm ↑ Summary lib/Perl/Critic/Policy/Variables/ProhibitPunctuationVars.pm »