File Coverage

blib/lib/Perl/Critic/Policy/TooMuchCode/ProhibitLargeTryBlock.pm
Criterion Covered Total %
statement 27 28 96.4
branch 6 10 60.0
condition 3 8 37.5
subroutine 7 8 87.5
pod 3 3 100.0
total 46 57 80.7


line stmt bran cond sub pod time code
1             package Perl::Critic::Policy::TooMuchCode::ProhibitLargeTryBlock;
2              
3 5     5   3056 use strict;
  5         11  
  5         157  
4 5     5   25 use warnings;
  5         10  
  5         159  
5 5     5   623 use Perl::Critic::Utils;
  5         126749  
  5         87  
6 5     5   4481 use parent 'Perl::Critic::Policy';
  5         12  
  5         33  
7              
8 0     0 1 0 sub default_themes { return qw(maintenance) }
9 2     2 1 42731 sub applies_to { return 'PPI::Structure::Block' }
10              
11             sub violates {
12 2     2 1 67 my ( $self, $elem, $doc ) = @_;
13 2   50     19 my $limit = $self->{try_block_statement_count_limit} || 10;
14              
15 2 50       9 return unless $self->__use_try_tiny($doc);
16 2         85 my @violations;
17              
18 2         13 my $word_before = $elem->sprevious_sibling;
19 2 50 33     98 return unless $word_before && $word_before->isa('PPI::Token::Word') && $word_before->content eq 'try';
      33        
20              
21 2 50       36 my $s = $elem->find('PPI::Statement') or return;
22 2         3302 my $statement_count = @$s;
23 2 100       14 return unless $statement_count > $limit;
24              
25 1         21 return $self->violation('try block is too large', "The statement count in this block is ${statement_count}, larger then the limit of ${limit}", $elem);
26             }
27              
28             sub __use_try_tiny {
29 2     2   6 my ($self, $elem) = @_;
30 2 50       8 my $includes = $elem->find('PPI::Statement::Include') or return 0;
31 2         26 return 0 < grep { $_->module eq 'Try::Tiny' } @$includes;
  2         15  
32             }
33              
34             1;
35              
36             =encoding utf-8
37              
38             =head1 NAME
39              
40             TooMuchCode::ProhibitLargeTryBlock -- Find oversized `try..catch` block.
41              
42             =head1 DESCRIPTION
43              
44             You may or may not consider it a bad idea to have a lot of code in a
45             C<try> block. If you do, this module can be used to catch the
46             oversized try blocks.
47              
48             =cut