« Perl/Lint/Policy/BuiltinFunctions/ProhibitReverseSortBlock.pm
↑ Summary
Perl/Lint/Policy/TestingAndDebugging/RequireUseStrict.pm »
| 1 | package Perl::Lint::Policy::RegularExpressions::ProhibitEnumeratedClasses; | ||||||||||||||||||||||||||||
| 2 | 3 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | 3 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 3 | ▶ | use List::Util (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 3 | ▶ | use Perl::Lint::Constants::Type; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 3 | ▶ | use parent "Perl::Lint::Policy"; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 3 | ▶ | use constant { | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 3 | DESC => 'Use named character classes (%s VS. %s)', | |||||||||||||||||||||||||||
| 10 | EXPL => [248], | ||||||||||||||||||||||||||||
| 11 | 3 | }; | |||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | 3 | 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 | 3 | my %ordinals = ( | |||||||||||||||||||||||||||
| 25 | ord "\n" => '\\n', | ||||||||||||||||||||||||||||
| 26 | ord "\f" => '\\f', | ||||||||||||||||||||||||||||
| 27 | ord "\r" => '\\r', | ||||||||||||||||||||||||||||
| 28 | ord q< > => q< >, | ||||||||||||||||||||||||||||
| 29 | ); | ||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | 2 | ▶ | sub evaluate { | ||||||||||||||||||||||||||
|
Subroutine
evaluate: called CC=20 SCAR=54.2
Pod
undocumented
| |||||||||||||||||||||||||||||
| 32 | 2 | my ($class, $file, $tokens, $src, $args) = @_; | |||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | 2 | my @violations; | |||||||||||||||||||||||||||
| 35 | 2 | ▶ | for (my $i = 0, my $token_type; my $token = $tokens->[$i]; $i++) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 36 | 51 | $token_type = $token->{type}; | |||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | 51 | ▶ | if ($token_type == REG_EXP || $token_type == REG_REPLACE_FROM) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$token_type == 187 or $token_type == 156
MC/DC50%1/2
$token_type == 187 or $token_type == 156
$token_type == 187
$token_type == 156
Truth table66%2/3
A or B
A= $token_type == 187B= $token_type == 156
| |||||||||||||||||||||||||||||
| 39 | 2 | my $regex = $token->{data}; | |||||||||||||||||||||||||||
| 40 | 2 | ▶ | if (my @captures = $regex =~ / (\\)* \[ (\^?) (.+) ] /gx) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 41 | 0 | ▶ | while (@captures) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 42 | 0 | my $backslashes = shift @captures; | |||||||||||||||||||||||||||
| 43 | 0 | my $is_negate = shift @captures; | |||||||||||||||||||||||||||
| 44 | 0 | my $in_brackets = shift @captures; | |||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | 0 | ▶ | if ($backslashes && length($backslashes) % 2 != 0) { # escaped | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$backslashes and length($backslashes) % 2 != 0
MC/DC0%0/2
$backslashes and length($backslashes) % 2 != 0
$backslashes
length($backslashes) % 2 != 0
Truth table0%0/3
A and B
A= $backslashesB= length($backslashes) % 2 != 0
| |||||||||||||||||||||||||||||
| 47 | 0 | next; | |||||||||||||||||||||||||||
| 48 | } | ||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | 0 | my @parts = $in_brackets =~ /([^\\]-[^\\] | [_ ] | \\[trnws])/gx; | |||||||||||||||||||||||||||
| 51 | 0 | my @octs = $in_brackets =~ /\\0([0-7]+)/gx; | |||||||||||||||||||||||||||
| 52 | 0 | for my $oct (@octs) { | |||||||||||||||||||||||||||
| 53 | 0 | ▶ | if (my $chr = $ordinals{oct $oct}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 54 | 0 | push @parts, $chr; | |||||||||||||||||||||||||||
| 55 | } | ||||||||||||||||||||||||||||
| 56 | } | ||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | 0 | my @hexs = $in_brackets =~ /\\x\{?([0-9a-f]+)}?/gx; | |||||||||||||||||||||||||||
| 59 | 0 | for my $hex (@hexs) { | |||||||||||||||||||||||||||
| 60 | 0 | ▶ | if (my $chr = $ordinals{hex $hex}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 61 | 0 | push @parts, $chr; | |||||||||||||||||||||||||||
| 62 | } | ||||||||||||||||||||||||||||
| 63 | } | ||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | 0 | my %parts = map {$_ => 1} @parts; | |||||||||||||||||||||||||||
| 66 | 0 | ▶ | for (my $j = 0; $j < @patterns; $j += 2) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 67 | 0 | ▶ | if (List::Util::all { exists $parts{$_} } @{$patterns[$j]}) { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 68 | 0 | my $index = 0; | |||||||||||||||||||||||||||
| 69 | 0 | ▶ | if ($is_negate) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 70 | 0 | $index = 1; | |||||||||||||||||||||||||||
| 71 | } | ||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | 0 | ▶ | if ($is_negate && ! defined $patterns[$j+1]->[0]) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$is_negate and not defined $patterns[$j + 1][0]
MC/DC0%0/2
$is_negate and not defined $patterns[$j + 1][0]
$is_negate
not defined $patterns[$j + 1][0]
Truth table0%0/3
A and not(B)
A= $is_negateB= not 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 | 0 | ▶ | next if 1 != scalar keys %parts; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 77 | } | ||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | 0 | ▶ | my $orig = join q{}, '[', ($is_negate ? q{^} : ()), @{$patterns[$j]}, ']'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 80 | 0 | ▶ | if (defined (my $improvement = $patterns[$j+1]->[$index])) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 81 | push @violations, { | ||||||||||||||||||||||||||||
| 82 | filename => $file, | ||||||||||||||||||||||||||||
| 83 | line => $token->{line}, | ||||||||||||||||||||||||||||
| 84 | 0 | description => sprintf(DESC, $orig, $improvement), | |||||||||||||||||||||||||||
| 85 | explanation => EXPL, | ||||||||||||||||||||||||||||
| 86 | policy => __PACKAGE__, | ||||||||||||||||||||||||||||
| 87 | }; | ||||||||||||||||||||||||||||
| 88 | 0 | last; | |||||||||||||||||||||||||||
| 89 | } | ||||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||
| 91 | } | ||||||||||||||||||||||||||||
| 92 | } | ||||||||||||||||||||||||||||
| 93 | } | ||||||||||||||||||||||||||||
| 94 | } | ||||||||||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | 2 | return \@violations; | |||||||||||||||||||||||||||
| 98 | } | ||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | 3 | 1; | |||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||