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

filter: Statementstmt 57.7% 26 / 45 Branchbran 5.5% 1 / 18 Conditioncond 0.0% 0 / 12 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 91.6% 11 / 12 Podpod 80.0% 4 / 5 totaltotal 42.0% 42 / 100 CC 16 SCAR 47.3
CC
16
Cov
36%
violates
202.6
__ANON__
6.0
BEGIN
1.0
CRAP 113.4
« blib/lib/Perl/Critic/Policy/Subroutines/RequireArgUnpacking.pm ↑ Summary blib/lib/Perl/Critic/TestUtils.pm »
1 package Perl::Critic::Policy::RegularExpressions::ProhibitSingleCharAlternation;
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  
7 40 ▶use Readonly;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 40 ▶use Perl::Critic::Utils qw( :characters :severities );
Subroutine BEGIN: called CC=1 SCAR=0.0
10 40 ▶use parent 'Perl::Critic::Policy';
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 40 our $VERSION = '1.150';
13  
14 #-----------------------------------------------------------------------------
15  
16 40 Readonly::Scalar my $EXPL => [265];
17  
18 #-----------------------------------------------------------------------------
19  
20 90 ▶sub supported_parameters { return qw() }
Subroutine supported_parameters: called CC=1 SCAR=0.0
Pod undocumented
21 75 ▶sub default_severity { return $SEVERITY_LOWEST }
Subroutine default_severity: called CC=1 SCAR=0.0
Pod documented
22 86 ▶sub default_themes { return qw( core pbp performance ) }
Subroutine default_themes: called CC=1 SCAR=0.0
Pod documented
23 31 ▶sub applies_to { return qw(PPI::Token::Regexp::Match
Subroutine applies_to: called CC=1 SCAR=0.0
Pod documented
24 PPI::Token::Regexp::Substitute
25 PPI::Token::QuoteLike::Regexp) }
26  
27 #-----------------------------------------------------------------------------
28  
29 sub violates {
30 1 ▶ my ( $self, $elem, $document ) = @_;
Subroutine violates: called CC=15 SCAR=53.1
Pod documented
31  
32 # optimization: don't bother parsing the regexp if there are no pipes
33 1 ▶ return if $elem !~ m/[|]/xms;
Branch: 1 branch
BranchTrueFalseTotal
unless $elem =~ /[|]/msx 1 0 1
34  
35 0 ▶ my $re = $document->ppix_regexp_from_element( $elem ) or return;
Branch: 1 branch
BranchTrueFalseTotal
my $re = $document->ppix_regexp_from_element($elem) or return 0 0 0
36 0 ▶ $re->failures() and return;
Branch: 1 branch
BranchTrueFalseTotal
$re->failures and return 0 0 0
37  
38 0 my @violations;
39 0 foreach my $node ( @{ $re->find_parents( sub {
40 0 ▶ return $_[1]->isa( 'PPIx::Regexp::Token::Operator' )
Subroutine __ANON__: not called CC=2 SCAR=17.9
Condition0%0/3
$_[1]->isa('PPIx::Regexp::Token::Operator') && $_[1]->content eq '|'
!ll&&!rl&&r
000
MC/DC0%0/2
$_[1]->isa('PPIx::Regexp::Token::Operator') && $_[1]->content eq '|'
$_[1]->isa('PPIx::Regexp::Token::Operator') $_[1]->content eq '|'
Truth table0%0/3
A && B
A= $_[1]->isa('PPIx::Regexp::Token::Operator')B= $_[1]->content eq '|'
ABresult
0X0
100
111
41 && $_[1]->content() eq q<|>;
42 0 ▶ } ) || [] } ) {
Branch: 1 branch
BranchTrueFalseTotal
$re->find_parents(sub { return $_[1]->isa("PPIx::Regexp::Token::Operator") && $_[1]->content eq "|"; } ) or [] 0 0 0
43  
44 0 my @singles;
45 my @alternative;
46 0 foreach my $kid ( $node->children() ) {
47 0 ▶ if ( $kid->isa( 'PPIx::Regexp::Token::Operator' )
Branch: 2 branches
BranchTrueFalseTotal
if ($kid->isa('PPIx::Regexp::Token::Operator') and $kid->content eq '|') { } 0 0 0
elsif ($kid->significant) { } 0 0 0
Condition0%0/3
$kid->isa("PPIx::Regexp::Token::Operator") and $kid->content eq "|"
!ll&&!rl&&r
000
MC/DC0%0/2
$kid->isa("PPIx::Regexp::Token::Operator") and $kid->content eq "|"
$kid->isa("PPIx::Regexp::Token::Operator") $kid->content eq "|"
Truth table0%0/3
A and B
A= $kid->isa("PPIx::Regexp::Token::Operator")B= $kid->content eq "|"
ABresult
0X0
100
111
48 && $kid->content() eq q<|>
49 ) {
50 @alternative == 1
51 and $alternative[0]->isa( 'PPIx::Regexp::Token::Literal' )
52 0 ▶ and push @singles, map { $_->content() } @alternative;
Branch: 1 branch
BranchTrueFalseTotal
@alternative == 1 and $alternative[0]->isa("PPIx::Regexp::Token::Literal") and push @singles, map({$_->content;} @alternative) 0 0 0
Condition0%0/3
@alternative == 1 and $alternative[0]->isa("PPIx::Regexp::Token::Literal")
!ll&&!rl&&r
000
MC/DC0%0/2
@alternative == 1 and $alternative[0]->isa("PPIx::Regexp::Token::Literal")
@alternative == 1 $alternative[0]->isa("PPIx::Regexp::Token::Literal")
Truth table0%0/3
A and B
A= @alternative == 1B= $alternative[0]->isa("PPIx::Regexp::Token::Literal")
ABresult
0X0
100
111
53 0 @alternative = ();
54 } elsif ( $kid->significant() ) {
55 0 push @alternative, $kid;
56 }
57 }
58 @alternative == 1
59 and $alternative[0]->isa( 'PPIx::Regexp::Token::Literal' )
60 0 ▶ and push @singles, map { $_->content() } @alternative;
Branch: 1 branch
BranchTrueFalseTotal
@alternative == 1 and $alternative[0]->isa("PPIx::Regexp::Token::Literal") and push @singles, map({$_->content;} @alternative) 0 0 0
Condition0%0/3
@alternative == 1 and $alternative[0]->isa("PPIx::Regexp::Token::Literal")
!ll&&!rl&&r
000
MC/DC0%0/2
@alternative == 1 and $alternative[0]->isa("PPIx::Regexp::Token::Literal")
@alternative == 1 $alternative[0]->isa("PPIx::Regexp::Token::Literal")
Truth table0%0/3
A and B
A= @alternative == 1B= $alternative[0]->isa("PPIx::Regexp::Token::Literal")
ABresult
0X0
100
111
61  
62 0 ▶ if ( 1 < @singles ) {
Branch: 1 branch
BranchTrueFalseTotal
if (1 < @singles) 0 0 0
63 0 my $description =
64 'Use ['
65 . join( $EMPTY, @singles )
66 . '] instead of '
67 . join q<|>, @singles;
68 0 push @violations, $self->violation( $description, $EXPL, $elem );
69 }
70 }
71  
72 0 return @violations;
73 }
74  
75 40 1;
76  
77 __END__
78  
79 #-----------------------------------------------------------------------------
80  
81 =pod
82  
83 =head1 NAME
84  
85 Perl::Critic::Policy::RegularExpressions::ProhibitSingleCharAlternation - Use C<[abc]> instead of C<a|b|c>.
86  
87  
88 =head1 AFFILIATION
89  
90 This Policy is part of the core L<Perl::Critic|Perl::Critic>
91 distribution.
92  
93  
94 =head1 DESCRIPTION
95  
96 Character classes (like C<[abc]>) are significantly faster than single
97 character alternations (like C<(?:a|b|c)>). This policy complains if
98 you have more than one instance of a single character in an
99 alternation. So C<(?:a|the)> is allowed, but C<(?:a|e|i|o|u)> is not.
100  
101  
102 =head1 CONFIGURATION
103  
104 This Policy is not configurable except for the standard options.
105  
106  
107 =head1 CREDITS
108  
109 Initial development of this policy was supported by a grant from the
110 Perl Foundation.
111  
112  
113 =head1 AUTHOR
114  
115 Chris Dolan <cdolan@cpan.org>
116  
117  
118 =head1 COPYRIGHT
119  
120 Copyright (c) 2007-2023 Chris Dolan
121  
122 This program is free software; you can redistribute it and/or modify
123 it under the same terms as Perl itself. The full text of this license
124 can be found in the LICENSE file included with this module
125  
126 =cut
127  
128 # Local Variables:
129 # mode: cperl
130 # cperl-indent-level: 4
131 # fill-column: 78
132 # indent-tabs-mode: nil
133 # c-indentation-style: bsd
134 # End:
135 # ex: set ts=8 sts=4 sw=4 tw=78 ft=perl expandtab shiftround :
« blib/lib/Perl/Critic/Policy/Subroutines/RequireArgUnpacking.pm ↑ Summary blib/lib/Perl/Critic/TestUtils.pm »