File Coverage

blib/lib/Perl/Critic/Policy/Mardem/ProhibitManyConditionsInSub.pm
Criterion Covered Total %
statement 38 40 95.0
branch 5 6 83.3
condition 3 3 100.0
subroutine 12 13 92.3
pod 4 5 80.0
total 62 67 92.5


line stmt bran cond sub pod time code
1             package Perl::Critic::Policy::Mardem::ProhibitManyConditionsInSub;
2              
3 10     10   9553 use utf8;
  10         26  
  10         67  
4              
5 10     10   560 use 5.010;
  10         37  
6              
7 10     10   54 use strict;
  10         22  
  10         315  
8 10     10   65 use warnings;
  10         20  
  10         936  
9              
10             our $VERSION = '0.06';
11              
12 10     10   64 use Readonly;
  10         19  
  10         692  
13              
14 10     10   101 use Perl::Critic::Utils qw{ :severities :data_conversion :classification };
  10         19  
  10         766  
15              
16 10     10   4745 use base 'Perl::Critic::Policy';
  10         24  
  10         5484  
17              
18             Readonly::Scalar my $EXPL => q{Consider refactoring};
19              
20             sub default_severity
21             {
22 8     8 1 143 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 13     13 1 382707 return 'PPI::Statement::Sub';
33             }
34              
35             sub supported_parameters
36             {
37             return (
38 13     13 0 2886354 { 'name' => 'condition_count_limit',
39             'description' => 'The maximum condition count allowed.',
40             'default_string' => '3',
41             'behavior' => 'integer',
42             'integer_minimum' => 1,
43             },
44             );
45             }
46              
47             sub violates
48             {
49 11     11 1 352 my ( $self, $elem, undef ) = @_;
50              
51             my $s = $elem->find(
52             sub
53             {
54 783     783   8879 my ( undef, $element ) = @_;
55              
56 783   100     5872 my $interesting =
57             $element->isa( 'PPI::Structure::Condition' )
58             || $element->isa( 'PPI::Structure::For' )
59             || $element->isa( 'PPI::Structure::Given' );
60              
61 783         1755 return $interesting;
62             }
63 11         96 );
64              
65 11 100       236 if ( !$s ) {
66 1         6 return;
67             }
68              
69 10         26 my $condition_count = @{ $s };
  10         28  
70 10 100       48 if ( $condition_count <= $self->{ '_condition_count_limit' } ) {
71 2         11 return;
72             }
73              
74 8         18 my $desc;
75 8 50       63 if ( my $name = $elem->name() ) {
76 8         706 $desc = qq<Subroutine "$name" with high condition count ($condition_count)>;
77             }
78             else {
79             # never the case becaus no PPI::Statement::Sub
80 0         0 $desc = qq<Anonymous subroutine with high condition count ($condition_count)>;
81             }
82              
83 8         51 return $self->violation( $desc, $EXPL, $elem );
84             }
85              
86             1;
87              
88             __END__
89              
90             #-----------------------------------------------------------------------------
91              
92             =pod
93              
94             =encoding utf8
95              
96             =head1 NAME
97              
98             Perl::Critic::Policy::Mardem::ProhibitManyConditionsInSub - subs has many conditionals "if, while, for, ..."
99              
100             =head1 DESCRIPTION
101              
102             This Policy counts the conditions within a sub.
103             (more precise the PPI::Structure::Condition's,
104             PPI::Structure::For's and PPI::Structure::Given's)
105              
106             =head1 CONFIGURATION
107              
108             The maximum acceptable Condition-Count can be set with the
109             C<condition_count_limit> configuration item. Any sub with a count higher than
110             this number will generate a policy violation. The default is 3.
111              
112             An example section for a F<.perlcriticrc>:
113              
114             [Mardem::ProhibitManyConditionsInSub]
115             condition_count_limit = 1
116              
117             =head1 AFFILIATION
118              
119             This policy is part of L<Perl::Critic::Mardem>.
120              
121             =head1 AUTHOR
122              
123             Markus Demml, mardem@cpan.com
124              
125             =head1 LICENSE AND COPYRIGHT
126              
127             Copyright (c) 2024, Markus Demml
128              
129             This library is free software; you can redistribute it and/or modify it
130             under the same terms as the Perl 5 programming language system itself.
131             The full text of this license can be found in the LICENSE file included
132             with this module.
133              
134             =cut