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   121950 use strict;
  134         349  
  134         5731  
3 134     134   633 use warnings;
  134         254  
  134         4256  
4 134     134   1162 use Perl::Lint::Constants::Type;
  134         221  
  134         91435  
5 134     134   814 use parent "Perl::Lint::Policy";
  134         235  
  134         926  
6              
7             use constant {
8 134         44891 DESC => 'Use "{" and "}" to delimit multi-line regexps',
9             EXPL => [242],
10 134     134   10010 };
  134         255  
11              
12             sub evaluate {
13 7     7 0 19 my ($class, $file, $tokens, $src, $args) = @_;
14              
15 7         35 my $brackets_regex = qr/\A\{\Z/;
16 7 100       34 if ($args->{require_braces_for_multiline}->{allow_all_brackets}) {
17 1         4 $brackets_regex = qr/\A[\{\(\[\<]\Z/;
18             }
19              
20 7         12 my @violations;
21 7         39 for (my $i = 0, my $token_type; my $token = $tokens->[$i]; $i++) {
22 210         159 $token_type = $token->{type};
23              
24 210 100 100     707 if ($token_type == REG_EXP || $token_type == REG_REPLACE_FROM) {
25 30 100       137 if (my @backslashes = $token->{data} =~ /(\\*)\n/g) {
26 24         31 my $reg_delim_token = $tokens->[$i-1];
27 24 100       210 if ($reg_delim_token->{data} !~ $brackets_regex) {
28 8         13 for my $backslash (@backslashes) {
29 8 50 33     15 if (!$backslash || length($backslash) % 2 == 0) {
30 8         31 push @violations, {
31             filename => $file,
32             line => $token->{line},
33             description => DESC,
34             explanation => EXPL,
35             policy => __PACKAGE__,
36             };
37 8         25 last;
38             }
39             }
40             }
41             }
42             }
43             }
44              
45 7         84 return \@violations;
46             }
47              
48             1;
49