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

filter: Statementstmt 99.0% 106 / 107 Branchbran 86.3% 57 / 66 Conditioncond 71.1% 42 / 59 MC/DCmcdc 52.7% 19 / 36 Subroutinesub 95.8% 23 / 24 Podpod 100% 5 / 5 totaltotal 84.8% 252 / 297 CC 54 SCAR 19.7
CC
54
Cov
88%
violates
13.1
_is_everything_before
12.9
_excluded_ordinal
9.1
CRAP 7.2
↑ Summary
1 package Perl::Critic::Policy::RegularExpressions::ProhibitRegexToSplitAtDelimiter 0.001;
2  
3 # ABSTRACT: Find the first literal separator with index and substr, not a regex.
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 };
Subroutine BEGIN: called CC=1 SCAR=0.0
13 2 ▶use Readonly;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 2 ▶use Perl::Critic::Utils qw{ :severities };
Subroutine BEGIN: called CC=1 SCAR=0.0
16 2 ▶use parent qw{Perl::Critic::Policy};
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18  
19 2 Readonly::Scalar my $DESC => q{Regex used to find the first literal separator};
20  
21 # The replacement for each form, by whether it substitutes in place or in a
22 # copy, and then whether it captures.
23 2 Readonly::Hash my %EXPL => (
24 match => {
25 capture => q{Find the separator with index(), then take the text before it with substr()},
26 none => q{Ask whether the separator is there with index() >= 0},
27 },
28 substitute => {
29 capture => q{Find the separator with index(), then take the text and the separator off with four-argument substr() and an empty replacement},
30 none => q{Find the separator with index(), then take everything up to and including it off with four-argument substr() and an empty replacement},
31 },
32 copy => {
33 capture => q{Find the separator with index(), then take the text before it and the text after it with substr()},
34 none => q{Find the separator with index(), then take the text after it with substr()},
35 },
36 );
37  
38 2 Readonly::Scalar my $NEWLINE => 10;
39  
40 # Modifiers that change what the pattern does in a way index cannot follow:
41 # /i compares without case, /g iterates. /m is handled at the anchor, since
42 # it matters only to ^, and /s at the dot.
43 2 Readonly::Array my @EXEMPTING_MODIFIERS => qw{ i g };
44  
45 # The quantifiers that still mean "all of it, up to the separator".
46 2 Readonly::Hash my %QUANTIFIER => map { $_ => 1 } qw{ * + };
47  
48 # The controls that quote what is between them, which a separator may sit in.
49 2 Readonly::Hash my %QUOTING => map { $_ => 1 } ( '\Q', '\E' );
50  
51  
52 2 ▶sub supported_parameters { return () }
Subroutine supported_parameters: called CC=1 SCAR=0.0
Pod documented
53 54 ▶sub default_severity { return $SEVERITY_MEDIUM }
Subroutine default_severity: called CC=1 SCAR=0.0
Pod documented
54 0 ▶sub default_themes { return qw{ performance maintenance } }
Subroutine default_themes: not called CC=1 SCAR=6.9
Pod documented
55 107 ▶sub applies_to { return qw{ PPI::Token::Regexp::Match PPI::Token::Regexp::Substitute } }
Subroutine applies_to: called CC=1 SCAR=0.0
Pod documented
56  
57  
58 104 ▶sub violates {
Subroutine violates: called CC=13 SCAR=25.7
Pod documented
59 104 my ( $self, $elem, $doc ) = @_;
60  
61 104 my $substitute = $elem->isa('PPI::Token::Regexp::Substitute');
62 104 ▶ return if $substitute && $elem->get_substitute_string ne q{};
Branch: 1 branch
BranchTrueFalseTotal
if $substitute and $elem->get_substitute_string ne "" 2 102 104
Condition100%3/3
$substitute and $elem->get_substitute_string ne ""
!ll&&!rl&&r
55472
MC/DC100%2/2
$substitute and $elem->get_substitute_string ne ""
$substitute $elem->get_substitute_string ne ""
Truth table100%3/3
A and B
A= $substituteB= $elem->get_substitute_string ne ""
ABresult
0X0
100
111
63  
64 # From the document rather than PPIx::Regexp->new, so that the modifiers of
65 # a use re in scope are asserted as well as the ones written on the regex.
66 102 ▶ my $regexp = $doc->ppix_regexp_from_element($elem) or return;
Branch: 1 branch
BranchTrueFalseTotal
my $regexp = $doc->ppix_regexp_from_element($elem) or return 102 0 102
67 102 ▶ return if any { $regexp->modifier_asserted($_) } @EXEMPTING_MODIFIERS;
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
if &any(sub { $regexp->modifier_asserted($_); } , @EXEMPTING_MODIFIERS) 6 96 102
68  
69 96 ▶ my $captures = $regexp->max_capture_number // 0;
Condition50%1/2
$regexp->max_capture_number // 0
l!l
960
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
70  
71 96 ▶ my $re = $regexp->regular_expression or return;
Branch: 1 branch
BranchTrueFalseTotal
my $re = $regexp->regular_expression or return 96 0 96
72 96 my @parts = _significant( $re->children );
73 96 ▶ return unless _is_start( shift @parts );
Branch: 1 branch
BranchTrueFalseTotal
unless _is_start(shift @parts) 6 90 96
74 90 ▶ return unless $captures ? _captures_before_a_separator( \@parts ) : _runs_to_a_separator( \@parts );
Branch: 2 branches
BranchTrueFalseTotal
unless $captures ? _captures_before_a_separator(\@parts) : _runs_to_a_separator(\@parts) 36 54 90
$captures ? : 58 32 90
75  
76 54 ▶ my $form = !$substitute ? 'match' : $regexp->modifier_asserted('r') ? 'copy' : 'substitute';
Branch: 2 branches
BranchTrueFalseTotal
!$substitute ? : 28 26 54
$regexp->modifier_asserted('r') ? : 4 22 26
77 54 ▶ my $expl = $EXPL{$form}{ $captures ? 'capture' : 'none' };
Branch: 1 branch
BranchTrueFalseTotal
$captures ? : 35 19 54
78 54 return $self->violation( $DESC, $expl, $elem );
79 }
80  
81 # Whether @$parts, after the anchor, is one capture of everything before the
82 # separator, and the separator, with nothing after it.
83 58 ▶sub _captures_before_a_separator {
Subroutine _captures_before_a_separator: called CC=4 SCAR=14.5
84 58 my ($parts) = @_;
85  
86 58 my ( $capture, @separator ) = @$parts;
87 58 ▶ return unless $capture && $capture->isa('PPIx::Regexp::Structure::Capture');
Branch: 1 branch
BranchTrueFalseTotal
unless $capture and $capture->isa("PPIx::Regexp::Structure::Capture") 0 58 58
Condition33%1/3
$capture and $capture->isa("PPIx::Regexp::Structure::Capture")
!ll&&!rl&&r
0058
MC/DC0%0/2
$capture and $capture->isa("PPIx::Regexp::Structure::Capture")
$capture $capture->isa("PPIx::Regexp::Structure::Capture")
Truth table33%1/3
A and B
A= $captureB= $capture->isa("PPIx::Regexp::Structure::Capture")
ABresult
0X0
100
111
88  
89 58 ▶ my @ordinals = _literal_ordinals(@separator) or return;
Branch: 1 branch
BranchTrueFalseTotal
my(@ordinals) = _literal_ordinals(@separator) or return 46 12 58
90 46 return _is_everything_before( [ _significant( $capture->children ) ], \@ordinals );
91 }
92  
93 # Whether @$parts, after the anchor, is the same run of everything before the
94 # separator with no capture around it, and the separator.
95 32 ▶sub _runs_to_a_separator {
Subroutine _runs_to_a_separator: called CC=4 SCAR=13.9
96 32 my ($parts) = @_;
97  
98 32 my ( $atom, $quantifier, @separator ) = @$parts;
99 32 my @run = ( $atom, $quantifier );
100 32 ▶ push @run, shift @separator if @separator && $separator[0]->isa('PPIx::Regexp::Token::Greediness');
Branch: 1 branch
BranchTrueFalseTotal
if @separator and $separator[0]->isa("PPIx::Regexp::Token::Greediness") 9 23 32
Condition100%3/3
@separator and $separator[0]->isa("PPIx::Regexp::Token::Greediness")
!ll&&!rl&&r
3209
MC/DC100%2/2
@separator and $separator[0]->isa("PPIx::Regexp::Token::Greediness")
@separator $separator[0]->isa("PPIx::Regexp::Token::Greediness")
Truth table100%3/3
A and B
A= @separatorB= $separator[0]->isa("PPIx::Regexp::Token::Greediness")
ABresult
0X0
100
111
101  
102 32 ▶ my @ordinals = _literal_ordinals(@separator) or return;
Branch: 1 branch
BranchTrueFalseTotal
my(@ordinals) = _literal_ordinals(@separator) or return 23 9 32
103 23 return _is_everything_before( \@run, \@ordinals );
104 }
105  
106 # Whether $part anchors the start of the string. Under /m, ^ anchors any line.
107 96 ▶sub _is_start {
Subroutine _is_start: called CC=5 SCAR=16.2
108 96 my ($part) = @_;
109  
110 96 ▶ return unless $part && $part->isa('PPIx::Regexp::Token::Assertion');
Branch: 1 branch
BranchTrueFalseTotal
unless $part and $part->isa("PPIx::Regexp::Token::Assertion") 3 93 96
Condition66%2/3
$part and $part->isa("PPIx::Regexp::Token::Assertion")
!ll&&!rl&&r
0393
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
111 93 my $anchor = $part->content;
112 93 ▶ return 1 if $anchor eq '\A';
Branch: 1 branch
BranchTrueFalseTotal
if $anchor eq "\\A" 88 5 93
113 5 ▶ return $anchor eq '^' && !$part->modifier_asserted('m');
Condition66%2/3
$anchor eq '^' && !$part->modifier_asserted('m')
!ll&&!rl&&r
032
MC/DC50%1/2
$anchor eq '^' && !$part->modifier_asserted('m')
$anchor eq '^' !$part->modifier_asserted('m')
Truth table66%2/3
A && not(B)
A= $anchor eq '^'B= !$part->modifier_asserted('m')
ABresult
0X0
100
111
114 }
115  
116 # The characters @parts match, in order, if each part is one literal character
117 # or a class of one, between an optional \Q and \E; otherwise nothing.
118 90 ▶sub _literal_ordinals {
Subroutine _literal_ordinals: called CC=4 SCAR=13.9
119 90 my (@separator) = @_;
120  
121 90 ▶ my @parts = grep { !( $_->isa('PPIx::Regexp::Token::Control') && $QUOTING{ $_->content } ) } @separator;
Condition66%2/3
$_->isa('PPIx::Regexp::Token::Control') && $QUOTING{$_->content}
!ll&&!rl&&r
10504
MC/DC50%1/2
$_->isa('PPIx::Regexp::Token::Control') && $QUOTING{$_->content}
$_->isa('PPIx::Regexp::Token::Control') $QUOTING{$_->content}
Truth table66%2/3
A && B
A= $_->isa('PPIx::Regexp::Token::Control')B= $QUOTING{$_->content}
ABresult
0X0
100
111
122  
123 90 my @ordinals = map { _ordinal($_) } @parts;
124 90 ▶ return unless @ordinals && @ordinals == @parts;
Branch: 1 branch
BranchTrueFalseTotal
unless @ordinals and @ordinals == @parts 21 69 90
Condition100%3/3
@ordinals and @ordinals == @parts
!ll&&!rl&&r
14769
MC/DC100%2/2
@ordinals and @ordinals == @parts
@ordinals @ordinals == @parts
Truth table100%3/3
A and B
A= @ordinalsB= @ordinals == @parts
ABresult
0X0
100
111
125 69 return @ordinals;
126 }
127  
128 # The character $part matches, if it matches one character and only that one.
129 105 ▶sub _ordinal {
Subroutine _ordinal: called CC=6 SCAR=18.2
130 105 my ($part) = @_;
131  
132 105 ▶ if ( $part->isa('PPIx::Regexp::Structure::CharClass') ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($part->isa("PPIx::Regexp::Structure::CharClass")) 4 101 105
133 4 ▶ return if $part->negated;
Branch: 1 branch
BranchTrueFalseTotal
if $part->negated 0 4 4
134 4 ▶ $part = _sole_member($part) or return;
Branch: 1 branch
BranchTrueFalseTotal
$part = _sole_member($part) or return 2 2 4
135 }
136 103 ▶ return unless $part->isa('PPIx::Regexp::Token::Literal');
Branch: 1 branch
BranchTrueFalseTotal
unless $part->isa("PPIx::Regexp::Token::Literal") 18 85 103
137 85 ▶ return $part->ordinal // ();
Condition33%1/3
$part->ordinal // ()
l!l&&r!l&&!r
8500
MC/DC0%0/2
$part->ordinal // ()
$part->ordinal ()
Truth table33%1/3
A // B
A= $part->ordinalB= ()
ABresult
1X1
011
000
138 }
139  
140 # The one member of the character class $class, if it has exactly one.
141 45 ▶sub _sole_member {
Subroutine _sole_member: called CC=2 SCAR=6.9
142 45 my ($class) = @_;
143  
144 45 my @members = $class->children;
145 45 ▶ return unless @members == 1;
Branch: 1 branch
BranchTrueFalseTotal
unless @members == 1 3 42 45
146 42 return $members[0];
147 }
148  
149 # Whether @$body, the inside of the capture, takes everything up to the first
150 # occurrence of the separator in @$ordinals. Either it is a run of anything but
151 # the separator's one character, with any greediness, or it is a lazy run of
152 # anything at all, which stops at the first occurrence of any separator.
153 69 ▶sub _is_everything_before {
Subroutine _is_everything_before: called CC=12 SCAR=25.6
154 69 my ( $body, $ordinals ) = @_;
155  
156 69 my ( $atom, $quantifier, $greediness, @rest ) = @$body;
157 69 ▶ return if @rest;
Branch: 1 branch
BranchTrueFalseTotal
if @rest 0 69 69
158 69 ▶ return unless $atom && $quantifier && $quantifier->isa('PPIx::Regexp::Token::Quantifier') && $QUANTIFIER{ $quantifier->content };
Branch: 1 branch
BranchTrueFalseTotal
unless $atom and $quantifier and $quantifier->isa("PPIx::Regexp::Token::Quantifier") and $QUANTIFIER{$quantifier->content} 3 66 69
Condition77%7/9
$atom and $quantifier and $quantifier->isa("PPIx::Regexp::Token::Quantifier") and $QUANTIFIER{$quantifier->content}
!ll&&!rl&&r
3066
$atom and $quantifier and $quantifier->isa("PPIx::Regexp::Token::Quantifier")
!ll&&!rl&&r
1266
$atom and $quantifier
!ll&&!rl&&r
0168
MC/DC50%2/4
$atom and $quantifier and $quantifier->isa("PPIx::Regexp::Token::Quantifier") and $QUANTIFIER{$quantifier->content}
$atom $quantifier $quantifier->isa("PPIx::Regexp::Token::Quantifier") $QUANTIFIER{$quantifier->content}
Truth table60%3/5
A and B and C and D
A= $atomB= $quantifierC= $quantifier->isa("PPIx::Regexp::Token::Quantifier")D= $QUANTIFIER{$quantifier->content}
ABCDresult
0XXX0
10XX0
110X0
11100
11111
159 66 ▶ return if $greediness && !$greediness->isa('PPIx::Regexp::Token::Greediness');
Branch: 1 branch
BranchTrueFalseTotal
if $greediness and not $greediness->isa("PPIx::Regexp::Token::Greediness") 0 66 66
Condition66%2/3
$greediness and not $greediness->isa("PPIx::Regexp::Token::Greediness")
!ll&&!rl&&r
47190
MC/DC0%0/2
$greediness and not $greediness->isa("PPIx::Regexp::Token::Greediness")
$greediness not $greediness->isa("PPIx::Regexp::Token::Greediness")
Truth table66%2/3
A and not(B)
A= $greedinessB= not $greediness->isa("PPIx::Regexp::Token::Greediness")
ABresult
0X0
100
111
160  
161 66 my $excluded = _excluded_ordinal($atom);
162 66 ▶ return @$ordinals == 1 && $ordinals->[0] == $excluded if defined $excluded;
Branch: 1 branch
BranchTrueFalseTotal
if defined $excluded 49 17 66
Condition100%3/3
@$ordinals == 1 && $ordinals->[0] == $excluded
!ll&&!rl&&r
1543
MC/DC100%2/2
@$ordinals == 1 && $ordinals->[0] == $excluded
@$ordinals == 1 $ordinals->[0] == $excluded
Truth table100%3/3
A && B
A= @$ordinals == 1B= $ordinals->[0] == $excluded
ABresult
0X0
100
111
163  
164 17 ▶ my $lazy = $greediness && $greediness->content eq '?';
Condition66%2/3
$greediness && $greediness->content eq '?'
!ll&&!rl&&r
6011
MC/DC50%1/2
$greediness && $greediness->content eq '?'
$greediness $greediness->content eq '?'
Truth table66%2/3
A && B
A= $greedinessB= $greediness->content eq '?'
ABresult
0X0
100
111
165 17 ▶ return $lazy && _is_anything($atom);
Condition66%2/3
$lazy && _is_anything($atom)
!ll&&!rl&&r
6011
MC/DC50%1/2
$lazy && _is_anything($atom)
$lazy _is_anything($atom)
Truth table66%2/3
A && B
A= $lazyB= _is_anything($atom)
ABresult
0X0
100
111
166 }
167  
168 # The one character $atom refuses, if it matches every character but one: a
169 # negated class of one, \N, or a dot without /s.
170 66 ▶sub _excluded_ordinal {
Subroutine _excluded_ordinal: called CC=9 SCAR=22.1
171 66 my ($atom) = @_;
172  
173 66 ▶ if ( $atom->isa('PPIx::Regexp::Structure::CharClass') ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($atom->isa("PPIx::Regexp::Structure::CharClass")) 41 25 66
174 41 ▶ return unless $atom->negated;
Branch: 1 branch
BranchTrueFalseTotal
unless $atom->negated 0 41 41
175 41 ▶ my $member = _sole_member($atom) or return;
Branch: 1 branch
BranchTrueFalseTotal
my $member = _sole_member($atom) or return 40 1 41
176 40 ▶ return unless $member->isa('PPIx::Regexp::Token::Literal');
Branch: 1 branch
BranchTrueFalseTotal
unless $member->isa("PPIx::Regexp::Token::Literal") 0 40 40
177 40 return $member->ordinal;
178 }
179 25 ▶ return unless $atom->isa('PPIx::Regexp::Token::CharClass::Simple');
Branch: 1 branch
BranchTrueFalseTotal
unless $atom->isa("PPIx::Regexp::Token::CharClass::Simple") 0 25 25
180 25 ▶ return $NEWLINE if $atom->content eq '\N';
Branch: 1 branch
BranchTrueFalseTotal
if $atom->content eq "\\N" 3 22 25
181 22 ▶ return $NEWLINE if $atom->content eq '.' && !$atom->modifier_asserted('s');
Branch: 1 branch
BranchTrueFalseTotal
if $atom->content eq "." and not $atom->modifier_asserted("s") 6 16 22
Condition100%3/3
$atom->content eq "." and not $atom->modifier_asserted("s")
!ll&&!rl&&r
2146
MC/DC100%2/2
$atom->content eq "." and not $atom->modifier_asserted("s")
$atom->content eq "." not $atom->modifier_asserted("s")
Truth table100%3/3
A and not(B)
A= $atom->content eq "."B= not $atom->modifier_asserted("s")
ABresult
0X0
100
111
182 16 return;
183 }
184  
185 # Whether $atom matches every character: a dot under /s.
186 11 ▶sub _is_anything {
Subroutine _is_anything: called CC=3 SCAR=14.2
187 11 my ($atom) = @_;
188  
189 11 ▶ return $atom->isa('PPIx::Regexp::Token::CharClass::Simple') && $atom->content eq '.' && $atom->modifier_asserted('s');
Condition33%2/6
$atom->isa('PPIx::Regexp::Token::CharClass::Simple') && $atom->content eq '.' && $atom->modifier_asserted('s')
!ll&&!rl&&r
0011
$atom->isa("PPIx::Regexp::Token::CharClass::Simple") and $atom->content eq "."
!ll&&!rl&&r
0011
MC/DC0%0/3
$atom->isa('PPIx::Regexp::Token::CharClass::Simple') && $atom->content eq '.' && $atom->modifier_asserted('s')
$atom->isa("PPIx::Regexp::Token::CharClass::Simple") $atom->content eq "." $atom->modifier_asserted('s')
Truth table25%1/4
A and B && C
A= $atom->isa("PPIx::Regexp::Token::CharClass::Simple")B= $atom->content eq "."C= $atom->modifier_asserted('s')
ABCresult
0XX0
10X0
1100
1111
190 }
191  
192 # The parts of a pattern that match something: whitespace and comments under
193 # /x do not.
194 142 ▶sub _significant {
Subroutine _significant: called CC=2 SCAR=6.9
195 142 my (@parts) = @_;
196  
197 142 ▶ return grep { !$_->isa('PPIx::Regexp::Token::Whitespace') && !$_->isa('PPIx::Regexp::Token::Comment') } @parts;
Condition100%3/3
$_->isa('PPIx::Regexp::Token::Whitespace') || $_->isa('PPIx::Regexp::Token::Comment')
l!l&&r!l&&!r
141449
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
198 }
199  
200 2 1;
201  
202 __END__
↑ Summary