File Coverage

blib/lib/Perl/Lint/Policy/RegularExpressions/RequireBracesForMultiline.pm
Criterion Covered Total %
statement 31 31 100.0
branch 9 10 90.0
condition 4 6 66.6
subroutine 6 6 100.0
pod 0 1 0.0
total 50 54 92.5


line stmt bran cond sub pod time code
1             package Perl::Lint::Policy::RegularExpressions::RequireBracesForMultiline;
2 134     134   80183 use strict;
  134         185  
  134         3390  
3 134     134   431 use warnings;
  134         164  
  134         2725  
4 134     134   778 use Perl::Lint::Constants::Type;
  134         176  
  134         61349  
5 134     134   597 use parent "Perl::Lint::Policy";
  134         173  
  134         627  
6              
7             use constant {
8 134         33697 DESC => 'Use "{" and "}" to delimit multi-line regexps',
9             EXPL => [242],
10 134     134   6825 };
  134         157  
11              
12             sub evaluate {
13 7     7 0 14 my ($class, $file, $tokens, $src, $args) = @_;
14              
15 7         25 my $brackets_regex = qr/\A\{\Z/;
16 7 100       25 if ($args->{require_braces_for_multiline}->{allow_all_brackets}) {
17 1         4 $brackets_regex = qr/\A[\{\(\[\<]\Z/;
18             }
19              
20 7         7 my @violations;
21 7         28 for (my $i = 0, my $token_type; my $token = $tokens->[$i]; $i++) {
22 210         137 $token_type = $token->{type};
23              
24 210 100 100     591 if ($token_type == REG_EXP || $token_type == REG_REPLACE_FROM) {
25 30 100       110 if (my @backslashes = $token->{data} =~ /(\\*)\n/g) {
26 24         33 my $reg_delim_token = $tokens->[$i-1];
27 24 100       87 if ($reg_delim_token->{data} !~ $brackets_regex) {
28 8         10 for my $backslash (@backslashes) {
29 8 50 33     16 if (!$backslash || length($backslash) % 2 == 0) {
30             push @violations, {
31             filename => $file,
32             line => $token->{line},
33 8         25 description => DESC,
34             explanation => EXPL,
35             policy => __PACKAGE__,
36             };
37 8         19 last;
38             }
39             }
40             }
41             }
42             }
43             }
44              
45 7         29 return \@violations;
46             }
47              
48             1;
49