File Coverage

blib/lib/Perl/Lint/Policy/ControlStructures/ProhibitNegativeExpressionsInUnlessAndUntilConditions.pm
Criterion Covered Total %
statement 36 38 94.7
branch 13 18 72.2
condition 3 3 100.0
subroutine 6 6 100.0
pod 0 1 0.0
total 58 66 87.8


line stmt bran cond sub pod time code
1             package Perl::Lint::Policy::ControlStructures::ProhibitNegativeExpressionsInUnlessAndUntilConditions;
2 133     133   72658 use strict;
  133         171  
  133         3178  
3 133     133   414 use warnings;
  133         142  
  133         2433  
4 133     133   785 use Perl::Lint::Constants::Type;
  133         145  
  133         62965  
5 133     133   598 use parent "Perl::Lint::Policy";
  133         169  
  133         577  
6              
7             use constant {
8 133         43202 DESC => 'Found "%s" in condition for an "%s"',
9             EXPL => [99],
10 133     133   6806 };
  133         185  
11              
12             my %invalid_op_types = (
13             &NOT => '!',
14             &ALPHABET_NOT => 'not',
15             &STRING_NOT_EQUAL => 'ne',
16             &NOT_EQUAL => '!=',
17             &LESS => '<',
18             &LESS_EQUAL => '<=',
19             &GREATER => '>',
20             &GREATER_EQUAL => '>=',
21             &COMPARE => '<=>',
22             &STRING_LESS => 'lt',
23             &STRING_GREATER => 'gt',
24             &STRING_LESS_EQUAL => 'le',
25             &STRING_GREATER_EQUAL => 'ge',
26             &STRING_COMPARE => 'cmp',
27             ®_NOT => '!~',
28             &STRING_NOT_EQUAL => 'ne',
29             &NOT_EQUAL => '!=',
30             );
31              
32             sub evaluate {
33 109     109 0 145 my ($class, $file, $tokens, $src, $args) = @_;
34              
35 109         75 my @violations;
36 109         231 for (my $i = 0, my $token_type; my $token = $tokens->[$i]; $i++) {
37 4163         2736 $token_type = $token->{type};
38 4163 100 100     12000 if ($token_type == UNLESS_STATEMENT || $token_type == UNTIL_STATEMENT) {
39 110         100 my $control_structure = $token->{data};
40              
41 110 50       178 $token = $tokens->[++$i] or last;
42 110 100       162 if ($token->{type} != LEFT_PAREN) {
43 54         77 for (; $token = $tokens->[$i]; $i++) {
44 218         155 $token_type = $token->{type};
45 218 100       405 if ($token_type == SEMI_COLON) {
    100          
46 54         54 last;
47             }
48             elsif ($invalid_op_types{$token_type}) {
49             push @violations, {
50             filename => $file,
51             line => $token->{line},
52 54         308 description => sprintf(DESC, $invalid_op_types{$token_type}, $control_structure),
53             explanation => EXPL,
54             policy => __PACKAGE__,
55             };
56             }
57             }
58              
59 54         80 next;
60             }
61              
62 56         41 my $lpnum = 1;
63 56         85 for ($i++; $token = $tokens->[$i]; $i++) {
64 108         85 $token_type = $token->{type};
65              
66 108 50       305 if ($token_type == LEFT_PAREN) {
    50          
    100          
67 0         0 $lpnum++;
68             }
69             elsif ($token_type == RIGHT_PAREN) {
70 0 0       0 last if --$lpnum <= 0;
71             }
72             elsif ($invalid_op_types{$token_type}) {
73             push @violations, {
74             filename => $file,
75             line => $token->{line},
76 56         265 description => sprintf(DESC, $invalid_op_types{$token_type}, $control_structure),
77             explanation => EXPL,
78             policy => __PACKAGE__,
79             };
80 56         130 last;
81             }
82             }
83             }
84             }
85              
86 109         330 return \@violations;
87             }
88              
89             1;
90