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

filter: Statementstmt 77.7% 7 / 9 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 3 / 3 Podpod n/a totaltotal 83.3% 10 / 12 SCAR 0.1
CC
1
Cov
78%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
« blib/lib/Perl/Lint/Constants/Type.pm ↑ Summary blib/lib/Perl/Lint/Policy/BuiltinFunctions/ProhibitComplexMappings.pm »
1 package Perl::Lint::Policy::BuiltinFunctions::ProhibitBooleanGrep;
2 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use Perl::Lint::Constants::Type;
Subroutine BEGIN: called CC=1 SCAR=2.6
5 use parent "Perl::Lint::Policy";
6  
7 use constant {
8 DESC => '"grep" used in boolean context',
9 EXPL => [71, 72],
10 };
11  
12 sub evaluate {
13 my ($class, $file, $tokens, $src, $args) = @_;
14  
15 my @violations;
16 my $is_grep_called = 0;
17 my $is_in_boolean_context = 0;
18 my $is_in_numeric_comparison_context = 0;
19 for (my $i = 0; my $token = $tokens->[$i]; $i++) {
20 my $token_type = $token->{type};
21  
22 if (
23 $token_type == IF_STATEMENT ||
24 $token_type == UNLESS_STATEMENT ||
25 $token_type == WHILE_STATEMENT ||
26 $token_type == UNTIL_STATEMENT
27 ) {
28 $token = $tokens->[++$i];
29 $token_type = $token->{type};
30 if ($token_type == LEFT_PAREN) {
31 my $is_grep_called = 0;
32 my $is_in_numeric_comparison_context = 0;
33  
34 my $left_paren_num = 1;
35 for ($i++; $token = $tokens->[$i]; $i++) {
36 $token_type = $token->{type};
37 if ($token_type == EQUAL_EQUAL) {
38 $is_in_numeric_comparison_context = 1;
39 }
40 elsif ($token_type == LEFT_PAREN) {
41 $left_paren_num++;
42 }
43 elsif ($token_type == RIGHT_PAREN) {
44 if (--$left_paren_num <= 0) {
45 if ($is_grep_called && !$is_in_numeric_comparison_context) {
46 push @violations, {
47 filename => $file,
48 line => $token->{line},
49 description => DESC,
50 explanation => EXPL,
51 policy => __PACKAGE__,
52 };
53 }
54 last;
55 }
56 }
57 elsif ($token_type == BUILTIN_FUNC && $token->{data} eq 'grep') {
58 $is_grep_called = 1;
59 }
60 }
61 }
62 elsif ($token_type == BUILTIN_FUNC && $token->{data} eq 'grep') {
63 push @violations, {
64 filename => $file,
65 line => $token->{line},
66 description => DESC,
67 explanation => EXPL,
68 policy => __PACKAGE__,
69 };
70 }
71  
72 next;
73 }
74  
75 if ($token_type == BUILTIN_FUNC && $token->{data} eq 'grep') {
76 $is_grep_called = 1;
77 }
78  
79 if (
80 $token_type == OR ||
81 $token_type == AND ||
82 $token_type == ALPHABET_OR ||
83 $token_type == ALPHABET_AND
84 ) {
85 $is_in_boolean_context = 1;
86 }
87  
88 if ($token_type == EQUAL_EQUAL) {
89 $is_in_numeric_comparison_context = 1;
90 }
91  
92 if ($token_type == SEMI_COLON) {
93 if ($is_grep_called && $is_in_boolean_context && !$is_in_numeric_comparison_context) {
94 push @violations, {
95 filename => $file,
96 line => $token->{line},
97 description => DESC,
98 explanation => EXPL,
99 policy => __PACKAGE__,
100 };
101 }
102 $is_grep_called = 0;
103 $is_in_boolean_context = 0;
104 $is_in_numeric_comparison_context = 0;
105 next;
106 }
107 }
108  
109 return \@violations;
110 }
111  
112 1;
113  
« blib/lib/Perl/Lint/Constants/Type.pm ↑ Summary blib/lib/Perl/Lint/Policy/BuiltinFunctions/ProhibitComplexMappings.pm »