Policy/Community/WhileDiamondDefaultAssignment.pm

filter: Statementstmt 97.7% 43 / 44 Branchbran 77.2% 17 / 22 Conditioncond 70.5% 12 / 17 MC/DCmcdc 50.0% 5 / 10 Subroutinesub 90.9% 10 / 11 Podpod 80.0% 4 / 5 totaltotal 83.4% 91 / 109 CC 18 SCAR 25.7
CC
18
Cov
87%
violates
19.7
default_themes
2.0
BEGIN
1.0
CRAP 13.0
« Policy/Community/StrictWarnings.pm ↑ Summary Policy/Community/Prototypes.pm »
1 package Perl::Critic::Policy::Community::WhileDiamondDefaultAssignment;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 ▶use Perl::Critic::Utils qw(:severities :classification :ppi);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use parent 'Perl::Critic::Policy';
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 1 our $VERSION = 'v1.0.4';
10  
11 1 ▶use constant DESC => '<>/<<>>/readline/readdir/each result not explicitly assigned in while condition';
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use constant EXPL => 'When used alone in a while condition, the <>/<<>> operator, readline, readdir, and each functions assign their result to $_, but do not localize it. Assign the result to an explicit lexical variable instead (my $line = <...>, my $dir = readdir ...)';
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 7 ▶sub supported_parameters { () }
Subroutine supported_parameters: called CC=1 SCAR=0.0
Pod undocumented
15 32 ▶sub default_severity { $SEVERITY_HIGH }
Subroutine default_severity: called CC=1 SCAR=0.0
Pod documented
16 0 ▶sub default_themes { 'community' }
Subroutine default_themes: not called CC=1 SCAR=6.9
Pod documented
17 7 ▶sub applies_to { 'PPI::Token::Word' }
Subroutine applies_to: called CC=1 SCAR=0.0
Pod documented
18  
19 1 my %bad_functions = (
20 each => 1,
21 readdir => 1,
22 readline => 1,
23 );
24  
25 110 ▶sub violates {
Subroutine violates: called CC=18 SCAR=29.8
Pod documented
26 110 my ($self, $elem) = @_;
27 110 ▶ return () unless $elem eq 'while' or $elem eq 'for';
Branch: 1 branch
BranchTrueFalseTotal
unless $elem eq "while" or $elem eq "for" 54 56 110
Condition100%3/3
$elem eq "while" or $elem eq "for"
l!l&&r!l&&!r
48854
MC/DC100%2/2
$elem eq "while" or $elem eq "for"
$elem eq "while" $elem eq "for"
Truth table100%3/3
A or B
A= $elem eq "while"B= $elem eq "for"
ABresult
1X1
011
000
28
29 56 ▶ my $next = $elem->snext_sibling || return ();
Condition50%1/2
$elem->snext_sibling || (return ())
l!l
560
MC/DC0%0/1
$elem->snext_sibling || (return ())
$elem->snext_sibling
Truth table50%1/2
A
A= $elem->snext_sibling
Aresult
11
00
30
31 # Detect for (;<>;)
32 56 ▶ if ($elem eq 'for') {
Branch: 2 branches
BranchTrueFalseTotal
if ($elem eq 'for') { } 8 48 56
elsif ($elem eq 'while') { } 48 0 48
33 8 ▶ return () unless $next->isa('PPI::Structure::For');
Branch: 1 branch
BranchTrueFalseTotal
unless $next->isa("PPI::Structure::For") 0 8 8
34 8 my @statements = grep { $_->isa('PPI::Statement') } $next->children;
35 8 ▶ return () unless @statements >= 2;
Branch: 1 branch
BranchTrueFalseTotal
unless @statements >= 2 0 8 8
36 8 my $middle = $statements[1];
37 8 ▶ return $self->violation(DESC, EXPL, $elem) if $middle->schildren
Branch: 1 branch
BranchTrueFalseTotal
if $middle->schildren and $middle->schild(0)->isa("PPI::Token::QuoteLike::Readline") 4 4 8
Condition66%2/3
$middle->schildren and $middle->schild(0)->isa("PPI::Token::QuoteLike::Readline")
!ll&&!rl&&r
044
MC/DC50%1/2
$middle->schildren and $middle->schild(0)->isa("PPI::Token::QuoteLike::Readline")
$middle->schildren $middle->schild(0)->isa("PPI::Token::QuoteLike::Readline")
Truth table66%2/3
A and B
A= $middle->schildrenB= $middle->schild(0)->isa("PPI::Token::QuoteLike::Readline")
ABresult
0X0
100
111
38 and $middle->schild(0)->isa('PPI::Token::QuoteLike::Readline');
39 } elsif ($elem eq 'while') {
40 # while (<>) {} or ... while <>
41 48 ▶ if ($next->isa('PPI::Structure::Condition')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($next->isa("PPI::Structure::Condition")) 24 24 48
42 24 $next = $next->schild(0);
43 24 ▶ return () unless defined $next and $next->isa('PPI::Statement');
Branch: 1 branch
BranchTrueFalseTotal
unless defined $next and $next->isa("PPI::Statement") 0 24 24
Condition33%1/3
defined $next and $next->isa("PPI::Statement")
!ll&&!rl&&r
0024
MC/DC0%0/2
defined $next and $next->isa("PPI::Statement")
defined $next $next->isa("PPI::Statement")
Truth table33%1/3
A and B
A= defined $nextB= $next->isa("PPI::Statement")
ABresult
0X0
100
111
44 24 $next = $next->schild(0);
45 24 ▶ return () unless defined $next;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $next 0 24 24
46 }
47
48 48 ▶ return $self->violation(DESC, EXPL, $elem) if $next->isa('PPI::Token::QuoteLike::Readline');
Branch: 1 branch
BranchTrueFalseTotal
if $next->isa("PPI::Token::QuoteLike::Readline") 8 40 48
49 40 ▶ if ($next->isa('PPI::Token::Word') and exists $bad_functions{$next} and is_function_call $next) {
Branch: 1 branch
BranchTrueFalseTotal
if ($next->isa("PPI::Token::Word") and exists $bad_functions{$next} and is_function_call($next)) 20 20 40
Condition83%5/6
$next->isa("PPI::Token::Word") and exists $bad_functions{$next} and is_function_call($next)
!ll&&!rl&&r
20020
$next->isa("PPI::Token::Word") and exists $bad_functions{$next}
!ll&&!rl&&r
101020
MC/DC66%2/3
$next->isa("PPI::Token::Word") and exists $bad_functions{$next} and is_function_call($next)
$next->isa("PPI::Token::Word") exists $bad_functions{$next} is_function_call($next)
Truth table75%3/4
A and B and C
A= $next->isa("PPI::Token::Word")B= exists $bad_functions{$next}C= is_function_call($next)
ABCresult
0XX0
10X0
1100
1111
50 20 return $self->violation(DESC, EXPL, $elem);
51 }
52 }
53
54 24 return ();
55 }
56  
57 1 1;
58  
59 =head1 NAME
60  
61 Perl::Critic::Policy::Community::WhileDiamondDefaultAssignment - Don't use
62 while with implicit assignment to $_
63  
64 =head1 DESCRIPTION
65  
66 The diamond operator C<E<lt>E<gt>> (or C<E<lt>E<lt>E<gt>E<gt>>), and functions
67 C<readline()>, C<readdir()>, and C<each()> are extra magical in a while
68 condition: if it is the only thing in the condition, it will assign its result
69 to C<$_>, but it does not localize C<$_> to the while loop. (Note, this also
70 applies to a C<for (;E<lt>E<gt>;)> construct.) This can unintentionally confuse
71 outer loops that are already using C<$_> to iterate. In addition, using C<$_>
72 at all means that your loop can get confused by other code which does not
73 politely localize its usage of the global variable. To avoid these
74 possibilities, assign the result of the diamond operator or these functions to
75 an explicit lexical variable.
76  
77 while (<$fh>) { ... } # not ok
78 while (<<>>) { ... } # not ok
79 ... while <STDIN>; # not ok
80 for (;<>;) { ... } # not ok
81 while (readline $fh) { ... } # not ok
82 while (readdir $dh) { ... } # not ok
83  
84 while (my $line = <$fh>) { ... } # ok
85 while (my $line = <<>>) { ... } # ok
86 ... while $line = <STDIN>; # ok
87 for (;my $line = <>;) { ... } # ok
88 while (my $line = readline $fh) { ... } # ok
89 while (my $dir = readdir $dh) { ... } # ok
90  
91 =head1 AFFILIATION
92  
93 This policy is part of L<Perl::Critic::Community>.
94  
95 =head1 CONFIGURATION
96  
97 This policy is not configurable except for the standard options.
98  
99 =head1 AUTHOR
100  
101 Dan Book, C<dbook@cpan.org>
102  
103 =head1 COPYRIGHT AND LICENSE
104  
105 Copyright 2015, Dan Book.
106  
107 This library is free software; you may redistribute it and/or modify it under
108 the terms of the Artistic License version 2.0.
109  
110 =head1 SEE ALSO
111  
112 L<Perl::Critic>
« Policy/Community/StrictWarnings.pm ↑ Summary Policy/Community/Prototypes.pm »