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

filter: Statementstmt 71.1% 32 / 45 Branchbran 6.2% 1 / 16 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 12 / 12 Podpod 80.0% 4 / 5 totaltotal 62.8% 49 / 78 CC 10 SCAR 37.0
CC
10
Cov
54%
violates
83.7
BEGIN
1.0
BEGIN
1.0
CRAP 40.4
« lib/Perl/Critic/Policy/BuiltinFunctions/RequireSimpleSortBlock.pm ↑ Summary lib/Perl/Critic/Policy/ValuesAndExpressions/ProhibitMismatchedOperators.pm »
1 package Perl::Critic::Policy::RegularExpressions::ProhibitComplexRegexes;
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::Util qw{ min };
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{Split long regexps into smaller qr// chunks};
19 40 Readonly::Scalar my $EXPL => [261];
20  
21 40 Readonly::Scalar my $MAX_LITERAL_LENGTH => 7;
22 40 Readonly::Scalar my $MAX_VARIABLE_LENGTH => 4;
23  
24 #-----------------------------------------------------------------------------
25  
26 91 ▶sub supported_parameters {
Subroutine supported_parameters: called CC=1 SCAR=0.0
Pod undocumented
27 return (
28 {
29 91 name => 'max_characters',
30 description =>
31 'The maximum number of characters to allow in a regular expression.',
32 default_string => '60',
33 behavior => 'integer',
34 integer_minimum => 1,
35 },
36 );
37 }
38  
39 74 ▶sub default_severity { return $SEVERITY_MEDIUM }
Subroutine default_severity: called CC=1 SCAR=0.0
Pod documented
40 85 ▶sub default_themes { return qw( core pbp maintenance ) }
Subroutine default_themes: called CC=1 SCAR=0.0
Pod documented
41 31 ▶sub applies_to { return qw(PPI::Token::Regexp::Match
Subroutine applies_to: called CC=1 SCAR=0.0
Pod documented
42 PPI::Token::Regexp::Substitute
43 PPI::Token::QuoteLike::Regexp) }
44  
45 #-----------------------------------------------------------------------------
46  
47 1 ▶sub violates {
Subroutine violates: called CC=10 SCAR=44.3
Pod documented
48 1 my ( $self, $elem, $document ) = @_;
49  
50 # Optimization: if its short enough now, parsing won't make it longer
51 1 ▶ return if $self->{_max_characters} >= length $elem->get_match_string();
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_max_characters"} >= length $elem->get_match_string 1 0 1
52  
53 0 ▶ my $re = $document->ppix_regexp_from_element( $elem )
Branch: 1 branch
BranchTrueFalseTotal
my $re = $document->ppix_regexp_from_element($elem) or return 0 0 0
54 or return; # Abort on syntax error.
55 0 ▶ $re->failures()
Branch: 1 branch
BranchTrueFalseTotal
$re->failures and return 0 0 0
56 and return; # Abort if parse errors found.
57 0 ▶ my $qr = $re->regular_expression()
Branch: 1 branch
BranchTrueFalseTotal
my $qr = $re->regular_expression or return 0 0 0
58 or return; # Abort if no regular expression.
59  
60 0 my $length = 0;
61 # We use map { $_->tokens() } qr->children() rather than just
62 # $qr->tokens() because we are not interested in the delimiters.
63 0 foreach my $token ( map { $_->tokens() } $qr->children() ) {
64  
65 # Do not count whitespace or comments
66 0 ▶ $token->significant() or next;
Branch: 1 branch
BranchTrueFalseTotal
$token->significant or next 0 0 0
67  
68 0 ▶ if ( $token->isa( 'PPIx::Regexp::Token::Interpolation' ) ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($token->isa('PPIx::Regexp::Token::Interpolation')) { } 0 0 0
elsif ($token->isa('PPIx::Regexp::Token::Literal')) { } 0 0 0
69  
70 # Do not penalize long variable names
71 0 $length += min( $MAX_VARIABLE_LENGTH, length $token->content() );
72  
73 } elsif ( $token->isa( 'PPIx::Regexp::Token::Literal' ) ) {
74  
75 # Do not penalize long literals like \p{...}
76 0 $length += min( $MAX_LITERAL_LENGTH, length $token->content() );
77  
78 } else {
79  
80 # Take everything else at face value
81 0 $length += length $token->content();
82  
83 }
84  
85 }
86  
87 0 ▶ return if $self->{_max_characters} >= $length;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_max_characters"} >= $length 0 0 0
88  
89 0 return $self->violation( $DESC, $EXPL, $elem );
90 }
91  
92 40 1;
93  
94 __END__
« lib/Perl/Critic/Policy/BuiltinFunctions/RequireSimpleSortBlock.pm ↑ Summary lib/Perl/Critic/Policy/ValuesAndExpressions/ProhibitMismatchedOperators.pm »