Perl/Lint/Policy/RegularExpressions/ProhibitEscapedMetacharacters.pm

filter: Statementstmt 72.0% 31 / 43 Branchbran 45.4% 10 / 22 Conditioncond 50.0% 3 / 6 MC/DCmcdc 25.0% 1 / 4 Subroutinesub 100% 6 / 6 Podpod 0.0% 0 / 1 totaltotal 62.2% 51 / 82 CC 15 SCAR 34.5
CC
15
Cov
62%
evaluate
41.6
BEGIN
1.0
BEGIN
1.0
CRAP 31.5
« Perl/Lint/Policy/InputOutput/ProhibitExplicitStdin.pm ↑ Summary Perl/Lint/Policy/ValuesAndExpressions/ProhibitSpecialHeredocTerminator.pm »
1 package Perl::Lint::Policy::RegularExpressions::ProhibitEscapedMetacharacters;
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 Perl::Lint::Constants::Type;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use parent "Perl::Lint::Policy";
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 3 ▶use constant {
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 DESC => 'Use character classes for literal metachars instead of escapes',
9 EXPL => [247],
10 3 };
11  
12 2 ▶sub evaluate {
Subroutine evaluate: called CC=15 SCAR=37.3
Pod undocumented
13 2 my ($class, $file, $tokens, $src, $args) = @_;
14  
15 2 my @violations;
16 2 ▶ for (my $i = 0, my $token_type, my $is_reg_quote; my $token = $tokens->[$i]; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $token = $tokens->[$i]) 51 2 53
17 51 $token_type = $token->{type};
18  
19 51 ▶ if ($token_type == REG_EXP || $token_type == REG_REPLACE_FROM) {
Branch: 2 branches
BranchTrueFalseTotal
if ($token_type == 187 or $token_type == 156) { } 2 49 51
elsif ($token_type == 145 or $token_type == 146) { } 0 49 49
Condition66%2/3
$token_type == 187 or $token_type == 156
l!l&&r!l&&!r
2049
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
ABresult
1X1
011
000
20 2 ▶ if ($is_reg_quote) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_reg_quote) 0 2 2
21 0 $is_reg_quote = 0;
22 0 next;
23 }
24  
25 2 my $regexp = $token->{data};
26  
27 2 my $maybe_regopt;
28 2 ▶ if ($token_type == REG_EXP) {
Branch: 1 branch
BranchTrueFalseTotal
if ($token_type == 187) { } 2 0 2
29 2 $maybe_regopt = $tokens->[$i+2];
30 }
31 else {
32 0 $maybe_regopt = $tokens->[$i+4];
33 0 ▶ if ($maybe_regopt->{type} == REG_DELIM) { # if it use brackets as delimiter
Branch: 1 branch
BranchTrueFalseTotal
if ($maybe_regopt->{"type"} == 151) 0 0 0
34 0 $maybe_regopt = $tokens->[$i+5];
35 }
36 }
37  
38 2 my $regex_to_detect = qr/[{}().*+?|# ]/;
39  
40 2 ▶ if ($maybe_regopt) {
Branch: 1 branch
BranchTrueFalseTotal
if ($maybe_regopt) 2 0 2
41 2 ▶ if ($maybe_regopt->{type} == REG_OPT) {
Branch: 1 branch
BranchTrueFalseTotal
if ($maybe_regopt->{"type"} == 144) 0 2 2
42 0 ▶ if ($maybe_regopt->{data} =~ /x/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($maybe_regopt->{"data"} =~ /x/) 0 0 0
43 0 $regex_to_detect = qr/[{}().*+?| ]/;
44 }
45 }
46 }
47  
48 2 ▶ if (my @backslashes = $token->{data} =~ /(\\+)$regex_to_detect/g) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@backslashes) = $token->{"data"} =~ /(\\+)$regex_to_detect/g) 0 2 2
49 0 for my $backslash (@backslashes) {
50 0 ▶ if (length($backslash) % 2 != 0) { # not escaped
Branch: 1 branch
BranchTrueFalseTotal
if (length($backslash) % 2 != 0) 0 0 0
51 push @violations, {
52 filename => $file,
53 line => $token->{line},
54 0 description => DESC,
55 explanation => EXPL,
56 policy => __PACKAGE__,
57 };
58 0 last;
59 }
60 }
61 }
62 }
63 ▶ elsif ($token_type == REG_QUOTE || $token_type == REG_DOUBLE_QUOTE) {
Condition33%1/3
$token_type == 145 or $token_type == 146
l!l&&r!l&&!r
0049
MC/DC0%0/2
$token_type == 145 or $token_type == 146
$token_type == 145 $token_type == 146
Truth table33%1/3
A or B
A= $token_type == 145B= $token_type == 146
ABresult
1X1
011
000
64 0 $is_reg_quote = 1;
65 }
66 }
67  
68 2 return \@violations;
69 }
70  
71 3 1;
72  
« Perl/Lint/Policy/InputOutput/ProhibitExplicitStdin.pm ↑ Summary Perl/Lint/Policy/ValuesAndExpressions/ProhibitSpecialHeredocTerminator.pm »