File Coverage

blib/lib/Perl/Lint/Policy/ValuesAndExpressions/ProhibitInterpolationOfLiterals.pm
Criterion Covered Total %
statement 53 53 100.0
branch 22 24 91.6
condition 2 2 100.0
subroutine 8 8 100.0
pod 0 1 0.0
total 85 88 96.5


line stmt bran cond sub pod time code
1             package Perl::Lint::Policy::ValuesAndExpressions::ProhibitInterpolationOfLiterals;
2 134     134   97579 use strict;
  134         307  
  134         5022  
3 134     134   686 use warnings;
  134         250  
  134         4026  
4 134     134   640 use List::Util qw/any/;
  134         224  
  134         9641  
5 134     134   1074 use Perl::Lint::Constants::Type;
  134         247  
  134         85661  
6 134     134   829 use parent "Perl::Lint::Policy";
  134         244  
  134         886  
7              
8             use constant {
9 134         69614 DESC => 'Useless interpolation of literal string',
10             EXPL => [51],
11 134     134   9126 };
  134         280  
12              
13             # TODO integrate duplicated functions
14              
15             sub evaluate {
16 17     17 0 37 my ($class, $file, $tokens, $src, $args) = @_;
17              
18 17         21 my @allow_double_quote_literals;
19             my $allow_if_string_contains_single_quote;
20 17 100       62 if (my $this_policies_arg = $args->{prohibit_interpolation_of_literals}) {
21 6         11 $allow_if_string_contains_single_quote = $this_policies_arg->{allow_if_string_contains_single_quote};
22              
23             # e.g. {allow => 'qq( qq{ qq[ qq/'}}
24 6         11 my $allow_double_quote_literals = $this_policies_arg->{allow};
25 6   100     31 for my $allowed_literal (split /\s+/, $allow_double_quote_literals || '') {
26 10         26 $allowed_literal =~ s/\Aqq//;
27 10         23 push @allow_double_quote_literals, substr $allowed_literal, 0, 1;
28             }
29             }
30              
31              
32 17         22 my @violations;
33 17         75 for (my $i = 0, my $token_type, my $token_data; my $token = $tokens->[$i]; $i++) {
34 355         306 $token_type = $token->{type};
35 355         328 $token_data = $token->{data};
36              
37 355 100       939 if ($token_type == STRING) {
    100          
38 23 100       32 if ($allow_if_string_contains_single_quote) {
39 2 50       6 if ($token_data =~ /'/) {
40 2         5 next;
41             }
42             }
43              
44             # XXX NP? :(
45 21 100       92 if ($token_data =~ /(\\*)(?:[\$\@]|\\\w)/) {
46 16 100       28 if (length($1) % 2 == 0) { # check escaped or not
47 8         16 next;
48             }
49             }
50              
51 13         75 push @violations, {
52             filename => $file,
53             line => $token->{line},
54             description => DESC,
55             explanation => EXPL,
56             policy => __PACKAGE__,
57             };
58             }
59             elsif ($token_type == REG_DOUBLE_QUOTE) {
60 40         42 $token = $tokens->[++$i];
61 40         43 $token_data = $token->{data};
62              
63 40 100   31   202 if (any {$_ eq $token_data} @allow_double_quote_literals) {
  31         50  
64 10         41 next;
65             }
66              
67 30         57 $token = $tokens->[++$i];
68 30         33 $token_data = $token->{data};
69 30 100       50 if ($allow_if_string_contains_single_quote) {
70 2 50       12 if ($token_data =~ /'/) {
71 2         6 next;
72             }
73             }
74              
75             # XXX NP? :(
76 28 100       135 if ($token_data =~ /(\\*)(?:[\$\@]\S+|\\\w)/) {
77 16 100       32 if (length($1) % 2 == 0) { # check escaped or not
78 8         14 next;
79             }
80             }
81              
82 20         90 push @violations, {
83             filename => $file,
84             line => $token->{line},
85             description => DESC,
86             explanation => EXPL,
87             policy => __PACKAGE__,
88             };
89             }
90             }
91              
92 17         75 return \@violations;
93             }
94              
95             1;
96