File Coverage

blib/lib/Perl/Critic/Policy/Mardem/ProhibitLargeSub.pm
Criterion Covered Total %
statement 35 37 94.5
branch 5 6 83.3
condition n/a
subroutine 11 12 91.6
pod 4 5 80.0
total 55 60 91.6


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