blib/lib/Perl/Lint/Policy/RegularExpressions/ProhibitEnumeratedClasses.pm

filter: Statementstmt 83.3% 10 / 12 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 4 / 4 Podpod n/a totaltotal 87.5% 14 / 16 SCAR 0.0
CC
1
Cov
83%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
« blib/lib/Perl/Lint/Policy/RegularExpressions/ProhibitCaptureWithoutTest.pm ↑ Summary blib/lib/Perl/Lint/Policy/Subroutines/RequireArgUnpacking.pm »
1 package Perl::Lint::Policy::RegularExpressions::ProhibitEnumeratedClasses;
2 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use List::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use Perl::Lint::Constants::Type;
Subroutine BEGIN: called CC=1 SCAR=2.6
6 use parent "Perl::Lint::Policy";
7  
8 use constant {
9 DESC => 'Use named character classes (%s VS. %s)',
10 EXPL => [248],
11 };
12  
13 my @patterns = (
14 [q{ },'\\t','\\r','\\n'] => ['\\s', '\\S'],
15 ['A-Z','a-z','0-9','_'] => ['\\w', '\\W'],
16 ['A-Z','a-z'] => ['[[:alpha:]]','[[:^alpha:]]'],
17 ['A-Z'] => ['[[:upper:]]','[[:^upper:]]'],
18 ['a-z'] => ['[[:lower:]]','[[:^lower:]]'],
19 ['0-9'] => ['\\d','\\D'],
20 ['\w'] => [undef, '\\W'],
21 ['\s'] => [undef, '\\S'],
22 );
23  
24 my %ordinals = (
25 ord "\n" => '\\n',
26 ord "\f" => '\\f',
27 ord "\r" => '\\r',
28 ord q< > => q< >,
29 );
30  
31 sub evaluate {
32 my ($class, $file, $tokens, $src, $args) = @_;
33  
34 my @violations;
35 for (my $i = 0, my $token_type; my $token = $tokens->[$i]; $i++) {
36 $token_type = $token->{type};
37  
38 if ($token_type == REG_EXP || $token_type == REG_REPLACE_FROM) {
39 my $regex = $token->{data};
40 if (my @captures = $regex =~ / (\\)* \[ (\^?) (.+) ] /gx) {
41 while (@captures) {
42 my $backslashes = shift @captures;
43 my $is_negate = shift @captures;
44 my $in_brackets = shift @captures;
45  
46 if ($backslashes && length($backslashes) % 2 != 0) { # escaped
47 next;
48 }
49  
50 my @parts = $in_brackets =~ /([^\\]-[^\\] | [_ ] | \\[trnws])/gx;
51 my @octs = $in_brackets =~ /\\0([0-7]+)/gx;
52 for my $oct (@octs) {
53 if (my $chr = $ordinals{oct $oct}) {
54 push @parts, $chr;
55 }
56 }
57  
58 my @hexs = $in_brackets =~ /\\x\{?([0-9a-f]+)}?/gx;
59 for my $hex (@hexs) {
60 if (my $chr = $ordinals{hex $hex}) {
61 push @parts, $chr;
62 }
63 }
64  
65 my %parts = map {$_ => 1} @parts;
66 for (my $j = 0; $j < @patterns; $j += 2) {
67 if (List::Util::all { exists $parts{$_} } @{$patterns[$j]}) {
68 my $index = 0;
69 if ($is_negate) {
70 $index = 1;
71 }
72  
73 if ($is_negate && ! defined $patterns[$j+1]->[0]) {
74 # the [^\w] => \W rule only applies if \w is the only token.
75 # that is it does not apply to [^\w\s]
76 next if 1 != scalar keys %parts;
77 }
78  
79 my $orig = join q{}, '[', ($is_negate ? q{^} : ()), @{$patterns[$j]}, ']';
80 if (defined (my $improvement = $patterns[$j+1]->[$index])) {
81 push @violations, {
82 filename => $file,
83 line => $token->{line},
84 description => sprintf(DESC, $orig, $improvement),
85 explanation => EXPL,
86 policy => __PACKAGE__,
87 };
88 last;
89 }
90 }
91 }
92 }
93 }
94 }
95 }
96  
97 return \@violations;
98 }
99  
100 1;
101  
« blib/lib/Perl/Lint/Policy/RegularExpressions/ProhibitCaptureWithoutTest.pm ↑ Summary blib/lib/Perl/Lint/Policy/Subroutines/RequireArgUnpacking.pm »