File Coverage

blib/lib/Perl/Critic/Policy/BuiltinFunctions/RequireSimpleSortBlock.pm
Criterion Covered Total %
statement 34 34 100.0
branch 14 14 100.0
condition 6 6 100.0
subroutine 11 11 100.0
pod 4 5 80.0
total 69 70 98.5


line stmt bran cond sub pod time code
1             package Perl::Critic::Policy::BuiltinFunctions::RequireSimpleSortBlock;
2              
3 40     40   26507 use 5.010001;
  40         208  
4 40     40   270 use strict;
  40         125  
  40         953  
5 40     40   251 use warnings;
  40         147  
  40         1246  
6 40     40   251 use Readonly;
  40         163  
  40         2298  
7              
8 40     40   327 use Perl::Critic::Utils qw{ :severities :classification };
  40         130  
  40         2197  
9 40     40   14552 use parent 'Perl::Critic::Policy';
  40         139  
  40         280  
10              
11             our $VERSION = '1.148';
12              
13             #-----------------------------------------------------------------------------
14              
15             Readonly::Scalar my $DESC => q{Sort blocks should have a single statement};
16             Readonly::Scalar my $EXPL => [ 149 ];
17              
18             #-----------------------------------------------------------------------------
19              
20 92     92 0 1678 sub supported_parameters { return () }
21 75     75 1 345 sub default_severity { return $SEVERITY_MEDIUM }
22 86     86 1 382 sub default_themes { return qw( core pbp maintenance complexity) }
23 33     33 1 100 sub applies_to { return 'PPI::Token::Word' }
24              
25             #-----------------------------------------------------------------------------
26              
27             sub violates {
28 351     351 1 827 my ( $self, $elem, undef ) = @_;
29              
30 351 100       735 return if $elem->content() ne 'sort';
31 18 100       98 return if ! is_function_call($elem);
32              
33 17         44 my $sib = $elem->snext_sibling();
34 17 100       329 return if !$sib;
35              
36 16         24 my $arg = $sib;
37 16 100       48 if ( $arg->isa('PPI::Structure::List') ) {
38 6         44 $arg = $arg->schild(0);
39             # Forward looking: PPI might change in v1.200 so schild(0) is a PPI::Statement::Expression
40 6 100 100     93 if ( $arg && $arg->isa('PPI::Statement::Expression') ) {
41 2         7 $arg = $arg->schild(0);
42             }
43             }
44 16 100 100     117 return if !$arg || !$arg->isa('PPI::Structure::Block');
45              
46             # If we get here, we found a sort with a block as the first arg
47 8 100       23 return if ( 1 >= $arg->schildren() );
48              
49             # more than one child statements
50 1         25 return $self->violation( $DESC, $EXPL, $elem );
51             }
52              
53             1;
54              
55             #-----------------------------------------------------------------------------
56              
57             __END__
58              
59             =pod
60              
61             =head1 NAME
62              
63             Perl::Critic::Policy::BuiltinFunctions::RequireSimpleSortBlock - Sort blocks should have a single statement.
64              
65              
66             =head1 AFFILIATION
67              
68             This Policy is part of the core L<Perl::Critic|Perl::Critic>
69             distribution.
70              
71              
72             =head1 DESCRIPTION
73              
74             Conway advises that sort functions should be simple. Any complicated
75             operations on list elements should be computed and cached (perhaps via
76             a Schwartzian Transform) before the sort, rather than computed inside
77             the sort block, because the sort block is called C<N log N> times
78             instead of just C<N> times.
79              
80             This policy prohibits the most blatant case of complicated sort
81             blocks: multiple statements. Future policies may wish to examine the
82             sort block in more detail -- looking for subroutine calls or large
83             numbers of operations.
84              
85              
86             =head1 CONFIGURATION
87              
88             This Policy is not configurable except for the standard options.
89              
90              
91             =head1 AUTHOR
92              
93             Chris Dolan <cdolan@cpan.org>
94              
95             =head1 COPYRIGHT
96              
97             Copyright (c) 2006-2011 Chris Dolan.
98              
99             This program is free software; you can redistribute it and/or modify
100             it under the same terms as Perl itself.
101              
102             =cut
103              
104             # Local Variables:
105             # mode: cperl
106             # cperl-indent-level: 4
107             # fill-column: 78
108             # indent-tabs-mode: nil
109             # c-indentation-style: bsd
110             # End:
111             # ex: set ts=8 sts=4 sw=4 tw=78 ft=perl expandtab shiftround :