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
« lib/Perl/Critic/Policy/Objects/ProhibitIndirectSyntax.pm ↑ Summary 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.156';
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 74 ▶sub default_severity { return $SEVERITY_LOW }
Subroutine default_severity: called CC=1 SCAR=0.0
Pod documented
25 86 ▶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 332 ▶sub violates {
Subroutine violates: called CC=4 SCAR=17.1
Pod documented
31 332 my ( $self, $elem, undef ) = @_;
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 0 ▶sub _is_in_boolean_context {
Subroutine _is_in_boolean_context: not called CC=2 SCAR=17.9
43 0 my ($token) = @_;
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 0 ▶sub _does_prev_sibling_cause_boolean {
Subroutine _does_prev_sibling_cause_boolean: not called CC=11 SCAR=48.8
49 0 my ($token) = @_;
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 0 ▶sub _does_parent_cause_boolean {
Subroutine _does_parent_cause_boolean: not called CC=5 SCAR=34.0
68 0 my ($token) = @_;
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__
« lib/Perl/Critic/Policy/Objects/ProhibitIndirectSyntax.pm ↑ Summary lib/Perl/Critic/Policy/ValuesAndExpressions/ProhibitLeadingZeros.pm »