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
« lib/Perl/Critic/Policy/Variables/ProhibitPunctuationVars.pm ↑ Summary 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.156';
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 74 ▶sub default_severity { return $SEVERITY_MEDIUM }
Subroutine default_severity: called CC=1 SCAR=0.0
Pod documented
26 73 ▶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 57 ▶sub violates {
Subroutine violates: called CC=5 SCAR=16.6
Pod documented
32 57 my ( $self, $elem, undef ) = @_;
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 _is_in_conditional_logic($elem) 0 57 57
Condition77%7/9
$elem->type eq "use" and not $elem->pragma and $elem->module and _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 _is_in_conditional_logic($elem)
$elem->type eq "use" not $elem->pragma $elem->module _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= _is_in_conditional_logic($elem)
ABCDresult
0XXX0
10XX0
110X0
11100
11111
34 && !$elem->pragma()
35 && $elem->module()
36 && _is_in_conditional_logic($elem);
37 57 return;
38 }
39  
40 #-----------------------------------------------------------------------------
41  
42 # is this a non-string eval statement
43  
44 0 ▶sub _is_eval {
Subroutine _is_eval: not called CC=4 SCAR=30.0
45 0 my ( $elem ) = @_;
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 0 ▶sub _is_in_do_conditional_block {
Subroutine _is_in_do_conditional_block: not called CC=10 SCAR=47.0
58 0 my ( $elem ) = @_;
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 0 ▶sub _is_compound_statement {
Subroutine _is_compound_statement: not called CC=3 SCAR=24.8
77 0 my ( $elem ) = @_;
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 1 ▶sub _is_in_conditional_logic {
Subroutine _is_in_conditional_logic: called CC=6 SCAR=27.6
88 1 my ( $elem ) = @_;
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 _is_compound_statement($elem)
Branch: 1 branch
BranchTrueFalseTotal
if _is_compound_statement($elem) or _is_eval($elem) or _is_in_do_conditional_block($elem) 0 0 0
Condition0%0/6
_is_compound_statement($elem) or _is_eval($elem) or _is_in_do_conditional_block($elem)
l!l&&r!l&&!r
000
_is_compound_statement($elem) or _is_eval($elem)
l!l&&r!l&&!r
000
MC/DC0%0/3
_is_compound_statement($elem) or _is_eval($elem) or _is_in_do_conditional_block($elem)
_is_compound_statement($elem) _is_eval($elem) _is_in_do_conditional_block($elem)
Truth table0%0/4
A or B or C
A= _is_compound_statement($elem)B= _is_eval($elem)C= _is_in_do_conditional_block($elem)
ABCresult
1XX1
01X1
0011
0000
92 || _is_eval($elem)
93 || _is_in_do_conditional_block($elem);
94 }
95 1 return;
96 }
97  
98 40 1;
99  
100 __END__
« lib/Perl/Critic/Policy/Variables/ProhibitPunctuationVars.pm ↑ Summary lib/Perl/Critic/Policy/Documentation/PodSpelling.pm »