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 / ControlStructures / ProhibitCascadingIfElse.pm
1 ##############################################################################
2 #      $URL: http://perlcritic.tigris.org/svn/perlcritic/trunk/Perl-Critic/lib/Perl/Critic/Policy/ControlStructures/ProhibitCascadingIfElse.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::ControlStructures::ProhibitCascadingIfElse;
9
10 use 5.006001;
11 use strict;
12 use warnings;
13 use Readonly;
14
15 use Perl::Critic::Utils qw{ :severities };
16 use base 'Perl::Critic::Policy';
17
18 our $VERSION = '1.088';
19
20 #-----------------------------------------------------------------------------
21
22 Readonly::Scalar my $DESC => q{Cascading if-elsif chain};
23 Readonly::Scalar my $EXPL => [ 117, 118 ];
24
25 #-----------------------------------------------------------------------------
26
27 sub supported_parameters {
28     return (
29         {
30             name            => 'max_elsif',
31             description     => 'The maximum number of alternatives that will be allowed.',
32             default_string  => '2',
33             behavior        => 'integer',
34             integer_minimum => 1,
35         },
36     );
37 }
38
39 sub default_severity { return $SEVERITY_MEDIUM                      }
40 sub default_themes   { return qw( core pbp maintenance complexity ) }
41 sub applies_to       { return 'PPI::Statement::Compound'            }
42
43 #-----------------------------------------------------------------------------
44
45 sub violates {
46     my ( $self, $elem, undef ) = @_;
47
48     return if ($elem->type() ne 'if');
49
50     if ( _count_elsifs($elem) > $self->{_max_elsif} ) {
51         return $self->violation( $DESC, $EXPL, $elem );
52     }
53     return;    #ok!
54 }
55
56 sub _count_elsifs {
57     my $elem = shift;
58     return
59       grep { $_->isa('PPI::Token::Word') && $_ eq 'elsif' } $elem->schildren();
60 }
61
62 1;
63
64 __END__
65
66 #-----------------------------------------------------------------------------
67
68 =pod
69
70 =for stopwords lookup
71
72 =head1 NAME
73
74 Perl::Critic::Policy::ControlStructures::ProhibitCascadingIfElse - Don't write long "if-elsif-elsif-elsif-elsif...else" chains.
75
76 =head1 AFFILIATION
77
78 This Policy is part of the core L<Perl::Critic> distribution.
79
80
81 =head1 DESCRIPTION
82
83 Long C<if-elsif> chains are hard to digest, especially if they are
84 longer than a single page or screen.  If testing for equality, use a
85 hash lookup instead.  See L<Switch> for another approach.
86
87   if ($condition1) {         #ok
88       $foo = 1;
89   }
90   elsif ($condition2) {      #ok
91       $foo = 2;
92   }
93   elsif ($condition3) {      #ok
94       $foo = 3;
95   }
96   elsif ($condition4) {      #too many!
97       $foo = 4;
98   }
99   else {                     #ok
100       $foo = $default;
101   }
102
103 =head1 CONFIGURATION
104
105 This policy can be configured with a maximum number of C<elsif> alternatives
106 to allow.  The default is 2.  This can be specified via a C<max_elsif> item in
107 the F<.perlcriticrc> file:
108
109  [ControlStructures::ProhibitCascadingIfElse]
110  max_elsif = 3
111
112 =head1 AUTHOR
113
114 Jeffrey Ryan Thalhammer <thaljef@cpan.org>
115
116 =head1 COPYRIGHT
117
118 Copyright (c) 2005-2008 Jeffrey Ryan Thalhammer.  All rights reserved.
119
120 This program is free software; you can redistribute it and/or modify
121 it under the same terms as Perl itself.  The full text of this license
122 can be found in the LICENSE file included with this module.
123
124 =cut
125
126 # Local Variables:
127 #   mode: cperl
128 #   cperl-indent-level: 4
129 #   fill-column: 78
130 #   indent-tabs-mode: nil
131 #   c-indentation-style: bsd
132 # End:
133 # ex: set ts=8 sts=4 sw=4 tw=78 ft=perl expandtab shiftround :