Modified source files and compiled any and armel versions of packages
[pkg-perl] / deb-src / libperl-critic-perl / libperl-critic-perl-1.088 / lib / Perl / Critic / Policy / BuiltinFunctions / ProhibitComplexMappings.pm
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) $
4 #   $Author: clonezone $
5 # $Revision: 2489 $
6 ##############################################################################
7
8 package Perl::Critic::Policy::BuiltinFunctions::ProhibitComplexMappings;
9
10 use 5.006001;
11 use strict;
12 use warnings;
13 use Readonly;
14
15 use Perl::Critic::Utils qw{ :severities :classification };
16 use base 'Perl::Critic::Policy';
17
18 our $VERSION = '1.088';
19
20 #-----------------------------------------------------------------------------
21
22 Readonly::Scalar my $DESC => q{Map blocks should have a single statement};
23 Readonly::Scalar my $EXPL => [ 113 ];
24
25 #-----------------------------------------------------------------------------
26
27 sub supported_parameters {
28     return (
29         {
30             name            => 'max_statements',
31             description     =>
32                 'The maximum number of statements to allow within a map block.',
33             default_string  => '1',
34             behavior        => 'integer',
35             integer_minimum => 1,
36         },
37     );
38 }
39
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'                   }
43
44 #-----------------------------------------------------------------------------
45
46 sub violates {
47     my ( $self, $elem, undef ) = @_;
48
49     return if $elem ne 'map';
50     return if ! is_function_call($elem);
51
52     my $sib = $elem->snext_sibling();
53     return if !$sib;
54
55     my $arg = $sib;
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);
61         }
62     }
63     # If it's not a block, it's an expression-style map, which is only one statement by definition
64     return if !$arg;
65     return if !$arg->isa('PPI::Structure::Block');
66
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();
70
71     # more than one child statements
72     return $self->violation( $DESC, $EXPL, $elem );
73 }
74
75 1;
76
77 #-----------------------------------------------------------------------------
78
79 __END__
80
81 =pod
82
83 =head1 NAME
84
85 Perl::Critic::Policy::BuiltinFunctions::ProhibitComplexMappings - Map blocks should have a single statement.
86
87 =head1 AFFILIATION
88
89 This Policy is part of the core L<Perl::Critic> distribution.
90
91
92 =head1 DESCRIPTION
93
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.
100
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
103 map as a for loop.
104
105 =head1 CAVEATS
106
107 This policy currently misses some compound statements inside of the
108 map.  For example, the following code incorrectly does not trigger a
109 violation:
110
111   map { do { foo(); bar() } } @list
112
113 =head1 CONFIGURATION
114
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:
118
119   [BuiltinFunctions::ProhibitComplexMappings]
120   max_statements = 2
121
122 =head1 AUTHOR
123
124 Chris Dolan <cdolan@cpan.org>
125
126 =head1 CREDITS
127
128 Initial development of this policy was supported by a grant from the Perl Foundation.
129
130 =head1 COPYRIGHT
131
132 Copyright (C) 2007 Chris Dolan.  All rights reserved.
133
134 This program is free software; you can redistribute it and/or modify
135 it under the same terms as Perl itself.
136
137 =cut
138
139 # Local Variables:
140 #   mode: cperl
141 #   cperl-indent-level: 4
142 #   fill-column: 78
143 #   indent-tabs-mode: nil
144 #   c-indentation-style: bsd
145 # End:
146 # ex: set ts=8 sts=4 sw=4 tw=78 ft=perl expandtab shiftround :