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) $
6 ##############################################################################
8 package Perl::Critic::Policy::ControlStructures::ProhibitCascadingIfElse;
15 use Perl::Critic::Utils qw{ :severities };
16 use base 'Perl::Critic::Policy';
18 our $VERSION = '1.088';
20 #-----------------------------------------------------------------------------
22 Readonly::Scalar my $DESC => q{Cascading if-elsif chain};
23 Readonly::Scalar my $EXPL => [ 117, 118 ];
25 #-----------------------------------------------------------------------------
27 sub supported_parameters {
31 description => 'The maximum number of alternatives that will be allowed.',
32 default_string => '2',
33 behavior => 'integer',
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' }
43 #-----------------------------------------------------------------------------
46 my ( $self, $elem, undef ) = @_;
48 return if ($elem->type() ne 'if');
50 if ( _count_elsifs($elem) > $self->{_max_elsif} ) {
51 return $self->violation( $DESC, $EXPL, $elem );
59 grep { $_->isa('PPI::Token::Word') && $_ eq 'elsif' } $elem->schildren();
66 #-----------------------------------------------------------------------------
74 Perl::Critic::Policy::ControlStructures::ProhibitCascadingIfElse - Don't write long "if-elsif-elsif-elsif-elsif...else" chains.
78 This Policy is part of the core L<Perl::Critic> distribution.
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.
87 if ($condition1) { #ok
90 elsif ($condition2) { #ok
93 elsif ($condition3) { #ok
96 elsif ($condition4) { #too many!
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:
109 [ControlStructures::ProhibitCascadingIfElse]
114 Jeffrey Ryan Thalhammer <thaljef@cpan.org>
118 Copyright (c) 2005-2008 Jeffrey Ryan Thalhammer. All rights reserved.
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.
128 # cperl-indent-level: 4
130 # indent-tabs-mode: nil
131 # c-indentation-style: bsd
133 # ex: set ts=8 sts=4 sw=4 tw=78 ft=perl expandtab shiftround :