blib/lib/Perl/Critic/Policy/Subroutines/ProhibitManyArgs.pm

filter: Statementstmt 66.2% 49 / 74 Branchbran 34.2% 13 / 38 Conditioncond 33.3% 6 / 18 MC/DCmcdc 9.0% 1 / 11 Subroutinesub 85.7% 12 / 14 Podpod 80.0% 4 / 5 totaltotal 53.1% 85 / 160 CC 26 SCAR 32.1
CC
26
Cov
52%
_count_list_elements
72.0
_count_args
23.3
_is_object_arg
12.0
CRAP 24.8
« blib/lib/Perl/Critic/Policy/Variables/ProhibitEvilVariables.pm ↑ Summary blib/lib/Perl/Critic/Policy/TestingAndDebugging/ProhibitProlongedStrictureOverride.pm »
1 package Perl::Critic::Policy::Subroutines::ProhibitManyArgs;
2  
3 40 ▶use 5.010001;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 40 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 40 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 40 ▶use Readonly;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 40 ▶use Perl::Critic::Utils qw( :severities split_nodes_on_comma hashify );
Subroutine BEGIN: called CC=1 SCAR=0.0
9 40 ▶use parent 'Perl::Critic::Policy';
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 40 our $VERSION = '1.150';
12  
13 #-----------------------------------------------------------------------------
14  
15 40 Readonly::Scalar my $AT_ARG => q{@_}; ## no critic (InterpolationOfMetachars)
16 40 Readonly::Scalar my $CLASS => q{$class}; ## no critic (InterpolationOfMetachars)
17 40 Readonly::Scalar my $SELF => q{$self}; ## no critic (InterpolationOfMetachars)
18  
19 40 Readonly::Scalar my $DESC => q{Too many arguments};
20 40 Readonly::Scalar my $EXPL => [182];
21  
22 #-----------------------------------------------------------------------------
23  
24 sub supported_parameters {
25 return (
26 {
27 92 ▶ name => 'max_arguments',
Subroutine supported_parameters: called CC=1 SCAR=0.0
Pod undocumented
28 description =>
29 'The maximum number of arguments to allow a subroutine to have.',
30 default_string => '5',
31 behavior => 'integer',
32 integer_minimum => 1,
33 },
34 {
35 name => 'skip_object',
36 description => q[Don't count $self or $class first argument], ## no critic (InterpolationOfMetachars)
37 default_string => '0',
38 behavior => 'boolean',
39 },
40 );
41 }
42  
43 75 ▶sub default_severity { return $SEVERITY_MEDIUM }
Subroutine default_severity: called CC=1 SCAR=0.0
Pod documented
44 86 ▶sub default_themes { return qw( core pbp maintenance ) }
Subroutine default_themes: called CC=1 SCAR=0.0
Pod documented
45 31 ▶sub applies_to { return 'PPI::Statement::Sub' }
Subroutine applies_to: called CC=1 SCAR=0.0
Pod documented
46  
47 #-----------------------------------------------------------------------------
48  
49 sub violates {
50 5 ▶ my ( $self, $elem, undef ) = @_;
Subroutine violates: called CC=4 SCAR=16.3
Pod documented
51  
52 # forward declaration?
53 5 ▶ return if !$elem->block;
Branch: 1 branch
BranchTrueFalseTotal
unless $elem->block 0 5 5
54  
55 5 my $num_args;
56 5 ▶ if ($elem->prototype) {
Branch: 1 branch
BranchTrueFalseTotal
if ($elem->prototype) { } 0 5 5
57 0 my $prototype = $elem->prototype();
58 0 $prototype =~ s/ \\ [[] .*? []] /*/smxg; # Allow for grouping
59 0 $num_args = $prototype =~ tr/$@%&*_+/$@%&*_+/; # RT 56627
60 } else {
61 5 $num_args = _count_args($self->{_skip_object}, $elem->block->schildren);
62 }
63  
64 5 ▶ if ($self->{_max_arguments} < $num_args) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"_max_arguments"} < $num_args) 0 5 5
65 0 return $self->violation( $DESC, $EXPL, $elem );
66 }
67 5 return; # OK
68 }
69  
70 sub _count_args {
71 5 ▶ my ($skip_object, @statements) = @_;
Subroutine _count_args: called CC=14 SCAR=31.5
72  
73 # look for these patterns:
74 # " ... = @_;" => then examine previous variable list
75 # " ... = shift;" => counts as one arg, then look for more
76  
77 5 ▶ return 0 if !@statements; # no statements
Branch: 1 branch
BranchTrueFalseTotal
unless @statements 0 5 5
78  
79 5 my $statement = shift @statements;
80 5 my @elements = $statement->schildren();
81 5 my $operand = pop @elements;
82 5 ▶ while ($operand && $operand->isa('PPI::Token::Structure') && q{;} eq $operand->content()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($operand and $operand->isa("PPI::Token::Structure") and ";" eq $operand->content) 4 5 9
Condition66%4/6
$operand and $operand->isa("PPI::Token::Structure") and ";" eq $operand->content
!ll&&!rl&&r
504
$operand and $operand->isa("PPI::Token::Structure")
!ll&&!rl&&r
054
MC/DC33%1/3
$operand and $operand->isa("PPI::Token::Structure") and ";" eq $operand->content
$operand $operand->isa("PPI::Token::Structure") ";" eq $operand->content
Truth table50%2/4
A and B and C
A= $operandB= $operand->isa("PPI::Token::Structure")C= ";" eq $operand->content
ABCresult
0XX0
10X0
1100
1111
83 4 $operand = pop @elements;
84 }
85 5 ▶ return 0 if !$operand;
Branch: 1 branch
BranchTrueFalseTotal
unless $operand 0 5 5
86  
87 #print "pulled off last, remaining: '@elements'\n";
88 5 my $operator = pop @elements;
89 5 ▶ return 0 if !$operator;
Branch: 1 branch
BranchTrueFalseTotal
unless $operator 0 5 5
90 5 ▶ return 0 if !$operator->isa('PPI::Token::Operator');
Branch: 1 branch
BranchTrueFalseTotal
unless $operator->isa("PPI::Token::Operator") 4 1 5
91 1 ▶ return 0 if q{=} ne $operator->content();
Branch: 1 branch
BranchTrueFalseTotal
if "=" ne $operator->content 0 1 1
92  
93 1 ▶ if ($operand->isa('PPI::Token::Magic') && $AT_ARG eq $operand->content()) {
Branch: 2 branches
BranchTrueFalseTotal
if ($operand->isa('PPI::Token::Magic') and $AT_ARG eq $operand->content) { } 0 1 1
elsif ($operand->isa('PPI::Token::Word') and 'shift' eq $operand->content) { } 0 2 2
Condition33%1/3
$operand->isa("PPI::Token::Magic") and $AT_ARG eq $operand->content
!ll&&!rl&&r
100
MC/DC0%0/2
$operand->isa("PPI::Token::Magic") and $AT_ARG eq $operand->content
$operand->isa("PPI::Token::Magic") $AT_ARG eq $operand->content
Truth table33%1/3
A and B
A= $operand->isa("PPI::Token::Magic")B= $AT_ARG eq $operand->content
ABresult
0X0
100
111
94 0 return _count_list_elements($skip_object, @elements);
95 ▶ } elsif ($operand->isa('PPI::Token::Word') && 'shift' eq $operand->content()) {
Condition33%1/3
$operand->isa("PPI::Token::Word") and "shift" eq $operand->content
!ll&&!rl&&r
100
MC/DC0%0/2
$operand->isa("PPI::Token::Word") and "shift" eq $operand->content
$operand->isa("PPI::Token::Word") "shift" eq $operand->content
Truth table33%1/3
A and B
A= $operand->isa("PPI::Token::Word")B= "shift" eq $operand->content
ABresult
0X0
100
111
96 0 ▶ my $count_first = $skip_object ? !_is_object_arg(pop @elements) : 1;
Branch: 1 branch
BranchTrueFalseTotal
$skip_object ? : 0 0 0
97 0 return $count_first + _count_args(0, @statements); # only check for object on first argument
98 }
99  
100 1 return 0;
101 }
102  
103 sub _count_list_elements {
104 0 ▶ my ($skip_object, @elements) = @_;
Subroutine _count_list_elements: not called CC=8 SCAR=42.8
105  
106 0 my $list = pop @elements;
107 0 ▶ return 0 if !$list;
Branch: 1 branch
BranchTrueFalseTotal
unless $list 0 0 0
108 0 ▶ return 0 if !$list->isa('PPI::Structure::List');
Branch: 1 branch
BranchTrueFalseTotal
unless $list->isa("PPI::Structure::List") 0 0 0
109 0 my @inner = $list->schildren;
110 0 ▶ if (1 == @inner && $inner[0]->isa('PPI::Statement::Expression')) {
Branch: 1 branch
BranchTrueFalseTotal
if (1 == @inner and $inner[0]->isa("PPI::Statement::Expression")) 0 0 0
Condition0%0/3
1 == @inner and $inner[0]->isa("PPI::Statement::Expression")
!ll&&!rl&&r
000
MC/DC0%0/2
1 == @inner and $inner[0]->isa("PPI::Statement::Expression")
1 == @inner $inner[0]->isa("PPI::Statement::Expression")
Truth table0%0/3
A and B
A= 1 == @innerB= $inner[0]->isa("PPI::Statement::Expression")
ABresult
0X0
100
111
111 0 @inner = $inner[0]->schildren;
112 }
113 0 my @args = split_nodes_on_comma(@inner);
114 0 ▶ return scalar @args if !$skip_object || !@args;
Branch: 1 branch
BranchTrueFalseTotal
unless $skip_object and @args 0 0 0
Condition0%0/3
$skip_object and @args
!ll&&!rl&&r
000
MC/DC0%0/2
$skip_object and @args
$skip_object @args
Truth table0%0/3
A and B
A= $skip_objectB= @args
ABresult
0X0
100
111
115  
116 # Check if first argument is $self/$class
117 0 my $first_ref = $args[0];
118 0 ▶ return scalar @args if scalar @{ $first_ref } != 1; # more complex than simple scalar
Branch: 1 branch
BranchTrueFalseTotal
if scalar @{$first_ref;} != 1 0 0 0
119 0 return scalar @args - !!_is_object_arg($first_ref->[0]);
120 }
121  
122 sub _is_object_arg {
123 0 ▶ my ($symbol) = @_;
Subroutine _is_object_arg: not called CC=3 SCAR=24.8
124 0 ▶ return 0 if !$symbol;
Branch: 1 branch
BranchTrueFalseTotal
unless $symbol 0 0 0
125 0 ▶ return 0 if !$symbol->isa('PPI::Token::Symbol');
Branch: 1 branch
BranchTrueFalseTotal
unless $symbol->isa("PPI::Token::Symbol") 0 0 0
126  
127 0 state $is_self_or_class = { hashify( $SELF, $CLASS ) };
128 0 return $is_self_or_class->{$symbol->content()};
129 }
130  
131 40 1;
132  
133 __END__
134  
135 #-----------------------------------------------------------------------------
136  
137 =pod
138  
139 =for stopwords refactored
140  
141 =head1 NAME
142  
143 Perl::Critic::Policy::Subroutines::ProhibitManyArgs - Too many arguments.
144  
145  
146 =head1 AFFILIATION
147  
148 This Policy is part of the core L<Perl::Critic|Perl::Critic>
149 distribution.
150  
151  
152 =head1 DESCRIPTION
153  
154 Subroutines that expect large numbers of arguments are hard to use
155 because programmers routinely have to look at documentation to
156 remember the order of those arguments. Many arguments is often a sign
157 that a subroutine should be refactored or that an object should be
158 passed to the routine.
159  
160  
161 =head1 CONFIGURATION
162  
163 By default, this policy allows up to 5 arguments without warning. To
164 change this threshold, put entries in a F<.perlcriticrc> file like
165 this:
166  
167 [Subroutines::ProhibitManyArgs]
168 max_arguments = 6
169  
170 To ignore C<$self> or C<$class> in your argument count, as long as they're
171 the first argument, use:
172  
173 [Subroutines::ProhibitManyArgs]
174 skip_object = 1
175  
176  
177 =head1 CAVEATS
178  
179 PPI doesn't currently detect anonymous subroutines, so we don't check
180 those. This should just work when PPI gains that feature.
181  
182 We don't check for C<@ARG>, the alias for C<@_> from English.pm.
183 That's deprecated anyway.
184  
185  
186 =head1 CREDITS
187  
188 Initial development of this policy was supported by a grant from the
189 Perl Foundation.
190  
191  
192 =head1 AUTHOR
193  
194 Chris Dolan <cdolan@cpan.org>
195  
196  
197 =head1 COPYRIGHT
198  
199 Copyright (c) 2007-2023 Chris Dolan. Many rights reserved.
200  
201 This program is free software; you can redistribute it and/or modify
202 it under the same terms as Perl itself. The full text of this license
203 can be found in the LICENSE file included with this module
204  
205 =cut
206  
207 # Local Variables:
208 # mode: cperl
209 # cperl-indent-level: 4
210 # fill-column: 78
211 # indent-tabs-mode: nil
212 # c-indentation-style: bsd
213 # End:
214 # ex: set ts=8 sts=4 sw=4 tw=78 ft=perl expandtab shiftround :
« blib/lib/Perl/Critic/Policy/Variables/ProhibitEvilVariables.pm ↑ Summary blib/lib/Perl/Critic/Policy/TestingAndDebugging/ProhibitProlongedStrictureOverride.pm »