File Coverage

blib/lib/Perl/Lint/Policy/ValuesAndExpressions/ProhibitLongChainsOfMethodCalls.pm
Criterion Covered Total %
statement 35 35 100.0
branch 10 10 100.0
condition 7 9 77.7
subroutine 7 7 100.0
pod 0 1 0.0
total 59 62 95.1


line stmt bran cond sub pod time code
1             package Perl::Lint::Policy::ValuesAndExpressions::ProhibitLongChainsOfMethodCalls;
2 134     134   95293 use strict;
  134         267  
  134         4902  
3 134     134   622 use warnings;
  134         202  
  134         3466  
4 134     134   1003 use Perl::Lint::Constants::Type;
  134         201  
  134         83210  
5 134     134   1247 use Perl::Lint::Constants::Kind;
  134         228  
  134         8901  
6 134     134   646 use parent "Perl::Lint::Policy";
  134         222  
  134         721  
7              
8             use constant {
9 134         35577 DESC => 'The number of chained calls to allow',
10             EXPL => 'Long chains of method calls indicate code that is too tightly coupled',
11 134     134   8630 };
  134         236  
12              
13             sub evaluate {
14 9     9 0 20 my ($class, $file, $tokens, $src, $args) = @_;
15              
16 9         12 my $max_chain_length = 3;
17 9 100       31 if (my $this_policies_arg = $args->{prohibit_long_chains_of_method_calls}) {
18 2   33     7 $max_chain_length = $this_policies_arg->{max_chain_length} || $max_chain_length;
19             }
20              
21 9         11 my @violations;
22 9         40 for (my $i = 0, my $token_type, my $num_of_chain = 0; my $token = $tokens->[$i]; $i++) {
23 599         546 $token_type = $token->{type};
24              
25 599 100 100     1999 if ($token_type == POINTER) {
    100          
26 104         93 my $next_token = $tokens->[$i+1];
27 104         88 my $next_token_type = $next_token->{type};
28 104 100 100     265 if ($next_token_type == LEFT_BRACE || $next_token_type == LEFT_BRACKET) {
29             # array and hash ref chains. They should be ignored.
30 16         28 next;
31             }
32              
33 88         150 $num_of_chain++;
34             }
35             elsif ($token_type == SEMI_COLON || $token->{kind} == KIND_STMT) {
36 46 100       72 if ($num_of_chain > $max_chain_length) {
37 12         41 push @violations, {
38             filename => $file,
39             line => $token->{line},
40             description => DESC,
41             explanation => EXPL,
42             policy => __PACKAGE__,
43             };
44             }
45 46         95 $num_of_chain = 0;
46             }
47             }
48              
49 9         44 return \@violations;
50             }
51              
52             1;
53