blib/lib/Perl/Lint/Policy/Subroutines/ProhibitExcessComplexity.pm

filter: Statementstmt 77.7% 7 / 9 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 3 / 3 Podpod n/a totaltotal 83.3% 10 / 12 SCAR 0.1
CC
1
Cov
78%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
« blib/lib/Perl/Lint/Policy/Subroutines/ProhibitBuiltinHomonyms.pm ↑ Summary blib/lib/Perl/Lint/Policy/Subroutines/ProhibitExplicitReturnUndef.pm »
1 package Perl::Lint::Policy::Subroutines::ProhibitExcessComplexity;
2 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use Perl::Lint::Constants::Type;
Subroutine BEGIN: called CC=1 SCAR=2.6
5 use parent "Perl::Lint::Policy";
6  
7 use constant {
8 DESC => 'The maximum complexity score allowed',
9 EXPL => 'Consider refactoring',
10 };
11  
12 sub evaluate {
13 my ($class, $file, $tokens, $src, $args) = @_;
14  
15 my $max_mccabe = $args->{prohibit_excess_complexity}->{max_mccabe} || 20;
16  
17 my @violations;
18 for (my $i = 0; my $token = $tokens->[$i]; $i++) {
19 my $token_type = $token->{type};
20  
21 my $left_brace_num = 0;
22 if ($token_type == FUNCTION_DECL) {
23 my $mccabe = 0;
24 my $function_decl_token = $token;
25 for ($i++; $token = $tokens->[$i]; $i++) {
26 $token_type = $token->{type};
27  
28 my $next_token = $tokens->[$i+1];
29 my $next_token_type = $next_token->{type};
30  
31 if ($token_type == LEFT_BRACE) {
32 $left_brace_num++;
33 }
34 elsif ($token_type == RIGHT_BRACE) {
35 if (--$left_brace_num < 0) {
36 last;
37 }
38 }
39 elsif (
40 $token_type == AND ||
41 $token_type == OR ||
42 $token_type == ALPHABET_AND ||
43 $token_type == ALPHABET_OR ||
44 $token_type == ALPHABET_XOR ||
45 $token_type == OR_EQUAL ||
46 $token_type == AND_EQUAL ||
47 $token_type == THREE_TERM_OP ||
48 $token_type == IF_STATEMENT ||
49 $token_type == ELSIF_STATEMENT ||
50 $token_type == ELSE_STATEMENT ||
51 $token_type == UNLESS_STATEMENT ||
52 $token_type == WHILE_STATEMENT ||
53 $token_type == UNTIL_STATEMENT ||
54 $token_type == FOR_STATEMENT ||
55 $token_type == FOREACH_STATEMENT ||
56 $token_type == LEFT_SHIFT_EQUAL ||
57 $token_type == RIGHT_SHIFT_EQUAL
58 ) {
59 $mccabe++;
60 }
61 }
62  
63 if ($mccabe > $max_mccabe) {
64 push @violations, {
65 filename => $file,
66 line => $function_decl_token->{line},
67 description => DESC,
68 explanation => EXPL,
69 policy => __PACKAGE__,
70 };
71 }
72 }
73 }
74  
75 return \@violations;
76 }
77  
78 1;
79  
« blib/lib/Perl/Lint/Policy/Subroutines/ProhibitBuiltinHomonyms.pm ↑ Summary blib/lib/Perl/Lint/Policy/Subroutines/ProhibitExplicitReturnUndef.pm »