lib/Perl/Critic/Policy/Subroutines/RequireFinalReturn.pm

filter: Statementstmt 52.7% 67 / 127 Branchbran 22.0% 15 / 68 Conditioncond 18.0% 9 / 50 MC/DCmcdc 6.4% 2 / 31 Subroutinesub 72.4% 21 / 29 Podpod 80.0% 4 / 5 totaltotal 38.0% 118 / 310 CC 55 SCAR 39.9
CC
55
Cov
37%
_is_given_when_return
192.0
_is_terminal_stmnt
72.0
_is_compound_return
50.9
CRAP 54.2
« lib/Perl/Critic/Policy/BuiltinFunctions/ProhibitStringyEval.pm ↑ Summary lib/Perl/Critic/Policy/Objects/ProhibitIndirectSyntax.pm »
1 package Perl::Critic::Policy::Subroutines::RequireFinalReturn;
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 List::SomeUtils qw(any);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 40 ▶use Perl::Critic::Exception::Fatal::Internal qw{ throw_internal };
Subroutine BEGIN: called CC=1 SCAR=0.0
10 40 ▶use Perl::Critic::Utils qw{ :characters :severities :data_conversion };
Subroutine BEGIN: called CC=1 SCAR=0.0
11 40 ▶use parent 'Perl::Critic::Policy';
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 40 our $VERSION = '1.156';
14  
15 #-----------------------------------------------------------------------------
16  
17 40 Readonly::Scalar my $EXPL => [ 197 ];
18  
19 40 Readonly::Hash my %CONDITIONALS => hashify( qw(if unless for foreach) );
20  
21 #-----------------------------------------------------------------------------
22  
23 92 ▶sub supported_parameters {
Subroutine supported_parameters: called CC=1 SCAR=0.0
Pod undocumented
24 return (
25 {
26 92 name => 'terminal_funcs',
27 description => 'The additional subroutines to treat as terminal.',
28 default_string => $EMPTY,
29 behavior => 'string list',
30 list_always_present_values =>
31 [ qw< croak confess die exec exit throw Carp::confess Carp::croak ...> ],
32 },
33 {
34 name => 'terminal_methods',
35 description => 'The additional methods to treat as terminal.',
36 default_string => $EMPTY,
37 behavior => 'string list',
38 list_always_present_values => [],
39 },
40 );
41 }
42  
43 75 ▶sub default_severity { return $SEVERITY_HIGH }
Subroutine default_severity: called CC=1 SCAR=0.0
Pod documented
44 91 ▶sub default_themes { return qw( core bugs pbp certrec ) }
Subroutine default_themes: called CC=1 SCAR=0.0
Pod documented
45 33 ▶sub applies_to { return 'PPI::Statement::Sub' }
Subroutine applies_to: called CC=1 SCAR=0.0
Pod documented
46  
47 #-----------------------------------------------------------------------------
48  
49 5 ▶sub violates {
Subroutine violates: called CC=7 SCAR=21.0
Pod documented
50 5 my ( $self, $elem, undef ) = @_;
51  
52 # skip BEGIN{} and INIT{} and END{} etc
53 5 ▶ return if $elem->isa('PPI::Statement::Scheduled');
Branch: 1 branch
BranchTrueFalseTotal
if $elem->isa("PPI::Statement::Scheduled") 0 5 5
54  
55 5 my @blocks = grep {$_->isa('PPI::Structure::Block')} $elem->schildren();
56 5 ▶ if (@blocks > 1) {
Branch: 2 branches
BranchTrueFalseTotal
if (@blocks > 1) { } 0 5 5
elsif (@blocks == 0) { } 0 5 5
57 # sanity check
58 0 throw_internal 'Subroutine should have no more than one block';
59 }
60 elsif (@blocks == 0) {
61 #Technically, subroutines don't have to have a block at all. In
62 # that case, its just a declaration so this policy doesn't really apply
63 0 return; # ok!
64 }
65  
66  
67 5 my ($block) = @blocks;
68 5 ▶ if (_block_is_empty($block) || $self->_block_has_return($block)) {
Branch: 1 branch
BranchTrueFalseTotal
if (_block_is_empty($block) or $self->_block_has_return($block)) 4 1 5
Condition66%2/3
_block_is_empty($block) or $self->_block_has_return($block)
l!l&&r!l&&!r
041
MC/DC50%1/2
_block_is_empty($block) or $self->_block_has_return($block)
_block_is_empty($block) $self->_block_has_return($block)
Truth table66%2/3
A or B
A= _block_is_empty($block)B= $self->_block_has_return($block)
ABresult
1X1
011
000
69 4 return; # OK
70 }
71  
72 # Must be a violation
73 1 my $desc;
74 1 ▶ if ( my $name = $elem->name() ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $name = $elem->name) { } 1 0 1
75 1 $desc = qq<Subroutine "$name" does not end with "return">;
76 }
77 else {
78 0 $desc = q<Subroutine does not end with "return">;
79 }
80  
81 1 return $self->violation( $desc, $EXPL, $elem );
82 }
83  
84 #-----------------------------------------------------------------------------
85  
86 5 ▶sub _block_is_empty {
Subroutine _block_is_empty: called CC=1 SCAR=0.0
87 5 my ( $block ) = @_;
88 5 return $block->schildren() == 0;
89 }
90  
91 #-----------------------------------------------------------------------------
92  
93 5 ▶sub _block_has_return {
Subroutine _block_has_return: called CC=4 SCAR=14.3
94 5 my ( $self, $block ) = @_;
95 5 my @blockparts = $block->schildren();
96 5 my $final = $blockparts[-1]; # always defined because we call _block_is_empty first
97 5 ▶ return if !$final;
Branch: 1 branch
BranchTrueFalseTotal
unless $final 0 5 5
98 5 ▶ return $self->_is_explicit_return($final)
Condition66%4/6
$self->_is_explicit_return($final) || $self->_is_given_when_return($final) || $self->_is_compound_return($final)
l!l&&r!l&&!r
401
$self->_is_explicit_return($final) or $self->_is_given_when_return($final)
l!l&&r!l&&!r
401
MC/DC33%1/3
$self->_is_explicit_return($final) || $self->_is_given_when_return($final) || $self->_is_compound_return($final)
$self->_is_explicit_return($final) $self->_is_given_when_return($final) $self->_is_compound_return($final)
Truth table50%2/4
A or B || C
A= $self->_is_explicit_return($final)B= $self->_is_given_when_return($final)C= $self->_is_compound_return($final)
ABCresult
1XX1
01X1
0011
0000
99 || $self->_is_given_when_return($final)
100 || $self->_is_compound_return($final);
101 }
102  
103 #-----------------------------------------------------------------------------
104  
105 5 ▶sub _is_explicit_return {
Subroutine _is_explicit_return: called CC=3 SCAR=11.4
106 5 my ( $self, $final ) = @_;
107  
108 5 ▶ return if _is_conditional_stmnt( $final );
Branch: 1 branch
BranchTrueFalseTotal
if _is_conditional_stmnt($final) 1 4 5
109 4 ▶ return _is_return_or_goto_stmnt( $final )
Condition33%1/3
_is_return_or_goto_stmnt($final) || $self->_is_terminal_stmnt($final)
l!l&&r!l&&!r
400
MC/DC0%0/2
_is_return_or_goto_stmnt($final) || $self->_is_terminal_stmnt($final)
_is_return_or_goto_stmnt($final) $self->_is_terminal_stmnt($final)
Truth table33%1/3
A || B
A= _is_return_or_goto_stmnt($final)B= $self->_is_terminal_stmnt($final)
ABresult
1X1
011
000
110 || $self->_is_terminal_stmnt( $final );
111 }
112  
113 #-----------------------------------------------------------------------------
114  
115 1 ▶sub _is_compound_return {
Subroutine _is_compound_return: called CC=8 SCAR=39.3
116 1 my ( $self, $final ) = @_;
117  
118 1 ▶ if (!$final->isa('PPI::Statement::Compound')) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($final->isa("PPI::Statement::Compound")) 1 0 1
119 1 return; #fail
120 }
121  
122 0 my $begin = $final->schild(0);
123 0 ▶ return if !$begin; #fail
Branch: 1 branch
BranchTrueFalseTotal
unless $begin 0 0 0
124  
125 0 state $is_if_or_unless = { hashify( qw( if unless ) ) };
126 0 ▶ if (!($begin->isa('PPI::Token::Word') && $is_if_or_unless->{$begin->content()})) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($begin->isa("PPI::Token::Word") and $$is_if_or_unless{$begin->content}) 0 0 0
Condition0%0/3
$begin->isa("PPI::Token::Word") and $$is_if_or_unless{$begin->content}
!ll&&!rl&&r
000
MC/DC0%0/2
$begin->isa("PPI::Token::Word") and $$is_if_or_unless{$begin->content}
$begin->isa("PPI::Token::Word") $$is_if_or_unless{$begin->content}
Truth table0%0/3
A and B
A= $begin->isa("PPI::Token::Word")B= $$is_if_or_unless{$begin->content}
ABresult
0X0
100
111
127 0 return; #fail
128 }
129  
130 0 ▶ my @blocks = grep {!$_->isa('PPI::Structure::Condition') &&
Condition0%0/3
$_->isa('PPI::Structure::Condition') || $_->isa('PPI::Token')
l!l&&r!l&&!r
000
MC/DC0%0/2
$_->isa('PPI::Structure::Condition') || $_->isa('PPI::Token')
$_->isa('PPI::Structure::Condition') $_->isa('PPI::Token')
Truth table0%0/3
A || B
A= $_->isa('PPI::Structure::Condition')B= $_->isa('PPI::Token')
ABresult
1X1
011
000
131 !$_->isa('PPI::Token')} $final->schildren();
132 # Sanity check:
133 0 ▶ if (any { !$_->isa('PPI::Structure::Block') } @blocks) {
Subroutine __ANON__: not called CC=1 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
if (&any(sub { not $_->isa("PPI::Structure::Block"); } , @blocks)) 0 0 0
134 0 throw_internal
135 'Expected only conditions, blocks and tokens in the if statement';
136 }
137  
138 0 ▶ return if any { ! $self->_block_has_return($_) } @blocks;
Subroutine __ANON__: not called CC=1 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
if &any(sub { not $self->_block_has_return($_); } , @blocks) 0 0 0
139  
140 0 return 1;
141 }
142  
143 #-----------------------------------------------------------------------------
144  
145 1 ▶sub _is_given_when_return {
Subroutine _is_given_when_return: called CC=15 SCAR=52.6
146 1 my ( $self, $final ) = @_;
147  
148 1 ▶ if ( ! $final->isa( 'PPI::Statement::Given' ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($final->isa("PPI::Statement::Given")) 1 0 1
149 1 return; #fail
150 }
151  
152 0 my $begin = $final->schild(0);
153 0 ▶ return if !$begin; #fail
Branch: 1 branch
BranchTrueFalseTotal
unless $begin 0 0 0
154 0 ▶ if ( ! ( $begin->isa( 'PPI::Token::Word' ) &&
Branch: 1 branch
BranchTrueFalseTotal
unless ($begin->isa("PPI::Token::Word") and $begin->content eq "given") 0 0 0
Condition0%0/3
$begin->isa("PPI::Token::Word") and $begin->content eq "given"
!ll&&!rl&&r
000
MC/DC0%0/2
$begin->isa("PPI::Token::Word") and $begin->content eq "given"
$begin->isa("PPI::Token::Word") $begin->content eq "given"
Truth table0%0/3
A and B
A= $begin->isa("PPI::Token::Word")B= $begin->content eq "given"
ABresult
0X0
100
111
155 $begin->content() eq 'given' ) ) {
156 0 return; #fail
157 }
158  
159 0 ▶ my @blocks = grep {!$_->isa( 'PPI::Structure::Given' ) &&
Condition0%0/3
$_->isa('PPI::Structure::Given') || $_->isa('PPI::Token')
l!l&&r!l&&!r
000
MC/DC0%0/2
$_->isa('PPI::Structure::Given') || $_->isa('PPI::Token')
$_->isa('PPI::Structure::Given') $_->isa('PPI::Token')
Truth table0%0/3
A || B
A= $_->isa('PPI::Structure::Given')B= $_->isa('PPI::Token')
ABresult
1X1
011
000
160 !$_->isa( 'PPI::Token' )} $final->schildren();
161 # Sanity check:
162 0 ▶ if (any { !$_->isa('PPI::Structure::Block') } @blocks) {
Subroutine __ANON__: not called CC=1 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
if (&any(sub { not $_->isa("PPI::Structure::Block"); } , @blocks)) 0 0 0
163 0 throw_internal
164 'Expected only givens, blocks and tokens in the given statement';
165 }
166 0 ▶ if (@blocks > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@blocks > 1) 0 0 0
167 # sanity check
168 0 throw_internal 'Given statement should have no more than one block';
169 }
170 0 ▶ @blocks or return; #fail
Branch: 1 branch
BranchTrueFalseTotal
@blocks or return 0 0 0
171  
172 0 my $have_default; # We have to fail unless a default block is present
173  
174 0 foreach my $stmnt ( $blocks[0]->schildren() ) {
175  
176 0 ▶ if ( $stmnt->isa( 'PPI::Statement::When' ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($stmnt->isa('PPI::Statement::When')) { } 0 0 0
177  
178 # Check for the default block.
179 0 my $first_token;
180 0 ▶ $first_token = $stmnt->schild( 0 )
Branch: 1 branch
BranchTrueFalseTotal
$first_token = $stmnt->schild(0) and "default" eq $first_token->content and $have_default = 1 0 0 0
Condition0%0/3
$first_token = $stmnt->schild(0) and "default" eq $first_token->content
!ll&&!rl&&r
000
MC/DC0%0/2
$first_token = $stmnt->schild(0) and "default" eq $first_token->content
$first_token = $stmnt->schild(0) "default" eq $first_token->content
Truth table0%0/3
A and B
A= $first_token = $stmnt->schild(0)B= "default" eq $first_token->content
ABresult
0X0
100
111
181 and 'default' eq $first_token->content()
182 and $have_default = 1;
183  
184 0 ▶ $self->_is_when_stmnt_with_return( $stmnt )
Branch: 1 branch
BranchTrueFalseTotal
$self->_is_when_stmnt_with_return($stmnt) or return 0 0 0
185 or return; #fail
186  
187 } else {
188  
189 0 ▶ $self->_is_suffix_when_with_return( $stmnt )
Branch: 1 branch
BranchTrueFalseTotal
$self->_is_suffix_when_with_return($stmnt) or return 0 0 0
190 or return; #fail
191  
192 }
193  
194 }
195  
196 0 return $have_default;
197 }
198  
199 #-----------------------------------------------------------------------------
200  
201 4 ▶sub _is_return_or_goto_stmnt {
Subroutine _is_return_or_goto_stmnt: called CC=4 SCAR=15.6
202 4 my ( $stmnt ) = @_;
203 4 ▶ return if not $stmnt->isa('PPI::Statement::Break');
Branch: 1 branch
BranchTrueFalseTotal
unless $stmnt->isa("PPI::Statement::Break") 0 4 4
204 4 ▶ my $first_token = $stmnt->schild(0) || return;
Condition50%1/2
$stmnt->schild(0) || (return)
l!l
40
MC/DC0%0/1
$stmnt->schild(0) || (return)
$stmnt->schild(0)
Truth table50%1/2
A
A= $stmnt->schild(0)
Aresult
11
00
205 4 ▶ return $first_token->content() eq 'return'
Condition33%1/3
$first_token->content eq 'return' || $first_token->content eq 'goto'
l!l&&r!l&&!r
400
MC/DC0%0/2
$first_token->content eq 'return' || $first_token->content eq 'goto'
$first_token->content eq 'return' $first_token->content eq 'goto'
Truth table33%1/3
A || B
A= $first_token->content eq 'return'B= $first_token->content eq 'goto'
ABresult
1X1
011
000
206 || $first_token->content() eq 'goto';
207 }
208  
209 #-----------------------------------------------------------------------------
210  
211 0 ▶sub _is_terminal_stmnt {
Subroutine _is_terminal_stmnt: not called CC=8 SCAR=42.8
212 0 my ( $self, $stmnt ) = @_;
213  
214 0 ▶ return if not $stmnt->isa('PPI::Statement');
Branch: 1 branch
BranchTrueFalseTotal
unless $stmnt->isa("PPI::Statement") 0 0 0
215  
216 0 ▶ my $first_token = $stmnt->schild(0) || return;
Condition0%0/2
$stmnt->schild(0) || (return)
l!l
00
MC/DC0%0/1
$stmnt->schild(0) || (return)
$stmnt->schild(0)
Truth table0%0/2
A
A= $stmnt->schild(0)
Aresult
11
00
217 0 ▶ return 1 if exists $self->{_terminal_funcs}->{$first_token};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"_terminal_funcs"}{$first_token} 0 0 0
218  
219 0 ▶ my $second_token = $stmnt->schild(1) || return;
Condition0%0/2
$stmnt->schild(1) || (return)
l!l
00
MC/DC0%0/1
$stmnt->schild(1) || (return)
$stmnt->schild(1)
Truth table0%0/2
A
A= $stmnt->schild(1)
Aresult
11
00
220 0 ▶ return if not ( $second_token->isa('PPI::Token::Operator') && ($second_token eq q{->}) );
Branch: 1 branch
BranchTrueFalseTotal
unless $second_token->isa("PPI::Token::Operator") and $second_token eq "->" 0 0 0
Condition0%0/3
$second_token->isa("PPI::Token::Operator") and $second_token eq "->"
!ll&&!rl&&r
000
MC/DC0%0/2
$second_token->isa("PPI::Token::Operator") and $second_token eq "->"
$second_token->isa("PPI::Token::Operator") $second_token eq "->"
Truth table0%0/3
A and B
A= $second_token->isa("PPI::Token::Operator")B= $second_token eq "->"
ABresult
0X0
100
111
221  
222 0 ▶ my $third_token = $stmnt->schild(2) || return;
Condition0%0/2
$stmnt->schild(2) || (return)
l!l
00
MC/DC0%0/1
$stmnt->schild(2) || (return)
$stmnt->schild(2)
Truth table0%0/2
A
A= $stmnt->schild(2)
Aresult
11
00
223 0 return exists $self->{_terminal_methods}->{$third_token};
224 }
225  
226 #-----------------------------------------------------------------------------
227  
228 5 ▶sub _is_conditional_stmnt {
Subroutine _is_conditional_stmnt: called CC=2 SCAR=7.0
229 5 my ( $stmnt ) = @_;
230 5 ▶ return if not $stmnt->isa('PPI::Statement');
Branch: 1 branch
BranchTrueFalseTotal
unless $stmnt->isa("PPI::Statement") 0 5 5
231  
232 5 ▶ return any { exists $CONDITIONALS{$_} && $_->isa('PPI::Token::Word') } $stmnt->schildren();
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
exists $CONDITIONALS{$_} and $_->isa("PPI::Token::Word") 1 14 15
233 }
234  
235 #-----------------------------------------------------------------------------
236  
237 0 ▶sub _is_when_stmnt_with_return {
Subroutine _is_when_stmnt_with_return: not called CC=6 SCAR=37.4
238 0 my ( $self, $stmnt ) = @_;
239  
240 0 ▶ my @inner = grep { ! $_->isa( 'PPI::Token' ) &&
Condition0%0/3
$_->isa('PPI::Token') || $_->isa('PPI::Structure::When')
l!l&&r!l&&!r
000
MC/DC0%0/2
$_->isa('PPI::Token') || $_->isa('PPI::Structure::When')
$_->isa('PPI::Token') $_->isa('PPI::Structure::When')
Truth table0%0/3
A || B
A= $_->isa('PPI::Token')B= $_->isa('PPI::Structure::When')
ABresult
1X1
011
000
241 ! $_->isa( 'PPI::Structure::When' ) }
242 $stmnt->schildren();
243 0 ▶ if ( any { ! $_->isa( 'PPI::Structure::Block' ) } @inner ) {
Subroutine __ANON__: not called CC=1 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
if (&any(sub { not $_->isa("PPI::Structure::Block"); } , @inner)) 0 0 0
244 0 throw_internal 'When statement should contain only tokens, conditions, and blocks';
245 }
246 0 ▶ @inner > 1
Branch: 1 branch
BranchTrueFalseTotal
@inner > 1 and throw_internal("When statement should have no more than one block") 0 0 0
247 and throw_internal 'When statement should have no more than one block';
248 0 ▶ @inner or return; #fail
Branch: 1 branch
BranchTrueFalseTotal
@inner or return 0 0 0
249  
250 0 ▶ return if any { ! $self->_block_has_return( $_ ) } @inner;
Subroutine __ANON__: not called CC=1 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
if &any(sub { not $self->_block_has_return($_); } , @inner) 0 0 0
251  
252 0 return 1; #succeed
253 }
254  
255 #-----------------------------------------------------------------------------
256  
257 0 ▶sub _is_suffix_when_with_return {
Subroutine _is_suffix_when_with_return: not called CC=6 SCAR=37.4
258 0 my ( $self, $stmnt ) = @_;
259 0 ▶ return if not $stmnt->isa('PPI::Statement');
Branch: 1 branch
BranchTrueFalseTotal
unless $stmnt->isa("PPI::Statement") 0 0 0
260 0 foreach my $elem ( $stmnt->schildren() ) {
261 0 ▶ return ( _is_return_or_goto_stmnt( $stmnt ) ||
Branch: 1 branch
BranchTrueFalseTotal
if $elem->isa("PPI::Token::Word") and "when" eq $elem->content 0 0 0
Condition0%0/6
$elem->isa("PPI::Token::Word") and "when" eq $elem->content
!ll&&!rl&&r
000
_is_return_or_goto_stmnt($stmnt) || $self->_is_terminal_stmnt($stmnt)
l!l&&r!l&&!r
000
MC/DC0%0/2
$elem->isa("PPI::Token::Word") and "when" eq $elem->content
$elem->isa("PPI::Token::Word") "when" eq $elem->content
MC/DC0%0/2
_is_return_or_goto_stmnt($stmnt) || $self->_is_terminal_stmnt($stmnt)
_is_return_or_goto_stmnt($stmnt) $self->_is_terminal_stmnt($stmnt)
Truth table0%0/3
A and B
A= $elem->isa("PPI::Token::Word")B= "when" eq $elem->content
ABresult
0X0
100
111
Truth table0%0/3
A || B
A= _is_return_or_goto_stmnt($stmnt)B= $self->_is_terminal_stmnt($stmnt)
ABresult
1X1
011
000
262 $self->_is_terminal_stmnt( $stmnt ) )
263 if $elem->isa( 'PPI::Token::Word' )
264 && 'when' eq $elem->content();
265 }
266 0 return;
267 }
268  
269 40 1;
270  
271 __END__
« lib/Perl/Critic/Policy/BuiltinFunctions/ProhibitStringyEval.pm ↑ Summary lib/Perl/Critic/Policy/Objects/ProhibitIndirectSyntax.pm »