| 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
| |||||||||||
| 33 | or return; | ||||||||||
| 34 | 24 | ▶ | $re->failures() | ||||||||
|
Branch: 1 branch
| |||||||||||
| 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
| |||||||||||
| 41 | 23 | ▶ | return if $re->modifier_asserted('m'); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 42 | 22 | ▶ | return if $re->modifier_asserted('s'); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 43 | 21 | ▶ | return if $re->modifier_asserted('x'); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 44 | |||||||||||
| 45 | 20 | ▶ | my $qr = $re->regular_expression() | ||||||||
|
Branch: 1 branch
| |||||||||||
| 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
| |||||||||||
| 54 | |||||||||||
| 55 | 66 | ▶ | if ( $token->isa('PPIx::Regexp::Token::Literal') ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 56 | 54 | $has_literal = 1; | |||||||||
| 57 | } | ||||||||||
| 58 | else { | ||||||||||
| 59 | 12 | return; | |||||||||
| 60 | } | ||||||||||
| 61 | } | ||||||||||
| 62 | |||||||||||
| 63 | 8 | ▶ | if ($has_literal) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 64 | 7 | return $self->violation( $DESC, $EXPL, $elem ); | |||||||||
| 65 | } | ||||||||||
| 66 | |||||||||||
| 67 | 1 | return; | |||||||||
| 68 | } | ||||||||||
| 69 | |||||||||||
| 70 | 2 | 1; | |||||||||
| 71 | |||||||||||
| 72 | #----------------------------------------------------------------------------- | ||||||||||
| 73 | |||||||||||
| 74 | __END__ | ||||||||||