File Coverage

blib/lib/Perl/Lint/Policy/RegularExpressions/ProhibitSingleCharAlternation.pm
Criterion Covered Total %
statement 34 34 100.0
branch 12 12 100.0
condition n/a
subroutine 6 6 100.0
pod 0 1 0.0
total 52 53 98.1


line stmt bran cond sub pod time code
1             package Perl::Lint::Policy::RegularExpressions::ProhibitSingleCharAlternation;
2 134     134   69764 use strict;
  134         182  
  134         3281  
3 134     134   465 use warnings;
  134         153  
  134         2548  
4 134     134   761 use Perl::Lint::Constants::Type;
  134         147  
  134         61556  
5 134     134   549 use parent "Perl::Lint::Policy";
  134         158  
  134         558  
6              
7             use constant {
8 134         37904 DESC => 'Use [%s] instead of %s',
9             EXPL => [265],
10 134     134   7278 };
  134         220  
11              
12             sub evaluate {
13 8     8 0 13 my ($class, $file, $tokens, $src, $args) = @_;
14              
15 8         8 my @violations;
16 8         24 for (my $i = 0, my $token_type, my $token_data; my $token = $tokens->[$i]; $i++) {
17 159         96 $token_type = $token->{type};
18 159         111 $token_data = $token->{data};
19              
20 159 100       284 if ($token_type == REG_EXP) {
21 18 100       90 if (my @groups = $token_data =~ /\( \?\: \s* (.+?) \s* \)/gx) {
22 15         14 TRAVERSE_REGEX: for my $group (@groups) {
23 15 100       28 if ($group =~ /\A \w \Z/x) {
24 1         4 last;
25             }
26              
27 14         8 my @singles;
28 14         61 for my $part (split /\s* \| \s*/x, $group) {
29 68 100       112 if ($part !~ /\A \w+ \Z/x) {
30 5         16 last TRAVERSE_REGEX;
31             }
32              
33 63 100       75 if (length $part == 1) { # if single char
34 33         29 push @singles, $part;
35             }
36             }
37              
38 9 100       21 if (scalar @singles > 1) {
39             push @violations, {
40             filename => $file,
41             line => $token->{line},
42 8         57 description => sprintf(
43             DESC,
44             join('', @singles),
45             join('|', @singles),
46             ),
47             explanation => EXPL,
48             policy => __PACKAGE__,
49             };
50             }
51             }
52             }
53             }
54             }
55              
56 8         24 return \@violations;
57             }
58              
59             1;
60