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   69627 use strict;
  134         184  
  134         3050  
3 134     134   458 use warnings;
  134         157  
  134         2435  
4 134     134   795 use Perl::Lint::Constants::Type;
  134         154  
  134         60973  
5 134     134   557 use parent "Perl::Lint::Policy";
  134         148  
  134         556  
6              
7             use constant {
8 134         37169 DESC => 'Use [%s] instead of %s',
9             EXPL => [265],
10 134     134   7187 };
  134         204  
11              
12             sub evaluate {
13 8     8 0 12 my ($class, $file, $tokens, $src, $args) = @_;
14              
15 8         7 my @violations;
16 8         26 for (my $i = 0, my $token_type, my $token_data; my $token = $tokens->[$i]; $i++) {
17 159         98 $token_type = $token->{type};
18 159         122 $token_data = $token->{data};
19              
20 159 100       286 if ($token_type == REG_EXP) {
21 18 100       93 if (my @groups = $token_data =~ /\( \?\: \s* (.+?) \s* \)/gx) {
22 15         17 TRAVERSE_REGEX: for my $group (@groups) {
23 15 100       27 if ($group =~ /\A \w \Z/x) {
24 1         3 last;
25             }
26              
27 14         8 my @singles;
28 14         63 for my $part (split /\s* \| \s*/x, $group) {
29 68 100       115 if ($part !~ /\A \w+ \Z/x) {
30 5         18 last TRAVERSE_REGEX;
31             }
32              
33 63 100       79 if (length $part == 1) { # if single char
34 33         32 push @singles, $part;
35             }
36             }
37              
38 9 100       19 if (scalar @singles > 1) {
39             push @violations, {
40             filename => $file,
41             line => $token->{line},
42 8         63 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         26 return \@violations;
57             }
58              
59             1;
60