1 ##############################################################################
2 # $URL: http://perlcritic.tigris.org/svn/perlcritic/trunk/Perl-Critic/lib/Perl/Critic/Policy/ValuesAndExpressions/ProhibitLongChainsOfMethodCalls.pm $
3 # $Date: 2008-07-03 10:19:10 -0500 (Thu, 03 Jul 2008) $
6 ##############################################################################
8 package Perl::Critic::Policy::ValuesAndExpressions::ProhibitLongChainsOfMethodCalls;
15 use Perl::Critic::Utils qw{ :characters :severities };
16 use Perl::Critic::Utils::PPI qw{ is_ppi_expression_or_generic_statement };
18 use base 'Perl::Critic::Policy';
20 our $VERSION = '1.088';
22 #-----------------------------------------------------------------------------
24 Readonly::Scalar my $EXPL =>
25 q{Long chains of method calls indicate code that is too tightly coupled};
27 #-----------------------------------------------------------------------------
29 sub supported_parameters {
32 name => 'max_chain_length',
33 description => 'The number of chained calls to allow.',
34 default_string => '3',
35 behavior => 'integer',
41 sub default_severity { return $SEVERITY_LOW }
42 sub default_themes { return qw( core maintenance ) }
43 sub applies_to { return qw{ PPI::Statement }; }
45 #-----------------------------------------------------------------------------
47 sub _max_chain_length {
50 return $self->{_max_chain_length};
53 #-----------------------------------------------------------------------------
56 my ( $self, $elem, undef ) = @_;
58 return if not is_ppi_expression_or_generic_statement($elem);
61 my $max_chain_length = $self->_max_chain_length();
62 my @children = $elem->schildren();
63 my $child = shift @children;
66 # if it looks like we've got a subroutine call, drop the parameter
69 $child->isa('PPI::Token::Word')
71 and $children[0]->isa('PPI::Structure::List')
77 $child->isa('PPI::Token::Word')
78 or $child->isa('PPI::Token::Symbol')
81 if ( $children[0]->isa('PPI::Token::Operator') ) {
82 if ( q{->} eq $children[0]->content() ) {
87 elsif ( not $children[0]->isa('PPI::Token::Structure') ) {
93 if ($chain_length > $max_chain_length) {
96 "Found method-call chain of length $chain_length.",
105 $child = shift @children;
108 if ($chain_length > $max_chain_length) {
111 "Found method-call chain of length $chain_length.",
125 #-----------------------------------------------------------------------------
129 =for stopwords MSCHWERN
133 Perl::Critic::Policy::ValuesAndExpressions::ProhibitLongChainsOfMethodCalls - Long chains of method calls indicate tightly coupled code.
138 This Policy is part of the core L<Perl::Critic> distribution.
143 A long chain of method calls usually indicates that the code knows too
144 much about the interrelationships between objects. If the code is
145 able to directly navigate far down a network of objects, then when the
146 network changes structure in the future, the code will need to be
147 modified to deal with the change. The code is too tightly coupled and
153 $x = $y->a->b->c; #questionable, but allowed by default
154 $x = $y->a->b->c->d; #not ok
159 This policy has one option: C<max_chain_length> which controls how far
160 the code is allowed to navigate. The default value is 3.
165 Elliot Shank C<< <perl@galumph.com> >>
170 Copyright (c) 2007-2008 Elliot Shank. All rights reserved.
172 This program is free software; you can redistribute it and/or modify
173 it under the same terms as Perl itself. The full text of this license
174 can be found in the LICENSE file included with this module.
180 # cperl-indent-level: 4
182 # indent-tabs-mode: nil
183 # c-indentation-style: bsd
185 # ex: set ts=8 sts=4 sw=4 tw=78 ft=perl expandtab shiftround :