File Coverage

blib/lib/Perl/Critic/Policy/Variables/ProhibitConditionalDeclarations.pm
Criterion Covered Total %
statement 27 32 84.3
branch 3 12 25.0
condition n/a
subroutine 12 12 100.0
pod 4 5 80.0
total 46 61 75.4


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