« 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
| |||||||||||||||||||||||||||||
| 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
Condition0%0/3
$front_anchor eq "^" or $end_anchor eq "\$"
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 "\$"
| |||||||||||||||||||||||||||||
| 46 | 0 | ▶ | my $regexp = $doc->ppix_regexp_from_element( $elem ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 47 | or return; | ||||||||||||||||||||||||||||
| 48 | 0 | ▶ | return if $regexp->modifier_asserted( 'm' ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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__ | ||||||||||||||||||||||||||||