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