Perl/Lint/Policy/RegularExpressions/ProhibitComplexRegexes.pm

filter: Statementstmt 34.0% 16 / 47 Branchbran 0.0% 0 / 22 Conditioncond 0.0% 0 / 11 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 83.3% 5 / 6 Podpod 0.0% 0 / 1 totaltotal 22.5% 21 / 93 CC 16 SCAR 53.4
CC
16
Cov
20%
evaluate
272.0
BEGIN
1.0
BEGIN
1.0
CRAP 207.5
« Perl/Lint/Policy/TestingAndDebugging/ProhibitNoStrict.pm ↑ Summary Perl/Lint/Policy/InputOutput/ProhibitReadlineInForLoop.pm »
1 package Perl::Lint::Policy::RegularExpressions::ProhibitComplexRegexes;
2 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use Perl::Lint::Constants::Type;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use parent "Perl::Lint::Policy";
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 2 ▶use constant {
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 DESC => 'Split long regexps into smaller qr// chunks',
9 EXPL => [261],
10 2 };
11  
12 0 ▶sub evaluate {
Subroutine evaluate: not called CC=16 SCAR=56.1
Pod undocumented
13 0 my ($class, $file, $tokens, $src, $args) = @_;
14  
15 0 ▶ my $max_characters = $args->{prohibit_complex_regexes}->{max_characters} || 60;
Condition0%0/2
$args->{'prohibit_complex_regexes'}{'max_characters'} || 60
l!l
00
MC/DC0%0/1
$args->{'prohibit_complex_regexes'}{'max_characters'} || 60
$args->{'prohibit_complex_regexes'}{'max_characters'}
Truth table0%0/2
A
A= $args->{'prohibit_complex_regexes'}{'max_characters'}
Aresult
11
00
16  
17 0 my @violations;
18 0 my $is_reg_quote = 0;
19 0 my $is_delimiter_single_quote = 0;
20 0 ▶ for (my $i = 0, my $token_type, my $token_data; my $token = $tokens->[$i]; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $token = $tokens->[$i]) 0 0 0
21 0 $token_type = $token->{type};
22 0 $token_data = $token->{data};
23  
24 0 ▶ if ($token_type == REG_DELIM) {
Branch: 3 branches
BranchTrueFalseTotal
if ($token_type == 151) { } 0 0 0
elsif ($token_type == 187) { } 0 0 0
elsif ($token_type == 145 or $token_type == 146) { } 0 0 0
25 0 ▶ if ($token_data eq q{'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($token_data eq "'") 0 0 0
26 0 $is_delimiter_single_quote = 1;
27 }
28 }
29 elsif ($token_type == REG_EXP) {
30 0 my $regexp = $token_data;
31  
32 0 my $_is_delimiter_single_quote = $is_delimiter_single_quote;
33 0 $is_delimiter_single_quote = 0;
34  
35 0 my $_is_reg_quote = $is_reg_quote;
36 0 $is_reg_quote = 0;
37  
38 0 ▶ if ($_is_reg_quote) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_is_reg_quote) 0 0 0
39 # ignore when reg quote
40 0 next;
41 }
42  
43 0 ▶ if (!$_is_delimiter_single_quote) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($_is_delimiter_single_quote) 0 0 0
44 # replace variables
45 0 ▶ while ($regexp =~ /(\\*)([\$\@]\S+)/gc) {
Branch: 1 branch
BranchTrueFalseTotal
if ($regexp =~ /(\\*)([\$\@]\S+)/cg) 0 0 0
46 0 ▶ if (length($1) % 2 == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (length($1) % 2 == 0) { } 0 0 0
47 # not escaped
48 0 $regexp =~ s/(\\*)[\$\@]\S+/$1xxxx/; # replace the variable to no-meaning 4 characters string
49 }
50 else {
51 # escaped
52 0 $regexp =~ s/(\\*)[\$\@]/$1X/; # replace the sigil to no-meaning character
53 }
54 }
55 }
56  
57 0 my $maybe_regopt = $tokens->[$i+2]; # XXX right!?
58 0 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if ($maybe_regopt and $maybe_regopt->{"type"} == 144 and $maybe_regopt->{"data"} =~ /x/) 0 0 0
Condition0%0/6
$maybe_regopt and $maybe_regopt->{"type"} == 144 and $maybe_regopt->{"data"} =~ /x/
!ll&&!rl&&r
000
$maybe_regopt and $maybe_regopt->{"type"} == 144
!ll&&!rl&&r
000
MC/DC0%0/3
$maybe_regopt and $maybe_regopt->{"type"} == 144 and $maybe_regopt->{"data"} =~ /x/
$maybe_regopt $maybe_regopt->{"type"} == 144 $maybe_regopt->{"data"} =~ /x/
Truth table0%0/4
A and B and C
A= $maybe_regoptB= $maybe_regopt->{"type"} == 144C= $maybe_regopt->{"data"} =~ /x/
ABCresult
0XX0
10X0
1100
1111
59 $maybe_regopt &&
60 $maybe_regopt->{type} == REG_OPT &&
61 $maybe_regopt->{data} =~ /x/
62 ) {
63 0 $regexp =~ s/#.*?\n//gs; # reduce comments
64 0 $regexp =~ s/\s//g; # reduce white spaces
65 }
66  
67 0 ▶ if (length $regexp > $max_characters) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $regexp > $max_characters) 0 0 0
68 push @violations, {
69 filename => $file,
70 line => $token->{line},
71 0 description => DESC,
72 explanation => EXPL,
73 policy => __PACKAGE__,
74 };
75 }
76 }
77 ▶ elsif ($token_type == REG_QUOTE || $token_type == REG_DOUBLE_QUOTE) {
Condition0%0/3
$token_type == 145 or $token_type == 146
l!l&&r!l&&!r
000
MC/DC0%0/2
$token_type == 145 or $token_type == 146
$token_type == 145 $token_type == 146
Truth table0%0/3
A or B
A= $token_type == 145B= $token_type == 146
ABresult
1X1
011
000
78 0 $is_reg_quote = 1;
79 }
80 }
81  
82 0 return \@violations;
83 }
84  
85 2 1;
86  
« Perl/Lint/Policy/TestingAndDebugging/ProhibitNoStrict.pm ↑ Summary Perl/Lint/Policy/InputOutput/ProhibitReadlineInForLoop.pm »