Perl/Lint/Policy/RegularExpressions/ProhibitFixedStringMatches.pm

filter: Statementstmt 65.5% 57 / 87 Branchbran 33.3% 14 / 42 Conditioncond 19.4% 7 / 36 MC/DCmcdc 4.5% 1 / 22 Subroutinesub 100% 8 / 8 Podpod 0.0% 0 / 1 totaltotal 44.3% 87 / 196 CC 34 SCAR 57.1
CC
34
Cov
47%
evaluate
365.0
BEGIN
1.0
BEGIN
1.0
CRAP 302.8
« Perl/Lint/Policy/BuiltinFunctions/ProhibitStringyEval.pm ↑ Summary Perl/Lint/Policy/Modules/RequireVersionVar.pm »
1 package Perl::Lint::Policy::RegularExpressions::ProhibitFixedStringMatches;
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 Regexp::Lexer qw(tokenize);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use Regexp::Lexer::TokenType;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 3 ▶use parent "Perl::Lint::Policy";
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 3 ▶use constant {
Subroutine BEGIN: called CC=1 SCAR=0.0
10 3 DESC => 'Use "eq" or hash instead of fixed-pattern regexps',
11 EXPL => [271, 272],
12 3 };
13  
14 # to use sanitize
15 3 my $alternation_id = Regexp::Lexer::TokenType::Alternation->{id};
16 3 my $lparen_id = Regexp::Lexer::TokenType::LeftParenthesis->{id};
17 3 my $rparen_id = Regexp::Lexer::TokenType::RightParenthesis->{id};
18 3 my $question_id = Regexp::Lexer::TokenType::Question->{id};
19 3 my $colon_id = Regexp::Lexer::TokenType::Colon->{id};
20  
21 # to use check fixed string
22 3 my $character_id = Regexp::Lexer::TokenType::Character->{id};
23 3 my $escaped_character_id = Regexp::Lexer::TokenType::EscapedCharacter->{id};
24  
25 # anchors
26 3 my $beginning_of_line_id = Regexp::Lexer::TokenType::BeginningOfLine->{id};
27 3 my $end_of_line_id = Regexp::Lexer::TokenType::EndOfLine->{id};
28 3 my $escaped_beginning_of_line_id = Regexp::Lexer::TokenType::EscapedBeginningOfString->{id};
29 3 my $escaped_end_of_line_id = Regexp::Lexer::TokenType::EscapedEndOfString->{id};
30  
31 2 ▶sub evaluate {
Subroutine evaluate: called CC=34 SCAR=59.0
Pod undocumented
32 2 my ($class, $file, $tokens, $src, $args) = @_;
33  
34 2 my @violations;
35 2 my $is_reg_quoted = 0;
36 2 ▶ for (my $i = 0, my $token_type; my $token = $tokens->[$i]; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $token = $tokens->[$i]) 51 2 53
37 51 $token_type = $token->{type};
38  
39 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
40 2 ▶ if ($is_reg_quoted) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_reg_quoted) 0 2 2
41 0 $is_reg_quoted = 0;
42 0 next;
43 }
44  
45 2 my $maybe_regopt;
46 2 ▶ if ($token_type == REG_EXP) {
Branch: 1 branch
BranchTrueFalseTotal
if ($token_type == 187) { } 2 0 2
47 2 $maybe_regopt = $tokens->[$i+2];
48 }
49 else {
50 0 $maybe_regopt = $tokens->[$i+4];
51 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
52 0 $maybe_regopt = $tokens->[$i+5];
53 }
54 }
55  
56 2 my $is_with_m_opt = 0;
57 2 ▶ if ($maybe_regopt) {
Branch: 1 branch
BranchTrueFalseTotal
if ($maybe_regopt) 2 0 2
58 2 ▶ if ($maybe_regopt->{type} == REG_OPT && $maybe_regopt->{data} =~ /m/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($maybe_regopt->{"type"} == 144 and $maybe_regopt->{"data"} =~ /m/) 0 2 2
Condition33%1/3
$maybe_regopt->{"type"} == 144 and $maybe_regopt->{"data"} =~ /m/
!ll&&!rl&&r
200
MC/DC0%0/2
$maybe_regopt->{"type"} == 144 and $maybe_regopt->{"data"} =~ /m/
$maybe_regopt->{"type"} == 144 $maybe_regopt->{"data"} =~ /m/
Truth table33%1/3
A and B
A= $maybe_regopt->{"type"} == 144B= $maybe_regopt->{"data"} =~ /m/
ABresult
0X0
100
111
59 0 $is_with_m_opt = 1;
60 }
61 }
62  
63 2 my @regexp_tokens = eval {
64 2 @{tokenize(qr/$token->{data}/)->{tokens}};
65 };
66  
67 2 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 2 2
68 # XXX First aid!
69 # Maybe regexp is produced by `tr///` or `y///` operator if it reaches here.
70 0 next;
71 }
72  
73 2 ▶ if (scalar @regexp_tokens < 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @regexp_tokens < 2) 0 2 2
74 0 next;
75 }
76  
77 2 my $first_token_type_id = (shift @regexp_tokens)->{type}->{id};
78 2 my $last_token_type_id = (pop @regexp_tokens)->{type}->{id};
79  
80 2 ▶ if (defined $first_token_type_id && defined $last_token_type_id) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $first_token_type_id and defined $last_token_type_id) 2 0 2
Condition33%1/3
defined $first_token_type_id and defined $last_token_type_id
!ll&&!rl&&r
002
MC/DC0%0/2
defined $first_token_type_id and defined $last_token_type_id
defined $first_token_type_id defined $last_token_type_id
Truth table33%1/3
A and B
A= defined $first_token_type_idB= defined $last_token_type_id
ABresult
0X0
100
111
81 2 ▶ if ($is_with_m_opt) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_with_m_opt) 0 2 2
82 0 ▶ if ($first_token_type_id == $beginning_of_line_id || $last_token_type_id == $end_of_line_id) {
Branch: 1 branch
BranchTrueFalseTotal
if ($first_token_type_id == $beginning_of_line_id or $last_token_type_id == $end_of_line_id) 0 0 0
Condition0%0/3
$first_token_type_id == $beginning_of_line_id or $last_token_type_id == $end_of_line_id
l!l&&r!l&&!r
000
MC/DC0%0/2
$first_token_type_id == $beginning_of_line_id or $last_token_type_id == $end_of_line_id
$first_token_type_id == $beginning_of_line_id $last_token_type_id == $end_of_line_id
Truth table0%0/3
A or B
A= $first_token_type_id == $beginning_of_line_idB= $last_token_type_id == $end_of_line_id
ABresult
1X1
011
000
83 0 next;
84 }
85 }
86  
87 2 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if ($first_token_type_id == $beginning_of_line_id || $first_token_type_id == $escaped_beginning_of_line_id and $last_token_type_id == $end_of_line_id || $last_token_type_id == $escaped_end_of_line_id) 0 2 2
Condition22%2/9
$first_token_type_id == $beginning_of_line_id || $first_token_type_id == $escaped_beginning_of_line_id and $last_token_type_id == $end_of_line_id || $last_token_type_id == $escaped_end_of_line_id
!ll&&!rl&&r
200
$first_token_type_id == $beginning_of_line_id or $first_token_type_id == $escaped_beginning_of_line_id
l!l&&r!l&&!r
002
$last_token_type_id == $end_of_line_id or $last_token_type_id == $escaped_end_of_line_id
l!l&&r!l&&!r
000
MC/DC0%0/4
$first_token_type_id == $beginning_of_line_id || $first_token_type_id == $escaped_beginning_of_line_id and $last_token_type_id == $end_of_line_id || $last_token_type_id == $escaped_end_of_line_id
$first_token_type_id == $beginning_of_line_id $first_token_type_id == $escaped_beginning_of_line_id $last_token_type_id == $end_of_line_id $last_token_type_id == $escaped_end_of_line_id
Truth table14%1/7
A or B and C or D
A= $first_token_type_id == $beginning_of_line_idB= $first_token_type_id == $escaped_beginning_of_line_idC= $last_token_type_id == $end_of_line_idD= $last_token_type_id == $escaped_end_of_line_id
ABCDresult
00XX0
1X000
01000
1X1X1
1X011
011X1
01011
88 ($first_token_type_id == $beginning_of_line_id || $first_token_type_id == $escaped_beginning_of_line_id) &&
89 ($last_token_type_id == $end_of_line_id || $last_token_type_id == $escaped_end_of_line_id)
90 ) {
91 0 my @not_character_tokens = ();
92  
93 0 ▶ for (my $j = 0, my $type_id; my $regexp_token = $regexp_tokens[$j]; $j++) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $regexp_token = $regexp_tokens[$j]) 0 0 0
94 0 $type_id = $regexp_token->{type}->{id};
95 0 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if ($type_id == $alternation_id or $type_id == $rparen_id) 0 0 0
Condition0%0/3
$type_id == $alternation_id or $type_id == $rparen_id
l!l&&r!l&&!r
000
MC/DC0%0/2
$type_id == $alternation_id or $type_id == $rparen_id
$type_id == $alternation_id $type_id == $rparen_id
Truth table0%0/3
A or B
A= $type_id == $alternation_idB= $type_id == $rparen_id
ABresult
1X1
011
000
96 $type_id == $alternation_id ||
97 $type_id == $rparen_id
98 ) {
99 0 next;
100 }
101  
102 0 ▶ if ($type_id == $lparen_id) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type_id == $lparen_id) 0 0 0
103 0 my $next_regexp_token = $regexp_tokens[$j+1];
104 0 ▶ if (defined $next_regexp_token && $next_regexp_token->{type}->{id} == $question_id) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $next_regexp_token and $next_regexp_token->{"type"}{"id"} == $question_id) 0 0 0
Condition0%0/3
defined $next_regexp_token and $next_regexp_token->{"type"}{"id"} == $question_id
!ll&&!rl&&r
000
MC/DC0%0/2
defined $next_regexp_token and $next_regexp_token->{"type"}{"id"} == $question_id
defined $next_regexp_token $next_regexp_token->{"type"}{"id"} == $question_id
Truth table0%0/3
A and B
A= defined $next_regexp_tokenB= $next_regexp_token->{"type"}{"id"} == $question_id
ABresult
0X0
100
111
105 0 $next_regexp_token = $regexp_tokens[$j+2];
106 0 ▶ if (defined $next_regexp_token && $next_regexp_token->{type}->{id} == $colon_id) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $next_regexp_token and $next_regexp_token->{"type"}{"id"} == $colon_id) 0 0 0
Condition0%0/3
defined $next_regexp_token and $next_regexp_token->{"type"}{"id"} == $colon_id
!ll&&!rl&&r
000
MC/DC0%0/2
defined $next_regexp_token and $next_regexp_token->{"type"}{"id"} == $colon_id
defined $next_regexp_token $next_regexp_token->{"type"}{"id"} == $colon_id
Truth table0%0/3
A and B
A= defined $next_regexp_tokenB= $next_regexp_token->{"type"}{"id"} == $colon_id
ABresult
0X0
100
111
107 0 $j += 2;
108 0 next;
109 }
110 }
111  
112 0 next;
113 }
114  
115 0 ▶ if ($type_id != $character_id && $type_id != $escaped_character_id) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type_id != $character_id and $type_id != $escaped_character_id) 0 0 0
Condition0%0/3
$type_id != $character_id and $type_id != $escaped_character_id
!ll&&!rl&&r
000
MC/DC0%0/2
$type_id != $character_id and $type_id != $escaped_character_id
$type_id != $character_id $type_id != $escaped_character_id
Truth table0%0/3
A and B
A= $type_id != $character_idB= $type_id != $escaped_character_id
ABresult
0X0
100
111
116 0 push @not_character_tokens, $regexp_token;
117 0 next;
118 }
119 }
120  
121 0 ▶ if (@not_character_tokens) {
Branch: 1 branch
BranchTrueFalseTotal
if (@not_character_tokens) 0 0 0
122 0 next;
123 }
124  
125 push @violations, {
126 filename => $file,
127 line => $token->{line},
128 0 description => DESC,
129 explanation => EXPL,
130 policy => __PACKAGE__,
131 };
132 }
133 }
134 }
135 ▶ 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
136 0 $is_reg_quoted = 1;
137 }
138 }
139  
140 2 return \@violations;
141 }
142  
143 3 1;
144  
« Perl/Lint/Policy/BuiltinFunctions/ProhibitStringyEval.pm ↑ Summary Perl/Lint/Policy/Modules/RequireVersionVar.pm »