line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Perl::Critic::Policy::TooMuchCode::ProhibitLargeTryBlock; |
2
|
|
|
|
|
|
|
|
3
|
5
|
|
|
5
|
|
3065
|
use strict; |
|
5
|
|
|
|
|
13
|
|
|
5
|
|
|
|
|
150
|
|
4
|
5
|
|
|
5
|
|
26
|
use warnings; |
|
5
|
|
|
|
|
20
|
|
|
5
|
|
|
|
|
120
|
|
5
|
5
|
|
|
5
|
|
648
|
use Perl::Critic::Utils; |
|
5
|
|
|
|
|
133987
|
|
|
5
|
|
|
|
|
85
|
|
6
|
5
|
|
|
5
|
|
4421
|
use parent 'Perl::Critic::Policy'; |
|
5
|
|
|
|
|
17
|
|
|
5
|
|
|
|
|
29
|
|
7
|
|
|
|
|
|
|
|
8
|
0
|
|
|
0
|
1
|
0
|
sub default_themes { return qw(maintenance) } |
9
|
2
|
|
|
2
|
1
|
39808
|
sub applies_to { return 'PPI::Structure::Block' } |
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
sub violates { |
12
|
2
|
|
|
2
|
1
|
48
|
my ( $self, $elem, $doc ) = @_; |
13
|
2
|
|
50
|
|
|
12
|
my $limit = $self->{try_block_statement_count_limit} || 10; |
14
|
|
|
|
|
|
|
|
15
|
2
|
50
|
|
|
|
6
|
return unless $self->__use_try_tiny($doc); |
16
|
2
|
|
|
|
|
63
|
my @violations; |
17
|
|
|
|
|
|
|
|
18
|
2
|
|
|
|
|
7
|
my $word_before = $elem->sprevious_sibling; |
19
|
2
|
50
|
33
|
|
|
69
|
return unless $word_before && $word_before->isa('PPI::Token::Word') && $word_before->content eq 'try'; |
|
|
|
33
|
|
|
|
|
20
|
|
|
|
|
|
|
|
21
|
2
|
50
|
|
|
|
15
|
my $s = $elem->find('PPI::Statement') or return; |
22
|
2
|
|
|
|
|
2998
|
my $statement_count = @$s; |
23
|
2
|
100
|
|
|
|
10
|
return unless $statement_count > $limit; |
24
|
|
|
|
|
|
|
|
25
|
1
|
|
|
|
|
22
|
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
|
|
|
|
7
|
my $includes = $elem->find('PPI::Statement::Include') or return 0; |
31
|
2
|
|
|
|
|
26
|
return 0 < grep { $_->module eq 'Try::Tiny' } @$includes; |
|
2
|
|
|
|
|
9
|
|
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 |