-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProhibitConditionComplexity.pm
145 lines (98 loc) · 3.1 KB
/
ProhibitConditionComplexity.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package Perl::Critic::Policy::Mardem::ProhibitConditionComplexity;
use utf8;
use 5.010;
use strict;
use warnings;
our $VERSION = '0.06';
use Readonly;
use Perl::Critic::Utils qw{ :severities :data_conversion :classification };
use Perl::Critic::Utils::McCabe qw{ calculate_mccabe_of_main };
use Perl::Critic::Mardem::Util qw( search_for_block_keyword);
use base 'Perl::Critic::Policy';
Readonly::Scalar my $EXPL => q{Consider refactoring};
# see lib\PPI\Lexer.pm
Readonly::Array my @BLOCK_SEARCH_KEYWORD => qw(
IF ELSIF UNLESS
WHILE UNTIL
FOR FOREACH );
sub default_severity
{
return $SEVERITY_MEDIUM;
}
sub default_themes
{
return qw(complexity maintenance);
}
sub applies_to
{
return ( 'PPI::Structure::Condition', 'PPI::Structure::For' );
}
sub supported_parameters
{
return (
{ 'name' => 'max_mccabe',
'description' => 'The maximum complexity score allowed.',
'default_string' => '2',
'behavior' => 'integer',
'integer_minimum' => 1,
},
);
}
sub violates
{
my ( $self, $elem, undef ) = @_;
my $score = calculate_mccabe_of_main( $elem );
if ( $score <= $self->{ '_max_mccabe' } ) {
return;
}
my $block_keyword = search_for_block_keyword( $elem );
if ( !$block_keyword ) {
$block_keyword = 'no-keyword-found';
}
else {
my @found = grep { $block_keyword eq $_ } @BLOCK_SEARCH_KEYWORD;
if ( !@found ) {
return; # if a keyword is found, but not for an conditional block - than ignore
}
}
my $desc = qq<"${block_keyword}" condition has a high complexity score ($score)>;
return $self->violation( $desc, $EXPL, $elem );
}
1;
__END__
#-----------------------------------------------------------------------------
=pod
=encoding utf8
=head1 NAME
Perl::Critic::Policy::Mardem::ProhibitConditionComplexity - condition complexity "if/while/for/... (...){}"
=head1 DESCRIPTION
This Policy approximates the McCabe score within a coditional block "eg if(...)".
See L<http://en.wikipedia.org/wiki/Cyclomatic_complexity>
It should help to find complex conditions, which should be extracted
into subs, to be more testable.
eg. from
if( $a && $b || $c > 20 ) { ... }
to
if( _some_test ($a, $b, $c) ) { .. }
sub _some_test {
my ($a, $b, $c ) = @_;
return $a && $b || $c > 20;
}
=head1 CONFIGURATION
The maximum acceptable McCabe can be set with the C<max_mccabe>
configuration item. Any block with a McCabe score higher than
this number will generate a policy violation. The default is 2.
An example section for a F<.perlcriticrc>:
[Mardem::ProhibitConditionComplexity]
max_mccabe = 1
=head1 AFFILIATION
This policy is part of L<Perl::Critic::Mardem>.
=head1 AUTHOR
Markus Demml, [email protected]
=head1 LICENSE AND COPYRIGHT
Copyright (c) 2024, Markus Demml
This library is free software; you can redistribute it and/or modify it
under the same terms as the Perl 5 programming language system itself.
The full text of this license can be found in the LICENSE file included
with this module.
=cut