| Filename | /Users/timbo/perl5/perlbrew/perls/perl-5.18.2/lib/site_perl/5.18.2/Perl/Critic/Policy/RegularExpressions/ProhibitComplexRegexes.pm |
| Statements | Executed 26 statements in 488µs |
| Calls | P | F | Exclusive Time |
Inclusive Time |
Subroutine |
|---|---|---|---|---|---|
| 1 | 1 | 1 | 16µs | 16µs | Perl::Critic::Policy::RegularExpressions::ProhibitComplexRegexes::BEGIN@10 |
| 1 | 1 | 1 | 12µs | 41µs | Perl::Critic::Policy::RegularExpressions::ProhibitComplexRegexes::BEGIN@16 |
| 1 | 1 | 1 | 10µs | 14µs | Perl::Critic::Policy::RegularExpressions::ProhibitComplexRegexes::BEGIN@12 |
| 1 | 1 | 1 | 8µs | 38µs | Perl::Critic::Policy::RegularExpressions::ProhibitComplexRegexes::BEGIN@14 |
| 1 | 1 | 1 | 7µs | 25µs | Perl::Critic::Policy::RegularExpressions::ProhibitComplexRegexes::BEGIN@17 |
| 1 | 1 | 1 | 7µs | 423µs | Perl::Critic::Policy::RegularExpressions::ProhibitComplexRegexes::BEGIN@15 |
| 1 | 1 | 1 | 7µs | 201µs | Perl::Critic::Policy::RegularExpressions::ProhibitComplexRegexes::BEGIN@19 |
| 1 | 1 | 1 | 7µs | 64µs | Perl::Critic::Policy::RegularExpressions::ProhibitComplexRegexes::BEGIN@21 |
| 1 | 1 | 1 | 7µs | 18µs | Perl::Critic::Policy::RegularExpressions::ProhibitComplexRegexes::BEGIN@11 |
| 1 | 1 | 1 | 6µs | 6µs | Perl::Critic::Policy::RegularExpressions::ProhibitComplexRegexes::supported_parameters |
| 1 | 1 | 1 | 6µs | 7µs | Perl::Critic::Policy::RegularExpressions::ProhibitComplexRegexes::default_severity |
| 0 | 0 | 0 | 0s | 0s | Perl::Critic::Policy::RegularExpressions::ProhibitComplexRegexes::applies_to |
| 0 | 0 | 0 | 0s | 0s | Perl::Critic::Policy::RegularExpressions::ProhibitComplexRegexes::default_themes |
| 0 | 0 | 0 | 0s | 0s | Perl::Critic::Policy::RegularExpressions::ProhibitComplexRegexes::violates |
| Line | State ments |
Time on line |
Calls | Time in subs |
Code |
|---|---|---|---|---|---|
| 1 | ############################################################################## | ||||
| 2 | # $URL$ | ||||
| 3 | # $Date$ | ||||
| 4 | # $Author$ | ||||
| 5 | # $Revision$ | ||||
| 6 | ############################################################################## | ||||
| 7 | |||||
| 8 | package Perl::Critic::Policy::RegularExpressions::ProhibitComplexRegexes; | ||||
| 9 | |||||
| 10 | 2 | 43µs | 1 | 16µs | # spent 16µs within Perl::Critic::Policy::RegularExpressions::ProhibitComplexRegexes::BEGIN@10 which was called:
# once (16µs+0s) by Module::Pluggable::Object::_require at line 10 # spent 16µs making 1 call to Perl::Critic::Policy::RegularExpressions::ProhibitComplexRegexes::BEGIN@10 |
| 11 | 2 | 23µs | 2 | 29µs | # spent 18µs (7+11) within Perl::Critic::Policy::RegularExpressions::ProhibitComplexRegexes::BEGIN@11 which was called:
# once (7µs+11µs) by Module::Pluggable::Object::_require at line 11 # spent 18µs making 1 call to Perl::Critic::Policy::RegularExpressions::ProhibitComplexRegexes::BEGIN@11
# spent 11µs making 1 call to strict::import |
| 12 | 2 | 20µs | 2 | 18µs | # spent 14µs (10+4) within Perl::Critic::Policy::RegularExpressions::ProhibitComplexRegexes::BEGIN@12 which was called:
# once (10µs+4µs) by Module::Pluggable::Object::_require at line 12 # spent 14µs making 1 call to Perl::Critic::Policy::RegularExpressions::ProhibitComplexRegexes::BEGIN@12
# spent 4µs making 1 call to warnings::import |
| 13 | |||||
| 14 | 2 | 22µs | 2 | 69µs | # spent 38µs (8+31) within Perl::Critic::Policy::RegularExpressions::ProhibitComplexRegexes::BEGIN@14 which was called:
# once (8µs+31µs) by Module::Pluggable::Object::_require at line 14 # spent 38µs making 1 call to Perl::Critic::Policy::RegularExpressions::ProhibitComplexRegexes::BEGIN@14
# spent 31µs making 1 call to Exporter::import |
| 15 | 2 | 24µs | 2 | 839µs | # spent 423µs (7+416) within Perl::Critic::Policy::RegularExpressions::ProhibitComplexRegexes::BEGIN@15 which was called:
# once (7µs+416µs) by Module::Pluggable::Object::_require at line 15 # spent 423µs making 1 call to Perl::Critic::Policy::RegularExpressions::ProhibitComplexRegexes::BEGIN@15
# spent 416µs making 1 call to English::import |
| 16 | 2 | 21µs | 2 | 52µs | # spent 41µs (12+30) within Perl::Critic::Policy::RegularExpressions::ProhibitComplexRegexes::BEGIN@16 which was called:
# once (12µs+30µs) by Module::Pluggable::Object::_require at line 16 # spent 41µs making 1 call to Perl::Critic::Policy::RegularExpressions::ProhibitComplexRegexes::BEGIN@16
# spent 11µs making 1 call to List::Util::import |
| 17 | 2 | 21µs | 2 | 42µs | # spent 25µs (7+17) within Perl::Critic::Policy::RegularExpressions::ProhibitComplexRegexes::BEGIN@17 which was called:
# once (7µs+17µs) by Module::Pluggable::Object::_require at line 17 # spent 25µs making 1 call to Perl::Critic::Policy::RegularExpressions::ProhibitComplexRegexes::BEGIN@17
# spent 17µs making 1 call to Exporter::import |
| 18 | |||||
| 19 | 2 | 24µs | 2 | 395µs | # spent 201µs (7+194) within Perl::Critic::Policy::RegularExpressions::ProhibitComplexRegexes::BEGIN@19 which was called:
# once (7µs+194µs) by Module::Pluggable::Object::_require at line 19 # spent 201µs making 1 call to Perl::Critic::Policy::RegularExpressions::ProhibitComplexRegexes::BEGIN@19
# spent 194µs making 1 call to Exporter::import |
| 20 | |||||
| 21 | 2 | 273µs | 2 | 121µs | # spent 64µs (7+57) within Perl::Critic::Policy::RegularExpressions::ProhibitComplexRegexes::BEGIN@21 which was called:
# once (7µs+57µs) by Module::Pluggable::Object::_require at line 21 # spent 64µs making 1 call to Perl::Critic::Policy::RegularExpressions::ProhibitComplexRegexes::BEGIN@21
# spent 57µs making 1 call to base::import |
| 22 | |||||
| 23 | 1 | 600ns | our $VERSION = '1.121'; | ||
| 24 | |||||
| 25 | #----------------------------------------------------------------------------- | ||||
| 26 | |||||
| 27 | 1 | 2µs | 1 | 30µs | Readonly::Scalar my $DESC => q{Split long regexps into smaller qr// chunks}; # spent 30µs making 1 call to Readonly::Scalar |
| 28 | 1 | 2µs | 1 | 44µs | Readonly::Scalar my $EXPL => [261]; # spent 44µs making 1 call to Readonly::Scalar |
| 29 | |||||
| 30 | 1 | 1µs | 1 | 24µs | Readonly::Scalar my $MAX_LITERAL_LENGTH => 7; # spent 24µs making 1 call to Readonly::Scalar |
| 31 | 1 | 900ns | 1 | 20µs | Readonly::Scalar my $MAX_VARIABLE_LENGTH => 4; # spent 20µs making 1 call to Readonly::Scalar |
| 32 | |||||
| 33 | #----------------------------------------------------------------------------- | ||||
| 34 | |||||
| 35 | # spent 6µs within Perl::Critic::Policy::RegularExpressions::ProhibitComplexRegexes::supported_parameters which was called:
# once (6µs+0s) by Perl::Critic::Policy::new at line 88 of Perl/Critic/Policy.pm | ||||
| 36 | return ( | ||||
| 37 | { | ||||
| 38 | 1 | 8µs | name => 'max_characters', | ||
| 39 | description => | ||||
| 40 | 'The maximum number of characters to allow in a regular expression.', | ||||
| 41 | default_string => '60', | ||||
| 42 | behavior => 'integer', | ||||
| 43 | integer_minimum => 1, | ||||
| 44 | }, | ||||
| 45 | ); | ||||
| 46 | } | ||||
| 47 | |||||
| 48 | 1 | 1µs | # spent 7µs (6+1) within Perl::Critic::Policy::RegularExpressions::ProhibitComplexRegexes::default_severity which was called:
# once (6µs+1µs) by Perl::Critic::Policy::get_severity at line 331 of Perl/Critic/Policy.pm | ||
| 49 | sub default_themes { return qw( core pbp maintenance ) } | ||||
| 50 | sub applies_to { return qw(PPI::Token::Regexp::Match | ||||
| 51 | PPI::Token::Regexp::Substitute | ||||
| 52 | PPI::Token::QuoteLike::Regexp) } | ||||
| 53 | |||||
| 54 | #----------------------------------------------------------------------------- | ||||
| 55 | |||||
| 56 | sub violates { | ||||
| 57 | my ( $self, $elem, $document ) = @_; | ||||
| 58 | |||||
| 59 | # Optimization: if its short enough now, parsing won't make it longer | ||||
| 60 | return if $self->{_max_characters} >= length $elem->get_match_string(); | ||||
| 61 | |||||
| 62 | my $re = $document->ppix_regexp_from_element( $elem ) | ||||
| 63 | or return; # Abort on syntax error. | ||||
| 64 | $re->failures() | ||||
| 65 | and return; # Abort if parse errors found. | ||||
| 66 | my $qr = $re->regular_expression() | ||||
| 67 | or return; # Abort if no regular expression. | ||||
| 68 | |||||
| 69 | my $length = 0; | ||||
| 70 | # We use map { $_->tokens() } qr->children() rather than just | ||||
| 71 | # $qr->tokens() because we are not interested in the delimiters. | ||||
| 72 | foreach my $token ( map { $_->tokens() } $qr->children() ) { | ||||
| 73 | |||||
| 74 | # Do not count whitespace or comments | ||||
| 75 | $token->significant() or next; | ||||
| 76 | |||||
| 77 | if ( $token->isa( 'PPIx::Regexp::Token::Interpolation' ) ) { | ||||
| 78 | |||||
| 79 | # Do not penalize long variable names | ||||
| 80 | $length += min( $MAX_VARIABLE_LENGTH, length $token->content() ); | ||||
| 81 | |||||
| 82 | } elsif ( $token->isa( 'PPIx::Regexp::Token::Literal' ) ) { | ||||
| 83 | |||||
| 84 | # Do not penalize long literals like \p{...} | ||||
| 85 | $length += min( $MAX_LITERAL_LENGTH, length $token->content() ); | ||||
| 86 | |||||
| 87 | } else { | ||||
| 88 | |||||
| 89 | # Take everything else at face value | ||||
| 90 | $length += length $token->content(); | ||||
| 91 | |||||
| 92 | } | ||||
| 93 | |||||
| 94 | } | ||||
| 95 | |||||
| 96 | return if $self->{_max_characters} >= $length; | ||||
| 97 | |||||
| 98 | return $self->violation( $DESC, $EXPL, $elem ); | ||||
| 99 | } | ||||
| 100 | |||||
| 101 | 1 | 4µs | 1; | ||
| 102 | |||||
| 103 | __END__ |