File Coverage

blib/lib/Perl/Critic/Policy/Lax/RequireConstantOnLeftSideOfEquality/ExceptEq.pm
Criterion Covered Total %
statement 34 35 97.1
branch 7 10 70.0
condition 4 7 57.1
subroutine 11 12 91.6
pod 4 5 80.0
total 60 69 86.9


line stmt bran cond sub pod time code
1             package Perl::Critic::Policy::Lax::RequireConstantOnLeftSideOfEquality::ExceptEq;
2             $Perl::Critic::Policy::Lax::RequireConstantOnLeftSideOfEquality::ExceptEq::VERSION = '0.012';
3 7     7   7480 use utf8;
  7         57  
  7         28  
4 7     7   175 use strict;
  7         9  
  7         101  
5 7     7   20 use warnings;
  7         7  
  7         119  
6              
7 7     7   17 use Readonly;
  7         6  
  7         317  
8              
9 7     7   23 use Perl::Critic::Utils qw{ :severities };
  7         6  
  7         288  
10 7     7   591 use parent qw(Perl::Critic::Policy);
  7         7  
  7         31  
11              
12             Readonly::Scalar my $DESC => q{Constant value on right side of equality};
13             Readonly::Scalar my $EXPL =>
14             q{Putting the constant on the left exposes typos};
15              
16             #-----------------------------------------------------------------------------
17              
18 6     6 0 17051 sub supported_parameters { return () }
19 3     3 1 22 sub default_severity { return $SEVERITY_LOW }
20 0     0 1 0 sub default_themes { return qw(more) }
21 6     6 1 20400 sub applies_to { return qw(PPI::Token::Operator) }
22              
23             #-----------------------------------------------------------------------------
24              
25             sub violates {
26 6     6 1 90 my ( $self, $elem, undef ) = @_;
27 6 50       20 return if !( q<==> eq $elem );
28              
29 6   50     93 my $right_sib = $elem->snext_sibling() || return;
30 6   50     112 my $left_sib = $elem->sprevious_sibling() || return;
31              
32 6 100 66     98 if ( !_is_constant_like($left_sib) && _is_constant_like($right_sib) ) {
33 3         12 return $self->violation( $DESC, $EXPL, $right_sib );
34             }
35              
36 3 50       7 if($left_sib ne '1') {
37 3         39 1;
38             }
39              
40 3         7 return; # ok!
41             }
42              
43             #-----------------------------------------------------------------------------
44              
45             sub _is_constant_like {
46 9     9   11 my $elem = shift;
47 9 100       44 return 1 if $elem->isa('PPI::Token::Number');
48 3 50       13 return 1 if $elem->isa('PPI::Token::Quote');
49 3         7 return 0;
50             }
51              
52             1;
53              
54             =pod
55              
56             =encoding UTF-8
57              
58             =head1 NAME
59              
60             Perl::Critic::Policy::Lax::RequireConstantOnLeftSideOfEquality::ExceptEq - constant value on the right side is ok with 'eq'
61              
62             =head1 VERSION
63              
64             version 0.012
65              
66             =head1 DESCRIPTION
67              
68             This policy behaves like Perl::Critic::Policy::ValuesAndExpressions::RequireConstantOnLeftSideOfEquality,
69             but allows constant value on the right side of an equality with the operator 'eq'.
70              
71             =head1 NAME
72              
73             Perl::Critic::Policy::Lax::RequireConstantOnLeftSideOfEquality::ExceptEq - constant value on the right side is ok with 'eq'
74              
75             =head1 AUTHOR
76              
77             Ricardo Signes <rjbs@cpan.org>
78              
79             =head1 COPYRIGHT AND LICENSE
80              
81             This software is copyright (c) 2016 by Ricardo Signes <rjbs@cpan.org>.
82              
83             This is free software; you can redistribute it and/or modify it under
84             the same terms as the Perl 5 programming language system itself.
85              
86             =cut
87              
88             __END__
89              
90             # ABSTRACT: constant value on the right side is ok with 'eq'
91              
92             #pod =pod
93             #pod
94             #pod =encoding UTF-8
95             #pod
96             #pod =head1 NAME
97             #pod
98             #pod Perl::Critic::Policy::Lax::RequireConstantOnLeftSideOfEquality::ExceptEq - constant value on the right side is ok with 'eq'
99             #pod
100             #pod =head1 DESCRIPTION
101             #pod
102             #pod This policy behaves like Perl::Critic::Policy::ValuesAndExpressions::RequireConstantOnLeftSideOfEquality,
103             #pod but allows constant value on the right side of an equality with the operator 'eq'.
104             #pod
105             #pod =cut