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 / ProhibitBooleanGrep.pm
1 ##############################################################################
2 #      $URL: http://perlcritic.tigris.org/svn/perlcritic/trunk/Perl-Critic/lib/Perl/Critic/Policy/BuiltinFunctions/ProhibitBooleanGrep.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::ProhibitBooleanGrep;
9
10 use 5.006001;
11 use strict;
12 use warnings;
13 use Readonly;
14
15 use Perl::Critic::Utils qw{ :severities :classification hashify };
16 use base 'Perl::Critic::Policy';
17
18 our $VERSION = '1.088';
19
20 #-----------------------------------------------------------------------------
21
22 Readonly::Scalar my $DESC => q{"grep" used in boolean context};
23 Readonly::Scalar my $EXPL => [71,72];
24
25 Readonly::Hash my %POSTFIX_CONDITIONALS => hashify( qw(if unless while until) );
26 Readonly::Hash my %BOOLEAN_OPERATORS => hashify( qw(&& || ! not or and));
27
28 #-----------------------------------------------------------------------------
29
30 sub supported_parameters { return ()                     }
31 sub default_severity     { return $SEVERITY_LOW          }
32 sub default_themes       { return qw( core pbp performance ) }
33 sub applies_to           { return 'PPI::Token::Word'     }
34
35 #-----------------------------------------------------------------------------
36
37 sub violates {
38     my ( $self, $elem, undef ) = @_;
39
40     return if $elem ne 'grep';
41     return if not is_function_call($elem);
42     return if not _is_in_boolean_context($elem);
43
44     return $self->violation( $DESC, $EXPL, $elem );
45 }
46
47 #-----------------------------------------------------------------------------
48
49 sub _is_in_boolean_context {
50     my ($token) = @_;
51
52     return _does_prev_sibling_cause_boolean($token) || _does_parent_cause_boolean($token);
53 }
54
55 sub _does_prev_sibling_cause_boolean {
56     my ($token) = @_;
57
58     my $prev = $token->sprevious_sibling;
59     return if !$prev;
60     return 1 if $prev->isa('PPI::Token::Word') and $POSTFIX_CONDITIONALS{$prev};
61     return if not ($prev->isa('PPI::Token::Operator') and $BOOLEAN_OPERATORS{$prev});
62     my $next = $token->snext_sibling;
63     return 1 if not $next; # bizarre: grep with no arguments
64
65     # loose heuristic: unparenthesized grep has no following non-boolean operators
66     return 1 if not $next->isa('PPI::Structure::List');
67
68     $next = $next->snext_sibling;
69     return 1 if not $next;
70     return 1 if $next->isa('PPI::Token::Operator') and $BOOLEAN_OPERATORS{$next};
71     return;
72 }
73
74 sub _does_parent_cause_boolean {
75     my ($token) = @_;
76
77     my $prev = $token->sprevious_sibling;
78     return if $prev;
79     my $parent = $token->statement->parent;
80     for (my $node = $parent; $node; $node = $node->parent) { ##no critic 'CStyleForLoop'
81         next if $node->isa('PPI::Structure::List');
82         return 1 if $node->isa('PPI::Structure::Condition');
83     }
84
85     return;
86 }
87
88 1;
89
90 __END__
91
92 #-----------------------------------------------------------------------------
93
94 =pod
95
96 =head1 NAME
97
98 Perl::Critic::Policy::BuiltinFunctions::ProhibitBooleanGrep - Use C<List::MoreUtils::any> instead of C<grep> in boolean context.
99
100 =head1 AFFILIATION
101
102 This Policy is part of the core L<Perl::Critic> distribution.
103
104
105 =head1 DESCRIPTION
106
107 Using C<grep> in boolean context is a common idiom for checking if any
108 elements in a list match a condition.  This works because boolean context is a
109 subset of scalar context, and grep returns the number of matches in scalar
110 context.  A non-zero number of matches means a match.
111
112 But consider the case of a long array where the first element is a match.
113 Boolean C<grep> still checks all of the rest of the elements needlessly.
114 Instead, a better solution is to use the C<any> function from
115 L<List::MoreUtils>, which short-circuits after the first successful match to
116 save time.
117
118
119 =head1 CONFIGURATION
120
121 This Policy is not configurable except for the standard options.
122
123
124 =head1 CAVEATS
125
126 The algorithm for detecting boolean context takes a LOT of shortcuts.  There
127 are lots of known false negatives.  But, I was conservative in writing this,
128 so I hope there are no false positives.
129
130 =head1 AUTHOR
131
132 Chris Dolan <cdolan@cpan.org>
133
134 =head1 CREDITS
135
136 Initial development of this policy was supported by a grant from the Perl
137 Foundation.
138
139 =head1 COPYRIGHT
140
141 Copyright (c) 2007-2008 Chris Dolan.  Many rights reserved.
142
143 This program is free software; you can redistribute it and/or modify
144 it under the same terms as Perl itself.  The full text of this license
145 can be found in the LICENSE file included with this module.
146
147 =cut
148
149 # Local Variables:
150 #   mode: cperl
151 #   cperl-indent-level: 4
152 #   fill-column: 78
153 #   indent-tabs-mode: nil
154 #   c-indentation-style: bsd
155 # End:
156 # ex: set ts=8 sts=4 sw=4 tw=78 ft=perl expandtab shiftround :