1 ##############################################################################
2 # $URL: http://perlcritic.tigris.org/svn/perlcritic/trunk/Perl-Critic/lib/Perl/Critic/Policy/BuiltinFunctions/RequireSimpleSortBlock.pm $
3 # $Date: 2008-07-03 10:19:10 -0500 (Thu, 03 Jul 2008) $
6 ##############################################################################
8 package Perl::Critic::Policy::BuiltinFunctions::RequireSimpleSortBlock;
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{Sort blocks should have a single statement};
23 Readonly::Scalar my $EXPL => [ 149 ];
25 #-----------------------------------------------------------------------------
27 sub supported_parameters { return () }
28 sub default_severity { return $SEVERITY_MEDIUM }
29 sub default_themes { return qw( core pbp maintenance complexity) }
30 sub applies_to { return 'PPI::Token::Word' }
32 #-----------------------------------------------------------------------------
35 my ( $self, $elem, undef ) = @_;
37 return if $elem ne 'sort';
38 return if ! is_function_call($elem);
40 my $sib = $elem->snext_sibling();
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);
51 return if !$arg || !$arg->isa('PPI::Structure::Block');
53 # If we get here, we found a sort with a block as the first arg
54 return if ( 1 >= $arg->schildren() );
56 # more than one child statements
57 return $self->violation( $DESC, $EXPL, $elem );
62 #-----------------------------------------------------------------------------
70 Perl::Critic::Policy::BuiltinFunctions::RequireSimpleSortBlock - Sort blocks should have a single statement.
74 This Policy is part of the core L<Perl::Critic> distribution.
79 Conway advises that sort functions should be simple. Any complicated
80 operations on list elements should be computed and cached (perhaps via
81 a Schwartzian Transform) before the sort, rather than computed inside
82 the sort block, because the sort block is called C<N log N> times
83 instead of just C<N> times.
85 This policy prohibits the most blatant case of complicated sort
86 blocks: multiple statements. Future policies may wish to examine the
87 sort block in more detail -- looking for subroutine calls or large
88 numbers of operations.
93 This Policy is not configurable except for the standard options.
98 Chris Dolan <cdolan@cpan.org>
102 Copyright (C) 2006 Chris Dolan. All rights reserved.
104 This program is free software; you can redistribute it and/or modify
105 it under the same terms as Perl itself.
111 # cperl-indent-level: 4
113 # indent-tabs-mode: nil
114 # c-indentation-style: bsd
116 # ex: set ts=8 sts=4 sw=4 tw=78 ft=perl expandtab shiftround :