File Coverage

blib/lib/Perl/Critic/Policy/Mardem/ProhibitBlockComplexity.pm
Criterion Covered Total %
statement 39 41 95.1
branch 5 6 83.3
condition n/a
subroutine 13 14 92.8
pod 4 5 80.0
total 61 66 92.4


line stmt bran cond sub pod time code
1             package Perl::Critic::Policy::Mardem::ProhibitBlockComplexity;
2              
3 10     10   1936487 use utf8;
  10         33  
  10         102  
4              
5 10     10   640 use 5.010;
  10         52  
6              
7 10     10   78 use strict;
  10         19  
  10         373  
8 10     10   56 use warnings;
  10         496  
  10         1028  
9              
10             our $VERSION = '0.06';
11              
12 10     10   66 use Readonly;
  10         17  
  10         776  
13              
14 10     10   71 use Perl::Critic::Utils qw{ :severities :data_conversion :classification };
  10         21  
  10         887  
15 10     10   11969 use Perl::Critic::Utils::McCabe qw{ calculate_mccabe_of_main };
  10         16390  
  10         265  
16              
17 10     10   4779 use Perl::Critic::Mardem::Util qw( search_for_block_keyword );
  10         33  
  10         800  
18              
19 10     10   77 use base 'Perl::Critic::Policy';
  10         24  
  10         6642  
20              
21             Readonly::Scalar my $EXPL => q{Consider refactoring};
22              
23             sub default_severity
24             {
25 18     18 1 249 return $SEVERITY_MEDIUM;
26             }
27              
28             sub default_themes
29             {
30 0     0 1 0 return qw(complexity maintenance);
31             }
32              
33             sub applies_to
34             {
35 23     23 1 353374 return 'PPI::Structure::Block';
36             }
37              
38             sub supported_parameters
39             {
40             return (
41 23     23 0 2636162 { 'name' => 'max_mccabe',
42             'description' => 'The maximum complexity score allowed.',
43             'default_string' => '10',
44             'behavior' => 'integer',
45             'integer_minimum' => 1,
46             },
47             );
48             }
49              
50             sub violates
51             {
52 24     24 1 1248 my ( $self, $elem, undef ) = @_;
53              
54 24         139 my $score = calculate_mccabe_of_main( $elem );
55 24 100       17652 if ( $score <= $self->{ '_max_mccabe' } ) {
56 5         21 return;
57             }
58              
59 19         109 my $block_keyword = search_for_block_keyword( $elem );
60 19 50       52 if ( !$block_keyword ) {
61 0         0 $block_keyword = 'no-keyword-found';
62             }
63              
64 19 100       65 if ( 'SUB' eq $block_keyword ) {
65 1         5 return; # no sub -> see SUB Perl::Critic::Policy::Subroutines::ProhibitExcessComplexity !
66             }
67              
68 18         49 my $desc = qq<"$block_keyword" code-block has a high complexity score ($score)>;
69 18         80 return $self->violation( $desc, $EXPL, $elem );
70             }
71              
72             1;
73              
74             __END__
75              
76             #-----------------------------------------------------------------------------
77              
78             =pod
79              
80             =encoding utf8
81              
82             =head1 NAME
83              
84             Perl::Critic::Policy::Mardem::ProhibitBlockComplexity - code block complexity "{...}"
85              
86             =head1 DESCRIPTION
87              
88             This Policy approximates the McCabe score within a code block "eg if() {...}".
89              
90             See L<http://en.wikipedia.org/wiki/Cyclomatic_complexity>
91              
92             It should help to find complex code block, which should be extracted
93             into subs, to be more testable.
94              
95             eg. from
96              
97             if( $a ) {
98             ...
99             ...
100             ...
101             }
102              
103             to
104              
105             if( $a ) {
106             do_something();
107             }
108              
109             =head1 CONFIGURATION
110              
111             The maximum acceptable McCabe can be set with the C<max_mccabe>
112             configuration item. Any block with a McCabe score higher than
113             this number will generate a policy violation. The default is 10.
114              
115             An example section for a F<.perlcriticrc>:
116              
117             [Mardem::ProhibitBlockComplexity]
118             max_mccabe = 1
119              
120             =head1 AFFILIATION
121              
122             This policy is part of L<Perl::Critic::Mardem>.
123              
124             =head1 AUTHOR
125              
126             Markus Demml, mardem@cpan.com
127              
128             =head1 LICENSE AND COPYRIGHT
129              
130             Copyright (c) 2024, Markus Demml
131              
132             This library is free software; you can redistribute it and/or modify it
133             under the same terms as the Perl 5 programming language system itself.
134             The full text of this license can be found in the LICENSE file included
135             with this module.
136              
137             =cut