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