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   68030 use strict;
  134         178  
  134         3192  
3 134     134   405 use warnings;
  134         159  
  134         2475  
4 134     134   767 use Perl::Lint::Constants::Type;
  134         157  
  134         59739  
5 134     134   902 use Perl::Lint::Constants::Kind;
  134         166  
  134         6777  
6 134     134   461 use parent "Perl::Lint::Policy";
  134         149  
  134         584  
7              
8             use constant {
9 134         27931 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   6600 };
  134         181  
12              
13             sub evaluate {
14 9     9 0 13 my ($class, $file, $tokens, $src, $args) = @_;
15              
16 9         13 my $max_chain_length = 3;
17 9 100       23 if (my $this_policies_arg = $args->{prohibit_long_chains_of_method_calls}) {
18 2   33     9 $max_chain_length = $this_policies_arg->{max_chain_length} || $max_chain_length;
19             }
20              
21 9         9 my @violations;
22 9         32 for (my $i = 0, my $token_type, my $num_of_chain = 0; my $token = $tokens->[$i]; $i++) {
23 599         410 $token_type = $token->{type};
24              
25 599 100 100     1678 if ($token_type == POINTER) {
    100          
26 104         74 my $next_token = $tokens->[$i+1];
27 104         75 my $next_token_type = $next_token->{type};
28 104 100 100     240 if ($next_token_type == LEFT_BRACE || $next_token_type == LEFT_BRACKET) {
29             # array and hash ref chains. They should be ignored.
30 16         26 next;
31             }
32              
33 88         120 $num_of_chain++;
34             }
35             elsif ($token_type == SEMI_COLON || $token->{kind} == KIND_STMT) {
36 46 100       61 if ($num_of_chain > $max_chain_length) {
37             push @violations, {
38             filename => $file,
39             line => $token->{line},
40 12         33 description => DESC,
41             explanation => EXPL,
42             policy => __PACKAGE__,
43             };
44             }
45 46         74 $num_of_chain = 0;
46             }
47             }
48              
49 9         34 return \@violations;
50             }
51              
52             1;
53