File: ProhibitLargeBlock.pm

package info (click to toggle)
libperl-critic-toomuchcode-perl 0.19-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 220 kB
  • sloc: perl: 776; makefile: 2
file content (57 lines) | stat: -rw-r--r-- 1,504 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package Perl::Critic::Policy::TooMuchCode::ProhibitLargeBlock;

use strict;
use warnings;
use List::Util qw(first);
use Perl::Critic::Utils;
use parent 'Perl::Critic::Policy';

sub default_themes       { return qw(maintenance)     }
sub applies_to           { return 'PPI::Structure::Block' }

sub violates {
    my ( $self, $elem, $doc ) = @_;
    my $limit = $self->{_config}->{block_statement_count_limit} ||
        $self->{block_statement_count_limit} ||
        10;

    my $word_before = $elem->sprevious_sibling;
    return unless $word_before && $word_before->isa('PPI::Token::Word');

    my ($block_keyword) = first { $_ eq $word_before->content } qw(map grep do);
    return unless $block_keyword;

    my $s = $elem->find('PPI::Statement') or return;
    my $statement_count = @$s;

    return unless $statement_count > $limit;

    return $self->violation('Oversize block', "The statement count in this ${block_keyword} block is ${statement_count}, larger than the limit of ${limit}", $elem);
}

1;

=encoding utf-8

=head1 NAME

TooMuchCode::ProhibitLargeBlock -- Find oversized blocks

=head1 DESCRIPTION

This policy scan for large code blocks of the following type.

    map { ... };
    grep { ... };
    do { ... };

By default a large block is one with more than 10 statements. If
you need another limit, you can set the parameter
C<block_statement_count_limit>.

For example in the I<.perlcriticrc> file

  [TooMuchCode::ProhibitLargeBlock]
  block_statement_count_limit = 20

=cut