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   70285 use strict;
  133         201  
  133         3226  
3 133     133   442 use warnings;
  133         172  
  133         2533  
4 133     133   917 use Perl::Lint::Constants::Type;
  133         171  
  133         60032  
5 133     133   602 use parent "Perl::Lint::Policy";
  133         171  
  133         624  
6              
7             use constant {
8 133         54679 DESC => 'Too many arguments',
9             EXPL => [182],
10 133     133   6750 };
  133         177  
11              
12             sub evaluate {
13 9     9 0 25 my ($class, $file, $tokens, $src, $args) = @_;
14              
15 9   100     34 my $max_arguments = $args->{prohibit_many_args}->{max_arguments} || 5;
16              
17 9         7 my @violations;
18 9         30 for (my $i = 0; my $token = $tokens->[$i]; $i++) {
19 47         34 my $token_type = $token->{type};
20              
21 47 100       90 if ($token_type == FUNCTION_DECL) {
22 9         20 for ($i++; $token = $tokens->[$i]; $i++) {
23 82         62 $token_type = $token->{type};
24 82 100       154 if ($token_type == LEFT_BRACE) {
    100          
25 17         18 my $left_brace_num = 1;
26 17         10 my $num_of_vars = 0;
27 17         15 my $num_of_vars_per_one_line = 0;
28 17         12 my $last_var_token;
29 17         29 for ($i++; $token = $tokens->[$i]; $i++) {
30 215         141 $token_type = $token->{type};
31              
32 215 50 66     2073 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       28 if (--$left_brace_num <= 0) {
37 17 100       25 if ($num_of_vars > $max_arguments) {
38             push @violations, {
39             filename => $file,
40             line => $last_var_token->{line},
41 6         29 description => DESC,
42             explanation => EXPL,
43             policy => __PACKAGE__,
44             };
45             }
46 17         43 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       46 if ($left_brace_num == 1) { # XXX
61 42         56 $num_of_vars_per_one_line++;
62             }
63             }
64             elsif ($token_type == ASSIGN) {
65 26         22 my $next_token = $tokens->[$i+1];
66 26         26 my $next_token_type = $next_token->{type};
67 26         25 my $next_token_data = $next_token->{data};
68              
69 26 50 33     91 if (
      66        
70             $next_token_type == ARGUMENT_ARRAY ||
71             ($next_token_type == BUILTIN_FUNC && $next_token_data eq 'shift')
72             ) {
73 26         37 $num_of_vars += $num_of_vars_per_one_line;
74 26         58 $last_var_token = $token;
75             }
76             }
77             elsif ($token_type == SEMI_COLON) {
78 30         40 $num_of_vars_per_one_line = 0;
79             }
80             }
81             }
82             elsif ($token_type == PROTOTYPE) {
83 11         29 (my $prototype = $token->{data}) =~ s/[ ;]//g;
84 11         17 $prototype =~ s/\\\[.+?\]/1/g; # XXX
85 11 100       27 if (length $prototype > $max_arguments) {
86             push @violations, {
87             filename => $file,
88             line => $token->{line},
89 4         17 description => DESC,
90             explanation => EXPL,
91             policy => __PACKAGE__,
92             };
93 4         11 last;
94             }
95             }
96             }
97             }
98             }
99              
100 9         37 return \@violations;
101             }
102              
103             1;
104