1 ##############################################################################
2 # $URL: http://perlcritic.tigris.org/svn/perlcritic/trunk/Perl-Critic/lib/Perl/Critic/Policy/ControlStructures/ProhibitNegativeExpressionsInUnlessAndUntilConditions.pm $
3 # $Date: 2008-07-03 10:19:10 -0500 (Thu, 03 Jul 2008) $
6 ##############################################################################
8 package Perl::Critic::Policy::ControlStructures::ProhibitNegativeExpressionsInUnlessAndUntilConditions;
13 use English qw(-no_match_vars);
16 use Perl::Critic::Utils qw< :characters :severities :classification hashify >;
18 use base 'Perl::Critic::Policy';
20 our $VERSION = '1.088';
22 #-----------------------------------------------------------------------------
24 Readonly::Scalar my $EXPL => [99];
26 #-----------------------------------------------------------------------------
28 sub supported_parameters { return qw< > }
29 sub default_severity { return $SEVERITY_MEDIUM }
30 sub default_themes { return qw( core maintenance ) }
31 sub applies_to { return 'PPI::Token::Word' }
33 #-----------------------------------------------------------------------------
36 my ( $self, $token, undef ) = @_;
38 return if $token ne 'until' && $token ne 'unless';
40 return if is_hash_key($token);
41 return if is_subroutine_name($token);
42 return if is_method_call($token);
43 return if is_included_module_name($token);
47 { $self->_violation_for_operator( $_, $token ) }
48 _get_negative_operators( $token );
51 #-----------------------------------------------------------------------------
53 sub _get_negative_operators {
57 foreach my $element ( _get_condition_elements($token) ) {
58 if ( $element->isa('PPI::Node') ) {
59 my $operators = $element->find( \&_is_negative_operator );
61 push @operators, @{$operators};
65 if ( _is_negative_operator( undef, $element ) ) {
66 push @operators, $element;
74 #-----------------------------------------------------------------------------
76 sub _get_condition_elements {
79 my $statement = $token->statement();
80 return if not $statement;
82 if ($statement->isa('PPI::Statement::Compound')) {
83 my $condition = $token->snext_sibling();
85 return if not $condition;
86 return if not $condition->isa('PPI::Structure::Condition');
88 return ( $condition );
91 my @condition_elements;
94 $element = $element->snext_sibling()
95 and $element ne $SCOLON
97 push @condition_elements, $element;
100 return @condition_elements;
103 #-----------------------------------------------------------------------------
105 Readonly::Hash my %NEGATIVE_OPERATORS => hashify(
114 sub _is_negative_operator {
115 my (undef, $element) = @_;
118 $element->isa('PPI::Token::Operator')
119 && $NEGATIVE_OPERATORS{$element};
122 #-----------------------------------------------------------------------------
124 sub _violation_for_operator {
125 my ($self, $operator, $control_structure) = @_;
129 qq<Found "$operator" in condition for an "$control_structure">,
137 #-----------------------------------------------------------------------------
147 Perl::Critic::Policy::ControlStructures::ProhibitNegativeExpressionsInUnlessAndUntilConditions - Don't use operators like C<not>, C<!~>, and C<le> within C<until> and C<unless>.
151 This Policy is part of the core L<Perl::Critic> distribution.
156 until ($foo ne 'blah') { #not ok
160 while ($foo eq 'blah') { #ok
164 A number of people have problems figuring out the meaning of doubly
165 negated expressions. C<unless> and C<until> are both negative
166 constructs, so any negative (e.g. C<!~>) or reversible operators (e.g.
167 C<le>) included in their conditional expressions are double negations.
168 Conway considers the following operators to be difficult to understand
169 within C<unless> and C<until>:
180 This Policy is not configurable except for the standard options.
185 L<Perl::Critic::Policy::ControlStructures::ProhibitUnlessBlocks>
189 Elliot Shank C<< <perl@galumph.com> >>
193 Copyright (c) 2007-2008 Elliot Shank. All rights reserved.
195 This program is free software; you can redistribute it and/or modify
196 it under the same terms as Perl itself. The full text of this license
197 can be found in the LICENSE file included with this module.
203 # cperl-indent-level: 4
205 # indent-tabs-mode: nil
206 # c-indentation-style: bsd
208 # ex: set ts=8 sts=4 sw=4 tw=78 ft=perl expandtab shiftround :