File Coverage

blib/lib/Perl/Critic/Policy/BuiltinFunctions/RequireBlockGrep.pm
Criterion Covered Total %
statement 28 28 100.0
branch 8 8 100.0
condition n/a
subroutine 11 11 100.0
pod 4 5 80.0
total 51 52 98.0


line stmt bran cond sub pod time code
1             package Perl::Critic::Policy::BuiltinFunctions::RequireBlockGrep;
2              
3             # DEVELOPER NOTE: this module is used as an example in DEVELOPER.pod.
4             # If you make changes in here, please reflect those changes in the
5             # examples.
6              
7 40     40   27059 use 5.010001;
  40         196  
8 40     40   267 use strict;
  40         112  
  40         854  
9 40     40   233 use warnings;
  40         114  
  40         1044  
10 40     40   297 use Readonly;
  40         114  
  40         2161  
11              
12 40     40   330 use Perl::Critic::Utils qw{ :severities :classification :ppi };
  40         136  
  40         2088  
13 40     40   15561 use parent 'Perl::Critic::Policy';
  40         130  
  40         300  
14              
15             our $VERSION = '1.146';
16              
17             #-----------------------------------------------------------------------------
18              
19             Readonly::Scalar my $DESC => q{Expression form of "grep"};
20             Readonly::Scalar my $EXPL => [ 169 ];
21              
22             #-----------------------------------------------------------------------------
23              
24 92     92 0 1666 sub supported_parameters { return () }
25 75     75 1 361 sub default_severity { return $SEVERITY_HIGH }
26 92     92 1 378 sub default_themes { return qw( core bugs pbp ) }
27 35     35 1 108 sub applies_to { return 'PPI::Token::Word' }
28              
29             #-----------------------------------------------------------------------------
30              
31             sub violates {
32 358     358 1 656 my ( $self, $elem, undef ) = @_;
33              
34 358 100       726 return if $elem->content() ne 'grep';
35 14 100       84 return if ! is_function_call($elem);
36              
37 9         26 my $arg = first_arg($elem);
38 9 100       54 return if !$arg;
39 6 100       31 return if $arg->isa('PPI::Structure::Block');
40              
41 2         16 return $self->violation( $DESC, $EXPL, $elem );
42             }
43              
44             1;
45              
46             __END__
47              
48             #-----------------------------------------------------------------------------
49              
50             =pod
51              
52             =head1 NAME
53              
54             Perl::Critic::Policy::BuiltinFunctions::RequireBlockGrep - Write C<grep { /$pattern/ } @list> instead of C<grep /$pattern/, @list>.
55              
56             =head1 AFFILIATION
57              
58             This Policy is part of the core L<Perl::Critic|Perl::Critic>
59             distribution.
60              
61             =head1 DESCRIPTION
62              
63             The expression forms of C<grep> and C<map> are awkward and hard to
64             read. Use the block forms instead.
65              
66             @matches = grep /pattern/, @list; #not ok
67             @matches = grep { /pattern/ } @list; #ok
68              
69             @mapped = map transform($_), @list; #not ok
70             @mapped = map { transform($_) } @list; #ok
71              
72             =head1 CONFIGURATION
73              
74             This Policy is not configurable except for the standard options.
75              
76             =head1 SEE ALSO
77              
78             L<Perl::Critic::Policy::BuiltinFunctions::ProhibitStringyEval|Perl::Critic::Policy::BuiltinFunctions::ProhibitStringyEval>
79              
80             L<Perl::Critic::Policy::BuiltinFunctions::RequireBlockMap|Perl::Critic::Policy::BuiltinFunctions::RequireBlockMap>
81              
82             =head1 AUTHOR
83              
84             Jeffrey Ryan Thalhammer <jeff@imaginative-software.com>
85              
86             =head1 COPYRIGHT
87              
88             Copyright (c) 2005-2013 Imaginative Software Systems. All rights reserved.
89              
90             This program is free software; you can redistribute it and/or modify
91             it under the same terms as Perl itself. The full text of this license
92             can be found in the LICENSE file included with this module.
93              
94             =cut
95              
96             # Local Variables:
97             # mode: cperl
98             # cperl-indent-level: 4
99             # fill-column: 78
100             # indent-tabs-mode: nil
101             # c-indentation-style: bsd
102             # End:
103             # ex: set ts=8 sts=4 sw=4 tw=78 ft=perl expandtab shiftround :