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

filter: Statementstmt 84.8% 28 / 33 Branchbran 16.6% 1 / 6 Conditioncond 33.3% 1 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 84.6% 11 / 13 Podpod 80.0% 4 / 5 totaltotal 72.5% 45 / 62 CC 5 SCAR 9.8
CC
5
Cov
71%
_is_initialized
6.0
__ANON__
6.0
violates
3.8
CRAP 2.7
« blib/lib/Perl/Critic/Theme.pm ↑ Summary blib/lib/Perl/Critic/Policy/Miscellanea/ProhibitUselessNoCritic.pm »
1 package Perl::Critic::Policy::Variables::RequireInitializationForLocalVars;
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 };
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{"local" variable not initialized};
16 40 Readonly::Scalar my $EXPL => [ 78 ];
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_MEDIUM }
Subroutine default_severity: called CC=1 SCAR=0.0
Pod documented
22 92 ▶sub default_themes { return qw(core pbp bugs certrec ) }
Subroutine default_themes: called CC=1 SCAR=0.0
Pod documented
23 31 ▶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 85 ▶ my ( $self, $elem, undef ) = @_;
Subroutine violates: called CC=3 SCAR=13.3
Pod documented
29 85 ▶ if ( $elem->type() eq 'local' && !_is_initialized($elem) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($elem->type eq "local" and not _is_initialized($elem)) 0 85 85
Condition33%1/3
$elem->type eq "local" and not _is_initialized($elem)
!ll&&!rl&&r
8500
MC/DC0%0/2
$elem->type eq "local" and not _is_initialized($elem)
$elem->type eq "local" not _is_initialized($elem)
Truth table33%1/3
A and not(B)
A= $elem->type eq "local"B= not _is_initialized($elem)
ABresult
0X0
100
111
30 0 return $self->violation( $DESC, $EXPL, $elem );
31 }
32 85 return; #ok!
33 }
34  
35 #-----------------------------------------------------------------------------
36  
37 sub _is_initialized {
38 0 ▶ my $elem = shift;
Subroutine _is_initialized: not called CC=2 SCAR=17.9
39 0 ▶ my $wanted = sub { $_[1]->isa('PPI::Token::Operator') && $_[1] eq q{=} };
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
$_[1]->isa("PPI::Token::Operator") and $_[1] eq "=" 0 0 0
40 0 ▶ return $elem->find( $wanted ) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$elem->find($wanted) ? : 0 0 0
41 }
42  
43 40 1;
44  
45 __END__
46  
47 #-----------------------------------------------------------------------------
48  
49 =pod
50  
51 =head1 NAME
52  
53 Perl::Critic::Policy::Variables::RequireInitializationForLocalVars - Write C<local $foo = $bar;> instead of just C<local $foo;>.
54  
55  
56 =head1 AFFILIATION
57  
58 This Policy is part of the core L<Perl::Critic|Perl::Critic>
59 distribution.
60  
61  
62 =head1 DESCRIPTION
63  
64 Most people don't realize that a localized copy of a variable does not
65 retain its original value. Unless you initialize the variable when
66 you C<local>-ize it, it defaults to C<undef>. If you want the
67 variable to retain its original value, just initialize it to itself.
68 If you really do want the localized copy to be undef, then make it
69 explicit.
70  
71 package Foo;
72 $Bar = '42';
73  
74 package Baz;
75  
76 sub frobulate {
77  
78 local $Foo::Bar; #not ok, local $Foo::Bar is 'undef'
79 local $Foo::Bar = undef; #ok, local $Foo::Bar is obviously 'undef'
80 local $Foo::Bar = $Foo::Bar; #ok, local $Foo::Bar still equals '42'
81  
82 }
83  
84  
85 =head1 CONFIGURATION
86  
87 This Policy is not configurable except for the standard options.
88  
89  
90 =head1 AUTHOR
91  
92 Jeffrey Ryan Thalhammer <jeff@imaginative-software.com>
93  
94  
95 =head1 COPYRIGHT
96  
97 Copyright (c) 2005-2011 Imaginative Software Systems. All rights reserved.
98  
99 This program is free software; you can redistribute it and/or modify
100 it under the same terms as Perl itself. The full text of this license
101 can be found in the LICENSE file included with this module.
102  
103 =cut
104  
105 # Local Variables:
106 # mode: cperl
107 # cperl-indent-level: 4
108 # fill-column: 78
109 # indent-tabs-mode: nil
110 # c-indentation-style: bsd
111 # End:
112 # ex: set ts=8 sts=4 sw=4 tw=78 ft=perl expandtab shiftround :
« blib/lib/Perl/Critic/Theme.pm ↑ Summary blib/lib/Perl/Critic/Policy/Miscellanea/ProhibitUselessNoCritic.pm »