blib/lib/Perl/Critic/Policy/Modules/ProhibitConditionalUseStatements.pm

filter: Statementstmt 63.4% 33 / 52 Branchbran 10.7% 3 / 28 Conditioncond 25.9% 7 / 27 MC/DCmcdc 0.0% 0 / 15 Subroutinesub 80.0% 12 / 15 Podpod 80.0% 4 / 5 totaltotal 41.5% 59 / 142 CC 24 SCAR 35.7
CC
24
Cov
40%
_is_in_do_conditional_block
110.0
_is_eval
20.0
_is_in_conditional_logic
15.8
CRAP 35.4
« blib/lib/Perl/Critic/Policy/Variables/ProhibitPunctuationVars.pm ↑ Summary blib/lib/Perl/Critic/Policy/Documentation/PodSpelling.pm »
1 package Perl::Critic::Policy::Modules::ProhibitConditionalUseStatements;
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{ :booleans :severities hashify };
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{Conditional "use" statement};
16 40 Readonly::Scalar my $EXPL => q{Use "require" to conditionally include a module.};
17  
18 # operators
19  
20 40 Readonly::Hash my %OPS => hashify( qw( || && or and ) );
21  
22 #-----------------------------------------------------------------------------
23  
24 90 ▶sub supported_parameters { return () }
Subroutine supported_parameters: called CC=1 SCAR=0.0
Pod undocumented
25 75 ▶sub default_severity { return $SEVERITY_MEDIUM }
Subroutine default_severity: called CC=1 SCAR=0.0
Pod documented
26 74 ▶sub default_themes { return qw( core bugs ) }
Subroutine default_themes: called CC=1 SCAR=0.0
Pod documented
27 31 ▶sub applies_to { return 'PPI::Statement::Include' }
Subroutine applies_to: called CC=1 SCAR=0.0
Pod documented
28  
29 #-----------------------------------------------------------------------------
30  
31 sub violates {
32 57 ▶ my ( $self, $elem, $doc ) = @_;
Subroutine violates: called CC=5 SCAR=16.6
Pod documented
33 57 ▶ return $self->violation( $DESC, $EXPL, $elem ) if $elem->type() eq 'use'
Branch: 1 branch
BranchTrueFalseTotal
if $elem->type eq "use" and not $elem->pragma and $elem->module and $self->_is_in_conditional_logic($elem) 0 57 57
Condition77%7/9
$elem->type eq "use" and not $elem->pragma and $elem->module and $self->_is_in_conditional_logic($elem)
!ll&&!rl&&r
5610
$elem->type eq "use" and not $elem->pragma and $elem->module
!ll&&!rl&&r
5601
$elem->type eq "use" and not $elem->pragma
!ll&&!rl&&r
2541
MC/DC0%0/4
$elem->type eq "use" and not $elem->pragma and $elem->module and $self->_is_in_conditional_logic($elem)
$elem->type eq "use" not $elem->pragma $elem->module $self->_is_in_conditional_logic($elem)
Truth table60%3/5
A and not(B) and C and D
A= $elem->type eq "use"B= not $elem->pragmaC= $elem->moduleD= $self->_is_in_conditional_logic($elem)
ABCDresult
0XXX0
10XX0
110X0
11100
11111
34 && !$elem->pragma()
35 && $elem->module()
36 && $self->_is_in_conditional_logic($elem);
37 57 return;
38 }
39  
40 #-----------------------------------------------------------------------------
41  
42 # is this a non-string eval statement
43  
44 sub _is_eval {
45 0 ▶ my ( $self, $elem ) = @_;
Subroutine _is_eval: not called CC=4 SCAR=30.0
46 0 ▶ $elem->isa('PPI::Statement') or return;
Branch: 1 branch
BranchTrueFalseTotal
$elem->isa("PPI::Statement") or return 0 0 0
47 0 my $first_elem = $elem->first_element();
48 0 ▶ return $TRUE if $first_elem->isa('PPI::Token::Word')
Branch: 1 branch
BranchTrueFalseTotal
if $first_elem->isa("PPI::Token::Word") and $first_elem eq "eval" 0 0 0
Condition0%0/3
$first_elem->isa("PPI::Token::Word") and $first_elem eq "eval"
!ll&&!rl&&r
000
MC/DC0%0/2
$first_elem->isa("PPI::Token::Word") and $first_elem eq "eval"
$first_elem->isa("PPI::Token::Word") $first_elem eq "eval"
Truth table0%0/3
A and B
A= $first_elem->isa("PPI::Token::Word")B= $first_elem eq "eval"
ABresult
0X0
100
111
49 && $first_elem eq 'eval';
50 0 return;
51 }
52  
53 #-----------------------------------------------------------------------------
54  
55 # is this in a conditional do block
56  
57 sub _is_in_do_conditional_block {
58 0 ▶ my ( $self, $elem ) = @_;
Subroutine _is_in_do_conditional_block: not called CC=10 SCAR=47.0
59 0 ▶ return if !$elem->isa('PPI::Structure::Block');
Branch: 1 branch
BranchTrueFalseTotal
unless $elem->isa("PPI::Structure::Block") 0 0 0
60 0 ▶ my $prev_sibling = $elem->sprevious_sibling() or return;
Branch: 1 branch
BranchTrueFalseTotal
my $prev_sibling = $elem->sprevious_sibling or return 0 0 0
61 0 ▶ if ($prev_sibling->isa('PPI::Token::Word') && $prev_sibling eq 'do') {
Branch: 1 branch
BranchTrueFalseTotal
if ($prev_sibling->isa("PPI::Token::Word") and $prev_sibling eq "do") 0 0 0
Condition0%0/3
$prev_sibling->isa("PPI::Token::Word") and $prev_sibling eq "do"
!ll&&!rl&&r
000
MC/DC0%0/2
$prev_sibling->isa("PPI::Token::Word") and $prev_sibling eq "do"
$prev_sibling->isa("PPI::Token::Word") $prev_sibling eq "do"
Truth table0%0/3
A and B
A= $prev_sibling->isa("PPI::Token::Word")B= $prev_sibling eq "do"
ABresult
0X0
100
111
62 0 my $next_sibling = $elem->snext_sibling();
63 0 ▶ return $TRUE if $next_sibling
Branch: 1 branch
BranchTrueFalseTotal
if $next_sibling and $next_sibling->isa("PPI::Token::Word") 0 0 0
Condition0%0/3
$next_sibling and $next_sibling->isa("PPI::Token::Word")
!ll&&!rl&&r
000
MC/DC0%0/2
$next_sibling and $next_sibling->isa("PPI::Token::Word")
$next_sibling $next_sibling->isa("PPI::Token::Word")
Truth table0%0/3
A and B
A= $next_siblingB= $next_sibling->isa("PPI::Token::Word")
ABresult
0X0
100
111
64 && $next_sibling->isa('PPI::Token::Word');
65 0 ▶ $prev_sibling = $prev_sibling->sprevious_sibling() or return;
Branch: 1 branch
BranchTrueFalseTotal
$prev_sibling = $prev_sibling->sprevious_sibling or return 0 0 0
66 return $TRUE if $prev_sibling->isa('PPI::Token::Operator')
67 0 ▶ && $OPS{$prev_sibling->content()};
Branch: 1 branch
BranchTrueFalseTotal
if $prev_sibling->isa("PPI::Token::Operator") and $OPS{$prev_sibling->content} 0 0 0
Condition0%0/3
$prev_sibling->isa("PPI::Token::Operator") and $OPS{$prev_sibling->content}
!ll&&!rl&&r
000
MC/DC0%0/2
$prev_sibling->isa("PPI::Token::Operator") and $OPS{$prev_sibling->content}
$prev_sibling->isa("PPI::Token::Operator") $OPS{$prev_sibling->content}
Truth table0%0/3
A and B
A= $prev_sibling->isa("PPI::Token::Operator")B= $OPS{$prev_sibling->content}
ABresult
0X0
100
111
68 }
69 0 return;
70 }
71  
72 #-----------------------------------------------------------------------------
73  
74 # is this a compound statement
75  
76 sub _is_compound_statement {
77 0 ▶ my ( $self, $elem ) = @_;
Subroutine _is_compound_statement: not called CC=3 SCAR=24.8
78 0 ▶ return if !$elem->isa('PPI::Statement::Compound');
Branch: 1 branch
BranchTrueFalseTotal
unless $elem->isa("PPI::Statement::Compound") 0 0 0
79 0 ▶ return $TRUE if $elem->type() ne 'continue'; # exclude bare blocks
Branch: 1 branch
BranchTrueFalseTotal
if $elem->type ne "continue" 0 0 0
80 0 return;
81 }
82  
83 #-----------------------------------------------------------------------------
84  
85 # is this contained in conditional logic
86  
87 sub _is_in_conditional_logic {
88 1 ▶ my ( $self, $elem ) = @_;
Subroutine _is_in_conditional_logic: called CC=6 SCAR=27.6
89 1 ▶ while ($elem = $elem->parent()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($elem = $elem->parent) 1 0 1
90 1 ▶ last if $elem->isa('PPI::Document');
Branch: 1 branch
BranchTrueFalseTotal
if $elem->isa("PPI::Document") 1 0 1
91 0 ▶ return $TRUE if $self->_is_compound_statement($elem)
Branch: 1 branch
BranchTrueFalseTotal
if $self->_is_compound_statement($elem) or $self->_is_eval($elem) or $self->_is_in_do_conditional_block($elem) 0 0 0
Condition0%0/6
$self->_is_compound_statement($elem) or $self->_is_eval($elem) or $self->_is_in_do_conditional_block($elem)
l!l&&r!l&&!r
000
$self->_is_compound_statement($elem) or $self->_is_eval($elem)
l!l&&r!l&&!r
000
MC/DC0%0/3
$self->_is_compound_statement($elem) or $self->_is_eval($elem) or $self->_is_in_do_conditional_block($elem)
$self->_is_compound_statement($elem) $self->_is_eval($elem) $self->_is_in_do_conditional_block($elem)
Truth table0%0/4
A or B or C
A= $self->_is_compound_statement($elem)B= $self->_is_eval($elem)C= $self->_is_in_do_conditional_block($elem)
ABCresult
1XX1
01X1
0011
0000
92 || $self->_is_eval($elem)
93 || $self->_is_in_do_conditional_block($elem);
94 }
95 1 return;
96 }
97  
98 40 1;
99  
100 __END__
101  
102 #-----------------------------------------------------------------------------
103  
104 =pod
105  
106 =for stopwords evals
107  
108 =head1 NAME
109  
110 Perl::Critic::Policy::Modules::ProhibitConditionalUseStatements - Avoid putting conditional logic around compile-time includes.
111  
112  
113 =head1 AFFILIATION
114  
115 This Policy is part of the core L<Perl::Critic|Perl::Critic>
116 distribution.
117  
118  
119 =head1 DESCRIPTION
120  
121 Modules included via "use" are loaded at compile-time. Placing conditional
122 logic around the "use" statement has no effect on whether the module will be
123 loaded. Doing so can also serve to confuse the reader as to the author's
124 original intent.
125  
126 If you need to conditionally load a module you should be using "require"
127 instead.
128  
129 This policy will catch the following forms of conditional "use" statements:
130  
131 # if-elsif-else
132 if ($a == 1) { use Module; }
133 if ($a == 1) { } elsif ($a == 2) { use Module; }
134 if ($a == 1) { } else { use Module; }
135  
136 # for/foreach
137 for (1..$a) { use Module; }
138 foreach (@a) { use Module; }
139  
140 # while
141 while ($a == 1) { use Module; }
142  
143 # unless
144 unless ($a == 1) { use Module; }
145  
146 # until
147 until ($a == 1) { use Module; }
148  
149 # do-condition
150 do { use Module; } if $a == 1;
151 do { use Module; } while $a == 1;
152 do { use Module; } unless $a == 1;
153 do { use Module; } until $a == 1;
154  
155 # operator-do
156 $a == 1 || do { use Module; };
157 $a == 1 && do { use Module; };
158 $a == 1 or do { use Module; };
159 $a == 1 and do { use Module; };
160  
161 # non-string eval
162 eval { use Module; };
163  
164 Including a module via "use" in bare blocks, standalone do blocks, or
165 string evals is allowed.
166  
167 # bare block
168 { use Module; }
169  
170 # do
171 do { use Module; }
172  
173 # string eval
174 eval "use Module";
175  
176 =head1 CONFIGURATION
177  
178 This Policy is not configurable except for the standard options.
179  
180  
181 =head1 AUTHOR
182  
183 Peter Guzis <pguzis@cpan.org>
184  
185  
186 =head1 COPYRIGHT
187  
188 Copyright (c) 2010-2011 Peter Guzis. All rights reserved.
189  
190 This program is free software; you can redistribute it and/or modify
191 it under the same terms as Perl itself. The full text of this license
192 can be found in the LICENSE file included with this module.
193  
194 =cut
195  
196 # Local Variables:
197 # mode: cperl
198 # cperl-indent-level: 4
199 # fill-column: 78
200 # indent-tabs-mode: nil
201 # c-indentation-style: bsd
202 # End:
203 # ex: set ts=8 sts=4 sw=4 tw=78 ft=perl expandtab shiftround :
« blib/lib/Perl/Critic/Policy/Variables/ProhibitPunctuationVars.pm ↑ Summary blib/lib/Perl/Critic/Policy/Documentation/PodSpelling.pm »