blib/lib/Perl/Critic/Policy/ControlStructures/ProhibitNegativeExpressionsInUnlessAndUntilConditions.pm

filter: Statementstmt 90.3% 56 / 62 Branchbran 65.3% 17 / 26 Conditioncond 83.3% 5 / 6 MC/DCmcdc 50.0% 2 / 4 Subroutinesub 93.3% 14 / 15 Podpod 80.0% 4 / 5 totaltotal 83.0% 98 / 118 CC 17 SCAR 14.6
CC
17
Cov
83%
_get_condition_elements
7.1
violates
6.7
_get_negative_operators
5.6
CRAP 4.3
« blib/lib/Perl/Critic/Policy/CodeLayout/ProhibitHardTabs.pm ↑ Summary Build »
1 package Perl::Critic::Policy::ControlStructures::ProhibitNegativeExpressionsInUnlessAndUntilConditions;
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< :characters :severities :classification hashify >;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 40 ▶use parent 'Perl::Critic::Policy';
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 40 our $VERSION = '1.150';
13  
14 #-----------------------------------------------------------------------------
15  
16 40 Readonly::Scalar my $EXPL => [99];
17  
18 #-----------------------------------------------------------------------------
19  
20 90 ▶sub supported_parameters { return qw< > }
Subroutine supported_parameters: called CC=1 SCAR=0.0
Pod undocumented
21 75 ▶sub default_severity { return $SEVERITY_MEDIUM }
Subroutine default_severity: called CC=1 SCAR=0.0
Pod documented
22 86 ▶sub default_themes { return qw( core maintenance pbp ) }
Subroutine default_themes: called CC=1 SCAR=0.0
Pod documented
23 31 ▶sub applies_to { return 'PPI::Token::Word' }
Subroutine applies_to: called CC=1 SCAR=0.0
Pod documented
24  
25 #-----------------------------------------------------------------------------
26  
27 sub violates {
28 332 ▶ my ( $self, $token, undef ) = @_;
Subroutine violates: called CC=6 SCAR=19.0
Pod documented
29  
30 332 state $until_or_unless = { hashify( qw( until unless ) ) };
31 332 ▶ return if !exists $until_or_unless->{$token->content};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $$until_or_unless{$token->content} 314 18 332
32  
33 18 ▶ return if is_hash_key($token);
Branch: 1 branch
BranchTrueFalseTotal
if is_hash_key($token) 0 18 18
34 18 ▶ return if is_subroutine_name($token);
Branch: 1 branch
BranchTrueFalseTotal
if is_subroutine_name($token) 0 18 18
35 18 ▶ return if is_method_call($token);
Branch: 1 branch
BranchTrueFalseTotal
if is_method_call($token) 0 18 18
36 18 ▶ return if is_included_module_name($token);
Branch: 1 branch
BranchTrueFalseTotal
if is_included_module_name($token) 0 18 18
37  
38 return
39 map
40 18 { $self->_violation_for_operator( $_, $token ) }
41 _get_negative_operators( $token );
42 }
43  
44 #-----------------------------------------------------------------------------
45  
46 sub _get_negative_operators {
47 18 ▶ my ($token) = @_;
Subroutine _get_negative_operators: called CC=5 SCAR=17.3
48  
49 18 my @operators;
50 18 foreach my $element ( _get_condition_elements($token) ) {
51 48 ▶ if ( $element->isa('PPI::Node') ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($element->isa('PPI::Node')) { } 2 46 48
52 2 my $operators = $element->find( \&_is_negative_operator );
53 2 ▶ if ($operators) {
Branch: 1 branch
BranchTrueFalseTotal
if ($operators) 0 2 2
54 0 push @operators, @{$operators};
55 }
56 }
57 else {
58 46 ▶ if ( _is_negative_operator( undef, $element ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (_is_negative_operator(undef, $element)) 0 46 46
59 0 push @operators, $element;
60 }
61 }
62 }
63  
64 18 return @operators;
65 }
66  
67 #-----------------------------------------------------------------------------
68  
69 sub _get_condition_elements {
70 18 ▶ my ($token) = @_;
Subroutine _get_condition_elements: called CC=7 SCAR=19.6
71  
72 18 my $statement = $token->statement();
73 18 ▶ return if not $statement;
Branch: 1 branch
BranchTrueFalseTotal
unless $statement 0 18 18
74  
75 18 ▶ if ($statement->isa('PPI::Statement::Compound')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($statement->isa("PPI::Statement::Compound")) 2 16 18
76 2 my $condition = $token->snext_sibling();
77  
78 2 ▶ return if not $condition;
Branch: 1 branch
BranchTrueFalseTotal
unless $condition 0 2 2
79 2 ▶ return if not $condition->isa('PPI::Structure::Condition');
Branch: 1 branch
BranchTrueFalseTotal
unless $condition->isa("PPI::Structure::Condition") 0 2 2
80  
81 2 return ( $condition );
82 }
83  
84 16 my @condition_elements;
85 16 my $element = $token;
86 16 ▶ while (
Branch: 1 branch
BranchTrueFalseTotal
if ($element = $element->snext_sibling and $element->content ne $Perl::Critic::Policy::ControlStructures::ProhibitNegativeExpressionsInUnlessAndUntilConditions::SCOLON) 46 16 62
Condition100%3/3
$element = $element->snext_sibling and $element->content ne $Perl::Critic::Policy::ControlStructures::ProhibitNegativeExpressionsInUnlessAndUntilConditions::SCOLON
!ll&&!rl&&r
11546
MC/DC100%2/2
$element = $element->snext_sibling and $element->content ne $Perl::Critic::Policy::ControlStructures::ProhibitNegativeExpressionsInUnlessAndUntilConditions::SCOLON
$element = $element->snext_sibling $element->content ne $Perl::Critic::Policy::ControlStructures::ProhibitNegativeExpressionsInUnlessAndUntilConditions::SCOLON
Truth table100%3/3
A and B
A= $element = $element->snext_siblingB= $element->content ne $Perl::Critic::Policy::ControlStructures::ProhibitNegativeExpressionsInUnlessAndUntilConditions::SCOLON
ABresult
0X0
100
111
87 $element = $element->snext_sibling()
88 and $element->content() ne $SCOLON
89 ) {
90 46 push @condition_elements, $element;
91 }
92  
93 16 return @condition_elements;
94 }
95  
96 #-----------------------------------------------------------------------------
97  
98 40 Readonly::Hash my %NEGATIVE_OPERATORS => hashify(
99 qw/
100 ! not
101 !~ ne !=
102 < > <= >= <=>
103 lt gt le ge cmp
104 /
105 );
106  
107 sub _is_negative_operator {
108 64 ▶ my (undef, $element) = @_;
Subroutine _is_negative_operator: called CC=2 SCAR=7.1
109  
110 return
111 $element->isa('PPI::Token::Operator')
112 64 ▶ && $NEGATIVE_OPERATORS{$element};
Condition66%2/3
$element->isa('PPI::Token::Operator') && $NEGATIVE_OPERATORS{$element}
!ll&&!rl&&r
47170
MC/DC0%0/2
$element->isa('PPI::Token::Operator') && $NEGATIVE_OPERATORS{$element}
$element->isa('PPI::Token::Operator') $NEGATIVE_OPERATORS{$element}
Truth table66%2/3
A && B
A= $element->isa('PPI::Token::Operator')B= $NEGATIVE_OPERATORS{$element}
ABresult
0X0
100
111
113 }
114  
115 #-----------------------------------------------------------------------------
116  
117 sub _violation_for_operator {
118 0 ▶ my ($self, $operator, $control_structure) = @_;
Subroutine _violation_for_operator: not called CC=1 SCAR=6.9
119  
120 return
121 0 $self->violation(
122 qq<Found "$operator" in condition for an "$control_structure">,
123 $EXPL,
124 $control_structure,
125 );
126 }
127  
128 40 1;
129  
130 #-----------------------------------------------------------------------------
131  
132 __END__
133  
134 =pod
135  
136 =for stopwords
137  
138 =head1 NAME
139  
140 Perl::Critic::Policy::ControlStructures::ProhibitNegativeExpressionsInUnlessAndUntilConditions - Don't use operators like C<not>, C<!~>, and C<le> within C<until> and C<unless>.
141  
142 =head1 AFFILIATION
143  
144 This Policy is part of the core L<Perl::Critic|Perl::Critic>
145 distribution.
146  
147  
148 =head1 DESCRIPTION
149  
150 until ($foo ne 'blah') { #not ok
151 ...
152 }
153  
154 while ($foo eq 'blah') { #ok
155 ...
156 }
157  
158 A number of people have problems figuring out the meaning of doubly
159 negated expressions. C<unless> and C<until> are both negative
160 constructs, so any negative (e.g. C<!~>) or reversible operators (e.g.
161 C<le>) included in their conditional expressions are double negations.
162 Conway considers the following operators to be difficult to understand
163 within C<unless> and C<until>:
164  
165 ! not
166 !~ ne !=
167 < > <= >= <=>
168 lt gt le ge cmp
169  
170  
171  
172 =head1 CONFIGURATION
173  
174 This Policy is not configurable except for the standard options.
175  
176  
177 =head1 SEE ALSO
178  
179 L<Perl::Critic::Policy::ControlStructures::ProhibitUnlessBlocks|Perl::Critic::Policy::ControlStructures::ProhibitUnlessBlocks>
180  
181 =head1 AUTHOR
182  
183 Elliot Shank C<< <perl@galumph.com> >>
184  
185 =head1 COPYRIGHT
186  
187 Copyright (c) 2007-2023 Elliot Shank
188  
189 This program is free software; you can redistribute it and/or modify
190 it under the same terms as Perl itself. The full text of this license
191 can be found in the LICENSE file included with this module.
192  
193 =cut
194  
195 # Local Variables:
196 # mode: cperl
197 # cperl-indent-level: 4
198 # fill-column: 78
199 # indent-tabs-mode: nil
200 # c-indentation-style: bsd
201 # End:
202 # ex: set ts=8 sts=4 sw=4 tw=78 ft=perl expandtab shiftround :
« blib/lib/Perl/Critic/Policy/CodeLayout/ProhibitHardTabs.pm ↑ Summary Build »