Policy/Subroutines/RequireConsistentReturn.pm

filter: Statementstmt 92.4% 122 / 132 Branchbran 83.9% 47 / 56 Conditioncond 82.8% 29 / 35 MC/DCmcdc 70.0% 14 / 20 Subroutinesub 94.1% 16 / 17 Podpod 60.0% 6 / 10 totaltotal 86.6% 234 / 270 CC 44 SCAR 21.9
CC
44
Cov
89%
__ANON__
16.0
violates
12.2
prepare_to_scan_document
7.4
CRAP 8.9
« Policy/Subroutines/RequireSubOrder.pm ↑ Summary Policy/ValuesAndExpressions/RequireSlices.pm »
1 package Perl::Critic::Policy::Subroutines::RequireConsistentReturn;
2  
3 11 ▶use 5.010001;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 11 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 11 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 11 ▶use Readonly;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 11 ▶use Perl::Critic::Utils qw/:severities :classification/;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 11 ▶use base 'Perl::Critic::Policy';
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 11 our $VERSION = '0.1.4';
12  
13 11 Readonly::Scalar my $DESC=>q{Don't use an implicit return in a subroutine with explicit returns.};
14 11 Readonly::Scalar my $EXPL=>undef;
15  
16 11 Readonly::Hash my %postconditional=>map {$_=>1} qw/for foreach if unless until when while/;
17 11 Readonly::Hash my %nonreturning =>map {$_=>1} qw/while foreach for/;
18 11 Readonly::Hash my %terminators =>map {$_=>1} qw/
19 confess
20 croak
21 die
22 exec
23 exit
24 goto
25 return
26 throw
27 ...
28 /;
29  
30 #-----------------------------------------------------------------------------
31  
32 3 ▶sub supported_parameters {
Subroutine supported_parameters: called CC=1 SCAR=0.0
Pod undocumented
33 return (
34 {
35 3 name => 'bare',
36 description => 'Include bare "return;" statements.',
37 default_string => '0',
38 behavior => 'boolean',
39 },
40 );
41 }
42 114 ▶sub applies_to { return qw/PPI::Statement/ }
Subroutine applies_to: called CC=1 SCAR=0.0
Pod documented
43 34 ▶sub default_severity { return $SEVERITY_LOW }
Subroutine default_severity: called CC=1 SCAR=0.0
Pod documented
44 0 ▶sub default_themes { return qw/cosmetic/ }
Subroutine default_themes: not called CC=1 SCAR=6.9
Pod documented
45  
46 #-----------------------------------------------------------------------------
47  
48 34 ▶sub invalid {
Subroutine invalid: called CC=3 SCAR=12.0
Pod undocumented
49 34 my ($self,$elem,$note)=@_;
50 34 ▶ $note//='';
Condition50%1/2
$note //= ""
l!l
034
MC/DC0%0/1
$note //= ""
$note
Truth table50%1/2
A
A= $note
Aresult
11
00
51 0 ▶ if($note) { $note=" ($note)" }
Branch: 1 branch
BranchTrueFalseTotal
if ($note) 0 34 34
52 34 return $self->violation(sprintf("%s%s",$DESC,$note),$EXPL,$elem);
53 }
54  
55 # Create a subroutine cache inside the policy instance. While unlikely, this permits
56 # parallel scans launched by perlcritic, and prevents bleed across scanned documents.
57 11 my $cachekey;
58 3 ▶sub initialize_if_enabled {
Subroutine initialize_if_enabled: called CC=4 SCAR=15.7
Pod documented
59 3 my ($self,$config)=@_;
60 3 my $name='__REQUIRECRETURN';
61 3 my $suffix=int(rand(1e9));
62 3 my $retry=3;
63 0 ▶ while($retry&&exists($$self{"$name$suffix"})) { $suffix=int(rand(1e9)); $retry-- }
Branch: 1 branch
BranchTrueFalseTotal
if ($retry and exists $$self{"$name$suffix"}) 0 3 3
Condition33%1/3
$retry and exists $$self{"$name$suffix"}
!ll&&!rl&&r
030
MC/DC0%0/2
$retry and exists $$self{"$name$suffix"}
$retry exists $$self{"$name$suffix"}
Truth table33%1/3
A and B
A= $retryB= exists $$self{"$name$suffix"}
ABresult
0X0
100
111
64 0 ▶ if(exists($$self{"$name$suffix"})) { warn 'RequireConsistentReturn unable to build subroutine cache' }
Branch: 1 branch
BranchTrueFalseTotal
if (exists $$self{"$name$suffix"}) { } 0 3 3
65 else {
66 3 $cachekey="$name$suffix";
67 3 $$self{$cachekey}={cache=>{},recent=>[]};
68 }
69 3 return 1;
70 }
71  
72 175 ▶sub hasReturn {
Subroutine hasReturn: called CC=2 SCAR=6.9
Pod undocumented
73 175 my ($bare,$block)=@_;
74 175 ▶ if($block->find_first(sub {
Subroutine __ANON__: called CC=16 SCAR=27.7
Branch: 1 branch
BranchTrueFalseTotal
if ($block->find_first(sub { my(undef, $e) = @_; if ($e->isa("PPI::Statement::Sub")) { return undef; } ; if ($e->isa("PPI::Structure::Block")) { my $prev = $e->sprevious_sibling; if ($prev and $prev->isa("PPI::Token::Word") and $prev->content eq "sub") { return undef; } ; return 0; } ; if ($e->isa("PPI::Statement::Break")) { return $bare; } ; if ($e->isa("PPI::Token::Word") and is_function_call($e) and $e->content eq "return") { if ($bare) { return 1; } ; my $next = $e->snext_sibling; unless ($next) { return 0; } ; if ($next->isa("PPI::Token::Structure") and $next->content eq ";") { return 0; } ; if ($next->isa("PPI::Token::Word") and $postconditional{$next->content}) { return 0; } ; return 1; } ; return 0; } )) 102 73 175
75 1686 my (undef,$e)=@_;
76 1686 ▶ if($e->isa('PPI::Statement::Sub')) { return undef }
Branch: 1 branch
BranchTrueFalseTotal
if ($e->isa("PPI::Statement::Sub")) 4 1682 1686
77 1682 ▶ if($e->isa('PPI::Structure::Block')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($e->isa("PPI::Structure::Block")) 171 1511 1682
78 171 my $prev=$e->sprevious_sibling();
79 171 ▶ if($prev && $prev->isa('PPI::Token::Word') && ($prev->content() eq 'sub')) { return undef }
Branch: 1 branch
BranchTrueFalseTotal
if ($prev and $prev->isa("PPI::Token::Word") and $prev->content eq "sub") 8 163 171
Condition100%6/6
$prev and $prev->isa("PPI::Token::Word") and $prev->content eq "sub"
!ll&&!rl&&r
149148
$prev and $prev->isa("PPI::Token::Word")
!ll&&!rl&&r
777222
MC/DC100%3/3
$prev and $prev->isa("PPI::Token::Word") and $prev->content eq "sub"
$prev $prev->isa("PPI::Token::Word") $prev->content eq "sub"
Truth table100%4/4
A and B and C
A= $prevB= $prev->isa("PPI::Token::Word")C= $prev->content eq "sub"
ABCresult
0XX0
10X0
1100
1111
80 163 return 0;
81 }
82 1511 ▶ if($e->isa('PPI::Statement::Break')) { return $bare }
Branch: 1 branch
BranchTrueFalseTotal
if ($e->isa("PPI::Statement::Break")) 134 1377 1511
83 1377 ▶ if($e->isa('PPI::Token::Word') && is_function_call($e) && ($e->content() eq 'return')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($e->isa("PPI::Token::Word") and is_function_call($e) and $e->content eq "return") 69 1308 1377
Condition100%6/6
$e->isa("PPI::Token::Word") and is_function_call($e) and $e->content eq "return"
!ll&&!rl&&r
12921669
$e->isa("PPI::Token::Word") and is_function_call($e)
!ll&&!rl&&r
118310985
MC/DC100%3/3
$e->isa("PPI::Token::Word") and is_function_call($e) and $e->content eq "return"
$e->isa("PPI::Token::Word") is_function_call($e) $e->content eq "return"
Truth table100%4/4
A and B and C
A= $e->isa("PPI::Token::Word")B= is_function_call($e)C= $e->content eq "return"
ABCresult
0XX0
10X0
1100
1111
84 69 ▶ if($bare) { return 1 }
Branch: 1 branch
BranchTrueFalseTotal
if ($bare) 1 68 69
85 68 my $next=$e->snext_sibling();
86 68 ▶ if(!$next) { return 0 }
Branch: 1 branch
BranchTrueFalseTotal
unless ($next) 16 52 68
87 52 ▶ if($next->isa('PPI::Token::Structure')&&($next->content() eq ';')) { return 0 }
Branch: 1 branch
BranchTrueFalseTotal
if ($next->isa("PPI::Token::Structure") and $next->content eq ";") 10 42 52
Condition66%2/3
$next->isa("PPI::Token::Structure") and $next->content eq ";"
!ll&&!rl&&r
42010
MC/DC50%1/2
$next->isa("PPI::Token::Structure") and $next->content eq ";"
$next->isa("PPI::Token::Structure") $next->content eq ";"
Truth table66%2/3
A and B
A= $next->isa("PPI::Token::Structure")B= $next->content eq ";"
ABresult
0X0
100
111
88 42 ▶ if($next->isa('PPI::Token::Word')&&$postconditional{$next->content()}) { return 0 }
Branch: 1 branch
BranchTrueFalseTotal
if ($next->isa("PPI::Token::Word") and $postconditional{$next->content}) 8 34 42
Condition66%2/3
$next->isa("PPI::Token::Word") and $postconditional{$next->content}
!ll&&!rl&&r
3408
MC/DC50%1/2
$next->isa("PPI::Token::Word") and $postconditional{$next->content}
$next->isa("PPI::Token::Word") $postconditional{$next->content}
Truth table66%2/3
A and B
A= $next->isa("PPI::Token::Word")B= $postconditional{$next->content}
ABresult
0X0
100
111
89 34 return 1;
90 }
91 1308 return 0;
92 102 })) { return 1 }
93 73 return;
94 }
95  
96 222 ▶sub finals {
Subroutine finals: called CC=6 SCAR=17.9
Pod undocumented
97 222 my ($block)=@_;
98 222 my @res;
99 222 my $final=($block->schildren())[-1];
100 222 ▶ if(!$final) { return }
Branch: 1 branch
BranchTrueFalseTotal
unless ($final) 1 221 222
101 221 ▶ if($final->isa('PPI::Statement::Compound')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($final->isa("PPI::Statement::Compound")) 69 152 221
102 69 my $type=$final->type(); # maybe not needed
103 69 ▶ if($nonreturning{$type}) { return }
Branch: 1 branch
BranchTrueFalseTotal
if ($nonreturning{$type}) 7 62 69
104 62 ▶ if($type eq 'continue') {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "continue") 30 32 62
105 30 my $inner=($final->schildren())[-1];
106 30 ▶ if($inner->isa('PPI::Structure::Block')) { return finals($inner) }
Branch: 1 branch
BranchTrueFalseTotal
if ($inner->isa("PPI::Structure::Block")) 30 0 30
107 0 die "RequireConsistentReturn encountered a 'continue' with no inner block";
108 }
109 32 return map {finals($_)} grep {$_->isa('PPI::Structure::Block')} $final->schildren();
110 }
111 152 return $final;
112 }
113  
114 # For each document, build the cache of final statements within the subroutines.
115 # This ensures that each subroutine is only scanned one time for inside-return statements.
116 114 ▶sub prepare_to_scan_document {
Subroutine prepare_to_scan_document: called CC=7 SCAR=20.0
Pod documented
117 114 my ($self,$doc)=@_;
118 0 ▶ if(!$cachekey) { return 1 }
Branch: 1 branch
BranchTrueFalseTotal
unless ($cachekey) 0 114 114
119 114 $$self{$cachekey}={cache=>{}};
120 114 my $cache=$$self{$cachekey}{cache};
121 114 ▶ foreach my $sub (@{ $doc->find('PPI::Statement::Sub')||[] }) {
Condition50%1/2
$doc->find('PPI::Statement::Sub') || []
l!l
1140
MC/DC0%0/1
$doc->find('PPI::Statement::Sub') || []
$doc->find('PPI::Statement::Sub')
Truth table50%1/2
A
A= $doc->find('PPI::Statement::Sub')
Aresult
11
00
122 114 my $block=$sub->block();
123 0 ▶ if(!$block) { next }
Branch: 1 branch
BranchTrueFalseTotal
unless ($block) 0 114 114
124 114 my $returns=hasReturn($$self{_bare},$block);
125 114 foreach my $final (finals($block)) {
126 133 ▶ my $skey=join(',',map {$_//'U'} @{$final->location()});
Condition100%2/2
$_ // "U"
l!l
532133
MC/DC100%1/1
$_ // "U"
$_
Truth table100%2/2
A
A= $_
Aresult
11
00
127 133 $$cache{$skey}={sub=>$sub,returns=>$returns};
128 }
129 }
130 114 return 1;
131 }
132  
133 541 ▶sub violates {
Subroutine violates: called CC=12 SCAR=25.0
Pod documented
134 541 my ($self,$elem,undef)=@_;
135 0 ▶ if(!$cachekey) { return }
Branch: 1 branch
BranchTrueFalseTotal
unless ($cachekey) 0 541 541
136 541 ▶ my $skey=join(',',map {$_//'U'} @{$elem->location()});
Condition100%2/2
$_ // "U"
l!l
2164541
MC/DC100%1/1
$_ // "U"
$_
Truth table100%2/2
A
A= $_
Aresult
11
00
137 541 my $cache=$$self{$cachekey}{cache}{$skey};
138 541 ▶ if(!$cache) { return }
Branch: 1 branch
BranchTrueFalseTotal
unless ($cache) 408 133 541
139 133 ▶ if(!$$cache{returns}) { return }
Branch: 1 branch
BranchTrueFalseTotal
unless ($cache->{"returns"}) 44 89 133
140 89 ▶ if($elem->isa('PPI::Statement::Break')) { return } # valid
Branch: 1 branch
BranchTrueFalseTotal
if ($elem->isa("PPI::Statement::Break")) 45 44 89
141 0 ▶ if($elem->isa('PPI::Statement::Compound')) { return } # ::Critic will pick up the inside statements
Branch: 1 branch
BranchTrueFalseTotal
if ($elem->isa("PPI::Statement::Compound")) 0 44 44
142 #
143 # everything else is an PPI::Statement via applies_to
144 44 my ($first)=$elem->schildren();
145 44 ▶ if($first) {
Branch: 1 branch
BranchTrueFalseTotal
if ($first) 44 0 44
146 44 ▶ if($first->isa('PPI::Token::Word') &&$terminators{$first->content()}) { return }
Branch: 1 branch
BranchTrueFalseTotal
if ($first->isa("PPI::Token::Word") and $terminators{$first->content}) 9 35 44
Condition100%3/3
$first->isa("PPI::Token::Word") and $terminators{$first->content}
!ll&&!rl&&r
3149
MC/DC100%2/2
$first->isa("PPI::Token::Word") and $terminators{$first->content}
$first->isa("PPI::Token::Word") $terminators{$first->content}
Truth table100%3/3
A and B
A= $first->isa("PPI::Token::Word")B= $terminators{$first->content}
ABresult
0X0
100
111
147 35 ▶ if($first->isa('PPI::Token::Operator')&&$terminators{$first->content()}) { return }
Branch: 1 branch
BranchTrueFalseTotal
if ($first->isa("PPI::Token::Operator") and $terminators{$first->content}) 1 34 35
Condition100%3/3
$first->isa("PPI::Token::Operator") and $terminators{$first->content}
!ll&&!rl&&r
3221
MC/DC100%2/2
$first->isa("PPI::Token::Operator") and $terminators{$first->content}
$first->isa("PPI::Token::Operator") $terminators{$first->content}
Truth table100%3/3
A and B
A= $first->isa("PPI::Token::Operator")B= $terminators{$first->content}
ABresult
0X0
100
111
148 }
149 34 return $self->invalid($elem->schild(0));
150 }
151  
152 #-----------------------------------------------------------------------------
153  
154 11 1;
155  
156 __END__
« Policy/Subroutines/RequireSubOrder.pm ↑ Summary Policy/ValuesAndExpressions/RequireSlices.pm »