blib/lib/Perl/Critic/Policy/ProhibitRegexForSimpleSubstring.pm

filter: Statementstmt 98.7% 77 / 78 Branchbran 91.1% 31 / 34 Conditioncond 76.6% 23 / 30 MC/DCmcdc 65.0% 13 / 20 Subroutinesub 94.1% 16 / 17 Podpod 100% 6 / 6 totaltotal 89.7% 166 / 185 CC 31 SCAR 17.6
CC
31
Cov
92%
_call_taking_it_first
10.2
_is_literal_text
8.1
_allowed
7.1
CRAP 5.8
↑ Summary
1 package Perl::Critic::Policy::ProhibitRegexForSimpleSubstring;
2 2 $Perl::Critic::Policy::ProhibitRegexForSimpleSubstring::VERSION = '1.001';
3 # ABSTRACT: Use index() to look for literal text, and leave split its pattern.
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 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 Readonly;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 2 ▶use Perl::Critic::Utils qw{ :severities :classification };
Subroutine BEGIN: called CC=1 SCAR=0.0
15 2 ▶use parent qw{Perl::Critic::Policy};
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17  
18 2 Readonly::Scalar my $DESC => q{Regex used for a simple substring match};
19 2 Readonly::Scalar my $EXPL => q{Use index() instead of a regex when looking for literal text};
20  
21 2 Readonly::Array my @DEFAULT_ALLOW => qw{ split };
22  
23 # The one modifier that changes what a pattern of literals matches: index() is
24 # case sensitive. See MODIFIERS in the POD for why /m, /s and /x are not here.
25 2 Readonly::Scalar my $EXEMPTING_MODIFIER => 'i';
26  
27 # Operators that sit between arguments rather than inside one. The
28 # low-precedence ones end a list operator's arguments: split m/,/, $s or die.
29 2 Readonly::Hash my %SEPARATOR => map { $_ => 1 } ( q{,}, q{=>}, qw{ or and xor } );
30  
31  
32 2 ▶sub supported_parameters {
Subroutine supported_parameters: called CC=1 SCAR=0.0
Pod documented
33 return (
34 {
35 2 name => 'allow',
36 description => 'Functions and methods whose first argument may be a literal regex, in addition to split.',
37 default_string => join( q{ }, @DEFAULT_ALLOW ),
38 behavior => 'string list',
39 }
40 );
41 }
42  
43  
44 2 ▶sub initialize_if_enabled {
Subroutine initialize_if_enabled: called CC=2 SCAR=6.9
Pod documented
45 2 my ( $self, $config ) = @_;
46  
47 # 'string list' hands us the configured value in place of the default, and
48 # somebody naming one function of their own did not mean to start reporting
49 # split.
50 2 $self->{_allow}{$_} = 1 for @DEFAULT_ALLOW;
51  
52 2 return $self->SUPER::initialize_if_enabled($config);
53 }
54  
55  
56 34 ▶sub default_severity { return $SEVERITY_MEDIUM }
Subroutine default_severity: called CC=1 SCAR=0.0
Pod documented
57 0 ▶sub default_themes { return qw{ performance } }
Subroutine default_themes: not called CC=1 SCAR=6.9
Pod documented
58 70 ▶sub applies_to { return 'PPI::Token::Regexp::Match' }
Subroutine applies_to: called CC=1 SCAR=0.0
Pod documented
59  
60  
61 69 ▶sub violates {
Subroutine violates: called CC=4 SCAR=13.9
Pod documented
62 69 my ( $self, $elem, $doc ) = @_;
63  
64 69 ▶ return if !_is_literal_text( $elem, $doc );
Branch: 1 branch
BranchTrueFalseTotal
unless _is_literal_text($elem, $doc) 12 57 69
65  
66 57 my $call = _call_taking_it_first($elem);
67 57 ▶ return if $call && $self->_allowed($call);
Branch: 1 branch
BranchTrueFalseTotal
if $call and $self->_allowed($call) 23 34 57
Condition100%3/3
$call and $self->_allowed($call)
!ll&&!rl&&r
27723
MC/DC100%2/2
$call and $self->_allowed($call)
$call $self->_allowed($call)
Truth table100%3/3
A and B
A= $callB= $self->_allowed($call)
ABresult
0X0
100
111
68  
69 34 return $self->violation( $DESC, $EXPL, $elem );
70 }
71  
72 # No /i, whether written on the match or in scope from a use re, and every
73 # significant token of the pattern a literal.
74 69 ▶sub _is_literal_text {
Subroutine _is_literal_text: called CC=8 SCAR=20.9
75 69 my ( $elem, $doc ) = @_;
76  
77 69 ▶ my $re = $doc->ppix_regexp_from_element($elem) or return 0;
Branch: 1 branch
BranchTrueFalseTotal
my $re = $doc->ppix_regexp_from_element($elem) or return 0 69 0 69
78 69 ▶ return 0 if $re->failures();
Branch: 1 branch
BranchTrueFalseTotal
if $re->failures 0 69 69
79 69 ▶ return 0 if $re->modifier_asserted($EXEMPTING_MODIFIER);
Branch: 1 branch
BranchTrueFalseTotal
if $re->modifier_asserted($EXEMPTING_MODIFIER) 3 66 69
80  
81 66 ▶ my $pattern = $re->regular_expression() or return 0;
Branch: 1 branch
BranchTrueFalseTotal
my $pattern = $re->regular_expression or return 0 66 0 66
82  
83 66 my $has_literal = 0;
84 66 foreach my $token ( map { $_->tokens() } $pattern->children() ) {
85 153 ▶ next if !$token->significant();
Branch: 1 branch
BranchTrueFalseTotal
unless $token->significant 3 150 153
86 150 ▶ return 0 if !$token->isa('PPIx::Regexp::Token::Literal');
Branch: 1 branch
BranchTrueFalseTotal
unless $token->isa("PPIx::Regexp::Token::Literal") 9 141 150
87 141 $has_literal = 1;
88 }
89 57 return $has_literal;
90 }
91  
92 # The word naming the call $node is the whole of the first argument to, or
93 # nothing. Looks through parentheses around it, and stops at anything else: a
94 # block, a subscript, a constructor, the statement.
95 58 ▶sub _call_taking_it_first {
Subroutine _call_taking_it_first: called CC=10 SCAR=23.2
96 58 my ($node) = @_;
97  
98 58 ▶ return if !_is_whole_argument($node);
Branch: 1 branch
BranchTrueFalseTotal
unless _is_whole_argument($node) 18 40 58
99  
100 # `split m/,/, $s`: the call is the word straight before it, and anything
101 # else straight before it means this is not the first argument.
102 40 my $before = $node->sprevious_sibling();
103 40 ▶ if ($before) {
Branch: 1 branch
BranchTrueFalseTotal
if ($before) 18 22 40
104 18 ▶ return $before if $before->isa('PPI::Token::Word') && is_function_call($before);
Branch: 1 branch
BranchTrueFalseTotal
if $before->isa("PPI::Token::Word") and is_function_call($before) 14 4 18
Condition100%3/3
$before->isa("PPI::Token::Word") and is_function_call($before)
!ll&&!rl&&r
3114
MC/DC100%2/2
$before->isa("PPI::Token::Word") and is_function_call($before)
$before->isa("PPI::Token::Word") is_function_call($before)
Truth table100%3/3
A and B
A= $before->isa("PPI::Token::Word")B= is_function_call($before)
ABresult
0X0
100
111
105 4 return;
106 }
107  
108 # `split( m/,/, $s )`: first in a list, which belongs to the word before it.
109 22 my $expression = $node->parent();
110 22 ▶ my $list = $expression && $expression->parent();
Condition33%1/3
$expression && $expression->parent
!ll&&!rl&&r
0022
MC/DC0%0/2
$expression && $expression->parent
$expression $expression->parent
Truth table33%1/3
A && B
A= $expressionB= $expression->parent
ABresult
0X0
100
111
111 22 ▶ return if !$list || !$list->isa('PPI::Structure::List');
Branch: 1 branch
BranchTrueFalseTotal
unless $list and $list->isa("PPI::Structure::List") 5 17 22
Condition66%2/3
$list and $list->isa("PPI::Structure::List")
!ll&&!rl&&r
0517
MC/DC50%1/2
$list and $list->isa("PPI::Structure::List")
$list $list->isa("PPI::Structure::List")
Truth table66%2/3
A and B
A= $listB= $list->isa("PPI::Structure::List")
ABresult
0X0
100
111
112  
113 17 my $caller = $list->sprevious_sibling();
114 17 ▶ return $caller if $caller && $caller->isa('PPI::Token::Word');
Branch: 1 branch
BranchTrueFalseTotal
if $caller and $caller->isa("PPI::Token::Word") 16 1 17
Condition66%2/3
$caller and $caller->isa("PPI::Token::Word")
!ll&&!rl&&r
1016
MC/DC50%1/2
$caller and $caller->isa("PPI::Token::Word")
$caller $caller->isa("PPI::Token::Word")
Truth table66%2/3
A and B
A= $callerB= $caller->isa("PPI::Token::Word")
ABresult
0X0
100
111
115  
116 # `split( ( m/,/ ), $s )`: parentheses of its own, first in the call's.
117 1 return _call_taking_it_first($list);
118 }
119  
120 58 ▶sub _is_whole_argument {
Subroutine _is_whole_argument: called CC=5 SCAR=16.1
121 58 my ($node) = @_;
122  
123 58 foreach my $neighbour ( $node->sprevious_sibling(), $node->snext_sibling() ) {
124 99 ▶ next if !$neighbour || !$neighbour->isa('PPI::Token::Operator');
Branch: 1 branch
BranchTrueFalseTotal
unless $neighbour and $neighbour->isa("PPI::Token::Operator") 53 46 99
Condition100%3/3
$neighbour and $neighbour->isa("PPI::Token::Operator")
!ll&&!rl&&r
332046
MC/DC100%2/2
$neighbour and $neighbour->isa("PPI::Token::Operator")
$neighbour $neighbour->isa("PPI::Token::Operator")
Truth table100%3/3
A and B
A= $neighbourB= $neighbour->isa("PPI::Token::Operator")
ABresult
0X0
100
111
125 46 ▶ return 0 if !$SEPARATOR{ $neighbour->content() };
Branch: 1 branch
BranchTrueFalseTotal
unless $SEPARATOR{$neighbour->content} 18 28 46
126 }
127 40 return 1;
128 }
129  
130 # An entry with no package matches the name however it is reached; one with a
131 # package matches only a call that names that package, as Pkg::name or
132 # Pkg->name.
133 30 ▶sub _allowed {
Subroutine _allowed: called CC=7 SCAR=19.6
134 30 my ( $self, $word ) = @_;
135  
136 30 my $name = $word->content();
137 30 my $arrow = $word->sprevious_sibling();
138 30 ▶ if ( $arrow && $arrow->content() eq '->' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($arrow and $arrow->content eq "->") 5 25 30
Condition100%3/3
$arrow and $arrow->content eq "->"
!ll&&!rl&&r
2415
MC/DC100%2/2
$arrow and $arrow->content eq "->"
$arrow $arrow->content eq "->"
Truth table100%3/3
A and B
A= $arrowB= $arrow->content eq "->"
ABresult
0X0
100
111
139 5 my $invocant = $arrow->sprevious_sibling();
140 5 ▶ return $self->{_allow}{"${invocant}::$name"} || $self->{_allow}{$name}
Branch: 1 branch
BranchTrueFalseTotal
if $invocant and $invocant->isa("PPI::Token::Word") 1 4 5
Condition50%3/6
$invocant and $invocant->isa("PPI::Token::Word")
!ll&&!rl&&r
041
$self->{'_allow'}{"${invocant}::$name"} || $self->{'_allow'}{$name}
l!l&&r!l&&!r
100
MC/DC50%1/2
$invocant and $invocant->isa("PPI::Token::Word")
$invocant $invocant->isa("PPI::Token::Word")
MC/DC0%0/2
$self->{'_allow'}{"${invocant}::$name"} || $self->{'_allow'}{$name}
$self->{'_allow'}{"${invocant}::$name"} $self->{'_allow'}{$name}
Truth table66%2/3
A and B
A= $invocantB= $invocant->isa("PPI::Token::Word")
ABresult
0X0
100
111
Truth table33%1/3
A || B
A= $self->{'_allow'}{"${invocant}::$name"}B= $self->{'_allow'}{$name}
ABresult
1X1
011
000
141 if $invocant && $invocant->isa('PPI::Token::Word');
142 4 return $self->{_allow}{$name};
143 }
144  
145 25 my $short = $name =~ s/\A.*:://r;
146 25 ▶ return $self->{_allow}{$name} || $self->{_allow}{$short};
Condition100%3/3
$self->{'_allow'}{$name} || $self->{'_allow'}{$short}
l!l&&r!l&&!r
1825
MC/DC100%2/2
$self->{'_allow'}{$name} || $self->{'_allow'}{$short}
$self->{'_allow'}{$name} $self->{'_allow'}{$short}
Truth table100%3/3
A || B
A= $self->{'_allow'}{$name}B= $self->{'_allow'}{$short}
ABresult
1X1
011
000
147 }
148  
149 2 1;
150  
151 __END__
↑ Summary