blib/lib/Perl/Critic/Policy/RegularExpressions/ProhibitRegexInStrings.pm

filter: Statementstmt 98.9% 92 / 93 Branchbran 83.9% 47 / 56 Conditioncond 70.4% 43 / 61 MC/DCmcdc 51.5% 17 / 33 Subroutinesub 94.4% 17 / 18 Podpod 100% 5 / 5 totaltotal 83.0% 221 / 266 CC 51 SCAR 25.9
CC
51
Cov
87%
_regex_use
24.7
violates
13.0
_declared_scalar
12.9
CRAP 13.3
↑ Summary
1 package Perl::Critic::Policy::RegularExpressions::ProhibitRegexInStrings 0.001;
2  
3 # ABSTRACT: Write a regular expression as qr//, not as a string that somebody compiles later.
4  
5 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use warnings FATAL => 'all';
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 2 ▶use 5.014;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 2 ▶use re '/aa';
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 2 ▶use Readonly;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 2 ▶use PPIx::Regexp;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 2 ▶use Perl::Critic::Utils qw{ :severities };
Subroutine BEGIN: called CC=1 SCAR=0.0
16 2 ▶use parent qw{Perl::Critic::Policy};
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18  
19 2 Readonly::Scalar my $DESC_KEY => q{Regular expression in a string, as the value of '%s'};
20 2 Readonly::Scalar my $DESC_VAR => q{Regular expression in a string, in %s, which a match uses};
21 2 Readonly::Scalar my $EXPL => q{Write it as qr//, and give "$qr" where a string is needed};
22  
23 # A double-quoted string that holds one scalar and nothing else, which is how
24 # a qr// becomes a string: "$name", "${name}" or "$Package::name".
25 2 Readonly::Scalar my $IDENTIFIER => qr/[[:alpha:]_][[:alnum:]_]*/xs;
26 2 Readonly::Scalar my $ONE_SCALAR => qr/\A \$ (?: $IDENTIFIER (?: :: [[:alnum:]_]+ )* | \{ $IDENTIFIER \} ) \z/xs;
27  
28 # An interpolation in braces, such as ${re}, and the name inside them.
29 2 Readonly::Scalar my $IN_BRACES => qr/\A \$ \{ ([^}]+) \} \z/xs;
30  
31 2 Readonly::Hash my %BINDING => map { $_ => 1 } qw{ =~ !~ };
32 2 Readonly::Hash my %REGEX_TOKEN => map { $_ => 1 } qw{ PPI::Token::Regexp::Match PPI::Token::Regexp::Substitute PPI::Token::QuoteLike::Regexp };
33  
34  
35 3 ▶sub supported_parameters {
Subroutine supported_parameters: called CC=1 SCAR=0.0
Pod documented
36 return (
37 {
38 3 name => 'keys',
39 description => 'The keys whose value is a regular expression',
40 default_string => 'pattern regex regexp',
41 behavior => 'string list',
42 },
43 );
44 }
45  
46 25 ▶sub default_severity { return $SEVERITY_MEDIUM }
Subroutine default_severity: called CC=1 SCAR=0.0
Pod documented
47 0 ▶sub default_themes { return qw{ bugs maintenance } }
Subroutine default_themes: not called CC=1 SCAR=6.9
Pod documented
48 46 ▶sub applies_to { return 'PPI::Token::Quote' }
Subroutine applies_to: called CC=1 SCAR=0.0
Pod documented
49  
50  
51 50 ▶sub violates {
Subroutine violates: called CC=13 SCAR=25.7
Pod documented
52 50 my ( $self, $elem, undef ) = @_;
53  
54 # A qr// in a string, which is what this policy asks for.
55 50 ▶ return if ( $elem->isa('PPI::Token::Quote::Double') || $elem->isa('PPI::Token::Quote::Interpolate') ) && $elem->string =~ $ONE_SCALAR;
Branch: 1 branch
BranchTrueFalseTotal
if $elem->isa("PPI::Token::Quote::Double") || $elem->isa("PPI::Token::Quote::Interpolate") and $elem->string =~ /$ONE_SCALAR/aa 4 46 50
Condition100%6/6
$elem->isa("PPI::Token::Quote::Double") || $elem->isa("PPI::Token::Quote::Interpolate") and $elem->string =~ /$ONE_SCALAR/aa
!ll&&!rl&&r
4154
$elem->isa("PPI::Token::Quote::Double") or $elem->isa("PPI::Token::Quote::Interpolate")
l!l&&r!l&&!r
7241
MC/DC100%3/3
$elem->isa("PPI::Token::Quote::Double") || $elem->isa("PPI::Token::Quote::Interpolate") and $elem->string =~ /$ONE_SCALAR/aa
$elem->isa("PPI::Token::Quote::Double") $elem->isa("PPI::Token::Quote::Interpolate") $elem->string =~ /$ONE_SCALAR/aa
Truth table100%5/5
A or B and C
A= $elem->isa("PPI::Token::Quote::Double")B= $elem->isa("PPI::Token::Quote::Interpolate")C= $elem->string =~ /$ONE_SCALAR/aa
ABCresult
00X0
1X00
0100
1X11
0111
56  
57 46 ▶ my $previous = $elem->sprevious_sibling or return;
Branch: 1 branch
BranchTrueFalseTotal
my $previous = $elem->sprevious_sibling or return 41 5 46
58 41 ▶ return unless $previous->isa('PPI::Token::Operator');
Branch: 1 branch
BranchTrueFalseTotal
unless $previous->isa("PPI::Token::Operator") 1 40 41
59  
60 40 ▶ if ( $previous->content eq '=>' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($previous->content eq "=>") 17 23 40
61 17 ▶ my $key = $previous->sprevious_sibling or return;
Branch: 1 branch
BranchTrueFalseTotal
my $key = $previous->sprevious_sibling or return 17 0 17
62 17 ▶ my $name = $key->isa('PPI::Token::Word') ? $key->content : $key->isa('PPI::Token::Quote') ? $key->string : return;
Branch: 2 branches
BranchTrueFalseTotal
$key->isa('PPI::Token::Word') ? : 14 3 17
$key->isa('PPI::Token::Quote') ? : 3 0 3
63 17 my $keys = $self->{_keys};
64 17 ▶ return unless $keys->{$name};
Branch: 1 branch
BranchTrueFalseTotal
unless $keys->{$name} 4 13 17
65 13 return $self->violation( sprintf( $DESC_KEY, $name ), $EXPL, $elem );
66 }
67  
68 23 ▶ my $name = _declared_scalar($elem) // return;
Condition100%2/2
_declared_scalar($elem) // (return)
l!l
194
MC/DC100%1/1
_declared_scalar($elem) // (return)
_declared_scalar($elem)
Truth table100%2/2
A
A= _declared_scalar($elem)
Aresult
11
00
69 19 ▶ return unless _used_as_regex( $elem->statement, $name );
Branch: 1 branch
BranchTrueFalseTotal
unless _used_as_regex($elem->statement, $name) 7 12 19
70 12 return $self->violation( sprintf( $DESC_VAR, $name ), $EXPL, $elem );
71 }
72  
73 # The name of the one scalar that a declaration sets to the string $quote and
74 # to nothing else, as a declaration of one lexical does, or undef.
75 23 ▶sub _declared_scalar {
Subroutine _declared_scalar: called CC=11 SCAR=25.6
76 23 my ($quote) = @_;
77  
78 23 my $previous = $quote->sprevious_sibling;
79 23 ▶ return unless $previous && $previous->isa('PPI::Token::Operator') && $previous->content eq q{=};
Branch: 1 branch
BranchTrueFalseTotal
unless $previous and $previous->isa("PPI::Token::Operator") and $previous->content eq "=" 2 21 23
Condition50%3/6
$previous and $previous->isa("PPI::Token::Operator") and $previous->content eq "="
!ll&&!rl&&r
0221
$previous and $previous->isa("PPI::Token::Operator")
!ll&&!rl&&r
0023
MC/DC33%1/3
$previous and $previous->isa("PPI::Token::Operator") and $previous->content eq "="
$previous $previous->isa("PPI::Token::Operator") $previous->content eq "="
Truth table50%2/4
A and B and C
A= $previousB= $previous->isa("PPI::Token::Operator")C= $previous->content eq "="
ABCresult
0XX0
10X0
1100
1111
80 21 my $statement = $quote->statement;
81 21 ▶ return unless $statement && $statement->isa('PPI::Statement::Variable');
Branch: 1 branch
BranchTrueFalseTotal
unless $statement and $statement->isa("PPI::Statement::Variable") 0 21 21
Condition33%1/3
$statement and $statement->isa("PPI::Statement::Variable")
!ll&&!rl&&r
0021
MC/DC0%0/2
$statement and $statement->isa("PPI::Statement::Variable")
$statement $statement->isa("PPI::Statement::Variable")
Truth table33%1/3
A and B
A= $statementB= $statement->isa("PPI::Statement::Variable")
ABresult
0X0
100
111
82 21 my @names = $statement->variables;
83 21 ▶ return unless @names == 1 && substr( $names[0], 0, 1 ) eq q{$};
Branch: 1 branch
BranchTrueFalseTotal
unless @names == 1 and substr($names[0], 0, 1) eq "\$" 1 20 21
Condition66%2/3
@names == 1 and substr($names[0], 0, 1) eq "\$"
!ll&&!rl&&r
1020
MC/DC50%1/2
@names == 1 and substr($names[0], 0, 1) eq "\$"
@names == 1 substr($names[0], 0, 1) eq "\$"
Truth table66%2/3
A and B
A= @names == 1B= substr($names[0], 0, 1) eq "\$"
ABresult
0X0
100
111
84 20 my $next = $quote->snext_sibling;
85 20 ▶ return if $next && !( $next->isa('PPI::Token::Structure') && $next->content eq q{;} );
Branch: 1 branch
BranchTrueFalseTotal
if $next and not $next->isa("PPI::Token::Structure") && $next->content eq ";" 1 19 20
Condition66%4/6
$next and not $next->isa("PPI::Token::Structure") && $next->content eq ";"
!ll&&!rl&&r
0191
$next->isa('PPI::Token::Structure') && $next->content eq ';'
!ll&&!rl&&r
1019
MC/DC33%1/3
$next and not $next->isa("PPI::Token::Structure") && $next->content eq ";"
$next $next->isa('PPI::Token::Structure') $next->content eq ';'
Truth table50%2/4
A and not(B && C)
A= $nextB= $next->isa('PPI::Token::Structure')C= $next->content eq ';'
ABCresult
0XX0
1110
10X1
1101
86 19 return $names[0];
87 }
88  
89 # Whether the block around the statement $declaration uses the scalar $name
90 # as a regular expression after it, as _regex_use says.
91 19 ▶sub _used_as_regex {
Subroutine _used_as_regex: called CC=3 SCAR=11.1
92 19 my ( $declaration, $name ) = @_;
93  
94 19 ▶ my $scope = $declaration->parent or return;
Branch: 1 branch
BranchTrueFalseTotal
my $scope = $declaration->parent or return 19 0 19
95 19 my $from = _position($declaration);
96 my $found = $scope->find(
97 446 ▶ sub {
Subroutine __ANON__: called CC=5 SCAR=16.2
98 446 my ( undef, $node ) = @_;
99 446 my $at = _position($node);
100 446 ▶ return 0 if $at->[0] < $from->[0] || ( $at->[0] == $from->[0] && $at->[1] < $from->[1] );
Branch: 1 branch
BranchTrueFalseTotal
if $at->[0] < $from->[0] or $at->[0] == $from->[0] and $at->[1] < $from->[1] 24 422 446
Condition66%4/6
$at->[0] < $from->[0] or $at->[0] == $from->[0] and $at->[1] < $from->[1]
l!l&&r!l&&!r
024422
$at->[0] == $from->[0] and $at->[1] < $from->[1]
!ll&&!rl&&r
042224
MC/DC33%1/3
$at->[0] < $from->[0] or $at->[0] == $from->[0] and $at->[1] < $from->[1]
$at->[0] < $from->[0] $at->[0] == $from->[0] $at->[1] < $from->[1]
Truth table50%2/4
A or B and C
A= $at->[0] < $from->[0]B= $at->[0] == $from->[0]C= $at->[1] < $from->[1]
ABCresult
1XX1
0111
00X0
0100
101 422 ▶ return _regex_use( $node, $name ) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
_regex_use($node, $name) ? : 12 410 422
102 }
103 19 );
104 19 ▶ return $found ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$found ? : 12 7 19
105 }
106  
107 # Whether the element $node uses the scalar $name as a regular expression:
108 # interpolated into the expression of a match, a substitution or a qr//, and
109 # not into the replacement of a substitution; bound with =~ or !~; or the first
110 # argument of split, with or without parentheses.
111 422 ▶sub _regex_use {
Subroutine _regex_use: called CC=22 SCAR=32.1
112 422 my ( $node, $name ) = @_;
113  
114 422 ▶ if ( $REGEX_TOKEN{ ref $node } ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($REGEX_TOKEN{ref $node}) 8 414 422
115 8 ▶ my $regexp = PPIx::Regexp->new($node) or return;
Branch: 1 branch
BranchTrueFalseTotal
my $regexp = "PPIx::Regexp"->new($node) or return 8 0 8
116 8 ▶ my $re = $regexp->regular_expression or return;
Branch: 1 branch
BranchTrueFalseTotal
my $re = $regexp->regular_expression or return 8 0 8
117 8 ▶ foreach my $interpolation ( @{ $re->find('PPIx::Regexp::Token::Interpolation') || [] } ) {
Condition100%2/2
$re->find('PPIx::Regexp::Token::Interpolation') || []
l!l
44
MC/DC100%1/1
$re->find('PPIx::Regexp::Token::Interpolation') || []
$re->find('PPIx::Regexp::Token::Interpolation')
Truth table100%2/2
A
A= $re->find('PPIx::Regexp::Token::Interpolation')
Aresult
11
00
118 4 my $content = $interpolation->content;
119 4 my ($inner) = $content =~ $IN_BRACES;
120 4 ▶ return 1 if ( defined $inner ? "\$$inner" : $content ) eq $name;
Branch: 2 branches
BranchTrueFalseTotal
if (defined $inner ? "\$$inner" : $content) eq $name 4 0 4
defined $inner ? : 1 3 4
121 }
122 4 return;
123 }
124  
125 414 ▶ return unless $node->isa('PPI::Token::Symbol') && $node->symbol eq $name;
Branch: 1 branch
BranchTrueFalseTotal
unless $node->isa("PPI::Token::Symbol") and $node->symbol eq $name 383 31 414
Condition100%3/3
$node->isa("PPI::Token::Symbol") and $node->symbol eq $name
!ll&&!rl&&r
3632031
MC/DC100%2/2
$node->isa("PPI::Token::Symbol") and $node->symbol eq $name
$node->isa("PPI::Token::Symbol") $node->symbol eq $name
Truth table100%3/3
A and B
A= $node->isa("PPI::Token::Symbol")B= $node->symbol eq $name
ABresult
0X0
100
111
126 31 my $before = $node->sprevious_sibling;
127 31 ▶ return 1 if $before && $before->isa('PPI::Token::Operator') && $BINDING{ $before->content };
Branch: 1 branch
BranchTrueFalseTotal
if $before and $before->isa("PPI::Token::Operator") and $BINDING{$before->content} 4 27 31
Condition100%6/6
$before and $before->isa("PPI::Token::Operator") and $BINDING{$before->content}
!ll&&!rl&&r
2524
$before and $before->isa("PPI::Token::Operator")
!ll&&!rl&&r
1246
MC/DC100%3/3
$before and $before->isa("PPI::Token::Operator") and $BINDING{$before->content}
$before $before->isa("PPI::Token::Operator") $BINDING{$before->content}
Truth table100%4/4
A and B and C
A= $beforeB= $before->isa("PPI::Token::Operator")C= $BINDING{$before->content}
ABCresult
0XX0
10X0
1100
1111
128 27 ▶ return 1 if $before && $before->isa('PPI::Token::Word') && $before->content eq 'split';
Branch: 1 branch
BranchTrueFalseTotal
if $before and $before->isa("PPI::Token::Word") and $before->content eq "split" 3 24 27
Condition100%6/6
$before and $before->isa("PPI::Token::Word") and $before->content eq "split"
!ll&&!rl&&r
3213
$before and $before->isa("PPI::Token::Word")
!ll&&!rl&&r
1224
MC/DC100%3/3
$before and $before->isa("PPI::Token::Word") and $before->content eq "split"
$before $before->isa("PPI::Token::Word") $before->content eq "split"
Truth table100%4/4
A and B and C
A= $beforeB= $before->isa("PPI::Token::Word")C= $before->content eq "split"
ABCresult
0XX0
10X0
1100
1111
129 24 ▶ return if $before;
Branch: 1 branch
BranchTrueFalseTotal
if $before 23 1 24
130  
131 1 ▶ my $list = $node->parent && $node->parent->parent;
Condition33%1/3
$node->parent && $node->parent->parent
!ll&&!rl&&r
001
MC/DC0%0/2
$node->parent && $node->parent->parent
$node->parent $node->parent->parent
Truth table33%1/3
A && B
A= $node->parentB= $node->parent->parent
ABresult
0X0
100
111
132 1 ▶ return unless $list && $list->isa('PPI::Structure::List');
Branch: 1 branch
BranchTrueFalseTotal
unless $list and $list->isa("PPI::Structure::List") 0 1 1
Condition33%1/3
$list and $list->isa("PPI::Structure::List")
!ll&&!rl&&r
001
MC/DC0%0/2
$list and $list->isa("PPI::Structure::List")
$list $list->isa("PPI::Structure::List")
Truth table33%1/3
A and B
A= $listB= $list->isa("PPI::Structure::List")
ABresult
0X0
100
111
133 1 my $call = $list->sprevious_sibling;
134 1 ▶ return $call && $call->isa('PPI::Token::Word') && $call->content eq 'split';
Condition33%2/6
$call && $call->isa('PPI::Token::Word') && $call->content eq 'split'
!ll&&!rl&&r
001
$call and $call->isa("PPI::Token::Word")
!ll&&!rl&&r
001
MC/DC0%0/3
$call && $call->isa('PPI::Token::Word') && $call->content eq 'split'
$call $call->isa("PPI::Token::Word") $call->content eq 'split'
Truth table25%1/4
A and B && C
A= $callB= $call->isa("PPI::Token::Word")C= $call->content eq 'split'
ABCresult
0XX0
10X0
1100
1111
135 }
136  
137 # The line and the column of $node.
138 465 ▶sub _position {
Subroutine _position: called CC=2 SCAR=7.0
139 465 my ($node) = @_;
140 465 ▶ my $location = $node->location or return [ 0, 0 ];
Branch: 1 branch
BranchTrueFalseTotal
my $location = $node->location or return [0, 0] 465 0 465
141 465 return [ @{$location}[ 0, 1 ] ];
142 }
143  
144 2 1;
145  
146 __END__
↑ Summary