Modified source files and compiled any and armel versions of packages
[pkg-perl] / deb-src / libperl-critic-perl / libperl-critic-perl-1.088 / lib / Perl / Critic / Policy / ValuesAndExpressions / ProhibitMixedBooleanOperators.pm
1 ##############################################################################
2 #      $URL: http://perlcritic.tigris.org/svn/perlcritic/trunk/Perl-Critic/lib/Perl/Critic/Policy/ValuesAndExpressions/ProhibitMixedBooleanOperators.pm $
3 #     $Date: 2008-07-03 10:19:10 -0500 (Thu, 03 Jul 2008) $
4 #   $Author: clonezone $
5 # $Revision: 2489 $
6 ##############################################################################
7
8 package Perl::Critic::Policy::ValuesAndExpressions::ProhibitMixedBooleanOperators;
9
10 use 5.006001;
11 use strict;
12 use warnings;
13 use Readonly;
14
15 use Perl::Critic::Utils qw{ :severities :data_conversion };
16 use base 'Perl::Critic::Policy';
17
18
19 #-----------------------------------------------------------------------------
20
21 our $VERSION = '1.088';
22 #-----------------------------------------------------------------------------
23
24 Readonly::Hash my %LOW_BOOLEANS  => hashify( qw( not or and ) );
25 Readonly::Hash my %HIGH_BOOLEANS => hashify( qw( ! || && ) );
26
27 Readonly::Hash my %EXEMPT_TYPES => hashify(
28     qw(
29         PPI::Statement::Block
30         PPI::Statement::Scheduled
31         PPI::Statement::Package
32         PPI::Statement::Include
33         PPI::Statement::Sub
34         PPI::Statement::Variable
35         PPI::Statement::Compound
36         PPI::Statement::Data
37         PPI::Statement::End
38     )
39 );
40
41 #-----------------------------------------------------------------------------
42
43 Readonly::Scalar my $DESC => q{Mixed high and low-precedence booleans};
44 Readonly::Scalar my $EXPL => [ 70 ];
45
46 #-----------------------------------------------------------------------------
47
48 sub supported_parameters { return ()                  }
49 sub default_severity     { return $SEVERITY_HIGH      }
50 sub default_themes       { return qw( core bugs pbp ) }
51 sub applies_to           { return 'PPI::Statement'    }
52
53 #-----------------------------------------------------------------------------
54
55 sub violates {
56
57     my ( $self, $elem, undef ) = @_;
58
59     # PPI::Statement is the ancestor of several types of PPI elements.
60     # But for this policy, we only want the ones that generally
61     # represent a single statement or expression.  There might be
62     # better ways to do this, such as scanning for a semi-colon or
63     # some other marker.
64
65     return if exists $EXEMPT_TYPES{ ref $elem };
66
67     if (    $elem->find_first(\&_low_boolean)
68          && $elem->find_first(\&_high_boolean) ) {
69
70         return $self->violation( $DESC, $EXPL, $elem );
71     }
72     return;    #ok!
73 }
74
75 #-----------------------------------------------------------------------------
76
77 sub _low_boolean {
78     my (undef, $elem) = @_;
79     return if $elem->isa('PPI::Statement');
80     $elem->isa('PPI::Token::Operator') || return 0;
81     return exists $LOW_BOOLEANS{$elem};
82 }
83
84 #-----------------------------------------------------------------------------
85
86 sub _high_boolean {
87     my (undef, $elem) = @_;
88     return if $elem->isa('PPI::Statement');
89     $elem->isa('PPI::Token::Operator') || return 0;
90     return exists $HIGH_BOOLEANS{$elem};
91 }
92
93 1;
94
95 __END__
96
97 #-----------------------------------------------------------------------------
98
99 =pod
100
101 =head1 NAME
102
103 Perl::Critic::Policy::ValuesAndExpressions::ProhibitMixedBooleanOperators - Write C< !$foo && $bar || $baz > instead of C< not $foo && $bar or $baz>.
104
105 =head1 AFFILIATION
106
107 This Policy is part of the core L<Perl::Critic> distribution.
108
109
110 =head1 DESCRIPTION
111
112 Conway advises against combining the low-precedence booleans ( C<and
113 or not> ) with the high-precedence boolean operators ( C<&& || !> )
114 in the same expression.  Unless you fully understand the differences
115 between the high and low-precedence operators, it is easy to
116 misinterpret expressions that use both.  And even if you do understand
117 them, it is not always clear if the author actually intended it.
118
119   next if not $foo || $bar;  #not ok
120   next if !$foo || $bar;     #ok
121   next if !( $foo || $bar ); #ok
122
123
124 =head1 CONFIGURATION
125
126 This Policy is not configurable except for the standard options.
127
128
129 =head1 AUTHOR
130
131 Jeffrey Ryan Thalhammer <thaljef@cpan.org>
132
133 =head1 COPYRIGHT
134
135 Copyright (c) 2005-2008 Jeffrey Ryan Thalhammer.  All rights reserved.
136
137 This program is free software; you can redistribute it and/or modify
138 it under the same terms as Perl itself.  The full text of this license
139 can be found in the LICENSE file included with this module.
140
141 =cut
142
143 # Local Variables:
144 #   mode: cperl
145 #   cperl-indent-level: 4
146 #   fill-column: 78
147 #   indent-tabs-mode: nil
148 #   c-indentation-style: bsd
149 # End:
150 # ex: set ts=8 sts=4 sw=4 tw=78 ft=perl expandtab shiftround :