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

filter: Statementstmt 89.7% 35 / 39 Branchbran 50.0% 4 / 8 Conditioncond 33.3% 3 / 9 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 92.3% 12 / 13 Podpod 80.0% 4 / 5 totaltotal 72.5% 58 / 80 CC 8 SCAR 13.1
CC
8
Cov
75%
_high_boolean
12.0
violates
4.6
_low_boolean
3.1
CRAP 3.7
« blib/lib/Perl/Critic/Policy/ClassHierarchies/ProhibitOneArgBless.pm ↑ Summary blib/lib/Perl/Critic/Policy/ValuesAndExpressions/ProhibitNoisyQuotes.pm »
1 package Perl::Critic::Policy::ValuesAndExpressions::ProhibitMixedBooleanOperators;
2  
3 40 ▶use 5.010001;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 40 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 40 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 40 ▶use Readonly;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 40 ▶use Perl::Critic::Utils qw{ :severities :data_conversion };
Subroutine BEGIN: called CC=1 SCAR=0.0
9 40 ▶use parent 'Perl::Critic::Policy';
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11  
12 #-----------------------------------------------------------------------------
13  
14 40 our $VERSION = '1.150';
15  
16 #-----------------------------------------------------------------------------
17  
18 40 Readonly::Hash my %LOW_BOOLEANS => hashify( qw( not or and ) );
19 40 Readonly::Hash my %HIGH_BOOLEANS => hashify( qw( ! || && ||= &&= //=) );
20  
21 40 Readonly::Hash my %EXEMPT_TYPES => hashify(
22 qw(
23 PPI::Statement::Block
24 PPI::Statement::Scheduled
25 PPI::Statement::Package
26 PPI::Statement::Include
27 PPI::Statement::Sub
28 PPI::Statement::Variable
29 PPI::Statement::Compound
30 PPI::Statement::Data
31 PPI::Statement::End
32 )
33 );
34  
35 #-----------------------------------------------------------------------------
36  
37 40 Readonly::Scalar my $DESC => q{Mixed high and low-precedence booleans};
38 40 Readonly::Scalar my $EXPL => [ 70 ];
39  
40 #-----------------------------------------------------------------------------
41  
42 90 ▶sub supported_parameters { return () }
Subroutine supported_parameters: called CC=1 SCAR=0.0
Pod undocumented
43 75 ▶sub default_severity { return $SEVERITY_HIGH }
Subroutine default_severity: called CC=1 SCAR=0.0
Pod documented
44 92 ▶sub default_themes { return qw( core bugs pbp certrec ) }
Subroutine default_themes: called CC=1 SCAR=0.0
Pod documented
45 33 ▶sub applies_to { return 'PPI::Statement' }
Subroutine applies_to: called CC=1 SCAR=0.0
Pod documented
46  
47 #-----------------------------------------------------------------------------
48  
49 sub violates {
50  
51 296 ▶ my ( $self, $elem, undef ) = @_;
Subroutine violates: called CC=4 SCAR=15.2
Pod documented
52  
53 # PPI::Statement is the ancestor of several types of PPI elements.
54 # But for this policy, we only want the ones that generally
55 # represent a single statement or expression. There might be
56 # better ways to do this, such as scanning for a semi-colon or
57 # some other marker.
58  
59 296 ▶ return if exists $EXEMPT_TYPES{ ref $elem };
Branch: 1 branch
BranchTrueFalseTotal
if exists $EXEMPT_TYPES{ref $elem} 197 99 296
60  
61 99 ▶ if ( $elem->find_first(\&_low_boolean)
Branch: 1 branch
BranchTrueFalseTotal
if ($elem->find_first(\&Perl::Critic::Policy::ValuesAndExpressions::ProhibitMixedBooleanOperators::_low_boolean) and $elem->find_first(\&Perl::Critic::Policy::ValuesAndExpressions::ProhibitMixedBooleanOperators::_high_boolean)) 0 99 99
Condition33%1/3
$elem->find_first(\&Perl::Critic::Policy::ValuesAndExpressions::ProhibitMixedBooleanOperators::_low_boolean) and $elem->find_first(\&Perl::Critic::Policy::ValuesAndExpressions::ProhibitMixedBooleanOperators::_high_boolean)
!ll&&!rl&&r
9900
MC/DC0%0/2
$elem->find_first(\&Perl::Critic::Policy::ValuesAndExpressions::ProhibitMixedBooleanOperators::_low_boolean) and $elem->find_first(\&Perl::Critic::Policy::ValuesAndExpressions::ProhibitMixedBooleanOperators::_high_boolean)
$elem->find_first(\&Perl::Critic::Policy::ValuesAndExpressions::ProhibitMixedBooleanOperators::_low_boolean) $elem->find_first(\&Perl::Critic::Policy::ValuesAndExpressions::ProhibitMixedBooleanOperators::_high_boolean)
Truth table33%1/3
A and B
A= $elem->find_first(\&Perl::Critic::Policy::ValuesAndExpressions::ProhibitMixedBooleanOperators::_low_boolean)B= $elem->find_first(\&Perl::Critic::Policy::ValuesAndExpressions::ProhibitMixedBooleanOperators::_high_boolean)
ABresult
0X0
100
111
62 && $elem->find_first(\&_high_boolean) ) {
63  
64 0 return $self->violation( $DESC, $EXPL, $elem );
65 }
66 99 return; #ok!
67 }
68  
69 #-----------------------------------------------------------------------------
70  
71 sub _low_boolean {
72 535 ▶ my (undef, $elem) = @_;
Subroutine _low_boolean: called CC=3 SCAR=11.4
73 535 ▶ return if $elem->isa('PPI::Statement');
Branch: 1 branch
BranchTrueFalseTotal
if $elem->isa("PPI::Statement") 0 535 535
74  
75 return
76 $elem->isa('PPI::Token::Operator')
77 535 ▶ && exists $LOW_BOOLEANS{$elem};
Condition66%2/3
$elem->isa('PPI::Token::Operator') && exists $LOW_BOOLEANS{$elem}
!ll&&!rl&&r
485500
MC/DC0%0/2
$elem->isa('PPI::Token::Operator') && exists $LOW_BOOLEANS{$elem}
$elem->isa('PPI::Token::Operator') exists $LOW_BOOLEANS{$elem}
Truth table66%2/3
A && B
A= $elem->isa('PPI::Token::Operator')B= exists $LOW_BOOLEANS{$elem}
ABresult
0X0
100
111
78 }
79  
80 #-----------------------------------------------------------------------------
81  
82 sub _high_boolean {
83 0 ▶ my (undef, $elem) = @_;
Subroutine _high_boolean: not called CC=3 SCAR=24.8
84 0 ▶ return if $elem->isa('PPI::Statement');
Branch: 1 branch
BranchTrueFalseTotal
if $elem->isa("PPI::Statement") 0 0 0
85  
86 return
87 $elem->isa('PPI::Token::Operator')
88 0 ▶ && exists $HIGH_BOOLEANS{$elem};
Condition0%0/3
$elem->isa('PPI::Token::Operator') && exists $HIGH_BOOLEANS{$elem}
!ll&&!rl&&r
000
MC/DC0%0/2
$elem->isa('PPI::Token::Operator') && exists $HIGH_BOOLEANS{$elem}
$elem->isa('PPI::Token::Operator') exists $HIGH_BOOLEANS{$elem}
Truth table0%0/3
A && B
A= $elem->isa('PPI::Token::Operator')B= exists $HIGH_BOOLEANS{$elem}
ABresult
0X0
100
111
89 }
90  
91 40 1;
92  
93 __END__
94  
95 #-----------------------------------------------------------------------------
96  
97 =pod
98  
99 =head1 NAME
100  
101 Perl::Critic::Policy::ValuesAndExpressions::ProhibitMixedBooleanOperators - Write C< !$foo && $bar || $baz > instead of C< not $foo && $bar or $baz>.
102  
103 =head1 AFFILIATION
104  
105 This Policy is part of the core L<Perl::Critic|Perl::Critic>
106 distribution.
107  
108  
109 =head1 DESCRIPTION
110  
111 Conway advises against combining the low-precedence booleans ( C<and
112 or not> ) with the high-precedence boolean operators ( C<&& || !> ) in
113 the same expression. Unless you fully understand the differences
114 between the high and low-precedence operators, it is easy to
115 misinterpret expressions that use both. And even if you do understand
116 them, it is not always clear if the author actually intended it.
117  
118 next if not $foo || $bar; #not ok
119 next if !$foo || $bar; #ok
120 next if !( $foo || $bar ); #ok
121  
122  
123 =head1 CONFIGURATION
124  
125 This Policy is not configurable except for the standard options.
126  
127  
128 =head1 AUTHOR
129  
130 Jeffrey Ryan Thalhammer <jeff@imaginative-software.com>
131  
132 =head1 COPYRIGHT
133  
134 Copyright (c) 2005-2023 Imaginative Software Systems
135  
136 This program is free software; you can redistribute it and/or modify
137 it under the same terms as Perl itself. The full text of this license
138 can be found in the LICENSE file included with this module.
139  
140 =cut
141  
142 # Local Variables:
143 # mode: cperl
144 # cperl-indent-level: 4
145 # fill-column: 78
146 # indent-tabs-mode: nil
147 # c-indentation-style: bsd
148 # End:
149 # ex: set ts=8 sts=4 sw=4 tw=78 ft=perl expandtab shiftround :
« blib/lib/Perl/Critic/Policy/ClassHierarchies/ProhibitOneArgBless.pm ↑ Summary blib/lib/Perl/Critic/Policy/ValuesAndExpressions/ProhibitNoisyQuotes.pm »