Login
Clean up Schwern's
[gknop/Perl-Critic.git] / lib / Perl / Critic / Policy / RegularExpressions / RequireExtendedFormatting.pm
CommitLineData
6036a254 1##############################################################################
dff08b70
JRT
2# $URL$
3# $Date$
4# $Author$
5# $Revision$
6036a254 6##############################################################################
dff08b70 7
59b05e08
JRT
8package Perl::Critic::Policy::RegularExpressions::RequireExtendedFormatting;
9
df6dee2b 10use 5.006001;
59b05e08
JRT
11use strict;
12use warnings;
c680a9c9
ES
13use Readonly;
14
bbf4108c 15use Perl::Critic::Utils qw{ :severities };
827f825f 16use Perl::Critic::Utils::PPIRegexp qw{ &get_modifiers &get_match_string };
59b05e08
JRT
17use base 'Perl::Critic::Policy';
18
be4331b3 19our $VERSION = '1.090';
59b05e08 20
6036a254 21#-----------------------------------------------------------------------------
7e86d49a 22
c680a9c9
ES
23Readonly::Scalar my $DESC => q{Regular expression without "/x" flag};
24Readonly::Scalar my $EXPL => [ 236 ];
59b05e08 25
6036a254 26#-----------------------------------------------------------------------------
59b05e08 27
827f825f
ES
28sub supported_parameters {
29 return (
30 {
9c7b2a74 31 name => 'minimum_regex_length_to_complain_about',
827f825f 32 description =>
9c7b2a74 33 q<The number of characters that a regular expression must contain before this policy will complain.>,
827f825f
ES
34 behavior => 'integer',
35 default_string => '0',
36 integer_minimum => 0,
37 },
38 {
9c7b2a74 39 name => 'strict',
827f825f 40 description =>
9c7b2a74 41 q<Should regexes that only contain whitespace and word characters be complained about?>,
827f825f 42 behavior => 'boolean',
9c7b2a74 43 default_string => '0',
827f825f
ES
44 },
45 );
46}
47
9c7b2a74
ES
48sub default_severity { return $SEVERITY_MEDIUM }
49sub default_themes { return qw< core pbp maintenance > }
50sub applies_to {
51 return qw<
52 PPI::Token::Regexp::Match
53 PPI::Token::Regexp::Substitute
54 PPI::Token::QuoteLike::Regexp
55 >;
56}
7e86d49a 57
6036a254 58#-----------------------------------------------------------------------------
bf159007 59
59b05e08 60sub violates {
e5f6c18d 61 my ( $self, $elem, undef ) = @_;
59b05e08 62
827f825f 63 my $match = get_match_string($elem);
9c7b2a74
ES
64 return if length $match <= $self->{_minimum_regex_length_to_complain_about};
65 return if not $self->{_strict} and $match =~ m< \A [\s\w]* \z >xms;
827f825f 66
8b5a642f 67 my %mods = get_modifiers($elem);
9c7b2a74 68 if ( not $mods{x} ) {
c680a9c9 69 return $self->violation( $DESC, $EXPL, $elem );
59b05e08 70 }
9c7b2a74
ES
71
72 return; # ok!;
59b05e08
JRT
73}
74
751;
76
77__END__
78
6036a254 79#-----------------------------------------------------------------------------
7e86d49a 80
59b05e08
JRT
81=pod
82
83=head1 NAME
84
2bd06403 85Perl::Critic::Policy::RegularExpressions::RequireExtendedFormatting - Always use the C</x> modifier with regular expressions.
59b05e08 86
11f53956 87
af93c316
ES
88=head1 AFFILIATION
89
11f53956
ES
90This Policy is part of the core L<Perl::Critic|Perl::Critic>
91distribution.
af93c316
ES
92
93
59b05e08
JRT
94=head1 DESCRIPTION
95
96Extended regular expression formatting allows you mix whitespace and
97comments into the pattern, thus making them much more readable.
98
99 # Match a single-quoted string efficiently...
100
101 m{'[^\\']*(?:\\.[^\\']*)*'}; #Huh?
102
9c7b2a74 103 # Same thing with extended format...
59b05e08 104
9c7b2a74
ES
105 m{
106 ' # an opening single quote
107 [^\\'] # any non-special chars (i.e. not backslash or single quote)
108 (?: # then all of...
109 \\ . # any explicitly backslashed char
110 [^\\']* # followed by an non-special chars
111 )* # ...repeated zero or more times
112 ' # a closing single quote
113 }x;
59b05e08 114
0cb729f0
ES
115
116=head1 CONFIGURATION
117
9c7b2a74
ES
118You might find that putting a C</x> on short regular expressions to be
119excessive. An exception can be made for them by setting
120C<minimum_regex_length_to_complain_about> to the minimum match length
121you'll allow without a C</x>. The length only counts the regular
122expression, not the braces or operators.
827f825f
ES
123
124 [RegularExpressions::RequireExtendedFormatting]
9c7b2a74 125 minimum_regex_length_to_complain_about = 5
827f825f 126
9c7b2a74
ES
127 $num =~ m<(\d+)>; # ok, only 5 characters
128 $num =~ m<\d\.(\d+)>; # not ok, 9 characters
827f825f 129
9c7b2a74
ES
130This option defaults to 0.
131
132Because using C</x> on a regex which has whitespace in it can make it
133harder to read (you have to escape all that innocent whitespace), by
134default, you can have a regular expression that only contains
135whitespace and word characters without the modifier. If you want to
136restrict this, turn on the C<strict> option.
827f825f
ES
137
138 [RegularExpressions::RequireExtendedFormatting]
9c7b2a74
ES
139 strict = 1
140
141 $string =~ m/Basset hounds got long ears/; # no longer ok
827f825f 142
9c7b2a74 143This option defaults to false.
0cb729f0
ES
144
145
59b05e08
JRT
146=head1 NOTES
147
821d0eb5 148For common regular expressions like e-mail addresses, phone numbers,
11f53956
ES
149dates, etc., have a look at the L<Regex::Common|Regex::Common> module.
150Also, be cautions about slapping modifier flags onto existing regular
59b05e08
JRT
151expressions, as they can drastically alter their meaning. See
152L<http://www.perlmonks.org/?node_id=484238> for an interesting
153discussion on the effects of blindly modifying regular expression
154flags.
155
11f53956 156
59b05e08
JRT
157=head1 AUTHOR
158
159Jeffrey Ryan Thalhammer <thaljef@cpan.org>
160
11f53956 161
59b05e08
JRT
162=head1 COPYRIGHT
163
20dfddeb 164Copyright (c) 2005-2008 Jeffrey Ryan Thalhammer. All rights reserved.
59b05e08
JRT
165
166This program is free software; you can redistribute it and/or modify
167it under the same terms as Perl itself. The full text of this license
168can be found in the LICENSE file included with this module.
169
170=cut
737d3b65
CD
171
172# Local Variables:
173# mode: cperl
174# cperl-indent-level: 4
175# fill-column: 78
176# indent-tabs-mode: nil
177# c-indentation-style: bsd
178# End:
96fed375 179# ex: set ts=8 sts=4 sw=4 tw=78 ft=perl expandtab shiftround :