blib/lib/Perl/Critic/Policy/BuiltinFunctions/RequireBlockGrep.pm

filter: Statementstmt 87.5% 28 / 32 Branchbran 37.5% 3 / 8 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 11 / 11 Podpod 80.0% 4 / 5 totaltotal 82.1% 46 / 56 CC 5 SCAR 14.2
CC
5
Cov
78%
violates
10.4
BEGIN
1.0
BEGIN
1.0
CRAP 4.1
« Build ↑ Summary blib/lib/Perl/Critic/Policy/InputOutput/ProhibitJoinedReadline.pm »
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 ▶use 5.010001;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 40 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 40 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 40 ▶use Readonly;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 40 ▶use Perl::Critic::Utils qw{ :severities :classification :ppi };
Subroutine BEGIN: called CC=1 SCAR=0.0
13 40 ▶use parent 'Perl::Critic::Policy';
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 40 our $VERSION = '1.150';
16  
17 #-----------------------------------------------------------------------------
18  
19 40 Readonly::Scalar my $DESC => q{Expression form of "grep"};
20 40 Readonly::Scalar my $EXPL => [ 169 ];
21  
22 #-----------------------------------------------------------------------------
23  
24 90 ▶sub supported_parameters { return () }
Subroutine supported_parameters: called CC=1 SCAR=0.0
Pod undocumented
25 74 ▶sub default_severity { return $SEVERITY_HIGH }
Subroutine default_severity: called CC=1 SCAR=0.0
Pod documented
26 92 ▶sub default_themes { return qw( core bugs pbp ) }
Subroutine default_themes: called CC=1 SCAR=0.0
Pod documented
27 33 ▶sub applies_to { return 'PPI::Token::Word' }
Subroutine applies_to: called CC=1 SCAR=0.0
Pod documented
28  
29 #-----------------------------------------------------------------------------
30  
31 sub violates {
32 349 ▶ my ( $self, $elem, undef ) = @_;
Subroutine violates: called CC=5 SCAR=23.4
Pod documented
33  
34 349 ▶ return if $elem->content() ne 'grep';
Branch: 1 branch
BranchTrueFalseTotal
if $elem->content ne "grep" 346 3 349
35 3 ▶ return if ! is_function_call($elem);
Branch: 1 branch
BranchTrueFalseTotal
unless is_function_call($elem) 3 0 3
36  
37 0 my $arg = first_arg($elem);
38 0 ▶ return if !$arg;
Branch: 1 branch
BranchTrueFalseTotal
unless $arg 0 0 0
39 0 ▶ return if $arg->isa('PPI::Structure::Block');
Branch: 1 branch
BranchTrueFalseTotal
if $arg->isa("PPI::Structure::Block") 0 0 0
40  
41 0 return $self->violation( $DESC, $EXPL, $elem );
42 }
43  
44 40 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 :
« Build ↑ Summary blib/lib/Perl/Critic/Policy/InputOutput/ProhibitJoinedReadline.pm »