File Coverage

blib/lib/Perl/Critic/Policy/ControlStructures/ProhibitDeepNests.pm
Criterion Covered Total %
statement 30 30 100.0
branch 4 4 100.0
condition n/a
subroutine 11 11 100.0
pod 4 5 80.0
total 49 50 98.0


line stmt bran cond sub pod time code
1             package Perl::Critic::Policy::ControlStructures::ProhibitDeepNests;
2              
3 40     40   26303 use 5.010001;
  40         168  
4 40     40   283 use strict;
  40         111  
  40         913  
5 40     40   233 use warnings;
  40         103  
  40         1090  
6 40     40   236 use Readonly;
  40         113  
  40         2251  
7              
8 40     40   332 use Perl::Critic::Utils qw{ :severities };
  40         106  
  40         2144  
9 40     40   5349 use parent 'Perl::Critic::Policy';
  40         116  
  40         256  
10              
11             our $VERSION = '1.148';
12              
13             #-----------------------------------------------------------------------------
14              
15             Readonly::Scalar my $DESC => q{Code structure is deeply nested};
16             Readonly::Scalar my $EXPL => q{Consider refactoring};
17              
18             #-----------------------------------------------------------------------------
19              
20             sub supported_parameters {
21             return (
22             {
23 97     97 0 2087 name => 'max_nests',
24             description => 'The maximum number of nested constructs to allow.',
25             default_string => '5',
26             behavior => 'integer',
27             integer_minimum => 1,
28             },
29             );
30             }
31              
32 77     77 1 381 sub default_severity { return $SEVERITY_MEDIUM }
33 74     74 1 346 sub default_themes { return qw(core maintenance complexity) }
34 37     37 1 128 sub applies_to { return 'PPI::Statement::Compound' }
35              
36             #-----------------------------------------------------------------------------
37              
38             sub violates {
39 51     51 1 125 my ( $self, $elem, undef ) = @_;
40              
41 51         93 my $nest_count = 1; #For _this_ element
42 51         119 my $parent = $elem;
43              
44 51         135 while ( $parent = $parent->parent() ){
45 203 100       1232 if( $parent->isa('PPI::Statement::Compound') ) {
46 76         144 $nest_count++;
47             }
48             }
49              
50 51 100       260 if ( $nest_count > $self->{_max_nests} ) {
51 3         19 return $self->violation( $DESC, $EXPL, $elem );
52             }
53 48         136 return; #ok!
54             }
55              
56              
57             1;
58              
59             __END__
60              
61              
62             #-----------------------------------------------------------------------------
63              
64             =pod
65              
66             =for stopwords refactored
67              
68             =head1 NAME
69              
70             Perl::Critic::Policy::ControlStructures::ProhibitDeepNests - Don't write deeply nested loops and conditionals.
71              
72             =head1 AFFILIATION
73              
74             This Policy is part of the core L<Perl::Critic|Perl::Critic>
75             distribution.
76              
77              
78             =head1 DESCRIPTION
79              
80             Deeply nested code is often hard to understand and may be a sign that
81             it needs to be refactored. There are several good books on how to
82             refactor code. I like Martin Fowler's "Refactoring: Improving The
83             Design of Existing Code".
84              
85              
86             =head1 CONFIGURATION
87              
88             The maximum number of nested control structures can be configured via
89             a value for C<max_nests> in a F<.perlcriticrc> file. Each for-loop,
90             if-else, while, and until block is counted as one nest. Postfix forms
91             of these constructs are not counted. The default maximum is 5.
92             Customization in a F<.perlcriticrc> file looks like this:
93              
94             [ControlStructures::ProhibitDeepNests]
95             max_nests = 3
96              
97             =head1 AUTHOR
98              
99             Jeffrey Ryan Thalhammer <jeff@imaginative-software.com>
100              
101             =head1 COPYRIGHT
102              
103             Copyright (c) 2005-2011 Imaginative Software Systems. All rights reserved.
104              
105             This program is free software; you can redistribute it and/or modify
106             it under the same terms as Perl itself. The full text of this license
107             can be found in the LICENSE file included with this module.
108              
109             =cut
110              
111             # Local Variables:
112             # mode: cperl
113             # cperl-indent-level: 4
114             # fill-column: 78
115             # indent-tabs-mode: nil
116             # c-indentation-style: bsd
117             # End:
118             # ex: set ts=8 sts=4 sw=4 tw=78 ft=perl expandtab shiftround :