File Coverage

blib/lib/Perl/Critic/Policy/TooMuchCode/ProhibitLargeBlock.pm
Criterion Covered Total %
statement 27 28 96.4
branch 5 8 62.5
condition 2 5 40.0
subroutine 8 9 88.8
pod 3 3 100.0
total 45 53 84.9


line stmt bran cond sub pod time code
1             package Perl::Critic::Policy::TooMuchCode::ProhibitLargeBlock;
2              
3 4     4   2580 use strict;
  4         11  
  4         120  
4 4     4   25 use warnings;
  4         15  
  4         115  
5 4     4   25 use List::Util qw(first);
  4         10  
  4         267  
6 4     4   25 use Perl::Critic::Utils;
  4         8  
  4         86  
7 4     4   3566 use parent 'Perl::Critic::Policy';
  4         56  
  4         31  
8              
9 0     0 1 0 sub default_themes { return qw(maintenance) }
10 4     4 1 268170 sub applies_to { return 'PPI::Structure::Block' }
11              
12             sub violates {
13 4     4 1 124 my ( $self, $elem, $doc ) = @_;
14             my $limit = $self->{_config}->{block_statement_count_limit} ||
15             $self->{block_statement_count_limit} ||
16 4   50     45 10;
17              
18 4         41 my $word_before = $elem->sprevious_sibling;
19 4 50 33     208 return unless $word_before && $word_before->isa('PPI::Token::Word');
20              
21 4     4   40 my ($block_keyword) = first { $_ eq $word_before->content } qw(map grep do);
  4         19  
22 4 50       39 return unless $block_keyword;
23              
24 4 50       24 my $s = $elem->find('PPI::Statement') or return;
25 4         8207 my $statement_count = @$s;
26              
27 4 100       23 return unless $statement_count > $limit;
28              
29 1         17 return $self->violation('Oversize block', "The statement count in this ${block_keyword} block is ${statement_count}, larger than the limit of ${limit}", $elem);
30             }
31              
32             1;
33              
34             =encoding utf-8
35              
36             =head1 NAME
37              
38             TooMuchCode::ProhibitLargeBlock -- Find oversized blocks
39              
40             =head1 DESCRIPTION
41              
42             This policy scan for large code blocks of the following type.
43              
44             map { ... };
45             grep { ... };
46             do { ... };
47              
48             By default a large block is one with more than 10 statements. If
49             you need another limit, you can set the parameter
50             C<block_statement_count_limit>.
51              
52             For example in the I<.perlcriticrc> file
53              
54             [TooMuchCode::ProhibitLargeBlock]
55             block_statement_count_limit = 20
56              
57             =cut