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

filter: Statementstmt 60.4% 49 / 81 Branchbran 30.9% 13 / 42 Conditioncond 33.3% 6 / 18 MC/DCmcdc 9.0% 1 / 11 Subroutinesub 85.7% 12 / 14 Podpod 80.0% 4 / 5 totaltotal 49.7% 85 / 171 CC 28 SCAR 32.3
CC
28
Cov
48%
_count_list_elements
72.0
_count_args
23.3
violates
15.6
CRAP 25.4
« lib/Perl/Critic/Policy/Variables/ProhibitEvilVariables.pm ↑ Summary 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.156';
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 92 ▶sub supported_parameters {
Subroutine supported_parameters: called CC=1 SCAR=0.0
Pod undocumented
25 return (
26 {
27 92 name => 'max_arguments',
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 74 ▶sub default_severity { return $SEVERITY_MEDIUM }
Subroutine default_severity: called CC=1 SCAR=0.0
Pod documented
44 85 ▶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 5 ▶sub violates {
Subroutine violates: called CC=6 SCAR=27.4
Pod documented
50 5 my ( $self, $elem, undef ) = @_;
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 ▶ if ($prototype =~ /[[:alpha:]]/smx) { # signature (probably)
Branch: 1 branch
BranchTrueFalseTotal
if ($prototype =~ /[[:alpha:]]/msx) { } 0 0 0
59 0 ▶ if ( $self->{_skip_object} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"_skip_object"}) 0 0 0
60 0 state $c = qr/\Q$CLASS/smx;
61 0 state $s = qr/\Q$SELF/smx;
62 0 state $invocant = qr/^(?:$c|$s),?/smx;
63 0 $prototype =~ s/$invocant//smx;
64 }
65 0 $num_args = $prototype =~ tr/$@%/$@%/;
66 } else { # prototype
67 0 $prototype =~ s/ \\ [[] .*? []] /*/smxg; # Allow for grouping
68 0 $num_args = $prototype =~ tr/$@%&*_+/$@%&*_+/; # RT 56627
69 }
70 } else {
71 5 $num_args = _count_args($self->{_skip_object}, $elem->block->schildren);
72 }
73  
74 5 ▶ if ($self->{_max_arguments} < $num_args) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"_max_arguments"} < $num_args) 0 5 5
75 0 return $self->violation( $DESC, $EXPL, $elem );
76 }
77 5 return; # OK
78 }
79  
80 5 ▶sub _count_args {
Subroutine _count_args: called CC=14 SCAR=31.5
81 5 my ($skip_object, @statements) = @_;
82  
83 # look for these patterns:
84 # " ... = @_;" => then examine previous variable list
85 # " ... = shift;" => counts as one arg, then look for more
86  
87 5 ▶ return 0 if !@statements; # no statements
Branch: 1 branch
BranchTrueFalseTotal
unless @statements 0 5 5
88  
89 5 my $statement = shift @statements;
90 5 my @elements = $statement->schildren();
91 5 my $operand = pop @elements;
92 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
93 4 $operand = pop @elements;
94 }
95 5 ▶ return 0 if !$operand;
Branch: 1 branch
BranchTrueFalseTotal
unless $operand 0 5 5
96  
97 #print "pulled off last, remaining: '@elements'\n";
98 5 my $operator = pop @elements;
99 5 ▶ return 0 if !$operator;
Branch: 1 branch
BranchTrueFalseTotal
unless $operator 0 5 5
100 5 ▶ return 0 if !$operator->isa('PPI::Token::Operator');
Branch: 1 branch
BranchTrueFalseTotal
unless $operator->isa("PPI::Token::Operator") 4 1 5
101 1 ▶ return 0 if q{=} ne $operator->content();
Branch: 1 branch
BranchTrueFalseTotal
if "=" ne $operator->content 0 1 1
102  
103 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
104 0 return _count_list_elements($skip_object, @elements);
105 ▶ } 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
106 0 ▶ my $count_first = $skip_object ? !_is_object_arg(pop @elements) : 1;
Branch: 1 branch
BranchTrueFalseTotal
$skip_object ? : 0 0 0
107 0 return $count_first + _count_args(0, @statements); # only check for object on first argument
108 }
109  
110 1 return 0;
111 }
112  
113 0 ▶sub _count_list_elements {
Subroutine _count_list_elements: not called CC=8 SCAR=42.8
114 0 my ($skip_object, @elements) = @_;
115  
116 0 my $list = pop @elements;
117 0 ▶ return 0 if !$list;
Branch: 1 branch
BranchTrueFalseTotal
unless $list 0 0 0
118 0 ▶ return 0 if !$list->isa('PPI::Structure::List');
Branch: 1 branch
BranchTrueFalseTotal
unless $list->isa("PPI::Structure::List") 0 0 0
119 0 my @inner = $list->schildren;
120 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
121 0 @inner = $inner[0]->schildren;
122 }
123 0 my @args = split_nodes_on_comma(@inner);
124 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
125  
126 # Check if first argument is $self/$class
127 0 my $first_ref = $args[0];
128 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
129 0 return scalar @args - !!_is_object_arg($first_ref->[0]);
130 }
131  
132 0 ▶sub _is_object_arg {
Subroutine _is_object_arg: not called CC=3 SCAR=24.8
133 0 my ($symbol) = @_;
134 0 ▶ return 0 if !$symbol;
Branch: 1 branch
BranchTrueFalseTotal
unless $symbol 0 0 0
135 0 ▶ return 0 if !$symbol->isa('PPI::Token::Symbol');
Branch: 1 branch
BranchTrueFalseTotal
unless $symbol->isa("PPI::Token::Symbol") 0 0 0
136  
137 0 state $is_self_or_class = { hashify( $SELF, $CLASS ) };
138 0 return $is_self_or_class->{$symbol->content()};
139 }
140  
141 40 1;
142  
143 __END__
« lib/Perl/Critic/Policy/Variables/ProhibitEvilVariables.pm ↑ Summary lib/Perl/Critic/Policy/TestingAndDebugging/ProhibitProlongedStrictureOverride.pm »