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

filter: Statementstmt 75.6% 28 / 37 Branchbran 7.1% 1 / 14 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 11 / 11 Podpod 80.0% 4 / 5 totaltotal 65.6% 44 / 67 CC 9 SCAR 34.3
CC
9
Cov
57%
violates
64.2
BEGIN
1.0
BEGIN
1.0
CRAP 30.9
« lib/Perl/Critic/Policy/InputOutput/ProhibitTwoArgOpen.pm ↑ Summary lib/Perl/Critic/Document.pm »
1 package Perl::Critic::Policy::RegularExpressions::ProhibitEscapedMetacharacters;
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( :severities hashify );
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.156';
13  
14 #-----------------------------------------------------------------------------
15  
16 40 Readonly::Scalar my $DESC => q{Use character classes for literal metachars instead of escapes};
17 40 Readonly::Scalar my $EXPL => [247];
18  
19 40 Readonly::Hash my %REGEXP_METACHARS => hashify(split / /xms, '{ } ( ) . * + ? |');
20  
21 #-----------------------------------------------------------------------------
22  
23 90 ▶sub supported_parameters { return qw() }
Subroutine supported_parameters: called CC=1 SCAR=0.0
Pod undocumented
24 74 ▶sub default_severity { return $SEVERITY_LOWEST }
Subroutine default_severity: called CC=1 SCAR=0.0
Pod documented
25 83 ▶sub default_themes { return qw( core pbp cosmetic ) }
Subroutine default_themes: called CC=1 SCAR=0.0
Pod documented
26 31 ▶sub applies_to { return qw(PPI::Token::Regexp::Match
Subroutine applies_to: called CC=1 SCAR=0.0
Pod documented
27 PPI::Token::Regexp::Substitute
28 PPI::Token::QuoteLike::Regexp) }
29  
30 #-----------------------------------------------------------------------------
31  
32 1 ▶sub violates {
Subroutine violates: called CC=9 SCAR=41.6
Pod documented
33 1 my ( $self, $elem, $document ) = @_;
34  
35 # optimization: don't bother parsing the regexp if there are no escapes
36 1 ▶ return if $elem !~ m/\\/xms;
Branch: 1 branch
BranchTrueFalseTotal
unless $elem =~ /\\/msx 1 0 1
37  
38 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
39 0 ▶ $re->failures() and return;
Branch: 1 branch
BranchTrueFalseTotal
$re->failures and return 0 0 0
40 0 ▶ my $qr = $re->regular_expression() or return;
Branch: 1 branch
BranchTrueFalseTotal
my $qr = $re->regular_expression or return 0 0 0
41  
42 0 ▶ my $exacts = $qr->find( 'PPIx::Regexp::Token::Literal' ) or return;
Branch: 1 branch
BranchTrueFalseTotal
my $exacts = $qr->find("PPIx::Regexp::Token::Literal") or return 0 0 0
43 0 foreach my $exact( @{ $exacts } ) {
44 0 ▶ $exact->content() =~ m/ \\ ( . ) /xms or next;
Branch: 1 branch
BranchTrueFalseTotal
$exact->content =~ / \\ ( . ) /msx or next 0 0 0
45 0 ▶ return $self->violation( $DESC, $EXPL, $elem ) if $REGEXP_METACHARS{$1};
Branch: 1 branch
BranchTrueFalseTotal
if $REGEXP_METACHARS{$1} 0 0 0
46 }
47  
48 0 return; # OK
49 }
50  
51 40 1;
52  
53 __END__
« lib/Perl/Critic/Policy/InputOutput/ProhibitTwoArgOpen.pm ↑ Summary lib/Perl/Critic/Document.pm »