1package Perl::Critic::Policy::ControlStructures::ProhibitDeepNests;
2
3use 5.006001;
4use strict;
5use warnings;
6use Readonly;
7
8use Perl::Critic::Utils qw{ :severities };
9use base 'Perl::Critic::Policy';
10
11our $VERSION = '1.140';
12
13#-----------------------------------------------------------------------------
14
15Readonly::Scalar my $DESC => q{Code structure is deeply nested};
16Readonly::Scalar my $EXPL => q{Consider refactoring};
17
18#-----------------------------------------------------------------------------
19
20sub supported_parameters {
21    return (
22        {
23            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
32sub default_severity { return $SEVERITY_MEDIUM                }
33sub default_themes   { return qw(core maintenance complexity) }
34sub applies_to       { return 'PPI::Statement::Compound'      }
35
36#-----------------------------------------------------------------------------
37
38sub violates {
39    my ( $self, $elem, undef ) = @_;
40
41    my $nest_count = 1;  #For _this_ element
42    my $parent = $elem;
43
44    while ( $parent = $parent->parent() ){
45        if( $parent->isa('PPI::Statement::Compound') ) {
46            $nest_count++;
47        }
48    }
49
50    if ( $nest_count > $self->{_max_nests} ) {
51        return $self->violation( $DESC, $EXPL, $elem );
52    }
53    return;    #ok!
54}
55
56
571;
58
59__END__
60
61
62#-----------------------------------------------------------------------------
63
64=pod
65
66=for stopwords refactored
67
68=head1 NAME
69
70Perl::Critic::Policy::ControlStructures::ProhibitDeepNests - Don't write deeply nested loops and conditionals.
71
72=head1 AFFILIATION
73
74This Policy is part of the core L<Perl::Critic|Perl::Critic>
75distribution.
76
77
78=head1 DESCRIPTION
79
80Deeply nested code is often hard to understand and may be a sign that
81it needs to be refactored.  There are several good books on how to
82refactor code.  I like Martin Fowler's "Refactoring: Improving The
83Design of Existing Code".
84
85
86=head1 CONFIGURATION
87
88The maximum number of nested control structures can be configured via
89a value for C<max_nests> in a F<.perlcriticrc> file.  Each for-loop,
90if-else, while, and until block is counted as one nest.  Postfix forms
91of these constructs are not counted.  The default maximum is 5.
92Customization in a F<.perlcriticrc> file looks like this:
93
94    [ControlStructures::ProhibitDeepNests]
95    max_nests = 3
96
97=head1 AUTHOR
98
99Jeffrey Ryan Thalhammer <jeff@imaginative-software.com>
100
101=head1 COPYRIGHT
102
103Copyright (c) 2005-2011 Imaginative Software Systems.  All rights reserved.
104
105This program is free software; you can redistribute it and/or modify
106it under the same terms as Perl itself.  The full text of this license
107can 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 :
119