File Coverage

blib/lib/Perl/Critic/Policy/Variables/ProhibitConditionalDeclarations.pm
Criterion Covered Total %
statement 32 32 100.0
branch 10 12 83.3
condition n/a
subroutine 12 12 100.0
pod 4 5 80.0
total 58 61 95.0


line stmt bran cond sub pod time code
1             package Perl::Critic::Policy::Variables::ProhibitConditionalDeclarations;
2              
3 40     40   26743 use 5.010001;
  40         170  
4 40     40   263 use strict;
  40         120  
  40         952  
5 40     40   255 use warnings;
  40         106  
  40         970  
6 40     40   242 use Readonly;
  40         117  
  40         2068  
7              
8 40     40   276 use Perl::Critic::Utils qw{ :severities :classification :data_conversion };
  40         150  
  40         1964  
9 40     40   14838 use parent 'Perl::Critic::Policy';
  40         180  
  40         269  
10              
11             our $VERSION = '1.148';
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 96     96 0 1699 sub supported_parameters { return () }
21 94     94 1 408 sub default_severity { return $SEVERITY_HIGHEST }
22 74     74 1 337 sub default_themes { return qw( core bugs ) }
23 43     43 1 211 sub applies_to { return 'PPI::Statement::Variable' }
24              
25             #-----------------------------------------------------------------------------
26              
27             sub violates {
28 126     126 1 444 my ( $self, $elem, undef ) = @_;
29 126 100       411 return if $elem->type() eq 'local';
30              
31 118 100       5477 if ( $elem->find(\&_is_conditional) ) {
32 20         317 return $self->violation( $DESC, $EXPL, $elem );
33             }
34 98         1552 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 1023     1023   11761 my (undef, $elem) = @_;
42              
43 1023 100       2064 return if !$conditionals{$elem};
44 21 50       146 return if ! $elem->isa('PPI::Token::Word');
45 21 50       80 return if is_hash_key($elem);
46 21 100       66 return if is_method_call($elem);
47              
48 20         58 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 :