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 / ProhibitPostfixControls.pm
1 ##############################################################################
2 #      $URL: http://perlcritic.tigris.org/svn/perlcritic/trunk/Perl-Critic/lib/Perl/Critic/Policy/ControlStructures/ProhibitPostfixControls.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::ProhibitPostfixControls;
9
10 use 5.006001;
11 use strict;
12 use warnings;
13 use Readonly;
14
15 use Perl::Critic::Utils qw{ :characters :severities :data_conversion :classification };
16 use base 'Perl::Critic::Policy';
17
18 our $VERSION = '1.088';
19
20 #-----------------------------------------------------------------------------
21
22 Readonly::Hash my %PAGES_OF => (
23     if      => [ 93, 94 ],
24     unless  => [ 96, 97 ],
25     until   => [ 96, 97 ],
26     for     => [ 96     ],
27     foreach => [ 96     ],
28     while   => [ 96     ],
29 );
30
31 # These functions can have postfix 'if'.
32 my @DEFAULT_FLOW_CONTROL = qw( warn die carp croak cluck confess goto exit );
33
34 #-----------------------------------------------------------------------------
35
36 sub supported_parameters {
37     return (
38         {
39             name               => 'allow',
40             description        => 'The permitted postfix controls.',
41             default_string     => $EMPTY,
42             behavior           => 'enumeration',
43             enumeration_values => [ sort keys %PAGES_OF ],
44             enumeration_allow_multiple_values   => 1,
45         },
46         {
47             name               => 'flowcontrol',
48             description        => 'The exempt flow control functions.',
49             default_string     => 'carp cluck confess croak die exit goto warn',
50             behavior           => 'string list',
51         },
52     );
53 }
54
55 sub default_severity { return $SEVERITY_LOW         }
56 sub default_themes   { return qw(core pbp cosmetic) }
57 sub applies_to       { return 'PPI::Token::Word'    }
58
59 #-----------------------------------------------------------------------------
60
61 sub violates {
62     my ( $self, $elem, undef ) = @_;
63
64     my $expl = $PAGES_OF{$elem};
65     return if not $expl;
66
67     return if is_hash_key($elem);
68     return if is_method_call($elem);
69     return if is_subroutine_name($elem);
70     return if is_included_module_name($elem);
71     return if is_package_declaration($elem);
72
73     # Skip controls that are allowed
74     return if exists $self->{_allow}->{$elem};
75
76     # Skip Compound variety (these are good)
77     my $stmnt = $elem->statement();
78     return if !$stmnt;
79     return if $stmnt->isa('PPI::Statement::Compound');
80
81     # Handle special cases
82     if ( $elem eq 'if' ) {
83         # Postfix 'if' allowed with loop breaks, or other
84         # flow-controls like 'die', 'warn', and 'croak'
85         return if $stmnt->isa('PPI::Statement::Break');
86         return if defined $self->{_flowcontrol}{ $stmnt->schild(0) };
87     }
88
89     # If we get here, it must be postfix.
90     my $desc = qq{Postfix control "$elem" used};
91     return $self->violation( $desc, $expl, $elem );
92 }
93
94 1;
95
96 __END__
97
98 =pod
99
100 =for stopwords flowcontrol
101
102 =head1 NAME
103
104 Perl::Critic::Policy::ControlStructures::ProhibitPostfixControls - Write C<if($condition){ do_something() }> instead of C<do_something() if $condition>.
105
106 =head1 AFFILIATION
107
108 This Policy is part of the core L<Perl::Critic> distribution.
109
110
111 =head1 DESCRIPTION
112
113 Conway discourages using postfix control structures (C<if>, C<for>,
114 C<unless>, C<until>, C<while>) because they hide control flow.  The
115 C<unless> and C<until> controls are particularly evil because they
116 lead to double-negatives that are hard to comprehend.  The only
117 tolerable usage of a postfix C<if> is when it follows a loop break
118 such as C<last>, C<next>, C<redo>, or C<continue>.
119
120   do_something() if $condition;         #not ok
121   if($condition){ do_something() }      #ok
122
123   do_something() while $condition;      #not ok
124   while($condition){ do_something() }   #ok
125
126   do_something() unless $condition;     #not ok
127   do_something() unless ! $condition;   #really bad
128   if(! $condition){ do_something() }    #ok
129
130   do_something() until $condition;      #not ok
131   do_something() until ! $condition;    #really bad
132   while(! $condition){ do_something() } #ok
133
134   do_something($_) for @list;           #not ok
135
136  LOOP:
137   for my $n (0..100){
138       next if $condition;               #ok
139       last LOOP if $other_condition;    #also ok
140   }
141
142 =head1 CONFIGURATION
143
144 A set of constructs to be ignored by this policy can specified by
145 giving a value for 'allow' of a string of space-delimited keywords:
146 C<if>, C<for>, C<unless>, C<until>, and/or C<while>.  An example of
147 specifying allowed flow-control structures in a F<.perlcriticrc> file:
148
149  [ControlStructures::ProhibitPostfixControls]
150  allow = for if until
151
152 By default, all postfix control keywords are prohibited.
153
154 The set of flow-control functions that are exempt from the restriction
155 can also be configured with the 'flowcontrol' directive in your
156 F<.perlcriticrc> file:
157
158  [ControlStructures::ProhibitPostfixControls]
159  flowcontrol = warn die carp croak cluck confess goto exit
160
161 =head1 NOTES
162
163 The C<die>, C<croak>, and C<confess> functions are frequently used as
164 flow-controls just like C<next> or C<last>.  So this Policy does
165 permit you to use a postfix C<if> when the statement begins with one
166 of those functions.  It is also pretty common to use C<warn>, C<carp>,
167 and C<cluck> with a postfix C<if>, so those are allowed too.
168
169
170 =head1 AUTHOR
171
172 Jeffrey Ryan Thalhammer <thaljef@cpan.org>
173
174 =head1 COPYRIGHT
175
176 Copyright (c) 2005-2008 Jeffrey Ryan Thalhammer.  All rights reserved.
177
178 This program is free software; you can redistribute it and/or modify
179 it under the same terms as Perl itself.  The full text of this license
180 can be found in the LICENSE file included with this module.
181
182 =cut
183
184 # Local Variables:
185 #   mode: cperl
186 #   cperl-indent-level: 4
187 #   fill-column: 78
188 #   indent-tabs-mode: nil
189 #   c-indentation-style: bsd
190 # End:
191 # ex: set ts=8 sts=4 sw=4 tw=78 ft=perl expandtab shiftround :