Login
Fixed ProhibitHardTabs so that it would allow leading tabs
[gknop/Perl-Critic.git] / lib / Perl / Critic / Policy / ValuesAndExpressions / RequireConstantOnLeftSideOfEquality.pm
CommitLineData
57aab077
JRT
1##############################################################################
2# $URL: http://perlcritic.tigris.org/svn/perlcritic/trunk/Perl-Critic/lib/Perl/Critic/Policy/ValuesAndExpressions/RequireInterpolationOfMetachars.pm $
3# $Date: 2008-07-07 09:09:13 -0700 (Mon, 07 Jul 2008) $
4# $Author: clonezone $
5# $Revision: 2537 $
6##############################################################################
7
8package Perl::Critic::Policy::ValuesAndExpressions::RequireConstantOnLeftSideOfEquality;
9
10use 5.006001;
11use strict;
12use warnings;
13use Readonly;
14
15use Perl::Critic::Utils qw{ :severities };
16use base 'Perl::Critic::Policy';
17
18#-----------------------------------------------------------------------------
19
20our $VERSION = '1.090';
21
22#-----------------------------------------------------------------------------
23
24Readonly::Scalar my $DESC => q{Constant value on right side of equality};
25Readonly::Scalar my $EXPL => q{Putting the constant on the left exposes typos};
26
27#-----------------------------------------------------------------------------
28
29sub supported_parameters { return () }
30sub default_severity { return $SEVERITY_LOW }
31sub default_themes { return qw(core cosmetic) }
32sub applies_to { return qw(PPI::Token::Operator) }
33
34#-----------------------------------------------------------------------------
35
36sub violates {
37 my ( $self, $elem, undef ) = @_;
38 return if !( $elem eq q<==> || $elem eq q<eq>);
39
40 my $right_sib = $elem->snext_sibling() || return;
41 my $left_sib = $elem->sprevious_sibling() || return;
42
43 if (!_is_constant_like($left_sib) && _is_constant_like($right_sib)) {
44 return $self->violation($DESC, $EXPL, $right_sib);
45 }
46
47 return; # ok!
48}
49
50#-----------------------------------------------------------------------------
51
52sub _is_constant_like {
53 my $elem = shift;
54 return 1 if $elem->isa('PPI::Token::Number');
55 return 1 if $elem->isa('PPI::Token::Quote');
56 return 0;
57}
58
591;
60
61__END__
62
63#-----------------------------------------------------------------------------
64
65=pod
66
67=head1 NAME
68
69Perl::Critic::Policy::ValuesAndExpressions::RequireConstantOnLeftSideOfEquality - Putting the constant value on the left side of an equality exposes typos.
70
71=head1 AFFILIATION
72
73This Policy is part of the core L<Perl::Critic|Perl::Critic>
74distribution.
75
76
77=head1 DESCRIPTION
78
79This policy warns you if you put a constant value (i.e. a literal number or
80some string) on the right side of a C<==> operator when there is a variable or
81some other lvalue on the left side. In a nutshell:
82
83 if($foo == 42){} # not ok
84 if(42 == $foo){} # ok
85
86 if($foo eq 'bar'){} # not ok
87 if('bar' eq $foo){} # ok
88
89The rationale is that sometimes you might mistype C<=> instead of C<==>, and
90if you're in the habit of putting the constant value on the left side of the
91equality, then Perl will give you a compile-time warning. Perhaps this is
92best explained with an example:
93
94 if ($foo == 42){} # This is what I want it to do.
95 if ($foo = 42){} # But suppose this is what I actually type.
96 if (42 = $foo){} # If I had (mis)typed it like this, then Perl gives a warning.
97 if (42 == $foo){} # So this is what I should have attempted to type.
98
99So this Policy doesn't actually tell you if you've mistyped C<=> instead of
100C<==>. Rather, it encourages you to write your expressions in a certain way
101so that Perl can warn you when you mistyped it.
102
103The C<eq> operator is not prone to the same type of typo as the C<==>
104operator, but this Policy still treats it the same way. Therefore, the rule
105is consistently applied to all equality operators, which helps you to get into
106the habit of writing compliant expressions faster.
107
108
109=head1 CONFIGURATION
110
111This Policy is not configurable except for the standard options.
112
113
114=head1 AUTHOR
115
116Jeffrey Ryan Thalhammer <thaljef@cpan.org>
117
118
119=head1 COPYRIGHT
120
121Copyright (c) 2005-2008 Jeffrey Ryan Thalhammer. All rights reserved.
122
123This program is free software; you can redistribute it and/or modify
124it under the same terms as Perl itself. The full text of this license
125can be found in the LICENSE file included with this module.
126
127=cut
128
129# Local Variables:
130# mode: cperl
131# cperl-indent-level: 4
132# fill-column: 78
133# indent-tabs-mode: nil
134# c-indentation-style: bsd
135# End:
136# ex: set ts=8 sts=4 sw=4 tw=78 ft=perl expandtab shiftround :