File Coverage

blib/lib/Perl/Critic/Policy/ControlStructures/ProhibitInlineDo.pm
Criterion Covered Total %
statement 50 51 98.0
branch 18 22 81.8
condition 7 10 70.0
subroutine 12 12 100.0
pod 4 5 80.0
total 91 100 91.0


line stmt bran cond sub pod time code
1             package Perl::Critic::Policy::ControlStructures::ProhibitInlineDo;
2              
3 5     5   4044 use 5.010001;
  5         22  
4 5     5   24 use strict;
  5         6  
  5         155  
5 5     5   18 use warnings;
  5         52  
  5         228  
6 5     5   22 use Readonly;
  5         12  
  5         327  
7              
8 5     5   25 use Perl::Critic::Utils qw/:severities :classification/;
  5         8  
  5         283  
9 5     5   1673 use base 'Perl::Critic::Policy';
  5         10  
  5         2477  
10              
11             our $VERSION = '0.0.7';
12              
13             Readonly::Scalar my $DESC => q{Do not use inline do-blocks};
14             Readonly::Scalar my $EXPL => undef; # [ ];
15              
16             #-----------------------------------------------------------------------------
17              
18 12     12 1 111376 sub applies_to { return 'PPI::Token::Word' }
19 4     4 1 69 sub default_severity { return $SEVERITY_LOW }
20 1     1 1 5989 sub default_themes { return qw/maintenance complexity/ }
21 2     2 0 2322589 sub supported_parameters { return }
22              
23             #-----------------------------------------------------------------------------
24              
25             sub _cmpLocation {
26 7     7   24 my ($L,$R)=@_;
27 7   50     19 my @ll=@{$L->location()//[]};
  7         37  
28 7   50     157 my @rr=@{$R->location()//[]};
  7         28  
29 7 50       102 @ll or return;
30 7 50       20 @rr or return;
31 7   66     53 return ($ll[0]<=>$rr[0]) || ($ll[1]<=>$rr[1]);
32             }
33              
34             sub violates {
35 33     33 1 1819 my ($self,$elem,undef)=@_;
36 33 100       143 if(!$elem->isa('PPI::Token::Word')) { return }
  1         6  
37 32 100       127 if(!is_perl_bareword($elem)) { return }
  9         294  
38 23 100       674 if(is_method_call($elem)) { return }
  1         74  
39 22 100       1130 if($elem->content() ne 'do') { return }
  8         103  
40              
41 14         106 my $next=$elem->snext_sibling();
42 14 100 100     469 if(!$next || !$next->isa('PPI::Structure::Block')) { return }
  4         22  
43              
44 10         50 my $parent=$elem->parent();
45 10 50       193 if(!$parent) { return $self->violation($DESC,$EXPL,$elem) } # impossible?
  0         0  
46 10 100       65 if($parent->isa('PPI::Statement::Sub')) { return }
  3         13  
47 7 50       33 if($parent->isa('PPI::Statement')) {
48 7         80 my $token=$parent->first_token();
49 7 100       137 if(0==_cmpLocation($elem,$token)) { return }
  3         15  
50             }
51 4         25 return $self->violation($DESC,$EXPL,$elem);
52             }
53              
54             #-----------------------------------------------------------------------------
55              
56             1;
57              
58             __END__
59              
60             =pod
61              
62             =head1 NAME
63              
64             Perl::Critic::Policy::ControlStructures::ProhibitInlineDo - Use subroutines instead of inline do-blocks.
65              
66             =head1 DESCRIPTION
67              
68             Functions permit code reuse, isolate scope, and reduce complexity.
69              
70             my $handler //= do { ... }; # no
71             my $handler //= build_handler(...); # ok
72              
73             my $value = 1 + do {...} + do {...}; # no
74             my $value = 1 + f(...) + g(...); # ok
75              
76             Standalone do-blocks are not considered violations.
77              
78             do { $x++ } foreach (...); # ok
79              
80             =head1 CONFIGURATION
81              
82             None.
83              
84             =head1 NOTES
85              
86             Custom subroutines called C<do> will be considered a violation if they are called as C<do {...}>.
87              
88             Right-hand evaluation of regular expressions is not checked. EG C<$x=~s/./do{-}/e>
89              
90             =cut