File Coverage

blib/lib/Perl/Critic/Policy/Mardem/ProhibitLargeBlock.pm
Criterion Covered Total %
statement 40 42 95.2
branch 7 8 87.5
condition n/a
subroutine 12 13 92.3
pod 4 5 80.0
total 63 68 92.6


line stmt bran cond sub pod time code
1             package Perl::Critic::Policy::Mardem::ProhibitLargeBlock;
2              
3 10     10   10110 use utf8;
  10         22  
  10         69  
4              
5 10     10   521 use 5.010;
  10         37  
6              
7 10     10   55 use strict;
  10         44  
  10         237  
8 10     10   41 use warnings;
  10         18  
  10         873  
9              
10             our $VERSION = '0.06';
11              
12 10     10   98 use Readonly;
  10         24  
  10         849  
13              
14 10     10   64 use Perl::Critic::Utils qw{ :severities :data_conversion :classification };
  10         27  
  10         719  
15              
16 10     10   4477 use Perl::Critic::Mardem::Util qw( search_for_block_keyword );
  10         24  
  10         623  
17              
18 10     10   61 use base 'Perl::Critic::Policy';
  10         28  
  10         5150  
19              
20             Readonly::Scalar my $EXPL => q{Consider refactoring};
21              
22             sub default_severity
23             {
24 18     18 1 305 return $SEVERITY_MEDIUM;
25             }
26              
27             sub default_themes
28             {
29 0     0 1 0 return qw(complexity maintenance);
30             }
31              
32             sub applies_to
33             {
34 25     25 1 461117 return 'PPI::Structure::Block';
35             }
36              
37             sub supported_parameters
38             {
39             return (
40 25     25 0 2649818 { 'name' => 'statement_count_limit',
41             'description' => 'The maximum statement count allowed.',
42             'default_string' => '20',
43             'behavior' => 'integer',
44             'integer_minimum' => 1,
45             },
46             );
47             }
48              
49             sub violates
50             {
51 27     27 1 1430 my ( $self, $elem, undef ) = @_;
52              
53 27         127 my $s = $elem->find( 'PPI::Statement' );
54              
55 27 100       28335 if ( !$s ) {
56 1         5 return;
57             }
58              
59 26         82 my $statement_count = @{ $s };
  26         71  
60 26 100       124 if ( $statement_count <= $self->{ '_statement_count_limit' } ) {
61 6         32 return;
62             }
63              
64 20         118 my $block_keyword = search_for_block_keyword( $elem );
65 20 50       95 if ( !$block_keyword ) {
66 0         0 $block_keyword = 'no-keyword-found';
67             }
68              
69 20 100       65 if ( 'SUB' eq $block_keyword ) {
70 2         12 return; # no sub -> see SUB Perl::Critic::Policy::Mardem::ProhibitLargeSub !
71             }
72              
73 18         58 my $desc = qq<"$block_keyword" code-block with high statement count ($statement_count)>;
74 18         93 return $self->violation( $desc, $EXPL, $elem );
75             }
76              
77             1;
78              
79             __END__
80              
81             #-----------------------------------------------------------------------------
82              
83             =pod
84              
85             =encoding utf8
86              
87             =head1 NAME
88              
89             Perl::Critic::Policy::Mardem::ProhibitLargeBlock - large code block as statement count "{...}"
90              
91             =head1 DESCRIPTION
92              
93             This Policy counts the statements within a code block { ... }
94             (more precise the PPI::Statement's)
95              
96             =head1 CONFIGURATION
97              
98             The maximum acceptable Statement-Count can be set with the
99             C<statement_count_limit> configuration item. Any block with a count higher
100             than this number will generate a policy violation. The default is 20.
101              
102             An example section for a F<.perlcriticrc>:
103              
104             [Mardem::ProhibitLargeBlock]
105             statement_count_limit = 10
106              
107             =head1 AFFILIATION
108              
109             This policy is part of L<Perl::Critic::Mardem>.
110              
111             =head1 AUTHOR
112              
113             Markus Demml, mardem@cpan.com
114              
115             =head1 LICENSE AND COPYRIGHT
116              
117             Copyright (c) 2024, Markus Demml
118              
119             This library is free software; you can redistribute it and/or modify it
120             under the same terms as the Perl 5 programming language system itself.
121             The full text of this license can be found in the LICENSE file included
122             with this module.
123              
124             =cut