File Coverage

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


line stmt bran cond sub pod time code
1             package Perl::Critic::Policy::ControlStructures::ProhibitCascadingIfElse;
2              
3 40     40   26928 use 5.010001;
  40         168  
4 40     40   283 use strict;
  40         133  
  40         869  
5 40     40   249 use warnings;
  40         110  
  40         1139  
6              
7 40     40   272 use Readonly;
  40         122  
  40         2166  
8              
9 40     40   317 use Perl::Critic::Utils qw{ :severities };
  40         136  
  40         2025  
10 40     40   5225 use parent 'Perl::Critic::Policy';
  40         112  
  40         238  
11              
12             our $VERSION = '1.150';
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 90     90 0 2045 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 74     74 1 289 sub default_severity { return $SEVERITY_MEDIUM }
34 86     86 1 399 sub default_themes { return qw( core pbp maintenance complexity ) }
35 30     30 1 83 sub applies_to { return 'PPI::Statement::Compound' }
36              
37             #-----------------------------------------------------------------------------
38              
39             sub violates {
40 11     11 1 38 my ( $self, $elem, undef ) = @_;
41              
42 11 100       49 return if ($elem->type() ne 'if');
43              
44 2 50       49 if ( _count_elsifs($elem) > $self->{_max_elsif} ) {
45 0         0 return $self->violation( $DESC, $EXPL, $elem );
46             }
47 2         5 return; #ok!
48             }
49              
50             sub _count_elsifs {
51 2     2   5 my $elem = shift;
52             return
53 2 100       7 grep { $_->isa('PPI::Token::Word') && $_->content() eq 'elsif' } $elem->schildren();
  6         53  
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.
81              
82             if ($condition1) { #ok
83             $foo = 1;
84             }
85             elsif ($condition2) { #ok
86             $foo = 2;
87             }
88             elsif ($condition3) { #ok
89             $foo = 3;
90             }
91             elsif ($condition4) { #too many!
92             $foo = 4;
93             }
94             else { #ok
95             $foo = $default;
96             }
97              
98             =head1 CONFIGURATION
99              
100             This policy can be configured with a maximum number of C<elsif>
101             alternatives to allow. The default is 2. This can be specified via a
102             C<max_elsif> item in the F<.perlcriticrc> file:
103              
104             [ControlStructures::ProhibitCascadingIfElse]
105             max_elsif = 3
106              
107             =head1 AUTHOR
108              
109             Jeffrey Ryan Thalhammer <jeff@imaginative-software.com>
110              
111             =head1 COPYRIGHT
112              
113             Copyright (c) 2005-2022 Imaginative Software Systems. All rights reserved.
114              
115             This program is free software; you can redistribute it and/or modify
116             it under the same terms as Perl itself. The full text of this license
117             can be found in the LICENSE file included with this module.
118              
119             =cut
120              
121             # Local Variables:
122             # mode: cperl
123             # cperl-indent-level: 4
124             # fill-column: 78
125             # indent-tabs-mode: nil
126             # c-indentation-style: bsd
127             # End:
128             # ex: set ts=8 sts=4 sw=4 tw=78 ft=perl expandtab shiftround :