Filename | /Users/timbo/perl5/perlbrew/perls/perl-5.18.2/lib/site_perl/5.18.2/Perl/Critic/Policy/RegularExpressions/ProhibitEnumeratedClasses.pm |
Statements | Executed 26 statements in 730µs |
Calls | P | F | Exclusive Time |
Inclusive Time |
Subroutine |
---|---|---|---|---|---|
1 | 1 | 1 | 27µs | 27µs | BEGIN@10 | Perl::Critic::Policy::RegularExpressions::ProhibitEnumeratedClasses::
1 | 1 | 1 | 8µs | 146µs | BEGIN@16 | Perl::Critic::Policy::RegularExpressions::ProhibitEnumeratedClasses::
1 | 1 | 1 | 8µs | 28µs | BEGIN@17 | Perl::Critic::Policy::RegularExpressions::ProhibitEnumeratedClasses::
1 | 1 | 1 | 8µs | 57µs | BEGIN@21 | Perl::Critic::Policy::RegularExpressions::ProhibitEnumeratedClasses::
1 | 1 | 1 | 7µs | 11µs | BEGIN@12 | Perl::Critic::Policy::RegularExpressions::ProhibitEnumeratedClasses::
1 | 1 | 1 | 7µs | 389µs | BEGIN@15 | Perl::Critic::Policy::RegularExpressions::ProhibitEnumeratedClasses::
1 | 1 | 1 | 7µs | 19µs | BEGIN@11 | Perl::Critic::Policy::RegularExpressions::ProhibitEnumeratedClasses::
1 | 1 | 1 | 7µs | 204µs | BEGIN@19 | Perl::Critic::Policy::RegularExpressions::ProhibitEnumeratedClasses::
1 | 1 | 1 | 7µs | 32µs | BEGIN@14 | Perl::Critic::Policy::RegularExpressions::ProhibitEnumeratedClasses::
1 | 1 | 1 | 5µs | 7µs | default_severity | Perl::Critic::Policy::RegularExpressions::ProhibitEnumeratedClasses::
1 | 1 | 1 | 2µs | 2µs | supported_parameters | Perl::Critic::Policy::RegularExpressions::ProhibitEnumeratedClasses::
0 | 0 | 0 | 0s | 0s | __ANON__[:82] | Perl::Critic::Policy::RegularExpressions::ProhibitEnumeratedClasses::
0 | 0 | 0 | 0s | 0s | _fixup | Perl::Critic::Policy::RegularExpressions::ProhibitEnumeratedClasses::
0 | 0 | 0 | 0s | 0s | _get_character_class_violations | Perl::Critic::Policy::RegularExpressions::ProhibitEnumeratedClasses::
0 | 0 | 0 | 0s | 0s | applies_to | Perl::Critic::Policy::RegularExpressions::ProhibitEnumeratedClasses::
0 | 0 | 0 | 0s | 0s | default_themes | Perl::Critic::Policy::RegularExpressions::ProhibitEnumeratedClasses::
0 | 0 | 0 | 0s | 0s | violates | Perl::Critic::Policy::RegularExpressions::ProhibitEnumeratedClasses::
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::ProhibitEnumeratedClasses; | ||||
9 | |||||
10 | 2 | 41µs | 1 | 27µs | # spent 27µs within Perl::Critic::Policy::RegularExpressions::ProhibitEnumeratedClasses::BEGIN@10 which was called:
# once (27µs+0s) by Module::Pluggable::Object::_require at line 10 # spent 27µs making 1 call to Perl::Critic::Policy::RegularExpressions::ProhibitEnumeratedClasses::BEGIN@10 |
11 | 2 | 20µs | 2 | 30µs | # spent 19µs (7+12) within Perl::Critic::Policy::RegularExpressions::ProhibitEnumeratedClasses::BEGIN@11 which was called:
# once (7µs+12µs) by Module::Pluggable::Object::_require at line 11 # spent 19µs making 1 call to Perl::Critic::Policy::RegularExpressions::ProhibitEnumeratedClasses::BEGIN@11
# spent 12µs making 1 call to strict::import |
12 | 2 | 21µs | 2 | 15µs | # spent 11µs (7+4) within Perl::Critic::Policy::RegularExpressions::ProhibitEnumeratedClasses::BEGIN@12 which was called:
# once (7µs+4µs) by Module::Pluggable::Object::_require at line 12 # spent 11µs making 1 call to Perl::Critic::Policy::RegularExpressions::ProhibitEnumeratedClasses::BEGIN@12
# spent 4µs making 1 call to warnings::import |
13 | |||||
14 | 2 | 22µs | 2 | 57µs | # spent 32µs (7+25) within Perl::Critic::Policy::RegularExpressions::ProhibitEnumeratedClasses::BEGIN@14 which was called:
# once (7µs+25µs) by Module::Pluggable::Object::_require at line 14 # spent 32µs making 1 call to Perl::Critic::Policy::RegularExpressions::ProhibitEnumeratedClasses::BEGIN@14
# spent 25µs making 1 call to Exporter::import |
15 | 2 | 24µs | 2 | 770µs | # spent 389µs (7+382) within Perl::Critic::Policy::RegularExpressions::ProhibitEnumeratedClasses::BEGIN@15 which was called:
# once (7µs+382µs) by Module::Pluggable::Object::_require at line 15 # spent 389µs making 1 call to Perl::Critic::Policy::RegularExpressions::ProhibitEnumeratedClasses::BEGIN@15
# spent 382µs making 1 call to English::import |
16 | 2 | 23µs | 2 | 284µs | # spent 146µs (8+138) within Perl::Critic::Policy::RegularExpressions::ProhibitEnumeratedClasses::BEGIN@16 which was called:
# once (8µs+138µs) by Module::Pluggable::Object::_require at line 16 # spent 146µs making 1 call to Perl::Critic::Policy::RegularExpressions::ProhibitEnumeratedClasses::BEGIN@16
# spent 138µs making 1 call to Exporter::Tiny::import |
17 | 2 | 22µs | 2 | 48µs | # spent 28µs (8+20) within Perl::Critic::Policy::RegularExpressions::ProhibitEnumeratedClasses::BEGIN@17 which was called:
# once (8µs+20µs) by Module::Pluggable::Object::_require at line 17 # spent 28µs making 1 call to Perl::Critic::Policy::RegularExpressions::ProhibitEnumeratedClasses::BEGIN@17
# spent 20µs making 1 call to Exporter::import |
18 | |||||
19 | 2 | 24µs | 2 | 402µs | # spent 204µs (7+198) within Perl::Critic::Policy::RegularExpressions::ProhibitEnumeratedClasses::BEGIN@19 which was called:
# once (7µs+198µs) by Module::Pluggable::Object::_require at line 19 # spent 204µs making 1 call to Perl::Critic::Policy::RegularExpressions::ProhibitEnumeratedClasses::BEGIN@19
# spent 198µs making 1 call to Exporter::import |
20 | |||||
21 | 2 | 502µs | 2 | 106µs | # spent 57µs (8+49) within Perl::Critic::Policy::RegularExpressions::ProhibitEnumeratedClasses::BEGIN@21 which was called:
# once (8µs+49µs) by Module::Pluggable::Object::_require at line 21 # spent 57µs making 1 call to Perl::Critic::Policy::RegularExpressions::ProhibitEnumeratedClasses::BEGIN@21
# spent 49µs making 1 call to base::import |
22 | |||||
23 | 1 | 700ns | our $VERSION = '1.121'; | ||
24 | |||||
25 | #----------------------------------------------------------------------------- | ||||
26 | |||||
27 | 1 | 2µs | 1 | 30µs | Readonly::Scalar my $DESC => q{Use named character classes}; # spent 30µs making 1 call to Readonly::Scalar |
28 | 1 | 2µs | 1 | 44µs | Readonly::Scalar my $EXPL => [248]; # spent 44µs making 1 call to Readonly::Scalar |
29 | |||||
30 | 1 | 12µs | 1 | 338µs | Readonly::Array my @PATTERNS => ( # order matters: most to least specific # spent 338µs making 1 call to Readonly::Array |
31 | [q{ },'\\t','\\r','\\n'] => ['\\s', '\\S'], | ||||
32 | ['A-Z','a-z','0-9','_'] => ['\\w', '\\W'], # RT 69322 | ||||
33 | ['A-Z','a-z'] => ['[[:alpha:]]','[[:^alpha:]]'], | ||||
34 | ['A-Z'] => ['[[:upper:]]','[[:^upper:]]'], | ||||
35 | ['a-z'] => ['[[:lower:]]','[[:^lower:]]'], | ||||
36 | ['0-9'] => ['\\d','\\D'], | ||||
37 | ['\w'] => [undef, '\\W'], | ||||
38 | ['\s'] => [undef, '\\S'], | ||||
39 | ); | ||||
40 | |||||
41 | #----------------------------------------------------------------------------- | ||||
42 | |||||
43 | 1 | 4µs | # spent 2µs within Perl::Critic::Policy::RegularExpressions::ProhibitEnumeratedClasses::supported_parameters which was called:
# once (2µs+0s) by Perl::Critic::Policy::new at line 88 of Perl/Critic/Policy.pm | ||
44 | 1 | 2µs | # spent 7µs (5+1) within Perl::Critic::Policy::RegularExpressions::ProhibitEnumeratedClasses::default_severity which was called:
# once (5µs+1µs) by Perl::Critic::Policy::get_severity at line 331 of Perl/Critic/Policy.pm | ||
45 | sub default_themes { return qw( core pbp cosmetic unicode ) } | ||||
46 | sub applies_to { return qw(PPI::Token::Regexp::Match | ||||
47 | PPI::Token::Regexp::Substitute | ||||
48 | PPI::Token::QuoteLike::Regexp) } | ||||
49 | |||||
50 | #----------------------------------------------------------------------------- | ||||
51 | |||||
52 | |||||
53 | sub violates { | ||||
54 | my ( $self, $elem, $document ) = @_; | ||||
55 | |||||
56 | # optimization: don't bother parsing the regexp if there are no character classes | ||||
57 | return if $elem !~ m/\[/xms; | ||||
58 | |||||
59 | my $re = $document->ppix_regexp_from_element( $elem ) or return; | ||||
60 | $re->failures() and return; | ||||
61 | |||||
62 | my $anyofs = $re->find( 'PPIx::Regexp::Structure::CharClass' ) | ||||
63 | or return; | ||||
64 | foreach my $anyof ( @{ $anyofs } ) { | ||||
65 | my $violation; | ||||
66 | $violation = $self->_get_character_class_violations( $elem, $anyof ) | ||||
67 | and return $violation; | ||||
68 | } | ||||
69 | |||||
70 | return; # OK | ||||
71 | } | ||||
72 | |||||
73 | sub _get_character_class_violations { | ||||
74 | my ($self, $elem, $anyof) = @_; | ||||
75 | |||||
76 | my %elements; | ||||
77 | foreach my $element ( $anyof->children() ) { | ||||
78 | $elements{ _fixup( $element ) } = 1; | ||||
79 | } | ||||
80 | |||||
81 | for (my $i = 0; $i < @PATTERNS; $i += 2) { ##no critic (CStyleForLoop) | ||||
82 | if (all { exists $elements{$_} } @{$PATTERNS[$i]}) { | ||||
83 | my $neg = $anyof->negated(); | ||||
84 | my $improvement = $PATTERNS[$i + 1]->[$neg ? 1 : 0]; | ||||
85 | next if !defined $improvement; | ||||
86 | |||||
87 | if ($neg && ! defined $PATTERNS[$i + 1]->[0]) { | ||||
88 | # the [^\w] => \W rule only applies if \w is the only token. | ||||
89 | # that is it does not apply to [^\w\s] | ||||
90 | next if 1 != scalar keys %elements; | ||||
91 | } | ||||
92 | |||||
93 | my $orig = join q{}, '[', ($neg ? q{^} : ()), @{$PATTERNS[$i]}, ']'; | ||||
94 | return $self->violation( $DESC . " ($orig vs. $improvement)", $EXPL, $elem ); | ||||
95 | } | ||||
96 | } | ||||
97 | |||||
98 | return; # OK | ||||
99 | } | ||||
100 | |||||
101 | 1 | 1µs | 1 | 28µs | Readonly::Hash my %ORDINALS => ( # spent 28µs making 1 call to Readonly::Hash |
102 | ord "\n" => '\\n', | ||||
103 | ord "\f" => '\\f', | ||||
104 | ord "\r" => '\\r', | ||||
105 | ord q< > => q< >, | ||||
106 | ); | ||||
107 | |||||
108 | sub _fixup { | ||||
109 | my ( $element ) = @_; | ||||
110 | if ( $element->isa( 'PPIx::Regexp::Token::Literal' ) ) { | ||||
111 | my $ord = $element->ordinal(); | ||||
112 | exists $ORDINALS{$ord} and return $ORDINALS{$ord}; | ||||
113 | return $element->content(); | ||||
114 | } elsif ( $element->isa( 'PPIx::Regexp::Node' ) ) { | ||||
115 | return join q{}, map{ _fixup( $_ ) } $element->elements(); | ||||
116 | } else { | ||||
117 | return $element->content(); | ||||
118 | } | ||||
119 | } | ||||
120 | |||||
121 | 1 | 9µs | 1; | ||
122 | |||||
123 | __END__ |