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
« lib/Perl/Critic/Policy/TestingAndDebugging/ProhibitNoStrict.pm ↑ Summary 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.156';
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 74 ▶sub default_severity { return $SEVERITY_LOW }
Subroutine default_severity: called CC=1 SCAR=0.0
Pod documented
23 85 ▶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 1 ▶sub violates {
Subroutine violates: called CC=8 SCAR=37.7
Pod documented
31 1 my ( $self, $elem, $doc ) = @_;
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__
« lib/Perl/Critic/Policy/TestingAndDebugging/ProhibitNoStrict.pm ↑ Summary lib/Perl/Critic/Policy/TestingAndDebugging/RequireUseStrict.pm »