lib/Perl/Critic/Policy/ControlStructures/ProhibitUnreachableCode.pm

filter: Statementstmt 83.0% 44 / 53 Branchbran 34.6% 9 / 26 Conditioncond 27.7% 5 / 18 MC/DCmcdc 16.6% 2 / 12 Subroutinesub 100% 12 / 12 Podpod 80.0% 4 / 5 totaltotal 60.3% 76 / 126 CC 21 SCAR 37.8
CC
21
Cov
60%
_gather_violations
106.3
violates
11.0
BEGIN
1.0
CRAP 43.6
« lib/Perl/Critic/Policy/CodeLayout/RequireTrailingCommas.pm ↑ Summary lib/Perl/Critic/Policy/InputOutput/ProhibitBarewordFileHandles.pm »
1 package Perl::Critic::Policy::ControlStructures::ProhibitUnreachableCode;
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 :data_conversion :classification };
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 40 Readonly::Array my @TERMINALS => qw( die exit croak confess );
14 40 Readonly::Hash my %TERMINALS => hashify( @TERMINALS );
15  
16 40 Readonly::Array my @CONDITIONALS => qw( if unless foreach while until for );
17 40 Readonly::Hash my %CONDITIONALS => hashify( @CONDITIONALS );
18  
19 40 Readonly::Array my @OPERATORS => qw( && || // and or err ? );
20 40 Readonly::Hash my %OPERATORS => hashify( @OPERATORS );
21  
22 #-----------------------------------------------------------------------------
23  
24 40 Readonly::Scalar my $DESC => q{Unreachable code};
25 40 Readonly::Scalar my $EXPL => q{Consider removing it};
26  
27 #-----------------------------------------------------------------------------
28  
29 90 ▶sub supported_parameters { return () }
Subroutine supported_parameters: called CC=1 SCAR=0.0
Pod undocumented
30 74 ▶sub default_severity { return $SEVERITY_HIGH }
Subroutine default_severity: called CC=1 SCAR=0.0
Pod documented
31 73 ▶sub default_themes { return qw( core bugs certrec ) }
Subroutine default_themes: called CC=1 SCAR=0.0
Pod documented
32 33 ▶sub applies_to { return 'PPI::Token::Word' }
Subroutine applies_to: called CC=1 SCAR=0.0
Pod documented
33  
34 #-----------------------------------------------------------------------------
35  
36 349 ▶sub violates {
Subroutine violates: called CC=10 SCAR=24.0
Pod documented
37 349 my ( $self, $elem, undef ) = @_;
38  
39 349 my $statement = $elem->statement();
40 349 ▶ return if not $statement;
Branch: 1 branch
BranchTrueFalseTotal
unless $statement 0 349 349
41  
42 # We check to see if this is an interesting token before calling
43 # is_function_call(). This weeds out most candidate tokens and
44 # prevents us from having to make an expensive function call.
45  
46 349 ▶ return if ( !exists $TERMINALS{$elem} ) &&
Branch: 1 branch
BranchTrueFalseTotal
unless exists $TERMINALS{$elem} or $statement->isa("PPI::Statement::Break") 342 7 349
Condition66%2/3
exists $TERMINALS{$elem} or $statement->isa("PPI::Statement::Break")
l!l&&r!l&&!r
07342
MC/DC50%1/2
exists $TERMINALS{$elem} or $statement->isa("PPI::Statement::Break")
exists $TERMINALS{$elem} $statement->isa("PPI::Statement::Break")
Truth table66%2/3
A or B
A= exists $TERMINALS{$elem}B= $statement->isa("PPI::Statement::Break")
ABresult
1X1
011
000
47 ( !$statement->isa('PPI::Statement::Break') );
48  
49 7 ▶ return if not is_function_call($elem);
Branch: 1 branch
BranchTrueFalseTotal
unless is_function_call($elem) 1 6 7
50  
51 # Scan the enclosing statement for conditional keywords or logical
52 # operators. If any are found, then this the following statements
53 # could _potentially_ be executed, so this policy is satisfied.
54  
55 # NOTE: When the first operand in a boolean expression is
56 # C<croak> or C<die>, etc., the second operand is technically
57 # unreachable. But this policy doesn't catch that situation.
58  
59 6 for my $child ( $statement->schildren() ) {
60 18 ▶ return if exists $OPERATORS{$child} && $child->isa('PPI::Token::Operator');
Branch: 1 branch
BranchTrueFalseTotal
if exists $OPERATORS{$child} and $child->isa("PPI::Token::Operator") 0 18 18
Condition33%1/3
exists $OPERATORS{$child} and $child->isa("PPI::Token::Operator")
!ll&&!rl&&r
1800
MC/DC0%0/2
exists $OPERATORS{$child} and $child->isa("PPI::Token::Operator")
exists $OPERATORS{$child} $child->isa("PPI::Token::Operator")
Truth table33%1/3
A and B
A= exists $OPERATORS{$child}B= $child->isa("PPI::Token::Operator")
ABresult
0X0
100
111
61 18 ▶ return if exists $CONDITIONALS{$child} && $child->isa('PPI::Token::Word');
Branch: 1 branch
BranchTrueFalseTotal
if exists $CONDITIONALS{$child} and $child->isa("PPI::Token::Word") 1 17 18
Condition66%2/3
exists $CONDITIONALS{$child} and $child->isa("PPI::Token::Word")
!ll&&!rl&&r
1701
MC/DC50%1/2
exists $CONDITIONALS{$child} and $child->isa("PPI::Token::Word")
exists $CONDITIONALS{$child} $child->isa("PPI::Token::Word")
Truth table66%2/3
A and B
A= exists $CONDITIONALS{$child}B= $child->isa("PPI::Token::Word")
ABresult
0X0
100
111
62 }
63  
64 5 return $self->_gather_violations($statement);
65 }
66  
67 5 ▶sub _gather_violations {
Subroutine _gather_violations: called CC=12 SCAR=46.7
68 5 my ($self, $statement) = @_;
69  
70 # If we get here, then the statement contained an unconditional
71 # die or exit or return. Then all the subsequent sibling
72 # statements are unreachable, except for those that have labels,
73 # which could be reached from anywhere using C<goto>. Subroutine
74 # declarations are also exempt for the same reason. "use" and
75 # "our" statements are exempt because they happen at compile time.
76  
77 5 my @violations;
78 5 ▶ while ( $statement = $statement->snext_sibling() ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($statement = $statement->snext_sibling) 0 5 5
79 0 my @children = $statement->schildren();
80 0 ▶ last if @children && $children[0]->isa('PPI::Token::Label');
Branch: 1 branch
BranchTrueFalseTotal
if @children and $children[0]->isa("PPI::Token::Label") 0 0 0
Condition0%0/3
@children and $children[0]->isa("PPI::Token::Label")
!ll&&!rl&&r
000
MC/DC0%0/2
@children and $children[0]->isa("PPI::Token::Label")
@children $children[0]->isa("PPI::Token::Label")
Truth table0%0/3
A and B
A= @childrenB= $children[0]->isa("PPI::Token::Label")
ABresult
0X0
100
111
81 0 ▶ next if $statement->isa('PPI::Statement::Sub');
Branch: 1 branch
BranchTrueFalseTotal
if $statement->isa("PPI::Statement::Sub") 0 0 0
82 0 ▶ next if $statement->isa('PPI::Statement::End');
Branch: 1 branch
BranchTrueFalseTotal
if $statement->isa("PPI::Statement::End") 0 0 0
83 0 ▶ next if $statement->isa('PPI::Statement::Data');
Branch: 1 branch
BranchTrueFalseTotal
if $statement->isa("PPI::Statement::Data") 0 0 0
84 0 ▶ next if $statement->isa('PPI::Statement::Package');
Branch: 1 branch
BranchTrueFalseTotal
if $statement->isa("PPI::Statement::Package") 0 0 0
85  
86 0 ▶ next if $statement->isa('PPI::Statement::Include') &&
Branch: 1 branch
BranchTrueFalseTotal
if $statement->isa("PPI::Statement::Include") and $statement->type ne "require" 0 0 0
Condition0%0/3
$statement->isa("PPI::Statement::Include") and $statement->type ne "require"
!ll&&!rl&&r
000
MC/DC0%0/2
$statement->isa("PPI::Statement::Include") and $statement->type ne "require"
$statement->isa("PPI::Statement::Include") $statement->type ne "require"
Truth table0%0/3
A and B
A= $statement->isa("PPI::Statement::Include")B= $statement->type ne "require"
ABresult
0X0
100
111
87 $statement->type() ne 'require';
88  
89 0 ▶ next if $statement->isa('PPI::Statement::Variable') &&
Branch: 1 branch
BranchTrueFalseTotal
if $statement->isa("PPI::Statement::Variable") and $statement->type eq "our" 0 0 0
Condition0%0/3
$statement->isa("PPI::Statement::Variable") and $statement->type eq "our"
!ll&&!rl&&r
000
MC/DC0%0/2
$statement->isa("PPI::Statement::Variable") and $statement->type eq "our"
$statement->isa("PPI::Statement::Variable") $statement->type eq "our"
Truth table0%0/3
A and B
A= $statement->isa("PPI::Statement::Variable")B= $statement->type eq "our"
ABresult
0X0
100
111
90 $statement->type() eq 'our';
91  
92 0 push @violations, $self->violation( $DESC, $EXPL, $statement );
93 }
94  
95 5 return @violations;
96 }
97  
98 40 1;
99  
100 __END__
« lib/Perl/Critic/Policy/CodeLayout/RequireTrailingCommas.pm ↑ Summary lib/Perl/Critic/Policy/InputOutput/ProhibitBarewordFileHandles.pm »