1 ##############################################################################
2 # $URL: http://perlcritic.tigris.org/svn/perlcritic/trunk/Perl-Critic/lib/Perl/Critic/Policy/BuiltinFunctions/ProhibitComplexMappings.pm $
3 # $Date: 2008-07-03 10:19:10 -0500 (Thu, 03 Jul 2008) $
6 ##############################################################################
8 package Perl::Critic::Policy::BuiltinFunctions::ProhibitComplexMappings;
15 use Perl::Critic::Utils qw{ :severities :classification };
16 use base 'Perl::Critic::Policy';
18 our $VERSION = '1.088';
20 #-----------------------------------------------------------------------------
22 Readonly::Scalar my $DESC => q{Map blocks should have a single statement};
23 Readonly::Scalar my $EXPL => [ 113 ];
25 #-----------------------------------------------------------------------------
27 sub supported_parameters {
30 name => 'max_statements',
32 'The maximum number of statements to allow within a map block.',
33 default_string => '1',
34 behavior => 'integer',
40 sub default_severity { return $SEVERITY_MEDIUM }
41 sub default_themes { return qw( core pbp maintenance complexity) }
42 sub applies_to { return 'PPI::Token::Word' }
44 #-----------------------------------------------------------------------------
47 my ( $self, $elem, undef ) = @_;
49 return if $elem ne 'map';
50 return if ! is_function_call($elem);
52 my $sib = $elem->snext_sibling();
56 if ( $arg->isa('PPI::Structure::List') ) {
57 $arg = $arg->schild(0);
58 # Forward looking: PPI might change in v1.200 so schild(0) is a PPI::Statement::Expression
59 if ( $arg && $arg->isa('PPI::Statement::Expression') ) {
60 $arg = $arg->schild(0);
63 # If it's not a block, it's an expression-style map, which is only one statement by definition
65 return if !$arg->isa('PPI::Structure::Block');
67 # If we get here, we found a sort with a block as the first arg
68 return if $self->{_max_statements} >= $arg->schildren()
69 && 0 == grep {$_->isa('PPI::Statement::Compound')} $arg->schildren();
71 # more than one child statements
72 return $self->violation( $DESC, $EXPL, $elem );
77 #-----------------------------------------------------------------------------
85 Perl::Critic::Policy::BuiltinFunctions::ProhibitComplexMappings - Map blocks should have a single statement.
89 This Policy is part of the core L<Perl::Critic> distribution.
94 The map function can be confusing to novices in the best of
95 circumstances. Mappings with multiple statements are even worse.
96 They're also a maintainer's nightmare because any added complexity
97 decreases readability precipitously. Why? Because map is
98 traditionally a one-liner converting one array to another. Trying to
99 cram lots of functionality into a one-liner is a bad idea in general.
101 The best solutions to a complex mapping are: 1) write a subroutine
102 that performs the manipulation and call that from map; 2) rewrite the
107 This policy currently misses some compound statements inside of the
108 map. For example, the following code incorrectly does not trigger a
111 map { do { foo(); bar() } } @list
115 By default this policy flags any mappings with more than one
116 statement. While we do not recommend it, you can increase this limit
117 as follows in a F<.perlcriticrc> file:
119 [BuiltinFunctions::ProhibitComplexMappings]
124 Chris Dolan <cdolan@cpan.org>
128 Initial development of this policy was supported by a grant from the Perl Foundation.
132 Copyright (C) 2007 Chris Dolan. All rights reserved.
134 This program is free software; you can redistribute it and/or modify
135 it under the same terms as Perl itself.
141 # cperl-indent-level: 4
143 # indent-tabs-mode: nil
144 # c-indentation-style: bsd
146 # ex: set ts=8 sts=4 sw=4 tw=78 ft=perl expandtab shiftround :