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   26151 use 5.010001;
  40         189  
4 40     40   269 use strict;
  40         112  
  40         847  
5 40     40   222 use warnings;
  40         126  
  40         964  
6 40     40   232 use Readonly;
  40         130  
  40         2091  
7              
8 40     40   287 use Perl::Critic::Utils qw{ :severities :classification :data_conversion };
  40         129  
  40         2011  
9 40     40   15044 use parent 'Perl::Critic::Policy';
  40         105  
  40         273  
10              
11             our $VERSION = '1.150';
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 89     89 0 1644 sub supported_parameters { return () }
21 74     74 1 322 sub default_severity { return $SEVERITY_HIGHEST }
22 74     74 1 339 sub default_themes { return qw( core bugs ) }
23 36     36 1 117 sub applies_to { return 'PPI::Statement::Variable' }
24              
25             #-----------------------------------------------------------------------------
26              
27             sub violates {
28 93     93 1 228 my ( $self, $elem, undef ) = @_;
29 93 50       208 return if $elem->type() eq 'local';
30              
31 93 50       3104 if ( $elem->find(\&_is_conditional) ) {
32 0         0 return $self->violation( $DESC, $EXPL, $elem );
33             }
34 93         1071 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   6651 my (undef, $elem) = @_;
42              
43 744 50       1182 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 :