File Coverage

blib/lib/Perl/Critic/Policy/ControlStructures/ProhibitCascadingIfElse.pm
Criterion Covered Total %
statement 29 29 100.0
branch 6 6 100.0
condition n/a
subroutine 12 12 100.0
pod 4 5 80.0
total 51 52 98.0


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