line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Perl::Critic::Policy::Subroutines::ProhibitExcessComplexity; |
2
|
|
|
|
|
|
|
|
3
|
40
|
|
|
40
|
|
26813
|
use 5.010001; |
|
40
|
|
|
|
|
156
|
|
4
|
40
|
|
|
40
|
|
266
|
use strict; |
|
40
|
|
|
|
|
93
|
|
|
40
|
|
|
|
|
809
|
|
5
|
40
|
|
|
40
|
|
221
|
use warnings; |
|
40
|
|
|
|
|
96
|
|
|
40
|
|
|
|
|
1001
|
|
6
|
40
|
|
|
40
|
|
236
|
use Readonly; |
|
40
|
|
|
|
|
87
|
|
|
40
|
|
|
|
|
2223
|
|
7
|
|
|
|
|
|
|
|
8
|
40
|
|
|
40
|
|
291
|
use Perl::Critic::Utils qw{ :severities :data_conversion :classification }; |
|
40
|
|
|
|
|
122
|
|
|
40
|
|
|
|
|
2230
|
|
9
|
40
|
|
|
40
|
|
32919
|
use Perl::Critic::Utils::McCabe qw{ calculate_mccabe_of_sub }; |
|
40
|
|
|
|
|
138
|
|
|
40
|
|
|
|
|
759
|
|
10
|
|
|
|
|
|
|
|
11
|
40
|
|
|
40
|
|
2493
|
use parent 'Perl::Critic::Policy'; |
|
40
|
|
|
|
|
108
|
|
|
40
|
|
|
|
|
201
|
|
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
our $VERSION = '1.148'; |
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
#----------------------------------------------------------------------------- |
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
Readonly::Scalar my $EXPL => q{Consider refactoring}; |
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
#----------------------------------------------------------------------------- |
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
sub supported_parameters { |
22
|
|
|
|
|
|
|
return ( |
23
|
|
|
|
|
|
|
{ |
24
|
93
|
|
|
93
|
0
|
2121
|
name => 'max_mccabe', |
25
|
|
|
|
|
|
|
description => 'The maximum complexity score allowed.', |
26
|
|
|
|
|
|
|
default_string => '20', |
27
|
|
|
|
|
|
|
behavior => 'integer', |
28
|
|
|
|
|
|
|
integer_minimum => 1, |
29
|
|
|
|
|
|
|
}, |
30
|
|
|
|
|
|
|
); |
31
|
|
|
|
|
|
|
} |
32
|
|
|
|
|
|
|
|
33
|
75
|
|
|
75
|
1
|
376
|
sub default_severity { return $SEVERITY_MEDIUM } |
34
|
74
|
|
|
74
|
1
|
327
|
sub default_themes { return qw(core complexity maintenance) } |
35
|
33
|
|
|
33
|
1
|
105
|
sub applies_to { return 'PPI::Statement::Sub' } |
36
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
#----------------------------------------------------------------------------- |
38
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
sub violates { |
40
|
8
|
|
|
8
|
1
|
27
|
my ( $self, $elem, undef ) = @_; |
41
|
|
|
|
|
|
|
|
42
|
8
|
|
|
|
|
42
|
my $score = calculate_mccabe_of_sub( $elem ); |
43
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
# Is it too complex? |
45
|
8
|
100
|
|
|
|
62
|
return if $score <= $self->{_max_mccabe}; |
46
|
|
|
|
|
|
|
|
47
|
1
|
|
|
|
|
4
|
my $desc; |
48
|
1
|
50
|
|
|
|
7
|
if ( my $name = $elem->name() ) { |
49
|
1
|
|
|
|
|
78
|
$desc = qq<Subroutine "$name" with high complexity score ($score)>; |
50
|
|
|
|
|
|
|
} |
51
|
|
|
|
|
|
|
else { |
52
|
0
|
|
|
|
|
0
|
$desc = qq<Anonymous subroutine with high complexity score ($score)>; |
53
|
|
|
|
|
|
|
} |
54
|
|
|
|
|
|
|
|
55
|
1
|
|
|
|
|
11
|
return $self->violation( $desc, $EXPL, $elem ); |
56
|
|
|
|
|
|
|
} |
57
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
1; |
60
|
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
__END__ |
62
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
#----------------------------------------------------------------------------- |
64
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
=pod |
66
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
=for stopwords McCabe |
68
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
=head1 NAME |
70
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
Perl::Critic::Policy::Subroutines::ProhibitExcessComplexity - Minimize complexity by factoring code into smaller subroutines. |
72
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
=head1 AFFILIATION |
74
|
|
|
|
|
|
|
|
75
|
|
|
|
|
|
|
This Policy is part of the core L<Perl::Critic|Perl::Critic> |
76
|
|
|
|
|
|
|
distribution. |
77
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
=head1 DESCRIPTION |
80
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
All else being equal, complicated code is more error-prone and more |
82
|
|
|
|
|
|
|
expensive to maintain than simpler code. The first step towards |
83
|
|
|
|
|
|
|
managing complexity is to establish formal complexity metrics. One |
84
|
|
|
|
|
|
|
such metric is the McCabe score, which describes the number of |
85
|
|
|
|
|
|
|
possible paths through a subroutine. This Policy approximates the |
86
|
|
|
|
|
|
|
McCabe score by summing the number of conditional statements and |
87
|
|
|
|
|
|
|
operators within a subroutine. Research has shown that a McCabe score |
88
|
|
|
|
|
|
|
higher than 20 is a sign of high-risk, potentially untestable code. |
89
|
|
|
|
|
|
|
See L<http://en.wikipedia.org/wiki/Cyclomatic_complexity> |
90
|
|
|
|
|
|
|
for some discussion about the McCabe number and other complexity |
91
|
|
|
|
|
|
|
metrics. |
92
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
The usual prescription for reducing complexity is to refactor code |
94
|
|
|
|
|
|
|
into smaller subroutines. Mark Dominus book "Higher Order Perl" also |
95
|
|
|
|
|
|
|
describes callbacks, recursion, memoization, iterators, and other |
96
|
|
|
|
|
|
|
techniques that help create simple and extensible Perl code. |
97
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
=head1 CONFIGURATION |
99
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
The maximum acceptable McCabe can be set with the C<max_mccabe> |
101
|
|
|
|
|
|
|
configuration item. Any subroutine with a McCabe score higher than |
102
|
|
|
|
|
|
|
this number will generate a policy violation. The default is 20. An |
103
|
|
|
|
|
|
|
example section for a F<.perlcriticrc>: |
104
|
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
[Subroutines::ProhibitExcessComplexity] |
106
|
|
|
|
|
|
|
max_mccabe = 30 |
107
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
=head1 NOTES |
109
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
"Everything should be made as simple as possible, but no simpler." |
112
|
|
|
|
|
|
|
|
113
|
|
|
|
|
|
|
-- Albert Einstein |
114
|
|
|
|
|
|
|
|
115
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
Complexity is subjective, but formal complexity metrics are still |
117
|
|
|
|
|
|
|
incredibly valuable. Every problem has an inherent level of |
118
|
|
|
|
|
|
|
complexity, so it is not necessarily optimal to minimize the McCabe |
119
|
|
|
|
|
|
|
number. So don't get offended if your code triggers this Policy. |
120
|
|
|
|
|
|
|
Just consider if there B<might> be a simpler way to get the job done. |
121
|
|
|
|
|
|
|
|
122
|
|
|
|
|
|
|
|
123
|
|
|
|
|
|
|
=head1 AUTHOR |
124
|
|
|
|
|
|
|
|
125
|
|
|
|
|
|
|
Jeffrey Ryan Thalhammer <jeff@imaginative-software.com> |
126
|
|
|
|
|
|
|
|
127
|
|
|
|
|
|
|
=head1 COPYRIGHT |
128
|
|
|
|
|
|
|
|
129
|
|
|
|
|
|
|
Copyright (c) 2005-2011 Imaginative Software Systems. All rights reserved. |
130
|
|
|
|
|
|
|
|
131
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify |
132
|
|
|
|
|
|
|
it under the same terms as Perl itself. The full text of this license |
133
|
|
|
|
|
|
|
can be found in the LICENSE file included with this module. |
134
|
|
|
|
|
|
|
|
135
|
|
|
|
|
|
|
=cut |
136
|
|
|
|
|
|
|
|
137
|
|
|
|
|
|
|
# Local Variables: |
138
|
|
|
|
|
|
|
# mode: cperl |
139
|
|
|
|
|
|
|
# cperl-indent-level: 4 |
140
|
|
|
|
|
|
|
# fill-column: 78 |
141
|
|
|
|
|
|
|
# indent-tabs-mode: nil |
142
|
|
|
|
|
|
|
# c-indentation-style: bsd |
143
|
|
|
|
|
|
|
# End: |
144
|
|
|
|
|
|
|
# ex: set ts=8 sts=4 sw=4 tw=78 ft=perl expandtab shiftround : |