File Coverage

blib/lib/Perl/Lint/Policy/Subroutines/ProhibitManyArgs.pm
Criterion Covered Total %
statement 50 51 98.0
branch 22 26 84.6
condition 18 32 56.2
subroutine 6 6 100.0
pod 0 1 0.0
total 96 116 82.7


line stmt bran cond sub pod time code
1             package Perl::Lint::Policy::Subroutines::ProhibitManyArgs;
2 133     133   97301 use strict;
  133         274  
  133         5190  
3 133     133   636 use warnings;
  133         247  
  133         3611  
4 133     133   1036 use Perl::Lint::Constants::Type;
  133         263  
  133         82541  
5 133     133   841 use parent "Perl::Lint::Policy";
  133         254  
  133         899  
6              
7             use constant {
8 133         66885 DESC => 'Too many arguments',
9             EXPL => [182],
10 133     133   8941 };
  133         270  
11              
12             sub evaluate {
13 9     9 0 28 my ($class, $file, $tokens, $src, $args) = @_;
14              
15 9   100     65 my $max_arguments = $args->{prohibit_many_args}->{max_arguments} || 5;
16              
17 9         21 my @violations;
18 9         57 for (my $i = 0; my $token = $tokens->[$i]; $i++) {
19 47         65 my $token_type = $token->{type};
20              
21 47 100       141 if ($token_type == FUNCTION_DECL) {
22 9         37 for ($i++; $token = $tokens->[$i]; $i++) {
23 82         96 $token_type = $token->{type};
24 82 100       233 if ($token_type == LEFT_BRACE) {
    100          
25 17         21 my $left_brace_num = 1;
26 17         29 my $num_of_vars = 0;
27 17         21 my $num_of_vars_per_one_line = 0;
28 17         24 my $last_var_token;
29 17         53 for ($i++; $token = $tokens->[$i]; $i++) {
30 215         235 $token_type = $token->{type};
31              
32 215 50 66     3031 if ($token_type == LEFT_BRACE) {
    100 100        
    100 66        
    100 66        
    100 33        
      33        
      33        
      33        
33 0         0 $left_brace_num++;
34             }
35             elsif ($token_type == RIGHT_BRACE) {
36 17 50       92 if (--$left_brace_num <= 0) {
37 17 100       46 if ($num_of_vars > $max_arguments) {
38 6         52 push @violations, {
39             filename => $file,
40             line => $last_var_token->{line},
41             description => DESC,
42             explanation => EXPL,
43             policy => __PACKAGE__,
44             };
45             }
46 17         76 last;
47             }
48             }
49             elsif (
50             $token_type == VAR ||
51             $token_type == LOCAL_VAR ||
52             $token_type == GLOBAL_VAR ||
53             $token_type == ARRAY_VAR ||
54             $token_type == LOCAL_ARRAY_VAR ||
55             $token_type == GLOBAL_ARRAY_VAR ||
56             $token_type == HASH_VAR ||
57             $token_type == LOCAL_HASH_VAR ||
58             $token_type == GLOBAL_HASH_VAR
59             ) {
60 42 50       80 if ($left_brace_num == 1) { # XXX
61 42         101 $num_of_vars_per_one_line++;
62             }
63             }
64             elsif ($token_type == ASSIGN) {
65 26         40 my $next_token = $tokens->[$i+1];
66 26         42 my $next_token_type = $next_token->{type};
67 26         40 my $next_token_data = $next_token->{data};
68              
69 26 50 33     142 if (
      66        
70             $next_token_type == ARGUMENT_ARRAY ||
71             ($next_token_type == BUILTIN_FUNC && $next_token_data eq 'shift')
72             ) {
73 26         28 $num_of_vars += $num_of_vars_per_one_line;
74 26         69 $last_var_token = $token;
75             }
76             }
77             elsif ($token_type == SEMI_COLON) {
78 30         75 $num_of_vars_per_one_line = 0;
79             }
80             }
81             }
82             elsif ($token_type == PROTOTYPE) {
83 11         56 (my $prototype = $token->{data}) =~ s/[ ;]//g;
84 11         27 $prototype =~ s/\\\[.+?\]/1/g; # XXX
85 11 100       51 if (length $prototype > $max_arguments) {
86 4         32 push @violations, {
87             filename => $file,
88             line => $token->{line},
89             description => DESC,
90             explanation => EXPL,
91             policy => __PACKAGE__,
92             };
93 4         59 last;
94             }
95             }
96             }
97             }
98             }
99              
100 9         69 return \@violations;
101             }
102              
103             1;
104