| 1 | package Perl::Critic::Policy::RegularExpressions::ProhibitRegexToStripAffix 0.001; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||||
| 3 | # ABSTRACT: Take a known prefix or suffix off a string without a regex capture. | ||||||||||||||||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||||||||||||||||
| 5 | 2 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 6 | 2 | ▶ | use warnings FATAL => 'all'; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||||||||||||||||
| 8 | 2 | ▶ | use 5.014; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||||||||||||||||
| 10 | 2 | ▶ | use re '/aa'; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||||||||||||||||
| 12 | 2 | ▶ | use List::Util qw{ any all }; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 13 | 2 | ▶ | use Readonly; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 14 | 2 | ▶ | use PPIx::Regexp; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||||||||||||||||
| 16 | 2 | ▶ | use Perl::Critic::Utils qw{ :severities }; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 17 | 2 | ▶ | use parent qw{Perl::Critic::Policy}; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||||||||||||||||
| 20 | 2 | Readonly::Scalar my $DESC => q{Regex used to strip a literal prefix or suffix}; | |||||||||||||||||||||||||||||||||||||||||
| 21 | 2 | Readonly::Scalar my $EXPL => q{Check the literal with substr() or index(), and take the rest with substr()}; | |||||||||||||||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||||||||||||||||
| 23 | # Modifiers that change what the pattern means in a way substr cannot follow: | ||||||||||||||||||||||||||||||||||||||||||
| 24 | # /i compares without case, /g iterates. /m is handled beside the anchors, | ||||||||||||||||||||||||||||||||||||||||||
| 25 | # since it matters only to ^ and $. | ||||||||||||||||||||||||||||||||||||||||||
| 26 | 2 | Readonly::Array my @EXEMPTING_MODIFIERS => qw{ i g }; | |||||||||||||||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||||||||||||||||
| 28 | 2 | Readonly::Hash my %START => map { $_ => 1 } ( '\A', '^' ); | |||||||||||||||||||||||||||||||||||||||||
| 29 | 2 | Readonly::Hash my %END => map { $_ => 1 } ( '\z', '\Z', '$' ); | |||||||||||||||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||||||||||||||||
| 31 | # A capture that runs to the end of the string it is anchored away from, so | ||||||||||||||||||||||||||||||||||||||||||
| 32 | # that end needs no anchor of its own. | ||||||||||||||||||||||||||||||||||||||||||
| 33 | 2 | Readonly::Hash my %TO_THE_END => map { $_ => 1 } ( '(.*)', '(.+)' ); | |||||||||||||||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||||||||||||||||
| 36 | 2 | ▶ | sub supported_parameters { return () } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
supported_parameters: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 37 | 16 | ▶ | sub default_severity { return $SEVERITY_MEDIUM } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
default_severity: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 38 | 0 | ▶ | sub default_themes { return qw{ performance } } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
default_themes: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 39 | 33 | ▶ | sub applies_to { return 'PPI::Token::Regexp::Match' } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
applies_to: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||||||||||||||||
| 42 | 31 | ▶ | sub violates { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
violates: called CC=8 SCAR=20.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 43 | 31 | my ( $self, $elem, undef ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||||||||||||||||
| 45 | 31 | my %modifiers = $elem->get_modifiers; | |||||||||||||||||||||||||||||||||||||||||
| 46 | 31 | ▶ | return if any { $modifiers{$_} } @EXEMPTING_MODIFIERS; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||||||||||||||||
| 48 | 29 | ▶ | my $regexp = PPIx::Regexp->new( $elem->content ) or return; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 49 | 29 | ▶ | return unless ( $regexp->max_capture_number // 0 ) == 1; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%1/2
$regexp->max_capture_number // 0
MC/DC0%0/1
$regexp->max_capture_number // 0
$regexp->max_capture_number
Truth table50%1/2
A
A= $regexp->max_capture_number
| |||||||||||||||||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||||||||||||||||
| 51 | 26 | ▶ | my $re = $regexp->regular_expression or return; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 52 | my @parts = | ||||||||||||||||||||||||||||||||||||||||||
| 53 | 26 | ▶ | grep { !$_->isa('PPIx::Regexp::Token::Whitespace') && !$_->isa('PPIx::Regexp::Token::Comment') } $re->children; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%3/3
$_->isa('PPIx::Regexp::Token::Whitespace') || $_->isa('PPIx::Regexp::Token::Comment')
MC/DC100%2/2
$_->isa('PPIx::Regexp::Token::Whitespace') || $_->isa('PPIx::Regexp::Token::Comment')
$_->isa('PPIx::Regexp::Token::Whitespace')
$_->isa('PPIx::Regexp::Token::Comment')
Truth table100%3/3
A || B
A= $_->isa('PPIx::Regexp::Token::Whitespace')B= $_->isa('PPIx::Regexp::Token::Comment')
| |||||||||||||||||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||||||||||||||||
| 55 | 26 | ▶ | return unless _strips_an_affix( \@parts, $modifiers{m} ); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 56 | 16 | return $self->violation( $DESC, $EXPL, $elem ); | |||||||||||||||||||||||||||||||||||||||||
| 57 | } | ||||||||||||||||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||||||||||||||||
| 59 | # Whether @$parts is an anchor, a run of literals and one capture, in either | ||||||||||||||||||||||||||||||||||||||||||
| 60 | # order, with the capture's own end anchored too unless it runs there anyway. | ||||||||||||||||||||||||||||||||||||||||||
| 61 | 26 | ▶ | sub _strips_an_affix { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_strips_an_affix: called CC=17 SCAR=28.4
| |||||||||||||||||||||||||||||||||||||||||||
| 62 | 26 | my ( $parts, $multiline ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||||||||||||||||
| 64 | 26 | my @parts = @$parts; | |||||||||||||||||||||||||||||||||||||||||
| 65 | 26 | ▶ | my $start = _anchor( $parts[0], \%START, $multiline ) ? shift @parts : undef; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 66 | 26 | ▶ | my $end = @parts && _anchor( $parts[-1], \%END, $multiline ) ? pop @parts : undef; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
@parts and _anchor($parts[-1], \%END, $multiline)
MC/DC50%1/2
@parts and _anchor($parts[-1], \%END, $multiline)
@parts
_anchor($parts[-1], \%END, $multiline)
Truth table66%2/3
A and B
A= @partsB= _anchor($parts[-1], \%END, $multiline)
| |||||||||||||||||||||||||||||||||||||||||||
| 67 | 26 | ▶ | return unless $start || $end; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$start or $end
MC/DC100%2/2
$start or $end
$start
$end
Truth table100%3/3
A or B
A= $startB= $end
| |||||||||||||||||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||||||||||||||||
| 69 | 24 | my ( $capture, $literals ); | |||||||||||||||||||||||||||||||||||||||||
| 70 | 24 | ▶ | if ( @parts && $parts[0]->isa('PPIx::Regexp::Structure::Capture') ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition66%2/3
@parts and $parts[0]->isa("PPIx::Regexp::Structure::Capture")
MC/DC50%1/2
@parts and $parts[0]->isa("PPIx::Regexp::Structure::Capture")
@parts
$parts[0]->isa("PPIx::Regexp::Structure::Capture")
Truth table66%2/3
A and B
A= @partsB= $parts[0]->isa("PPIx::Regexp::Structure::Capture")
| |||||||||||||||||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||||||||||||||||
| 72 | # A suffix: the capture comes first, and has to reach the start. | ||||||||||||||||||||||||||||||||||||||||||
| 73 | 18 | $capture = shift @parts; | |||||||||||||||||||||||||||||||||||||||||
| 74 | 18 | $literals = \@parts; | |||||||||||||||||||||||||||||||||||||||||
| 75 | 18 | ▶ | return unless $end && ( $start || $TO_THE_END{ $capture->content } ); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition83%5/6
$end and $start || $TO_THE_END{$capture->content}
$start or $TO_THE_END{$capture->content}
MC/DC66%2/3
$end and $start || $TO_THE_END{$capture->content}
$end
$start
$TO_THE_END{$capture->content}
Truth table75%3/4
A and B or C
A= $endB= $startC= $TO_THE_END{$capture->content}
| |||||||||||||||||||||||||||||||||||||||||||
| 76 | } | ||||||||||||||||||||||||||||||||||||||||||
| 77 | ▶ | elsif ( @parts && $parts[-1]->isa('PPIx::Regexp::Structure::Capture') ) { | |||||||||||||||||||||||||||||||||||||||||
|
Condition66%2/3
@parts and $parts[-1]->isa("PPIx::Regexp::Structure::Capture")
MC/DC50%1/2
@parts and $parts[-1]->isa("PPIx::Regexp::Structure::Capture")
@parts
$parts[-1]->isa("PPIx::Regexp::Structure::Capture")
Truth table66%2/3
A and B
A= @partsB= $parts[-1]->isa("PPIx::Regexp::Structure::Capture")
| |||||||||||||||||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||||||||||||||||
| 79 | # A prefix: the capture comes last, and has to reach the end. | ||||||||||||||||||||||||||||||||||||||||||
| 80 | 5 | $capture = pop @parts; | |||||||||||||||||||||||||||||||||||||||||
| 81 | 5 | $literals = \@parts; | |||||||||||||||||||||||||||||||||||||||||
| 82 | 5 | ▶ | return unless $start && ( $end || $TO_THE_END{ $capture->content } ); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition83%5/6
$start and $end || $TO_THE_END{$capture->content}
$end or $TO_THE_END{$capture->content}
MC/DC66%2/3
$start and $end || $TO_THE_END{$capture->content}
$start
$end
$TO_THE_END{$capture->content}
Truth table75%3/4
A and B or C
A= $startB= $endC= $TO_THE_END{$capture->content}
| |||||||||||||||||||||||||||||||||||||||||||
| 83 | } | ||||||||||||||||||||||||||||||||||||||||||
| 84 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 85 | 1 | return; | |||||||||||||||||||||||||||||||||||||||||
| 86 | } | ||||||||||||||||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||||||||||||||||
| 88 | 21 | ▶ | return unless @$literals; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 89 | 20 | ▶ | return all { $_->isa('PPIx::Regexp::Token::Literal') } @$literals; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||||||||||||||||
| 92 | # Whether $part is one of the anchors in %$which. Under /m, ^ and $ anchor | ||||||||||||||||||||||||||||||||||||||||||
| 93 | # lines rather than the string, so only \A, \z and \Z count. | ||||||||||||||||||||||||||||||||||||||||||
| 94 | 52 | ▶ | sub _anchor { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_anchor: called CC=6 SCAR=17.9
| |||||||||||||||||||||||||||||||||||||||||||
| 95 | 52 | my ( $part, $which, $multiline ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||||||||||||||||
| 97 | 52 | ▶ | return unless $part && $part->isa('PPIx::Regexp::Token::Assertion'); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$part and $part->isa("PPIx::Regexp::Token::Assertion")
MC/DC50%1/2
$part and $part->isa("PPIx::Regexp::Token::Assertion")
$part
$part->isa("PPIx::Regexp::Token::Assertion")
Truth table66%2/3
A and B
A= $partB= $part->isa("PPIx::Regexp::Token::Assertion")
| |||||||||||||||||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||||||||||||||||
| 99 | 44 | my $anchor = $part->content; | |||||||||||||||||||||||||||||||||||||||||
| 100 | 44 | ▶ | return if $multiline && ( $anchor eq '^' || $anchor eq '$' ); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%6/6
$multiline and $anchor eq "^" || $anchor eq "\$"
$anchor eq "^" or $anchor eq "\$"
MC/DC100%3/3
$multiline and $anchor eq "^" || $anchor eq "\$"
$multiline
$anchor eq "^"
$anchor eq "\$"
Truth table100%4/4
A and B or C
A= $multilineB= $anchor eq "^"C= $anchor eq "\$"
| |||||||||||||||||||||||||||||||||||||||||||
| 101 | 42 | return $which->{$anchor}; | |||||||||||||||||||||||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||||||||||||||||
| 104 | 2 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||||||||||||||||
| 106 | __END__ | ||||||||||||||||||||||||||||||||||||||||||