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

filter: Statementstmt 76.3% 29 / 38 Branchbran 8.3% 1 / 12 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 100% 11 / 11 Podpod 80.0% 4 / 5 totaltotal 63.3% 45 / 71 CC 8 SCAR 29.9
CC
8
Cov
57%
violates
43.5
BEGIN
1.0
BEGIN
1.0
CRAP 19.9
« blib/lib/Perl/Critic/Policy/TestingAndDebugging/ProhibitNoStrict.pm ↑ Summary blib/lib/Perl/Critic/Policy/TestingAndDebugging/RequireUseStrict.pm »
1 package Perl::Critic::Policy::RegularExpressions::ProhibitFixedStringMatches;
2  
3 40 ▶use 5.010001;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 40 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 40 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 40 ▶use Readonly;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 40 ▶use Perl::Critic::Utils qw( :severities );
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 40 ▶use parent 'Perl::Critic::Policy';
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 40 our $VERSION = '1.150';
13  
14 #-----------------------------------------------------------------------------
15  
16 40 Readonly::Scalar my $DESC => q{Use 'eq' or hash instead of fixed-pattern regexps};
17 40 Readonly::Scalar my $EXPL => [271,272];
18  
19 #-----------------------------------------------------------------------------
20  
21 90 ▶sub supported_parameters { return qw() }
Subroutine supported_parameters: called CC=1 SCAR=0.0
Pod undocumented
22 75 ▶sub default_severity { return $SEVERITY_LOW }
Subroutine default_severity: called CC=1 SCAR=0.0
Pod documented
23 86 ▶sub default_themes { return qw( core pbp performance ) }
Subroutine default_themes: called CC=1 SCAR=0.0
Pod documented
24 31 ▶sub applies_to { return qw(PPI::Token::Regexp::Match
Subroutine applies_to: called CC=1 SCAR=0.0
Pod documented
25 PPI::Token::Regexp::Substitute
26 PPI::Token::QuoteLike::Regexp) }
27  
28 #-----------------------------------------------------------------------------
29  
30 sub violates {
31 1 ▶ my ( $self, $elem, $doc ) = @_;
Subroutine violates: called CC=8 SCAR=37.7
Pod documented
32  
33 1 my $re = $elem->get_match_string();
34  
35 # only flag regexps that are anchored front and back
36 1 ▶ if ($re =~ m{\A \s*
Branch: 1 branch
BranchTrueFalseTotal
if ($re =~ /\A \s* (\\A|\^) # front anchor == $1 (.*?) (\\z|\$) # end anchor == $2 \s* \z/msx) { } 0 1 1
37 (\\A|\^) # front anchor == $1
38 (.*?)
39 (\\z|\$) # end anchor == $2
40 \s* \z}xms) {
41  
42 0 my ($front_anchor, $words, $end_anchor) = ($1, $2, $3);
43  
44 # If it's a multiline match, then end-of-line anchors don't represent the whole string
45 0 ▶ if ($front_anchor eq q{^} || $end_anchor eq q{$}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($front_anchor eq "^" or $end_anchor eq "\$") 0 0 0
Condition0%0/3
$front_anchor eq "^" or $end_anchor eq "\$"
l!l&&r!l&&!r
000
MC/DC0%0/2
$front_anchor eq "^" or $end_anchor eq "\$"
$front_anchor eq "^" $end_anchor eq "\$"
Truth table0%0/3
A or B
A= $front_anchor eq "^"B= $end_anchor eq "\$"
ABresult
1X1
011
000
46 0 ▶ my $regexp = $doc->ppix_regexp_from_element( $elem )
Branch: 1 branch
BranchTrueFalseTotal
my $regexp = $doc->ppix_regexp_from_element($elem) or return 0 0 0
47 or return;
48 0 ▶ return if $regexp->modifier_asserted( 'm' );
Branch: 1 branch
BranchTrueFalseTotal
if $regexp->modifier_asserted("m") 0 0 0
49 }
50  
51 # check for grouping and optional alternation. Grouping may or may not capture
52 0 ▶ if ($words =~ m{\A \s*
Branch: 1 branch
BranchTrueFalseTotal
if ($words =~ /\A \s* [(] # start group (?:[?]:)? # optional non-capturing indicator \s* (.*?) \s* # interior of group [)] # end of group \s* \z/msx) 0 0 0
53 [(] # start group
54 (?:[?]:)? # optional non-capturing indicator
55 \s* (.*?) \s* # interior of group
56 [)] # end of group
57 \s* \z}xms) {
58 0 $words = $1;
59 0 $words =~ s/[|]//gxms; # ignore alternation inside of parens -- just look at words
60 }
61  
62 # Regexps that contain metachars are not fixed strings
63 0 ▶ return if $words =~ m/[\\#\$()*+.?\@\[\]^{|}]/xms;
Branch: 1 branch
BranchTrueFalseTotal
if $words =~ /[\\#\$()*+.?\@\[\]^{|}]/msx 0 0 0
64  
65  
66 0 return $self->violation( $DESC, $EXPL, $elem );
67  
68 } else {
69 1 return; # OK
70 }
71 }
72  
73 40 1;
74  
75 __END__
76  
77 #-----------------------------------------------------------------------------
78  
79 =pod
80  
81 =head1 NAME
82  
83 Perl::Critic::Policy::RegularExpressions::ProhibitFixedStringMatches - Use C<eq> or hash instead of fixed-pattern regexps.
84  
85  
86 =head1 AFFILIATION
87  
88 This Policy is part of the core L<Perl::Critic|Perl::Critic>
89 distribution.
90  
91  
92 =head1 DESCRIPTION
93  
94 A regular expression that matches just a fixed set of constant strings
95 is wasteful of performance and is hard on maintainers. It is much
96 more readable and often faster to use C<eq> or a hash to match such
97 strings.
98  
99 # Bad
100 my $is_file_function = $token =~ m/\A (?: open | close | read ) \z/xms;
101  
102 # Faster and more readable
103 my $is_file_function = $token eq 'open' ||
104 $token eq 'close' ||
105 $token eq 'read';
106  
107 For larger numbers of strings, a hash is superior:
108  
109 # Bad
110 my $is_perl_keyword =
111 $token =~ m/\A (?: chomp | chop | chr | crypt | hex | index
112 lc | lcfirst | length | oct | ord | ... ) \z/xms;
113  
114 # Better
115 Readonly::Hash my %PERL_KEYWORDS => map {$_ => 1} qw(
116 chomp chop chr crypt hex index lc lcfirst length oct ord ...
117 );
118 my $is_perl_keyword = $PERL_KEYWORD{$token};
119  
120 Conway also suggests using C<lc()> instead of a case-insensitive match.
121  
122  
123 =head2 VARIANTS
124  
125 This policy detects both grouped and non-grouped strings. The
126 grouping may or may not be capturing. The grouped body may or may not
127 be alternating. C<\A> and C<\z> are always considered anchoring which
128 C<^> and C<$> are considered anchoring is the C<m> regexp option is
129 not in use. Thus, all of these are violations:
130  
131 m/^foo$/;
132 m/\A foo \z/x;
133 m/\A foo \z/xm;
134 m/\A(foo)\z/;
135 m/\A(?:foo)\z/;
136 m/\A(foo|bar)\z/;
137 m/\A(?:foo|bar)\z/;
138  
139 Furthermore, this policy detects violations in C<m//>, C<s///> and
140 C<qr//> constructs, as you would expect.
141  
142  
143 =head1 CONFIGURATION
144  
145 This Policy is not configurable except for the standard options.
146  
147  
148 =head1 CREDITS
149  
150 Initial development of this policy was supported by a grant from the
151 Perl Foundation.
152  
153  
154 =head1 AUTHOR
155  
156 Chris Dolan <cdolan@cpan.org>
157  
158  
159 =head1 COPYRIGHT
160  
161 Copyright (c) 2007-2023 Chris Dolan
162  
163 This program is free software; you can redistribute it and/or modify
164 it under the same terms as Perl itself. The full text of this license
165 can be found in the LICENSE file included with this module
166  
167 =cut
168  
169 # Local Variables:
170 # mode: cperl
171 # cperl-indent-level: 4
172 # fill-column: 78
173 # indent-tabs-mode: nil
174 # c-indentation-style: bsd
175 # End:
176 # ex: set ts=8 sts=4 sw=4 tw=78 ft=perl expandtab shiftround :
« blib/lib/Perl/Critic/Policy/TestingAndDebugging/ProhibitNoStrict.pm ↑ Summary blib/lib/Perl/Critic/Policy/TestingAndDebugging/RequireUseStrict.pm »