lib/Perl/Critic/Policy/ValuesAndExpressions/ProhibitLongChainsOfMethodCalls.pm

filter: Statementstmt 92.3% 48 / 52 Branchbran 85.0% 17 / 20 Conditioncond 88.8% 8 / 9 MC/DCmcdc 80.0% 4 / 5 Subroutinesub 100% 13 / 13 Podpod 80.0% 4 / 5 totaltotal 90.3% 94 / 104 CC 14 SCAR 21.3
CC
14
Cov
90%
violates
14.7
BEGIN
1.0
BEGIN
1.0
CRAP 8.4
« lib/Perl/Critic/Annotation.pm ↑ Summary lib/Perl/Critic/Policy/Variables/ProhibitUnusedVariables.pm »
1 package Perl::Critic::Policy::ValuesAndExpressions::ProhibitLongChainsOfMethodCalls;
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{ :characters :severities };
Subroutine BEGIN: called CC=1 SCAR=0.0
9 40 ▶use Perl::Critic::Utils::PPI qw{ is_ppi_expression_or_generic_statement };
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 40 ▶use parent 'Perl::Critic::Policy';
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 40 our $VERSION = '1.156';
14  
15 #-----------------------------------------------------------------------------
16  
17 40 Readonly::Scalar my $EXPL =>
18 q{Long chains of method calls indicate code that is too tightly coupled};
19  
20 #-----------------------------------------------------------------------------
21  
22 91 ▶sub supported_parameters {
Subroutine supported_parameters: called CC=1 SCAR=0.0
Pod undocumented
23 return (
24 {
25 91 name => 'max_chain_length',
26 description => 'The number of chained calls to allow.',
27 default_string => '3',
28 behavior => 'integer',
29 integer_minimum => 1,
30 },
31 );
32 }
33  
34 74 ▶sub default_severity { return $SEVERITY_LOW }
Subroutine default_severity: called CC=1 SCAR=0.0
Pod documented
35 73 ▶sub default_themes { return qw( core maintenance ) }
Subroutine default_themes: called CC=1 SCAR=0.0
Pod documented
36 31 ▶sub applies_to { return qw{ PPI::Statement }; }
Subroutine applies_to: called CC=1 SCAR=0.0
Pod documented
37  
38 #-----------------------------------------------------------------------------
39  
40 176 ▶sub _max_chain_length {
Subroutine _max_chain_length: called CC=1 SCAR=0.0
41 176 my ( $self ) = @_;
42  
43 176 return $self->{_max_chain_length};
44 }
45  
46 #-----------------------------------------------------------------------------
47  
48 283 ▶sub violates {
Subroutine violates: called CC=14 SCAR=26.9
Pod documented
49 283 my ( $self, $elem, undef ) = @_;
50  
51 283 ▶ return if not is_ppi_expression_or_generic_statement($elem);
Branch: 1 branch
BranchTrueFalseTotal
unless is_ppi_expression_or_generic_statement($elem) 107 176 283
52  
53 176 my $chain_length = 0;
54 176 my $max_chain_length = $self->_max_chain_length();
55 176 my @children = $elem->schildren();
56 176 my $child = shift @children;
57  
58 176 ▶ while ($child) {
Branch: 1 branch
BranchTrueFalseTotal
if ($child) 743 176 919
59 # if it looks like we've got a subroutine call, drop the parameter
60 # list.
61 743 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if ($child->isa("PPI::Token::Word") and @children and $children[0]->isa("PPI::Structure::List")) 16 727 743
Condition83%5/6
$child->isa("PPI::Token::Word") and @children and $children[0]->isa("PPI::Structure::List")
!ll&&!rl&&r
61211516
$child->isa("PPI::Token::Word") and @children
!ll&&!rl&&r
6120131
MC/DC66%2/3
$child->isa("PPI::Token::Word") and @children and $children[0]->isa("PPI::Structure::List")
$child->isa("PPI::Token::Word") @children $children[0]->isa("PPI::Structure::List")
Truth table75%3/4
A and B and C
A= $child->isa("PPI::Token::Word")B= @childrenC= $children[0]->isa("PPI::Structure::List")
ABCresult
0XX0
10X0
1100
1111
62 $child->isa('PPI::Token::Word')
63 and @children
64 and $children[0]->isa('PPI::Structure::List')
65 ) {
66 16 shift @children;
67 }
68  
69 743 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if ($child->isa('PPI::Token::Word') or $child->isa('PPI::Token::Symbol')) { } 294 449 743
Condition100%3/3
$child->isa("PPI::Token::Word") or $child->isa("PPI::Token::Symbol")
l!l&&r!l&&!r
131163449
MC/DC100%2/2
$child->isa("PPI::Token::Word") or $child->isa("PPI::Token::Symbol")
$child->isa("PPI::Token::Word") $child->isa("PPI::Token::Symbol")
Truth table100%3/3
A or B
A= $child->isa("PPI::Token::Word")B= $child->isa("PPI::Token::Symbol")
ABresult
1X1
011
000
70 $child->isa('PPI::Token::Word')
71 or $child->isa('PPI::Token::Symbol')
72 ) {
73 294 ▶ if ( @children ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@children) 284 10 294
74 284 ▶ if ( $children[0]->isa('PPI::Token::Operator') ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($children[0]->isa('PPI::Token::Operator')) { } 134 150 284
elsif (not $children[0]->isa('PPI::Token::Structure')) { } 133 17 150
75 134 ▶ if ( q{->} eq $children[0]->content() ) {
Branch: 1 branch
BranchTrueFalseTotal
if ("->" eq $children[0]->content) 0 134 134
76 0 $chain_length++;
77 0 shift @children;
78 }
79 }
80 elsif ( not $children[0]->isa('PPI::Token::Structure') ) {
81 133 $chain_length = 0;
82 }
83 }
84 }
85 else {
86 449 ▶ if ($chain_length > $max_chain_length) {
Branch: 1 branch
BranchTrueFalseTotal
if ($chain_length > $max_chain_length) 0 449 449
87 return
88 0 $self->violation(
89 "Found method-call chain of length $chain_length.",
90 $EXPL,
91 $elem,
92 );
93 }
94  
95 449 $chain_length = 0;
96 }
97  
98 743 $child = shift @children;
99 }
100  
101 176 ▶ if ($chain_length > $max_chain_length) {
Branch: 1 branch
BranchTrueFalseTotal
if ($chain_length > $max_chain_length) 0 176 176
102 return
103 0 $self->violation(
104 "Found method-call chain of length $chain_length.",
105 $EXPL,
106 $elem,
107 );
108 }
109  
110 176 return;
111 }
112  
113  
114 40 1;
115  
116 __END__
« lib/Perl/Critic/Annotation.pm ↑ Summary lib/Perl/Critic/Policy/Variables/ProhibitUnusedVariables.pm »