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

filter: Statementstmt 55.5% 30 / 54 Branchbran 10.7% 3 / 28 Conditioncond 0.0% 0 / 12 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 78.5% 11 / 14 Podpod 80.0% 4 / 5 totaltotal 39.6% 48 / 121 CC 19 SCAR 39.4
CC
19
Cov
35%
_does_prev_sibling_cause_boolean
132.0
_does_parent_cause_boolean
30.0
_is_in_boolean_context
6.0
CRAP 51.4
« blib/lib/Perl/Critic/Policy/Objects/ProhibitIndirectSyntax.pm ↑ Summary blib/lib/Perl/Critic/Policy/ValuesAndExpressions/ProhibitLeadingZeros.pm »
1 package Perl::Critic::Policy::BuiltinFunctions::ProhibitBooleanGrep;
2  
3 40 ▶use 5.010001;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 40 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 40 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 40 ▶use Readonly;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 40 ▶use Perl::Critic::Utils qw{ :severities :classification hashify };
Subroutine BEGIN: called CC=1 SCAR=0.0
9 40 ▶use parent 'Perl::Critic::Policy';
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 40 our $VERSION = '1.150';
12  
13 #-----------------------------------------------------------------------------
14  
15 40 Readonly::Scalar my $DESC => q{"grep" used in boolean context};
16 40 Readonly::Scalar my $EXPL => [71,72];
17  
18 40 Readonly::Hash my %POSTFIX_CONDITIONALS => hashify( qw(if unless while until) );
19 40 Readonly::Hash my %BOOLEAN_OPERATORS => hashify( qw(&& || ! not or and));
20  
21 #-----------------------------------------------------------------------------
22  
23 90 ▶sub supported_parameters { return () }
Subroutine supported_parameters: called CC=1 SCAR=0.0
Pod undocumented
24 75 ▶sub default_severity { return $SEVERITY_LOW }
Subroutine default_severity: called CC=1 SCAR=0.0
Pod documented
25 87 ▶sub default_themes { return qw( core pbp performance certrec ) }
Subroutine default_themes: called CC=1 SCAR=0.0
Pod documented
26 31 ▶sub applies_to { return 'PPI::Token::Word' }
Subroutine applies_to: called CC=1 SCAR=0.0
Pod documented
27  
28 #-----------------------------------------------------------------------------
29  
30 sub violates {
31 332 ▶ my ( $self, $elem, undef ) = @_;
Subroutine violates: called CC=4 SCAR=17.1
Pod documented
32  
33 332 ▶ return if $elem->content() ne 'grep';
Branch: 1 branch
BranchTrueFalseTotal
if $elem->content ne "grep" 329 3 332
34 3 ▶ return if not is_function_call($elem);
Branch: 1 branch
BranchTrueFalseTotal
unless is_function_call($elem) 3 0 3
35 0 ▶ return if not _is_in_boolean_context($elem);
Branch: 1 branch
BranchTrueFalseTotal
unless _is_in_boolean_context($elem) 0 0 0
36  
37 0 return $self->violation( $DESC, $EXPL, $elem );
38 }
39  
40 #-----------------------------------------------------------------------------
41  
42 sub _is_in_boolean_context {
43 0 ▶ my ($token) = @_;
Subroutine _is_in_boolean_context: not called CC=2 SCAR=17.9
44  
45 0 ▶ return _does_prev_sibling_cause_boolean($token) || _does_parent_cause_boolean($token);
Condition0%0/3
_does_prev_sibling_cause_boolean($token) || _does_parent_cause_boolean($token)
l!l&&r!l&&!r
000
MC/DC0%0/2
_does_prev_sibling_cause_boolean($token) || _does_parent_cause_boolean($token)
_does_prev_sibling_cause_boolean($token) _does_parent_cause_boolean($token)
Truth table0%0/3
A || B
A= _does_prev_sibling_cause_boolean($token)B= _does_parent_cause_boolean($token)
ABresult
1X1
011
000
46 }
47  
48 sub _does_prev_sibling_cause_boolean {
49 0 ▶ my ($token) = @_;
Subroutine _does_prev_sibling_cause_boolean: not called CC=11 SCAR=48.8
50  
51 0 my $prev = $token->sprevious_sibling;
52 0 ▶ return if !$prev;
Branch: 1 branch
BranchTrueFalseTotal
unless $prev 0 0 0
53 0 ▶ return 1 if $prev->isa('PPI::Token::Word') and $POSTFIX_CONDITIONALS{$prev};
Branch: 1 branch
BranchTrueFalseTotal
if $prev->isa("PPI::Token::Word") and $POSTFIX_CONDITIONALS{$prev} 0 0 0
Condition0%0/3
$prev->isa("PPI::Token::Word") and $POSTFIX_CONDITIONALS{$prev}
!ll&&!rl&&r
000
MC/DC0%0/2
$prev->isa("PPI::Token::Word") and $POSTFIX_CONDITIONALS{$prev}
$prev->isa("PPI::Token::Word") $POSTFIX_CONDITIONALS{$prev}
Truth table0%0/3
A and B
A= $prev->isa("PPI::Token::Word")B= $POSTFIX_CONDITIONALS{$prev}
ABresult
0X0
100
111
54 0 ▶ return if not ($prev->isa('PPI::Token::Operator') and $BOOLEAN_OPERATORS{$prev});
Branch: 1 branch
BranchTrueFalseTotal
unless $prev->isa("PPI::Token::Operator") and $BOOLEAN_OPERATORS{$prev} 0 0 0
Condition0%0/3
$prev->isa("PPI::Token::Operator") and $BOOLEAN_OPERATORS{$prev}
!ll&&!rl&&r
000
MC/DC0%0/2
$prev->isa("PPI::Token::Operator") and $BOOLEAN_OPERATORS{$prev}
$prev->isa("PPI::Token::Operator") $BOOLEAN_OPERATORS{$prev}
Truth table0%0/3
A and B
A= $prev->isa("PPI::Token::Operator")B= $BOOLEAN_OPERATORS{$prev}
ABresult
0X0
100
111
55 0 my $next = $token->snext_sibling;
56 0 ▶ return 1 if not $next; # bizarre: grep with no arguments
Branch: 1 branch
BranchTrueFalseTotal
unless $next 0 0 0
57  
58 # loose heuristic: unparenthesized grep has no following non-boolean operators
59 0 ▶ return 1 if not $next->isa('PPI::Structure::List');
Branch: 1 branch
BranchTrueFalseTotal
unless $next->isa("PPI::Structure::List") 0 0 0
60  
61 0 $next = $next->snext_sibling;
62 0 ▶ return 1 if not $next;
Branch: 1 branch
BranchTrueFalseTotal
unless $next 0 0 0
63 0 ▶ return 1 if $next->isa('PPI::Token::Operator') and $BOOLEAN_OPERATORS{$next};
Branch: 1 branch
BranchTrueFalseTotal
if $next->isa("PPI::Token::Operator") and $BOOLEAN_OPERATORS{$next} 0 0 0
Condition0%0/3
$next->isa("PPI::Token::Operator") and $BOOLEAN_OPERATORS{$next}
!ll&&!rl&&r
000
MC/DC0%0/2
$next->isa("PPI::Token::Operator") and $BOOLEAN_OPERATORS{$next}
$next->isa("PPI::Token::Operator") $BOOLEAN_OPERATORS{$next}
Truth table0%0/3
A and B
A= $next->isa("PPI::Token::Operator")B= $BOOLEAN_OPERATORS{$next}
ABresult
0X0
100
111
64 0 return;
65 }
66  
67 sub _does_parent_cause_boolean {
68 0 ▶ my ($token) = @_;
Subroutine _does_parent_cause_boolean: not called CC=5 SCAR=34.0
69  
70 0 my $prev = $token->sprevious_sibling;
71 0 ▶ return if $prev;
Branch: 1 branch
BranchTrueFalseTotal
if $prev 0 0 0
72 0 my $parent = $token->statement->parent;
73 0 ▶ for (my $node = $parent; $node; $node = $node->parent) { ## no critic (CStyleForLoop)
Branch: 1 branch
BranchTrueFalseTotal
if ($node) 0 0 0
74 0 ▶ next if $node->isa('PPI::Structure::List');
Branch: 1 branch
BranchTrueFalseTotal
if $node->isa("PPI::Structure::List") 0 0 0
75 0 ▶ return 1 if $node->isa('PPI::Structure::Condition');
Branch: 1 branch
BranchTrueFalseTotal
if $node->isa("PPI::Structure::Condition") 0 0 0
76 }
77  
78 0 return;
79 }
80  
81 40 1;
82  
83 __END__
84  
85 #-----------------------------------------------------------------------------
86  
87 =pod
88  
89 =head1 NAME
90  
91 Perl::Critic::Policy::BuiltinFunctions::ProhibitBooleanGrep - Use C<any> from C<List::Util>, C<List::SomeUtils>, or C<List::MoreUtils> instead of C<grep> in boolean context.
92  
93  
94 =head1 AFFILIATION
95  
96 This Policy is part of the core L<Perl::Critic|Perl::Critic>
97 distribution.
98  
99  
100 =head1 DESCRIPTION
101  
102 Using C<grep> in boolean context is a common idiom for checking if any
103 elements in a list match a condition. This works because boolean
104 context is a subset of scalar context, and grep returns the number of
105 matches in scalar context. A non-zero number of matches means a
106 match.
107  
108 But consider the case of a long array where the first element is a
109 match. Boolean C<grep> still checks all of the rest of the elements
110 needlessly. Instead, a better solution is to use the C<any> function
111 from either L<List::Util|List::Util>, L<List::SomeUtils|List::SomeUtils>, or
112 L<List::MoreUtils|List::MoreUtils>. The C<any> function will return as soon
113 as a successful match is found, rather than processing the entire list.
114 This saves time.
115  
116  
117 =head1 CONFIGURATION
118  
119 This Policy is not configurable except for the standard options.
120  
121  
122 =head1 CAVEATS
123  
124 The algorithm for detecting boolean context takes a LOT of shortcuts.
125 There are lots of known false negatives. But, I was conservative in
126 writing this, so I hope there are no false positives.
127  
128  
129 =head1 AUTHOR
130  
131 Chris Dolan <cdolan@cpan.org>
132  
133  
134 =head1 CREDITS
135  
136 Initial development of this policy was supported by a grant from the
137 Perl Foundation.
138  
139  
140 =head1 COPYRIGHT
141  
142 Copyright (c) 2007-2021 Chris Dolan. Many rights reserved.
143  
144 This program is free software; you can redistribute it and/or modify
145 it under the same terms as Perl itself. The full text of this license
146 can be found in the LICENSE file included with this module.
147  
148 =cut
149  
150 # Local Variables:
151 # mode: cperl
152 # cperl-indent-level: 4
153 # fill-column: 78
154 # indent-tabs-mode: nil
155 # c-indentation-style: bsd
156 # End:
157 # ex: set ts=8 sts=4 sw=4 tw=78 ft=perl expandtab shiftround :
« blib/lib/Perl/Critic/Policy/Objects/ProhibitIndirectSyntax.pm ↑ Summary blib/lib/Perl/Critic/Policy/ValuesAndExpressions/ProhibitLeadingZeros.pm »