blib/lib/Perl/Critic/Policy/RegularExpressions/ProhibitRegexToStripAffix.pm

filter: Statementstmt 98.6% 72 / 73 Branchbran 93.3% 28 / 30 Conditioncond 81.5% 31 / 38 MC/DCmcdc 68.1% 15 / 22 Subroutinesub 94.4% 17 / 18 Podpod 100% 5 / 5 totaltotal 90.3% 168 / 186 CC 29 SCAR 21.9
CC
29
Cov
93%
_strips_an_affix
17.2
violates
8.1
_anchor
6.0
CRAP 8.9
↑ Summary
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
BranchTrueFalseTotal
if &any(sub { $modifiers{$_}; } , @EXEMPTING_MODIFIERS) 2 29 31
47  
48 29 ▶ my $regexp = PPIx::Regexp->new( $elem->content ) or return;
Branch: 1 branch
BranchTrueFalseTotal
my $regexp = "PPIx::Regexp"->new($elem->content) or return 29 0 29
49 29 ▶ return unless ( $regexp->max_capture_number // 0 ) == 1;
Branch: 1 branch
BranchTrueFalseTotal
unless ($regexp->max_capture_number // 0) == 1 3 26 29
Condition50%1/2
$regexp->max_capture_number // 0
l!l
290
MC/DC0%0/1
$regexp->max_capture_number // 0
$regexp->max_capture_number
Truth table50%1/2
A
A= $regexp->max_capture_number
Aresult
11
00
50  
51 26 ▶ my $re = $regexp->regular_expression or return;
Branch: 1 branch
BranchTrueFalseTotal
my $re = $regexp->regular_expression or return 26 0 26
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')
l!l&&r!l&&!r
41148
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')
ABresult
1X1
011
000
54  
55 26 ▶ return unless _strips_an_affix( \@parts, $modifiers{m} );
Branch: 1 branch
BranchTrueFalseTotal
unless _strips_an_affix(\@parts, $modifiers{"m"}) 10 16 26
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
BranchTrueFalseTotal
_anchor($parts[0], \%START, $multiline) ? : 21 5 26
66 26 ▶ my $end = @parts && _anchor( $parts[-1], \%END, $multiline ) ? pop @parts : undef;
Branch: 1 branch
BranchTrueFalseTotal
@parts && _anchor($parts[-1], \%END, $multiline) ? : 21 5 26
Condition66%2/3
@parts and _anchor($parts[-1], \%END, $multiline)
!ll&&!rl&&r
0521
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)
ABresult
0X0
100
111
67 26 ▶ return unless $start || $end;
Branch: 1 branch
BranchTrueFalseTotal
unless $start or $end 2 24 26
Condition100%3/3
$start or $end
l!l&&r!l&&!r
2132
MC/DC100%2/2
$start or $end
$start $end
Truth table100%3/3
A or B
A= $startB= $end
ABresult
1X1
011
000
68  
69 24 my ( $capture, $literals );
70 24 ▶ if ( @parts && $parts[0]->isa('PPIx::Regexp::Structure::Capture') ) {
Branch: 2 branches
BranchTrueFalseTotal
if (@parts and $parts[0]->isa('PPIx::Regexp::Structure::Capture')) { } 18 6 24
elsif (@parts and $parts[-1]->isa('PPIx::Regexp::Structure::Capture')) { } 5 1 6
Condition66%2/3
@parts and $parts[0]->isa("PPIx::Regexp::Structure::Capture")
!ll&&!rl&&r
0618
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")
ABresult
0X0
100
111
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
BranchTrueFalseTotal
unless $end and $start || $TO_THE_END{$capture->content} 1 17 18
Condition83%5/6
$end and $start || $TO_THE_END{$capture->content}
!ll&&!rl&&r
0117
$start or $TO_THE_END{$capture->content}
l!l&&r!l&&!r
1521
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}
ABCresult
0XX0
1000
11X1
1011
76 }
77 ▶ elsif ( @parts && $parts[-1]->isa('PPIx::Regexp::Structure::Capture') ) {
Condition66%2/3
@parts and $parts[-1]->isa("PPIx::Regexp::Structure::Capture")
!ll&&!rl&&r
015
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")
ABresult
0X0
100
111
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
BranchTrueFalseTotal
unless $start and $end || $TO_THE_END{$capture->content} 1 4 5
Condition83%5/6
$start and $end || $TO_THE_END{$capture->content}
!ll&&!rl&&r
014
$end or $TO_THE_END{$capture->content}
l!l&&r!l&&!r
221
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}
ABCresult
0XX0
1000
11X1
1011
83 }
84 else {
85 1 return;
86 }
87  
88 21 ▶ return unless @$literals;
Branch: 1 branch
BranchTrueFalseTotal
unless @$literals 1 20 21
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
BranchTrueFalseTotal
unless $part and $part->isa("PPIx::Regexp::Token::Assertion") 8 44 52
Condition66%2/3
$part and $part->isa("PPIx::Regexp::Token::Assertion")
!ll&&!rl&&r
0844
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")
ABresult
0X0
100
111
98  
99 44 my $anchor = $part->content;
100 44 ▶ return if $multiline && ( $anchor eq '^' || $anchor eq '$' );
Branch: 1 branch
BranchTrueFalseTotal
if $multiline and $anchor eq "^" || $anchor eq "\$" 2 42 44
Condition100%6/6
$multiline and $anchor eq "^" || $anchor eq "\$"
!ll&&!rl&&r
4022
$anchor eq "^" or $anchor eq "\$"
l!l&&r!l&&!r
112
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 "\$"
ABCresult
0XX0
1000
11X1
1011
101 42 return $which->{$anchor};
102 }
103  
104 2 1;
105  
106 __END__
↑ Summary