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 / ProhibitReverseSortBlock.pm
1 ##############################################################################
2 #      $URL: http://perlcritic.tigris.org/svn/perlcritic/trunk/Perl-Critic/lib/Perl/Critic/Policy/BuiltinFunctions/ProhibitReverseSortBlock.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::ProhibitReverseSortBlock;
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{Forbid $b before $a in sort blocks}; ## no critic (Interpolation)
23 Readonly::Scalar my $EXPL => [ 152 ];
24
25 #-----------------------------------------------------------------------------
26
27 sub supported_parameters { return ()                    }
28 sub default_severity     { return $SEVERITY_LOWEST      }
29 sub default_themes       { return qw(core pbp cosmetic) }
30 sub applies_to           { return 'PPI::Token::Word'    }
31
32 #-----------------------------------------------------------------------------
33
34 sub violates {
35     my ($self, $elem, $doc) = @_;
36
37     return if $elem ne 'sort';
38     return if ! is_function_call($elem);
39
40     my $sib = $elem->snext_sibling();
41     return if !$sib;
42
43     my $arg = $sib;
44     if ( $arg->isa('PPI::Structure::List') ) {
45         $arg = $arg->schild(0);
46         # Forward looking: PPI might change in v1.200 so schild(0) is a PPI::Statement::Expression
47         if ( $arg && $arg->isa('PPI::Statement::Expression') ) {
48             $arg = $arg->schild(0);
49         }
50     }
51     return if !$arg || !$arg->isa('PPI::Structure::Block');
52
53     # If we get here, we found a sort with a block as the first arg
54
55     # Look at each statement in the block separately.
56     # $a is +1, $b is -1, sum should always be >= 0.
57     # This may go badly if there are conditionals or loops or other
58     # sub-statements...
59     for my $statement ($arg->children) {
60         my @sort_vars = $statement =~ m/\$([ab])\b/gxms;
61         my $count = 0;
62         for my $sort_var (@sort_vars) {
63             if ($sort_var eq 'a') {
64                 $count++;
65             } else {
66                 $count--;
67                 if ($count < 0) {
68                     # Found too many C<$b>s too early
69                     my $sev = $self->get_severity();
70                     return $self->violation( $DESC, $EXPL, $elem, $sev );
71                 }
72             }
73         }
74     }
75     return; #ok
76 }
77
78 1;
79
80 #-----------------------------------------------------------------------------
81
82 __END__
83
84 =pod
85
86 =head1 NAME
87
88 Perl::Critic::Policy::BuiltinFunctions::ProhibitReverseSortBlock - Forbid $b before $a in sort blocks.
89
90 =head1 AFFILIATION
91
92 This Policy is part of the core L<Perl::Critic> distribution.
93
94
95 =head1 DESCRIPTION
96
97 Conway says that it is much clearer to use C<reverse> than to flip C<$a> and
98 C<$b> around in a C<sort> block.  He also suggests that, in newer perls,
99 C<reverse> is specifically looked for and optimized, and in the case of a
100 simple reversed string C<sort>, using C<reverse> with a C<sort> with no block
101 is faster even in old perls.
102
103   my @foo = sort { $b cmp $a } @bar;         #not ok
104   my @foo = reverse sort @bar;               #ok
105
106   my @foo = sort { $b <=> $a } @bar;         #not ok
107   my @foo = reverse sort { $a <=> $b } @bar; #ok
108
109
110 =head1 CONFIGURATION
111
112 This Policy is not configurable except for the standard options.
113
114
115 =head1 AUTHOR
116
117 Chris Dolan <cdolan@cpan.org>
118
119 =head1 COPYRIGHT
120
121 Copyright (C) 2006 Chris Dolan.  All rights reserved.
122
123 This program is free software; you can redistribute it and/or modify
124 it under the same terms as Perl itself.
125
126 =cut
127
128 # Local Variables:
129 #   mode: cperl
130 #   cperl-indent-level: 4
131 #   fill-column: 78
132 #   indent-tabs-mode: nil
133 #   c-indentation-style: bsd
134 # End:
135 # ex: set ts=8 sts=4 sw=4 tw=78 ft=perl expandtab shiftround :