blib/lib/Perl/Critic/Policy/ControlStructures/ProhibitCascadingIfElse.pm

filter: Statementstmt 96.9% 32 / 33 Branchbran 83.3% 5 / 6 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 12 / 12 Podpod 80.0% 4 / 5 totaltotal 94.6% 53 / 56 CC 4 SCAR 4.4
CC
4
Cov
95%
violates
3.1
_count_elsifs
2.0
BEGIN
1.0
CRAP 1.6
« blib/lib/Perl/Critic/Policy/RegularExpressions/RequireLineBoundaryMatching.pm ↑ Summary blib/lib/Perl/Critic/Policy/ValuesAndExpressions/ProhibitConstantPragma.pm »
1 package Perl::Critic::Policy::ControlStructures::ProhibitCascadingIfElse;
2  
3 40 ▶use 5.010001;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 40 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 40 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 40 ▶use Readonly;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 40 ▶use Perl::Critic::Utils qw{ :severities };
Subroutine BEGIN: called CC=1 SCAR=0.0
10 40 ▶use parent 'Perl::Critic::Policy';
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 40 our $VERSION = '1.150';
13  
14 #-----------------------------------------------------------------------------
15  
16 40 Readonly::Scalar my $DESC => q{Cascading if-elsif chain};
17 40 Readonly::Scalar my $EXPL => [ 117, 118 ];
18  
19 #-----------------------------------------------------------------------------
20  
21 sub supported_parameters {
22 return (
23 {
24 91 ▶ name => 'max_elsif',
Subroutine supported_parameters: called CC=1 SCAR=0.0
Pod undocumented
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 75 ▶sub default_severity { return $SEVERITY_MEDIUM }
Subroutine default_severity: called CC=1 SCAR=0.0
Pod documented
34 86 ▶sub default_themes { return qw( core pbp maintenance complexity ) }
Subroutine default_themes: called CC=1 SCAR=0.0
Pod documented
35 31 ▶sub applies_to { return 'PPI::Statement::Compound' }
Subroutine applies_to: called CC=1 SCAR=0.0
Pod documented
36  
37 #-----------------------------------------------------------------------------
38  
39 sub violates {
40 11 ▶ my ( $self, $elem, undef ) = @_;
Subroutine violates: called CC=3 SCAR=11.3
Pod documented
41  
42 11 ▶ return if ($elem->type() ne 'if');
Branch: 1 branch
BranchTrueFalseTotal
if $elem->type ne "if" 9 2 11
43  
44 2 ▶ if ( _count_elsifs($elem) > $self->{_max_elsif} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (_count_elsifs($elem) > $self->{"_max_elsif"}) 0 2 2
45 0 return $self->violation( $DESC, $EXPL, $elem );
46 }
47 2 return; #ok!
48 }
49  
50 sub _count_elsifs {
51 2 ▶ my $elem = shift;
Subroutine _count_elsifs: called CC=2 SCAR=6.9
52 return
53 2 ▶ grep { $_->isa('PPI::Token::Word') && $_->content() eq 'elsif' } $elem->schildren();
Branch: 1 branch
BranchTrueFalseTotal
$_->isa("PPI::Token::Word") and $_->content eq "elsif" 2 4 6
54 }
55  
56 40 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 :
« blib/lib/Perl/Critic/Policy/RegularExpressions/RequireLineBoundaryMatching.pm ↑ Summary blib/lib/Perl/Critic/Policy/ValuesAndExpressions/ProhibitConstantPragma.pm »