blib/lib/Perl/Critic/Policy/Variables/ProhibitConditionalDeclarations.pm

filter: Statementstmt 86.8% 33 / 38 Branchbran 25.0% 3 / 12 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 12 / 12 Podpod 80.0% 4 / 5 totaltotal 77.6% 52 / 67 CC 7 SCAR 17.7
CC
7
Cov
72%
_is_conditional
17.1
violates
3.3
BEGIN
1.0
CRAP 5.9
« blib/lib/Perl/Critic/Policy/RegularExpressions/RequireExtendedFormatting.pm ↑ Summary blib/lib/Perl/Critic/Config.pm »
1 package Perl::Critic::Policy::Variables::ProhibitConditionalDeclarations;
2  
3 40 ▶use 5.010001;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 40 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 40 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 40 ▶use Readonly;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 40 ▶use Perl::Critic::Utils qw{ :severities :classification :data_conversion };
Subroutine BEGIN: called CC=1 SCAR=0.0
9 40 ▶use parent 'Perl::Critic::Policy';
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 40 our $VERSION = '1.150';
12  
13 #-----------------------------------------------------------------------------
14  
15 40 Readonly::Scalar my $DESC => q{Variable declared in conditional statement};
16 40 Readonly::Scalar my $EXPL => q{Declare variables outside of the condition};
17  
18 #-----------------------------------------------------------------------------
19  
20 90 ▶sub supported_parameters { return () }
Subroutine supported_parameters: called CC=1 SCAR=0.0
Pod undocumented
21 75 ▶sub default_severity { return $SEVERITY_HIGHEST }
Subroutine default_severity: called CC=1 SCAR=0.0
Pod documented
22 74 ▶sub default_themes { return qw( core bugs ) }
Subroutine default_themes: called CC=1 SCAR=0.0
Pod documented
23 37 ▶sub applies_to { return 'PPI::Statement::Variable' }
Subroutine applies_to: called CC=1 SCAR=0.0
Pod documented
24  
25 #-----------------------------------------------------------------------------
26  
27 sub violates {
28 93 ▶ my ( $self, $elem, undef ) = @_;
Subroutine violates: called CC=3 SCAR=12.0
Pod documented
29 93 ▶ return if $elem->type() eq 'local';
Branch: 1 branch
BranchTrueFalseTotal
if $elem->type eq "local" 0 93 93
30  
31 93 ▶ if ( $elem->find(\&_is_conditional) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($elem->find(\&Perl::Critic::Policy::Variables::ProhibitConditionalDeclarations::_is_conditional)) 0 93 93
32 0 return $self->violation( $DESC, $EXPL, $elem );
33 }
34 93 return; #ok!
35 }
36  
37 40 my @conditionals = qw( if while foreach for until unless );
38 40 my %conditionals = hashify( @conditionals );
39  
40 sub _is_conditional {
41 744 ▶ my (undef, $elem) = @_;
Subroutine _is_conditional: called CC=5 SCAR=28.4
42  
43 744 ▶ return if !$conditionals{$elem};
Branch: 1 branch
BranchTrueFalseTotal
unless $conditionals{$elem} 744 0 744
44 0 ▶ return if ! $elem->isa('PPI::Token::Word');
Branch: 1 branch
BranchTrueFalseTotal
unless $elem->isa("PPI::Token::Word") 0 0 0
45 0 ▶ return if is_hash_key($elem);
Branch: 1 branch
BranchTrueFalseTotal
if is_hash_key($elem) 0 0 0
46 0 ▶ return if is_method_call($elem);
Branch: 1 branch
BranchTrueFalseTotal
if is_method_call($elem) 0 0 0
47  
48 0 return 1;
49 }
50  
51 40 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 :
« blib/lib/Perl/Critic/Policy/RegularExpressions/RequireExtendedFormatting.pm ↑ Summary blib/lib/Perl/Critic/Config.pm »