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   68308 use strict;
  134         186  
  134         3191  
3 134     134   405 use warnings;
  134         176  
  134         2968  
4 134     134   435 use List::Util qw/any/;
  134         166  
  134         6996  
5 134     134   843 use Perl::Lint::Constants::Type;
  134         178  
  134         58928  
6 134     134   555 use parent "Perl::Lint::Policy";
  134         172  
  134         557  
7              
8             use constant {
9 134         48857 DESC => 'Useless interpolation of literal string',
10             EXPL => [51],
11 134     134   6414 };
  134         249  
12              
13             # TODO integrate duplicated functions
14              
15             sub evaluate {
16 17     17 0 27 my ($class, $file, $tokens, $src, $args) = @_;
17              
18 17         17 my @allow_double_quote_literals;
19             my $allow_if_string_contains_single_quote;
20 17 100       40 if (my $this_policies_arg = $args->{prohibit_interpolation_of_literals}) {
21 6         10 $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         7 my $allow_double_quote_literals = $this_policies_arg->{allow};
25 6   100     28 for my $allowed_literal (split /\s+/, $allow_double_quote_literals || '') {
26 10         19 $allowed_literal =~ s/\Aqq//;
27 10         16 push @allow_double_quote_literals, substr $allowed_literal, 0, 1;
28             }
29             }
30              
31              
32 17         13 my @violations;
33 17         49 for (my $i = 0, my $token_type, my $token_data; my $token = $tokens->[$i]; $i++) {
34 355         248 $token_type = $token->{type};
35 355         244 $token_data = $token->{data};
36              
37 355 100       679 if ($token_type == STRING) {
    100          
38 23 100       31 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       72 if ($token_data =~ /(\\*)(?:[\$\@]|\\\w)/) {
46 16 100       26 if (length($1) % 2 == 0) { # check escaped or not
47 8         13 next;
48             }
49             }
50              
51             push @violations, {
52             filename => $file,
53             line => $token->{line},
54 13         51 description => DESC,
55             explanation => EXPL,
56             policy => __PACKAGE__,
57             };
58             }
59             elsif ($token_type == REG_DOUBLE_QUOTE) {
60 40         40 $token = $tokens->[++$i];
61 40         23 $token_data = $token->{data};
62              
63 40 100   31   153 if (any {$_ eq $token_data} @allow_double_quote_literals) {
  31         39  
64 10         27 next;
65             }
66              
67 30         50 $token = $tokens->[++$i];
68 30         25 $token_data = $token->{data};
69 30 100       44 if ($allow_if_string_contains_single_quote) {
70 2 50       6 if ($token_data =~ /'/) {
71 2         4 next;
72             }
73             }
74              
75             # XXX NP? :(
76 28 100       101 if ($token_data =~ /(\\*)(?:[\$\@]\S+|\\\w)/) {
77 16 100       30 if (length($1) % 2 == 0) { # check escaped or not
78 8         10 next;
79             }
80             }
81              
82             push @violations, {
83             filename => $file,
84             line => $token->{line},
85 20         77 description => DESC,
86             explanation => EXPL,
87             policy => __PACKAGE__,
88             };
89             }
90             }
91              
92 17         56 return \@violations;
93             }
94              
95             1;
96