File Coverage

blib/lib/Perl/Lint/Policy/BuiltinFunctions/ProhibitStringyEval.pm
Criterion Covered Total %
statement 47 47 100.0
branch 28 30 93.3
condition 5 5 100.0
subroutine 6 6 100.0
pod 0 1 0.0
total 86 89 96.6


line stmt bran cond sub pod time code
1             package Perl::Lint::Policy::BuiltinFunctions::ProhibitStringyEval;
2 133     133   98429 use strict;
  133         275  
  133         5035  
3 133     133   684 use warnings;
  133         216  
  133         3522  
4 133     133   1266 use Perl::Lint::Constants::Type;
  133         223  
  133         86017  
5 133     133   898 use parent "Perl::Lint::Policy";
  133         253  
  133         879  
6              
7             use constant {
8 133         77343 DESC => 'Expression form of "eval"',
9             EXPL => [161],
10 133     133   9285 };
  133         255  
11              
12             sub evaluate {
13 11     11 0 33 my ($class, $file, $tokens, $src, $args) = @_;
14              
15 11   100     67 my $allow_includes = $args->{prohibit_stringy_eval}->{allow_includes} || 0;
16              
17 11         15 my @violations;
18 11         89 for (my $i = 0; my $token = $tokens->[$i]; $i++) {
19 347         303 my $token_type = $token->{type};
20 347         298 my $token_data = $token->{data};
21              
22 347 100 100     951 if ($token_type == BUILTIN_FUNC && $token_data eq 'eval') {
23 101         93 $token = $tokens->[++$i];
24 101 100       176 if ($token->{type} == LEFT_PAREN) {
25 7         11 $token = $tokens->[++$i];
26 7 100       17 if ($token->{type} == RIGHT_PAREN) {
27 1         3 next;
28             }
29             }
30 100         84 $token_type = $token->{type};
31              
32 100 100       157 if ($token_type != LEFT_BRACE) {
33 98 100       142 if ($allow_includes) {
34 48 100       122 if ($token_type == STRING) {
    100          
    100          
    50          
35 5 100       18 if ($token->{data} =~ /\A(?:use|require)[^;]*(?:;|;\s*1;)?\Z/) {
36 4         8 next;
37             }
38             }
39             elsif ($token_type == RAW_STRING) {
40 20 100       81 if ($token->{data} =~ /\A(?:use|require)\s+([^;\s]+)[^;]*(?:;|;\s*1;)?\Z/) {
41 17 100       40 if ($1 !~ /\A\$/) {
42 16         31 next;
43             }
44             }
45             }
46             elsif ($token_type == REG_DOUBLE_QUOTE) {
47 4         4 $i += 2; # skip reg delimiter
48 4         6 $token = $tokens->[$i];
49 4 50       14 if ($token->{data} =~ /\A(?:use|require)[^;]*(?:;|;\s*1;)?\Z/) {
50 4         9 next;
51             }
52             }
53             elsif ($token_type == REG_QUOTE) {
54 19         13 $i += 2; # skip reg delimiter
55 19         22 $token = $tokens->[$i];
56 19 100       80 if ($token->{data} =~ /\A(?:use|require)\s+([^;\s]+)[^;]*(?:;|;\s*1;)?\Z/) {
57 17 100       35 if ($1 !~ /\A\$/) {
58 16         34 next;
59             }
60             }
61             }
62             }
63              
64 58         275 push @violations, {
65             filename => $file,
66             line => $token->{line},
67             description => DESC,
68             explanation => EXPL,
69             policy => __PACKAGE__,
70             };
71             }
72             }
73             }
74              
75 11         57 return \@violations;
76             }
77              
78             1;
79