File Coverage

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