lib/Perl/Critic/Policy/Performance/ProhibitRegexForSimpleSubstring.pm

filter: Statementstmt 97.3% 37 / 38 Branchbran 80.0% 16 / 20 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 88.8% 8 / 9 Podpod 100% 5 / 5 totaltotal 91.6% 66 / 72 CC 12 SCAR 20.5
CC
12
Cov
91%
violates
12.2
default_themes
2.0
BEGIN
1.0
CRAP 7.8
↑ Summary
1 #!/bin/false
2 # PODNAME: Perl::Critic::Policy::Performance::ProhibitRegexForSimpleSubstring
3 # ABSTRACT: Use index() instead of regex for literal substring matching
4  
5 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 package Perl::Critic::Policy::Performance::ProhibitRegexForSimpleSubstring;
9 2 $Perl::Critic::Policy::Performance::ProhibitRegexForSimpleSubstring::VERSION = '0.01';
10 2 ▶use Perl::Critic::Utils qw{ :severities };
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use parent 'Perl::Critic::Policy';
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 #-----------------------------------------------------------------------------
14  
15 2 my $DESC = q{Regex used for a simple substring match};
16 ## no critic (RequireInterpolationOfMetachars)
17 2 my $EXPL = q{Use index($string, $substring) instead of a regex when looking for literal text.};
18 ## use critic
19  
20 #-----------------------------------------------------------------------------
21  
22 24 ▶sub supported_parameters { return () }
Subroutine supported_parameters: called CC=1 SCAR=0.0
Pod documented
23 7 ▶sub default_severity { return $SEVERITY_MEDIUM }
Subroutine default_severity: called CC=1 SCAR=0.0
Pod documented
24 0 ▶sub default_themes { return qw( performance ) }
Subroutine default_themes: not called CC=1 SCAR=6.9
Pod documented
25 24 ▶sub applies_to { return 'PPI::Token::Regexp::Match' }
Subroutine applies_to: called CC=1 SCAR=0.0
Pod documented
26  
27 #-----------------------------------------------------------------------------
28  
29 24 ▶sub violates {
Subroutine violates: called CC=12 SCAR=25.0
Pod documented
30 24 my ( $self, $elem, $doc ) = @_;
31  
32 24 ▶ my $re = $doc->ppix_regexp_from_element($elem)
Branch: 1 branch
BranchTrueFalseTotal
my $re = $doc->ppix_regexp_from_element($elem) or return 24 0 24
33 or return;
34 24 ▶ $re->failures()
Branch: 1 branch
BranchTrueFalseTotal
$re->failures and return 0 24 24
35 and return;
36  
37 # /i: index() is case-sensitive, cannot replicate
38 # /m,/s: not relevant for a simple literal match
39 # /x: changes whitespace/comment meaning, skip for simplicity
40 24 ▶ return if $re->modifier_asserted('i');
Branch: 1 branch
BranchTrueFalseTotal
if $re->modifier_asserted("i") 1 23 24
41 23 ▶ return if $re->modifier_asserted('m');
Branch: 1 branch
BranchTrueFalseTotal
if $re->modifier_asserted("m") 1 22 23
42 22 ▶ return if $re->modifier_asserted('s');
Branch: 1 branch
BranchTrueFalseTotal
if $re->modifier_asserted("s") 1 21 22
43 21 ▶ return if $re->modifier_asserted('x');
Branch: 1 branch
BranchTrueFalseTotal
if $re->modifier_asserted("x") 1 20 21
44  
45 20 ▶ my $qr = $re->regular_expression()
Branch: 1 branch
BranchTrueFalseTotal
my $qr = $re->regular_expression or return 20 0 20
46 or return;
47  
48 # If every significant token in the regex is a Literal,
49 # then the regex is just a substring match replaceable with index().
50 20 my $has_literal = 0;
51  
52 20 for my $token ( map { $_->tokens() } $qr->children() ) {
53 66 ▶ $token->significant() or next;
Branch: 1 branch
BranchTrueFalseTotal
$token->significant or next 66 0 66
54  
55 66 ▶ if ( $token->isa('PPIx::Regexp::Token::Literal') ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($token->isa('PPIx::Regexp::Token::Literal')) { } 54 12 66
56 54 $has_literal = 1;
57 }
58 else {
59 12 return;
60 }
61 }
62  
63 8 ▶ if ($has_literal) {
Branch: 1 branch
BranchTrueFalseTotal
if ($has_literal) 7 1 8
64 7 return $self->violation( $DESC, $EXPL, $elem );
65 }
66  
67 1 return;
68 }
69  
70 2 1;
71  
72 #-----------------------------------------------------------------------------
73  
74 __END__
↑ Summary