File Coverage

blib/lib/Perl/Critic/Policy/ControlStructures/ProhibitDeepNests.pm
Criterion Covered Total %
statement 28 30 93.3
branch 2 4 50.0
condition n/a
subroutine 11 11 100.0
pod 4 5 80.0
total 45 50 90.0


line stmt bran cond sub pod time code
1             package Perl::Critic::Policy::ControlStructures::ProhibitDeepNests;
2              
3 40     40   26246 use 5.010001;
  40         172  
4 40     40   253 use strict;
  40         136  
  40         928  
5 40     40   217 use warnings;
  40         137  
  40         1205  
6 40     40   288 use Readonly;
  40         153  
  40         2238  
7              
8 40     40   311 use Perl::Critic::Utils qw{ :severities };
  40         111  
  40         1966  
9 40     40   5148 use parent 'Perl::Critic::Policy';
  40         139  
  40         275  
10              
11             our $VERSION = '1.150';
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 90     90 0 1972 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 74     74 1 314 sub default_severity { return $SEVERITY_MEDIUM }
33 74     74 1 369 sub default_themes { return qw(core maintenance complexity) }
34 30     30 1 89 sub applies_to { return 'PPI::Statement::Compound' }
35              
36             #-----------------------------------------------------------------------------
37              
38             sub violates {
39 11     11 1 43 my ( $self, $elem, undef ) = @_;
40              
41 11         31 my $nest_count = 1; #For _this_ element
42 11         18 my $parent = $elem;
43              
44 11         47 while ( $parent = $parent->parent() ){
45 11 50       126 if( $parent->isa('PPI::Statement::Compound') ) {
46 0         0 $nest_count++;
47             }
48             }
49              
50 11 50       80 if ( $nest_count > $self->{_max_nests} ) {
51 0         0 return $self->violation( $DESC, $EXPL, $elem );
52             }
53 11         31 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 :