« blib/lib/Perl/Critic/Policy/RegularExpressions/RequireBracesForMultiline.pm
↑ Summary
blib/lib/Perl/Critic/Policy/ValuesAndExpressions/RequireNumberSeparators.pm »
| 1 | package Perl::Critic::Policy::ControlStructures::ProhibitDeepNests; | ||||||||||
| 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 | 40 | ▶ | use Readonly; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | |||||||||||
| 8 | 40 | ▶ | use Perl::Critic::Utils qw{ :severities }; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | 40 | ▶ | use parent 'Perl::Critic::Policy'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | |||||||||||
| 11 | 40 | our $VERSION = '1.150'; | |||||||||
| 12 | |||||||||||
| 13 | #----------------------------------------------------------------------------- | ||||||||||
| 14 | |||||||||||
| 15 | 40 | Readonly::Scalar my $DESC => q{Code structure is deeply nested}; | |||||||||
| 16 | 40 | Readonly::Scalar my $EXPL => q{Consider refactoring}; | |||||||||
| 17 | |||||||||||
| 18 | #----------------------------------------------------------------------------- | ||||||||||
| 19 | |||||||||||
| 20 | sub supported_parameters { | ||||||||||
| 21 | return ( | ||||||||||
| 22 | { | ||||||||||
| 23 | 91 | ▶ | name => 'max_nests', | ||||||||
|
Subroutine
supported_parameters: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 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 | 75 | ▶ | sub default_severity { return $SEVERITY_MEDIUM } | ||||||||
|
Subroutine
default_severity: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 33 | 74 | ▶ | sub default_themes { return qw(core maintenance complexity) } | ||||||||
|
Subroutine
default_themes: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 34 | 31 | ▶ | sub applies_to { return 'PPI::Statement::Compound' } | ||||||||
|
Subroutine
applies_to: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 35 | |||||||||||
| 36 | #----------------------------------------------------------------------------- | ||||||||||
| 37 | |||||||||||
| 38 | sub violates { | ||||||||||
| 39 | 11 | ▶ | my ( $self, $elem, undef ) = @_; | ||||||||
|
Subroutine
violates: called CC=4 SCAR=14.6
Pod
documented
| |||||||||||
| 40 | |||||||||||
| 41 | 11 | my $nest_count = 1; #For _this_ element | |||||||||
| 42 | 11 | my $parent = $elem; | |||||||||
| 43 | |||||||||||
| 44 | 11 | ▶ | while ( $parent = $parent->parent() ){ | ||||||||
|
Branch: 1 branch
| |||||||||||
| 45 | 11 | ▶ | if( $parent->isa('PPI::Statement::Compound') ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 46 | 0 | $nest_count++; | |||||||||
| 47 | } | ||||||||||
| 48 | } | ||||||||||
| 49 | |||||||||||
| 50 | 11 | ▶ | if ( $nest_count > $self->{_max_nests} ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 51 | 0 | return $self->violation( $DESC, $EXPL, $elem ); | |||||||||
| 52 | } | ||||||||||
| 53 | 11 | return; #ok! | |||||||||
| 54 | } | ||||||||||
| 55 | |||||||||||
| 56 | |||||||||||
| 57 | 40 | 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 : | ||||||||||