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   27138 use 5.010001;
  40         197  
4 40     40   319 use strict;
  40         109  
  40         887  
5 40     40   231 use warnings;
  40         108  
  40         1080  
6              
7 40     40   253 use Readonly;
  40         157  
  40         2181  
8              
9 40     40   293 use Perl::Critic::Utils qw{ :severities };
  40         109  
  40         1985  
10 40     40   5249 use parent 'Perl::Critic::Policy';
  40         111  
  40         275  
11              
12             our $VERSION = '1.148';
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 2095 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 352 sub default_severity { return $SEVERITY_MEDIUM }
34 86     86 1 420 sub default_themes { return qw( core pbp maintenance complexity ) }
35 33     33 1 101 sub applies_to { return 'PPI::Statement::Compound' }
36              
37             #-----------------------------------------------------------------------------
38              
39             sub violates {
40 17     17 1 78 my ( $self, $elem, undef ) = @_;
41              
42 17 100       61 return if ($elem->type() ne 'if');
43              
44 7 100       239 if ( _count_elsifs($elem) > $self->{_max_elsif} ) {
45 2         13 return $self->violation( $DESC, $EXPL, $elem );
46             }
47 5         32 return; #ok!
48             }
49              
50             sub _count_elsifs {
51 7     7   15 my $elem = shift;
52             return
53 7 100       24 grep { $_->isa('PPI::Token::Word') && $_->content() eq 'elsif' } $elem->schildren();
  50         408  
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 :