File Coverage

blib/lib/Perl/Critic/Policy/RegularExpressions/RequireExtendedFormatting.pm
Criterion Covered Total %
statement 28 28 100.0
branch 7 8 87.5
condition 3 3 100.0
subroutine 11 11 100.0
pod 4 5 80.0
total 53 55 96.3


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