File: ProhibitLargeTryBlock.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 (48 lines) | stat: -rw-r--r-- 1,373 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
package Perl::Critic::Policy::TooMuchCode::ProhibitLargeTryBlock;

use strict;
use warnings;
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->{try_block_statement_count_limit} || 10;

    return unless $self->__use_try_tiny($doc);
    my @violations;

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

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

    return $self->violation('try block is too large', "The statement count in this block is ${statement_count}, larger then the limit of ${limit}", $elem);
}

sub __use_try_tiny {
    my ($self, $elem) = @_;
    my $includes = $elem->find('PPI::Statement::Include') or return 0;
    return 0 < grep { $_->module eq 'Try::Tiny' } @$includes;
}

1;

=encoding utf-8

=head1 NAME

TooMuchCode::ProhibitLargeTryBlock -- Find oversized `try..catch` block.

=head1 DESCRIPTION

You may or may not consider it a bad idea to have a lot of code in a
C<try> block.  If you do, this module can be used to catch the
oversized try blocks.

=cut