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 / Variables / ProhibitConditionalDeclarations.pm
1 ##############################################################################
2 #      $URL: http://perlcritic.tigris.org/svn/perlcritic/trunk/Perl-Critic/lib/Perl/Critic/Policy/Variables/ProhibitConditionalDeclarations.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::Variables::ProhibitConditionalDeclarations;
9
10 use 5.006001;
11 use strict;
12 use warnings;
13 use Readonly;
14
15 use Perl::Critic::Utils qw{ :severities :classification :data_conversion };
16 use base 'Perl::Critic::Policy';
17
18 our $VERSION = '1.088';
19
20 #-----------------------------------------------------------------------------
21
22 Readonly::Scalar my $DESC => q{Variable declared in conditional statement};
23 Readonly::Scalar my $EXPL => q{Declare variables outside of the condition};
24
25 #-----------------------------------------------------------------------------
26
27 sub supported_parameters { return ()                         }
28 sub default_severity     { return $SEVERITY_HIGHEST          }
29 sub default_themes       { return qw( core bugs )            }
30 sub applies_to           { return 'PPI::Statement::Variable' }
31
32 #-----------------------------------------------------------------------------
33
34 sub violates {
35     my ( $self, $elem, undef ) = @_;
36     return if $elem->type() eq 'local';
37
38     if ( $elem->find(\&_is_conditional) ) {
39         return $self->violation( $DESC, $EXPL, $elem );
40     }
41     return;    #ok!
42 }
43
44 my @conditionals = qw( if while foreach for until unless );
45 my %conditionals = hashify( @conditionals );
46
47 sub _is_conditional {
48     my (undef, $elem) = @_;
49
50     return if !$conditionals{$elem};
51     return if ! $elem->isa('PPI::Token::Word');
52     return if is_hash_key($elem);
53     return if is_method_call($elem);
54
55     return 1;
56 }
57
58 1;
59
60 __END__
61
62 #-----------------------------------------------------------------------------
63
64 =pod
65
66 =head1 NAME
67
68 Perl::Critic::Policy::Variables::ProhibitConditionalDeclarations - Do not write C< my $foo = $bar if $baz; >.
69
70 =head1 AFFILIATION
71
72 This Policy is part of the core L<Perl::Critic> distribution.
73
74
75 =head1 DESCRIPTION
76
77 Declaring a variable with a postfix conditional is really confusing.
78 If the conditional is false, its not clear if the variable will
79 be false, undefined, undeclared, or what.  It's much more straightforward
80 to make variable declarations separately.
81
82   my $foo = $baz if $bar;          #not ok
83   my $foo = $baz unless $bar;      #not ok
84   our $foo = $baz for @list;       #not ok
85   local $foo = $baz foreach @list; #not ok
86
87
88 =head1 CONFIGURATION
89
90 This Policy is not configurable except for the standard options.
91
92
93 =head1 AUTHOR
94
95 Jeffrey R. Thalhammer <thaljef@cpan.org>
96
97 =head1 COPYRIGHT
98
99 Copyright (c) 2006-2008 Chris Dolan.  All rights reserved.
100
101 This program is free software; you can redistribute it and/or modify
102 it under the same terms as Perl itself.  The full text of this license
103 can be found in the LICENSE file included with this module.
104
105 =cut
106
107 # Local Variables:
108 #   mode: cperl
109 #   cperl-indent-level: 4
110 #   fill-column: 78
111 #   indent-tabs-mode: nil
112 #   c-indentation-style: bsd
113 # End:
114 # ex: set ts=8 sts=4 sw=4 tw=78 ft=perl expandtab shiftround :