lib/Perl/Critic/Policy/RegularExpressions/ProhibitEnumeratedClasses.pm

filter: Statementstmt 49.2% 32 / 65 Branchbran 3.3% 1 / 30 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 80.0% 12 / 15 Podpod 80.0% 4 / 5 totaltotal 40.8% 49 / 120 CC 19 SCAR 37.8
CC
19
Cov
34%
_get_character_class_violations
110.0
violates
37.1
_fixup
20.0
CRAP 44.0
« lib/Perl/Critic/Policy/ValuesAndExpressions/ProhibitLeadingZeros.pm ↑ Summary lib/Perl/Critic/Policy/CodeLayout/RequireTrailingCommas.pm »
1 package Perl::Critic::Policy::RegularExpressions::ProhibitEnumeratedClasses;
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 List::SomeUtils qw(all);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 40 ▶use Readonly;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 40 ▶use Perl::Critic::Utils qw( :severities );
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 40 ▶use parent 'Perl::Critic::Policy';
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 40 our $VERSION = '1.156';
15  
16 #-----------------------------------------------------------------------------
17  
18 40 Readonly::Scalar my $DESC => q{Use named character classes};
19 40 Readonly::Scalar my $EXPL => [248];
20  
21 40 Readonly::Array my @PATTERNS => ( # order matters: most to least specific
22 [q{ },'\\t','\\r','\\n'] => ['\\s', '\\S'],
23 ['A-Z','a-z','0-9','_'] => ['\\w', '\\W'], # RT 69322
24 ['A-Z','a-z'] => ['[[:alpha:]]','[[:^alpha:]]'],
25 ['A-Z'] => ['[[:upper:]]','[[:^upper:]]'],
26 ['a-z'] => ['[[:lower:]]','[[:^lower:]]'],
27 ['0-9'] => ['\\d','\\D'],
28 ['\w'] => [undef, '\\W'],
29 ['\s'] => [undef, '\\S'],
30 );
31  
32 #-----------------------------------------------------------------------------
33  
34 90 ▶sub supported_parameters { return qw() }
Subroutine supported_parameters: called CC=1 SCAR=0.0
Pod undocumented
35 74 ▶sub default_severity { return $SEVERITY_LOWEST }
Subroutine default_severity: called CC=1 SCAR=0.0
Pod documented
36 83 ▶sub default_themes { return qw( core pbp cosmetic unicode ) }
Subroutine default_themes: called CC=1 SCAR=0.0
Pod documented
37 31 ▶sub applies_to { return qw(PPI::Token::Regexp::Match
Subroutine applies_to: called CC=1 SCAR=0.0
Pod documented
38 PPI::Token::Regexp::Substitute
39 PPI::Token::QuoteLike::Regexp) }
40  
41 #-----------------------------------------------------------------------------
42  
43  
44 1 ▶sub violates {
Subroutine violates: called CC=7 SCAR=36.1
Pod documented
45 1 my ( $self, $elem, $document ) = @_;
46  
47 # optimization: don't bother parsing the regexp if there are no character classes
48 1 ▶ return if $elem !~ m/\[/xms;
Branch: 1 branch
BranchTrueFalseTotal
unless $elem =~ /\[/msx 1 0 1
49  
50 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
51 0 ▶ $re->failures() and return;
Branch: 1 branch
BranchTrueFalseTotal
$re->failures and return 0 0 0
52  
53 0 ▶ my $anyofs = $re->find( 'PPIx::Regexp::Structure::CharClass' )
Branch: 1 branch
BranchTrueFalseTotal
my $anyofs = $re->find("PPIx::Regexp::Structure::CharClass") or return 0 0 0
54 or return;
55 0 foreach my $anyof ( @{ $anyofs } ) {
56 0 my $violation;
57 0 ▶ $violation = $self->_get_character_class_violations( $elem, $anyof )
Branch: 1 branch
BranchTrueFalseTotal
$violation = $self->_get_character_class_violations($elem, $anyof) and return $violation 0 0 0
58 and return $violation;
59 }
60  
61 0 return; # OK
62 }
63  
64 0 ▶sub _get_character_class_violations {
Subroutine _get_character_class_violations: not called CC=10 SCAR=47.0
65 0 my ($self, $elem, $anyof) = @_;
66  
67 0 my %elements;
68 0 foreach my $element ( $anyof->children() ) {
69 0 $elements{ _fixup( $element ) } = 1;
70 }
71  
72 0 ▶ for (my $i = 0; $i < @PATTERNS; $i += 2) { ##no critic (CStyleForLoop)
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @PATTERNS) 0 0 0
73 0 ▶ if (all { exists $elements{$_} } @{$PATTERNS[$i]}) {
Subroutine __ANON__: not called CC=1 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
if (&all(sub { exists $elements{$_}; } , @{$PATTERNS[$i];})) 0 0 0
74 0 my $neg = $anyof->negated();
75 0 ▶ my $improvement = $PATTERNS[$i + 1]->[$neg ? 1 : 0];
Branch: 1 branch
BranchTrueFalseTotal
$neg ? : 0 0 0
76 0 ▶ next if !defined $improvement;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $improvement 0 0 0
77  
78 0 ▶ if ($neg && ! defined $PATTERNS[$i + 1]->[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($neg and not defined $PATTERNS[$i + 1][0]) 0 0 0
Condition0%0/3
$neg and not defined $PATTERNS[$i + 1][0]
!ll&&!rl&&r
000
MC/DC0%0/2
$neg and not defined $PATTERNS[$i + 1][0]
$neg not defined $PATTERNS[$i + 1][0]
Truth table0%0/3
A and not(B)
A= $negB= not defined $PATTERNS[$i + 1][0]
ABresult
0X0
100
111
79 # the [^\w] => \W rule only applies if \w is the only token.
80 # that is it does not apply to [^\w\s]
81 0 ▶ next if 1 != scalar keys %elements;
Branch: 1 branch
BranchTrueFalseTotal
if 1 != scalar keys %elements 0 0 0
82 }
83  
84 0 ▶ my $orig = join q{}, '[', ($neg ? q{^} : ()), @{$PATTERNS[$i]}, ']';
Branch: 1 branch
BranchTrueFalseTotal
$neg ? : 0 0 0
85 0 return $self->violation( $DESC . " ($orig vs. $improvement)", $EXPL, $elem );
86 }
87 }
88  
89 0 return; # OK
90 }
91  
92 40 Readonly::Hash my %ORDINALS => (
93 ord "\n" => '\\n',
94 ord "\f" => '\\f',
95 ord "\r" => '\\r',
96 ord q< > => q< >,
97 );
98  
99 0 ▶sub _fixup {
Subroutine _fixup: not called CC=4 SCAR=30.0
100 0 my ( $element ) = @_;
101 0 ▶ if ( $element->isa( 'PPIx::Regexp::Token::Literal' ) ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($element->isa('PPIx::Regexp::Token::Literal')) { } 0 0 0
elsif ($element->isa('PPIx::Regexp::Node')) { } 0 0 0
102 0 my $ord = $element->ordinal();
103 0 ▶ exists $ORDINALS{$ord} and return $ORDINALS{$ord};
Branch: 1 branch
BranchTrueFalseTotal
exists $ORDINALS{$ord} and return $ORDINALS{$ord} 0 0 0
104 0 return $element->content();
105 } elsif ( $element->isa( 'PPIx::Regexp::Node' ) ) {
106 0 return join q{}, map{ _fixup( $_ ) } $element->elements();
107 } else {
108 0 return $element->content();
109 }
110 }
111  
112 40 1;
113  
114 __END__
« lib/Perl/Critic/Policy/ValuesAndExpressions/ProhibitLeadingZeros.pm ↑ Summary lib/Perl/Critic/Policy/CodeLayout/RequireTrailingCommas.pm »