blib/lib/Perl/Critic/Policy/ValuesAndExpressions/ProhibitEmptyPostfixLoop.pm

filter: Statementstmt 97.3% 36 / 37 Branchbran 90.0% 9 / 10 Conditioncond 33.3% 1 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 92.3% 12 / 13 Podpod 80.0% 4 / 5 totaltotal 88.5% 62 / 70 CC 7 SCAR 12.0
CC
7
Cov
92%
violates
7.1
default_themes
2.0
BEGIN
1.0
CRAP 3.3
↑ Summary
1 package Perl::Critic::Policy::ValuesAndExpressions::ProhibitEmptyPostfixLoop;
2 2 our $AUTHORITY = 'cpan:XSAWYERX';
3 # ABSTRACT: Prohibit writing an postfix loop with no statement
4 2 $Perl::Critic::Policy::ValuesAndExpressions::ProhibitEmptyPostfixLoop::VERSION = '0.001';
5 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 2 ▶use parent 'Perl::Critic::Policy';
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use Perl::Critic::Utils qw(:severities :classification :ppi);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use List::Util qw< first >;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 2 ▶use constant 'DESC' => 'Loop with no statement body';
Subroutine BEGIN: called CC=1 SCAR=0.0
13 2 ▶use constant 'EXPL' => 'You wrote a postix loop but did not '
Subroutine BEGIN: called CC=1 SCAR=0.0
14 . 'write a body for it. Maybe you accidentally '
15 2 . 'terminated the previous line?';
16  
17 13 ▶sub supported_parameters { () }
Subroutine supported_parameters: called CC=1 SCAR=0.0
Pod undocumented
18 20 ▶sub default_severity {$SEVERITY_HIGH}
Subroutine default_severity: called CC=1 SCAR=0.0
Pod documented
19 0 ▶sub default_themes {'bugs'}
Subroutine default_themes: not called CC=1 SCAR=6.9
Pod documented
20 13 ▶sub applies_to {'PPI::Token::Word'}
Subroutine applies_to: called CC=1 SCAR=0.0
Pod documented
21  
22 53 ▶sub violates {
Subroutine violates: called CC=7 SCAR=19.6
Pod documented
23 53 my ( $self, $elem ) = @_;
24  
25 53 ▶ first { $elem eq $_ } qw< for foreach >
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
&first(sub { $elem eq $_; } , "for", "foreach") or return () 26 27 53
26 or return ();
27  
28 # Don't be fooled by 'for' being used in a hash or somesuch
29 26 ▶ $elem->parent && $elem->parent->isa('PPI::Statement::Expression')
Branch: 1 branch
BranchTrueFalseTotal
$elem->parent and $elem->parent->isa("PPI::Statement::Expression") and return () 0 26 26
Condition33%1/3
$elem->parent and $elem->parent->isa("PPI::Statement::Expression")
!ll&&!rl&&r
0260
MC/DC0%0/2
$elem->parent and $elem->parent->isa("PPI::Statement::Expression")
$elem->parent $elem->parent->isa("PPI::Statement::Expression")
Truth table33%1/3
A and B
A= $elem->parentB= $elem->parent->isa("PPI::Statement::Expression")
ABresult
0X0
100
111
30 and return ();
31  
32 # Detect if this is postfix or not by looking for a block
33 26 my $next = $elem;
34 26 ▶ while ( $next = $next->snext_sibling ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($next = $next->snext_sibling) 16 23 39
35 16 ▶ $next->isa('PPI::Structure::Block')
Branch: 1 branch
BranchTrueFalseTotal
$next->isa("PPI::Structure::Block") and return () 3 13 16
36 and return ();
37 }
38  
39 # Make sure this isn't a correct postfix loop
40 $elem->sprevious_sibling
41 23 ▶ and return ();
Branch: 1 branch
BranchTrueFalseTotal
$elem->sprevious_sibling and return () 3 20 23
42  
43 20 return $self->violation( DESC(), EXPL(), $elem );
44 }
45  
46 2 1;
47  
48 __END__
↑ Summary